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.

376<br />

Understanding Graphs<br />

457 miles<br />

504 miles<br />

406 miles<br />

387 miles<br />

509 miles<br />

Figure 5-1:<br />

Modeling<br />

a map of<br />

different<br />

cities.<br />

153 miles<br />

287 miles<br />

325 miles<br />

You could model this problem as a two-dimensional array like this:<br />

Los San Las San Salt Lake<br />

Angeles Diego Vegas Francisco City<br />

Los Angeles 0 153 287 387 X<br />

San Diego 153 0 325 509 X<br />

Las Vegas 287 325 0 504 406<br />

San Francisco 387 X 504 0 457<br />

Salt Lake City X X 406 457 0<br />

Although this two-dimensional array accurately models the map of different<br />

city distances, it’s not easy to understand what this data represents. A better<br />

data structure would be a graph.<br />

Understanding Graphs<br />

A graph is typically created by using a linked list that can point to multiple<br />

nodes. As a result, a graph doesn’t follow a linear structure but a more haphazard<br />

appearance, which makes it perfect <strong>for</strong> modeling non-linear data,<br />

such as the map of different cities and distances, as shown in Figure 5-2.

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

Saved successfully!

Ooh no, something went wrong!