Download presentation
1
Graph Traversals Depth-First Traversal. The Algorithm.
Animated Example. Implementation. Breadth-First Traversal. Review Questions.
2
Depth-First Traversal Algorithm
In this method, After visiting a vertex v, which is adjacent to w1, w2, w3, ...; Next we visit one of v's adjacent vertices, w1 say. Next, we visit all vertices adjacent to w1 before coming back to w2, etc. Must keep track of vertices already visited to avoid cycles. The method can be implemented using recursion or stack. The algorithm using stack is as follows: 1 Push the starting vertex onto the stack 2 Repeat the following until the stack is empty pop a vertex off the stack, call it v if v is not already visited, visit it push vertices adjacent to v, not visited, onto the stack
3
Animated Example Demonstrates depth-first traversal using an explicit stack. Order of Traversal Stack A B C F E G D H I
4
Depth-First Traversal Implementation
The following shows a code for the depthFirstTraversal method of the AbstractGraph class, using recursion. It involves two methods, one public, shown below, and the other private, shown in the next page. The public method accepts any PrePostVisitor and the number of the starting vertex. 1 public abstract class AbstractGraph extends AbstractContainer implements Graph { 3 4 public void depthFirstTraversal(PrePostVisitor visitor, int start) { 6 boolean[] visited = new boolean [numberOfVertices]; 7 for (int v = 0; v < numberOfVertices; ++v) visited [v] = false; 9 depthFirstTraversal(visitor, vertex[start], visited); 10 }
5
Depth-First Traversal Implementation - Cont'd
This method recursively visits all the vertices in the depth-first order. 11 private void depthFirstTraversal(PrePostVisitor visitor, Vertex v, boolean[] visited) { 13 if (visitor.isDone ()) return; 15 visitor.preVisit (v); 16 visited [v.getNumber ()] = true; 17 Enumeration p = v.getSuccessors (); 18 while (p.hasMoreElements ()) { Vertex to = (Vertex) p.nextElement (); if (!visited [to.getNumber ()]) depthFirstTraversal (visitor, to, visited); 22 } 23 visitor.postVisit (v); 24 } 25 //… 26 }
6
Breadth-First Traversal Algorithm
In this method, After visiting a vertex v, we must visit all its adjacent vertices w1, w2, w3, ..., before going down next level to visit vertices adjacent to w1 etc. The method can be implemented using a queue. A boolean array is used to ensure that a vertex is enqueued only once. 1 enqueue the starting vertex 2 Repeat the following until the queue is empty. 3 Remove the vertex at the front of the queue, call it v. 4 visit v. 5 enqueue vertices adjacent to v that were never enqueued.
7
Animated Example Demonstrating breadth-first traversal using a queue.
Order of Traversal Queue rear A B D E C G F H I Queue front
8
Breadth-First Traversal Implementation
The following gives the code for the BreadthFirstTraversal method of the AbstractGraph. A boolean-valued array, enqueued, is used to keep track of the vertices enqueued. The array is initialized to false, then a queue is created and the starting vertex in enqueued. 1 public abstract class AbstractGraph extends AbstractContainer implements Graph { 3 4 public void breadthFirstTraversal(Visitor visitor, int start) { 6 boolean[] enqueued = new boolean[numberOfVertices]; 7 for (int v = 0; v < numberOfVertices; ++v) enqueued [v] = false; 9 Queue queue = new QueueAsLinkedList (); 10 enqueued [start] = true; 11 queue.enqueue (vertex [start]);
9
Breadth-First Traversal Implementation - Cont'd
12 while (!queue.isEmpty () && !visitor.isDone ()) { Vertex v = (Vertex) queue.dequeue (); visitor.visit (v); Enumeration p = v.getSuccessors (); 16 while (p.hasMoreElements ()) { Vertex to = (Vertex) p.nextElement (); if (!enqueued [to.getNumber ()]) { queue.enqueue (to); enqueued [to.getNumber ()] = true; } } } 25 } 26 //… 27 }
10
Review Questions 1. Consider a depth-first traversal of the undirected graph GA shown above, starting from vertex a. List the order in which the nodes are visited in a preorder traversal. List the order in which the nodes are visited in a postorder traversal 2. Repeat exercise 1 above for a depth-first traversal starting from vertex d. 3. List the order in which the nodes of the undirected graph GA shown above are visited by a breadth first traversal that starts from vertex a. Repeat this exercise for a breadth-first traversal starting from vertex d. 4. Repeat Exercises 1 and 3 for the directed graph GB.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.