There are usually two standard ways to represent a graph*G = (V, E)*in a computer program:

- As a collection of adjacency lists
- As an adjacency matrix

You can use either way to represent both directed and undirected graphs. We'll start by looking at the adjacency list representation.

The adjacency list representation of a graph consists of an array of*|V|*lists, one for each vertex in*V*. For each vertex*u*in*V*, there's a list containing all vertices*v* so that there is an edge connecting*u*and*v*in*E*.*Figure 6.3*shows the adjacency list representation of the directed graph in*Figure 6.1*:

Figure 6.3: Adjacency list representation of the directed graph in Figure 6.1

For undirected graphs, we follow a similar strategy and build the adjacency list as if it were a directed graph with two edges between each pair of vertices*u*and*v*, which are*(u, v)*and*(v, u)*.

*Figure 6.4*shows the adjacency list representation of the undirected graph in*Figure 6.2*:

Figure 6.4: Adjacency list representation...