Adjacency-lists data structure. The standard graph representation for graphs that are not dense is called the adjacency-lists data structure, where we keep track of all the vertices adjacent to each vertex on a linked list that is associated with that vertex. We maintain an array of lists so that, given a vertex, we can immediately access its list. To implement lists, we use our Bag ADT from Section 1.3 with a linked-list implementation, so that we can add new edges in constant time and iterate through adjacent vertices in constant time per adjacent vertex. The Graph implementation on page 526 is based on this approach, and the figure on the facing page depicts the data structures built by this code for tinyG.txt. To add an edge connecting v and w, we add w to v’s adjacency list and v to w’s adjacency list. Thus, each edge appears twice in the data structure. This Graph implementation achieves the following performance characteristics: ■ Space usage proportional to V + E ■ Constant time to add an edge ■ Time proportional to the degree of v to iterate through vertices adjacent to v (constant time per adjacent vertex processed)
Adjacency-lists data structure. The standard graph representation for graphs that are
not dense is called the adjacency-lists data structure, where we keep track of all the
vertices adjacent to each vertex on a linked list that is associated with that vertex. We
maintain an array of lists so that, given a vertex, we can immediately access its list. To
implement lists, we use our Bag ADT from Section 1.3 with a linked-list implementation, so that we can add new edges in constant time and iterate through adjacent vertices in constant time per adjacent vertex. The Graph implementation on page 526 is based
on this approach, and the figure on the facing page depicts the data structures built by
this code for tinyG.txt. To add an edge connecting v and w, we add w to v’s adjacency
list and v to w’s adjacency list. Thus, each edge appears twice in the data structure. This
Graph implementation achieves the following performance characteristics:
■ Space usage proportional to V + E
■ Constant time to add an edge
■ Time proportional to the degree of v to iterate through vertices adjacent to v
(constant time per adjacent vertex processed)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps