8.6 Graphs

Trees are a useful way to represent relationships in which a hierarchy exists. That is, a node is pointed to by at most one other node (its parent). If we remove the restriction that each node may have only one parent node, we have a data structure called a graph. A graph is made up of a set of nodes called vertices and a set of lines called edges (or arcs) that connect the nodes.

The vertices in the graph represent objects, and the edges describe relationships among the vertices. For instance, if the graph is representing a map, the vertices might be the names of cities, and the edges that link the vertices could represent roads between pairs of cities. Because the roads that run between cities are two-way paths, the edges in this ...

Get Computer Science Illuminated, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.