# Basic Graph Algorithms

## 1  Representations of Graphs

There are two standard ways to represent a graph $G\left(V,E\right)$ where $V$ is the set of vertices and $E$ is the set of edges.
Say we have a graph with five vertices and six edges as follows.
In an adjacency list representation, we will have a linked-list for each vertex. From list 2, for example, we can tell that vertex 2 has edges to both vertices 1 and 3.
The adjacency matrix representation for the same graph is as follows. From row 2 of the matrix, we find that there are 1s in columns 1 and 3. This indicates that there are edges from vertex 2 to vertices 1 and 3.

 $\begin{array}{cc}\hfill & \hfill \begin{array}{ccccc}\hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill & \hfill 4\hfill & \hfill 5\hfill \end{array}\hfill \\ \hfill \begin{array}{c}\hfill 1\\ \hfill 2\\ \hfill 3\\ \hfill 4\\ \hfill 5\end{array}& \hfill \left(\begin{array}{ccccc}\hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 1\hfill \\ \hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill \\ \hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill \\ \hfill 1\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill \end{array}\right)\hfill \end{array}$

These representation techniques can also be used for directed graphs. Consider the following directed graph.

 $\begin{array}{cc}\hfill & \hfill \begin{array}{ccccc}\hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill & \hfill 4\hfill & \hfill 5\hfill \end{array}\hfill \\ \hfill \begin{array}{c}\hfill 1\\ \hfill 2\\ \hfill 3\\ \hfill 4\\ \hfill 5\end{array}& \hfill \left(\begin{array}{ccccc}\hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 1\hfill \\ \hfill 1\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill \end{array}\right)\hfill \end{array}$

## 2  Basic Search Techniques for Graphs

### 2.1  Classification of edges

A search done on a graph $G$ results in some solution forest (set of trees) consisting of a subset of edges of $G$. The edges of $G$ can be classified as follows:
• Tree edges - edges in the solution forest.
• Back edges - edges of the original graph that connects a vertex to its ancestor in a solution tree.
• Forward edges - edges not in the solution forest that connect a vertex to a decendent in a solution tree.
• Cross edges - all other edges. They can go between vertices in the same solution tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different solution trees.

Breadth-first search (BFS) is a search technique that expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. That is, the algorithm discovers all vertices at distance $k$ from $s$ before discovering any vertices at distance $k+1$.
To keep track of progress, BFS colors each vertex WHITE, GRAY, or BLACK. All vertices start out WHITE and may later become GRAY and then BLACK.
BFS $\left(G,s\right)$
1    for each vertex $u\in V\left[G\right]-\left\{s\right\}$
2             do  $\mathrm{color}\left[u\right]$ $←$ WHITE
3                    $d\left[u\right]$ $←$  $\infty$
4                    $p\left[u\right]$ $←$ NIL
5     $\mathrm{color}\left[s\right]$ $←$ GRAY
6     $d\left[s\right]$ $←$  $0$
7     $p\left[s\right]$ $←$ NIL
8     $Q$ $←$  $\left\{s\right\}$  /* Q always contains the set of GRAY vertices */
9    while  $Q\ne \varnothing$
10                  do  $u$ $←$  $\mathrm{head}\left[Q\right]$
11                        for each $v\in \mathrm{adj}\left[u\right]$
12                                 do if  $\mathrm{color}\left[v\right]=\text{WHITE}$
13                                             then  $\mathrm{color}\left[v\right]$ $←$ GRAY
14                                                          $d\left[v\right]$ $←$  $d\left[u\right]+1$
15                                                          $p\left[v\right]$ $←$  $u$
16                                                          ENQUEUE $\left(Q,v\right)$
17                         DEQUEUE $\left(Q,u\right)$
18                         $\mathrm{color}\left[u\right]$ $←$ BLACK
Figure 1: BFS example on an undirected graph with 9 vertices called with $\text{source}=e$.

### 2.3  Depth First Search

The strategy followed by DFS is to search deeper in the graph whenever possible. In DFS, edges are explored out of the most recently discovered vertex $v$ that still has unexplored edges leaving it. When all of $v$'s edges have been explored, the search backtracks to explore edges leaving the vertex from which $v$ was discovered. $d\left(u\right)$ is the start time of visiting $u$ and $f\left(u\right)$ is the finish time of having explored all the neighboring vertices of $u$.
DFS $\left(G\right)$
1    for each vertex $u\in V\left[G\right]-\left\{s\right\}$
2             do  $\mathrm{color}\left[u\right]$ $←$ WHITE
3                    $p\left[u\right]$ $←$ NIL
4     $\mathrm{count}$ $←$  $0$
5    for each $u\in V\left[G\right]$
6             do if  $\mathrm{color}\left[u\right]=\text{WHITE}$
7                         then  DFS-VISIT $\left(u\right)$
DFS-VISIT $\left(u\right)$
1     $\mathrm{color}\left[u\right]$ $←$ GRAY
2     $d\left[u\right]$ $←$  $\mathrm{count}$
3     $\mathrm{count}$ $←$  $\mathrm{count}+1$
4    for each $v\in \mathrm{adj}\left[u\right]$
5             do if  $\mathrm{color}\left[v\right]=\text{WHITE}$
6                         then  $p\left[v\right]$ $←$  $u$
7                                      DFS-VISIT $\left(v\right)$
8     $\mathrm{color}\left[u\right]$ $←$ BLACK
9     $f\left[u\right]$ $←$  $\mathrm{count}$
10     $\mathrm{count}$ $←$  $\mathrm{count}+1$
Consider the following undirected graph. In DFS, all vertices are initialised to WHITE, then each one is visited if they are white. Assuming that $V\left[G\right]=\left\{a,b,c,d,e,f,g,h,i\right\}$, DFS-VISIT is first invoked on $a$. Within DFS-VISIT, $a$ is coloured GRAY, then the vertices adjacent to $a$ are visited recursively if they are WHITE. When all visits are completed, $a$ is coloured BLACK (note that DFS still works if we leave it as GRAY). This results in the visiting order $\left\{a,b,d,h,g,c,e,f,i\right\}$.
The following diagram shows the DFS trees created. The top row shows at which level of recursion (if any) a vertex was visited by DFS-VISIT. The bold line traces the visiting order of the DFS and DFS-VISIT algorithms. The start and finish times are illustrated using the " $d\left[u\right]/f\left[u\right]$" notation; where "time" is basically a counter that gets incremented as DFS discovers or leaves a vertex.

#### 2.3.1  Properties of DFS

1. In any DFS of a (directed or undirected) graph $G\left(V,E\right)$, for any two vertices $u$ and $v$, exactly one of the following three conditions holds:
• the intervals $\left[d\left[u\right],f\left[u\right]\right]$ and $\left[d\left[v\right],f\left[v\right]\right]$ are entirely disjoint,
• the interval $\left[d\left[u\right],f\left[u\right]\right]$ is contained entirely within the interval $\left[d\left[v\right],f\left[v\right]\right]$, and $u$ is a descendant of $v$ in the DFS tree, or
• the interval $\left[d\left[v\right],f\left[v\right]\right]$ is contained entirely within the interval $\left[d\left[u\right],f\left[u\right]\right]$, and $v$ is a descendant of $u$ in the DFS tree.
For instance, in the previous example, $d\left[c\right]=11$, and $f\left[c\right]=18$. This contains all of $\left\{e,f,i\right\}$ because $d\left[c\right] and $f\left[\left\{e,f,i\right\}\right].
2. In a DFS forest of a directed or undirected graph $G\left(V,E\right)$, vertex $v$ is a descendant of vertex $u$ if and only if at the time $d\left[u\right]$ that the search discovers $u$, vertex $v$ can be reached from $u$ along a path consisting entirely of white vertices.
3. In a depth-first search of an undirected graph $G$, every edge of $G$ is either a tree edge or a back edge.

## 3  Topological Sorting

A topological sort of a Directed Acyclic Graph (DAG) $G=\left(V,E\right)$ is a linear ordering of vertices in the DAG such that if $G$ contains a directed edge $⟨u,v⟩$, then $u$ appears before $v$ in the ordering (see Figure 2). A directed graph is acyclic if and only if a depth-first search on the graph yields no back edges.
Figure 2: Topological Sorting. $c\le a\le d\le b$.
TOPOLOGICAL-SORT( $G$) can be determined by sorting the finish times $f\left[v\right]$ of each vertex $v\in V\left[G\right]$ in decreasing order. One way to implement this is to modify DFS-VISIT to place each finished vertex into the front of a linked-list. The steps of this approach are as follows:
1. Initialise an empty linked list.
2. Execute DFS(G). As each vertex is coloured BLACK, prepend it to the front of the linked list.
3. Return the linked list. The rank of each node is its position in the linked list started from the head of the list.
The running time is $O\left(m+n\right)$, where $m$ is the number of edges and $n$ is the number of vertices in $G$.

## 4  Strongly Connected Components

The strongly connected components (SCC) of a directed graph $G\left(V,E\right)$ is a maximal set of vertices $U\subseteq V$ such that for every pair of vertices $u$ and $v$ in $U$, $u$ and $v$ are mutually reachable.
Take the following graph of nine vertices for example. The strongly connected components in this graph are shaded in grey bubbles. Every vertex in each bubble is reachable by every other vertex in the same bubble.
STRONGLY-CONNECTED-COMPONENTS $\left(G\right)$
1    finish-order $←$ finish order of DFS( $G$)
2     ${G}^{T}$ $←$ transpose of $G$
3    dfs-forest $←$ DFS trees from DFS( ${G}^{T}$)
.                                               visited in reverse finish-order.
4    return dfs-forest
.       /* each tree in dfs-forest is a strongly connected component. */
The running time of the algorithm is $O\left(m+n\right)$. Steps 1 and 3 take $O\left(m+n\right)$ time. Step 2 requires $O\left(m+n\right)$ time, and Step 4 takes $O\left(n\right)$ time. Thus, the algorithm takes $O\left(m+n\right)$ time.

File translated from TEX by TTM, version 3.67.
On 31 Mar 2006, 18:12.