### The Depth First Search Graph traversal algorithm

• Depth First Search: visit nodes adjacent to the last visited node first

• Description of the Depth First Search algorithm:

• Start at some node (e.g., node 0):

• Visit one of the unvisited neighbors of node 0:

• Then visit one of the unvisited neighbors of node 1:

• Then visit one of the unvisited neighbors of node 7:

• And so on

• Implementing the DFS algorithm

• The DFS algorithm is implmented by:

 Using a stack to store the nodes in the toVisitNodes data structure.

• Pseudo code:

 ``` Set all nodes to "not visited"; s = new Stack(); ******* Change to use a stack s.push(initial node); ***** Push() stores a value in a stack while ( s ≠ empty ) do { x = s.pop(); ****** Pop() remove a value from the stack if ( x has not been visited ) { visited[x] = true; // Visit node x ! for ( every edge (x, y) /* we are using all edges ! */ ) if ( y has not been visited ) s.push(y); ***** Use push() ! } } ```

• Example of the DFS algorithm

• Example:

• Graph:

• Initial state: node 0 is pushed

• State after visiting 0

Push the unvisited neighbor nodes: 8, 3, 1 (I used the reverse order to visit smaller node id first)

Next, visit the top node in the stack: 1

• State after visiting 1

Push the unvisited neighbor nodes: 7

Next, visit the top node in the stack: 7

• State after visiting 7

Push the unvisited neighbor nodes: 2

Next, visit the top node in the stack: 2

• State after visiting 2

Push the unvisited neighbor nodes: 5, 3 (Note: 3 is pushed again, and the previous value will be cancelled later -- as we will see)

Next, visit the top node in the stack: 3

• State after visiting 3

Push the unvisited neighbor nodes: 4

Next, visit the top node in the stack: 4

• State after visiting 4

Push the unvisited neighbor nodes: 8 (Note: 8 is pushed again, and the previous value will be cancelled later -- as we will see)

Next, visit the top node in the stack: 8

• State after visiting 8

Push the unvisited neighbor nodes: none

Next, visit the top node in the stack: 5

• State after visiting 5

Push the unvisited neighbor nodes: 6

Next, visit the top node in the stack: 6

• State after visiting 6

Push the unvisited neighbor nodes: none

Next, visit the top node in the stack: 3

 3 is visited: skip

Result:

Next, visit the top node in the stack: 8

 8 is visited: skip

Result:

• DONE

(The stack has become empty)

• Implementation in Java

• Java code:

 ``` public void DFS() { // DFS uses a Stack data structure Stack s = new Stack(); // I use Stack class in Java's library for (i = 0; i < visited.length; i++) visited[i] = false; // Clear visited[] s.push(0); // Start the "to visit" at node 0 /* =========================================== Loop as long as there are "active" node =========================================== */ while( ! s.isEmpty() ) { int nextNode; // Next node to visit int i; nextNode = s.pop(); if ( ! visited[nextNode] ) { visited[nextNode] = true; // Mark node as visited System.out.println("nextNode = " + nextNode ); for ( i = NNodes-1; i >= 0; i-- ) if ( adjMatrix[nextNode][i] > 0 && ! visited[i] ) s.push(i); } } } ```

(I pushed the nodes in reverse order to make the traversal visit smaller node IDs first)

• Test program using the graph given in this webpage:

 ``` public static void main(String[] args) { // 0 1 2 3 4 5 6 7 8 // =================================================== int[][] conn = { { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, // 0 { 1, 0, 0, 0, 0, 0, 0, 1, 0 }, // 1 { 0, 0, 0, 1, 0, 1, 0, 1, 0 }, // 2 { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, // 3 { 0, 0, 0, 1, 0, 0, 0, 0, 1 }, // 4 { 0, 0, 1, 0, 0, 0, 1, 0, 0 }, // 5 { 0, 0, 0, 0, 0, 1, 0, 0, 0 }, // 6 { 0, 1, 1, 0, 0, 0, 0, 0, 0 }, // 7 { 1, 0, 0, 0, 1, 0, 0, 0, 0 } };// 8 Graph G = new Graph(conn); G.DFS(); } ```

Output:

 ``` nextNode = 0 nextNode = 1 nextNode = 7 nextNode = 2 nextNode = 3 nextNode = 4 nextNode = 8 nextNode = 5 nextNode = 6 ```

Traversal order:

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac TestProg.java To run:          java TestProg