AdjacencyLists: A Graph as a Collection of Lists
Learn about the representation of graphs by lists.
We'll cover the following...
Adjacency list representations of graphs take a more vertex-centric approach. There are many possible implementations of adjacency lists. In this section, we present a simple one. At the end of the section, we discuss different possibilities. In an adjacency list representation, the graph is represented as an array, adj
, of lists. The adj[i]
list contains a list of all the vertices adjacent to vertex i
. That is, it contains every index such that (i,j)
.
Visual demonstration of the adjacency list
The visual demonstration of the adjacency list is shown below:
The constructor for creating an AdjacencyLists
is as follows:
int n;List < Integer > [] adj;AdjacencyLists(int n0) {n = n0;adj = (List < Integer > []) new List[n];for (int i = 0; i < n; i++)adj[i] = new ArrayStack < Integer > ();}
In this particular implementation, we represent each list in adj
as an ArrayStack
, because we would like constant ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy