Graph Traversal

 

Objectives of this lecture

q       Learn two algorithms for traversing a graph

 

Traversal methods

q       Two popular methods for traversing a graph are Depth-first and Breadth-first.  First we explain how they work before we consider their algorithms.

 

Depth-first traversal:

q       In this method, when we visit a vertex v, which is adjacent to vertices w1, w2, …, wk; we should visit w1 and keep w2, …, wk waiting.  After visiting w1, we should visit all the vertices adjacent to w1 before turning back to w2, …, wk.

q       Notice that this is similar to pre-order traversal of a tree.

q       The following figure illustrates Depth-first traversal.  Notice that the numbers indicate the order in which the vertices are visited.

 

 

Breadth-first traversal:

q       In this method, when we visit a vertex v, which is adjacent to vertices w1, w2, …,wk; we should visit w1,w2, …, wk before we consider vertices adjacent to w1 and then those adjacent to w2, etc.

q       Notice that this is similar to level-by-level traversal of a tree.

q       The following figure illustrates Breadth-first traversal.

 

 

Depth-first Algorithm:

q       Depth first traversal is naturally a recursive algorithm, which goes as follows:

 

Vist(v)
for all vertices w adjacent to v do
   Traverse(w);

 

Problems with graph algorithms

q       In graph traversal, two problems arise that do not appear in tree traversal.  These are:

Cycles:  Since graphs may contain cycles, the above algorithm may reach a vertex more than one time.

 

q       To prevent this, we introduce a Boolean array visited.

q       We set visited[v] to true just before visiting v and check the value visited[w] before processing it.

 

Disconnection: Since some vertices may not be connected to the graph, our algorithm may fail to reach all vertices.

 

To prevent this, we use an outer loop that runs for all vertices of the graph.

 

q       With this refinement, we obtain the following algorithm.  Notice that further details depend on the choice of implementation of graph.


/* DepthFirst: depth-first traversal of a graph.
Pre:  The graph G has been created.
Post: The function Visit has been performed at each vertex of G in
depth-first order.
Uses: Function Traverse produces the recursive depth-first order.
 */
void DepthFirst(Graph G)
{
    Boolean visited[MAXVERTEX];
    Vertex v;
 
    for (all v in G)
        visited[v] = FALSE;
    for (all v in G)
        if (!visited[v])
            Traverse(v, Visit);
}
 
/* Traverse: recursive traversal of a graph
Pre:  v is a vertex of the graph G.
Post: The depth-first traversal, using function Visit, has been 
         completed for v and for all vertices adjacent to v.
Uses: Traverse recursively.
 */
void Traverse(Vertex v)
{
    Vertex w;
 
    visited[v] = TRUE;
    Visit(v);
    for (all w adjacent to v)
        if (!visited[w])
            Traverse(w, Visit);
}

 

Breadth-first Algorithm:

q       Notice that we could implement the depth-first algorithm using stack instead of recursion.

q       The algorithm for breadth-first is similar to the resulting algorithm, except that a queue is used instead of stack.  The outline is as follows:


 /* BreadthFirst: breadth-first traversal of a graph.
Pre:  The graph G has been created.
Post: The function Visit has been performed at each vertex of G, 
         where the vertices are chosen in breadth-first order.
Uses: Queue functions.
 */
void BreadthFirst(Graph G)
{
    Queue Q;        /* QueueEntry defined to be Vertex. */
    Boolean visited[MAXVERTEX];
    Vertex v, w;
 
    for (all v in G)
        visited[v] = FALSE;
    CreateQueue(Q);
 
    for (all v in G)
        if (!visited[v]) {
            Append(v, Q);
 
            do {
                Serve(v,Q);
                if (!visited[v]) {
                    visited[v] = TRUE;
                    Visit(v);
                }
 
                for (all w adjacent to v)
                    if (!visited[w])
                        Append(w, Q);
            } while (!QueueEmpty(Q));
        }
}