15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

380<br />

Creating Trees<br />

A related problem, dubbed the Traveling Salesman problem, takes this one<br />

step further by finding the shortest round-trip route through a graph where<br />

the same node is both the starting and ending point. In this case, the shortest<br />

route through a graph may not necessarily be the shortest round-trip<br />

route to return to the same starting point.<br />

Both the Traveling Salesman and the Chinese Postman problem can get<br />

more complicated with a weighted or directed graph. A directed graph may<br />

restrict movement in one direction, such as traveling through one-way<br />

streets in a city, whereas a weighted graph can make one route longer than<br />

two shorter routes combined. Adding in directed and weighted graphs can<br />

alter the best solution.<br />

If you ever looked up directions on a map Web site such as MapQuest,<br />

you’ve used a graph to find the most efficient way from one location to<br />

another.<br />

Connecting nodes in a graph<br />

Another use <strong>for</strong> graphs involves topological graph theory. This problem is<br />

highlighted by the Three Cottage problem with three cottages needing to<br />

connect to the gas, water, and electricity companies, but their lines can’t<br />

cross each other. (It’s impossible, by the way.)<br />

Connecting lines in a graph without crossing is a problem that circuit board<br />

designers face in the placement of chips. Another example of eliminating<br />

intersections involves transportation designs, such as the design of highways<br />

or railroad tracks.<br />

Creating Trees<br />

Graphs typically represent a chaotic arrangement of data with little or no<br />

structure. To give graphs some <strong>for</strong>m of organization, computer scientists<br />

have created special graphs dubbed trees. Like a graph, a tree consists of<br />

nodes and edges, but unlike a graph, a tree organizes data in a hierarchy, as<br />

shown in Figure 5-5.<br />

A tree arranges a graph in a hierarchy with a single node appearing at the<br />

top (or the root node) and additional nodes appearing connected underneath<br />

(or internal nodes). If a node has no additional nodes connected underneath,<br />

those nodes are leaf nodes, as shown in Figure 5-6.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!