Depth First Search
Depth First Search
Iterative Algorithm
//Push all the neighbors of v that are not visited into the stack
6. for all neighbors w of v in Graph G
7. if w is not visited then
8. S.push( w )
9. mark w as visited, visited[w] = true
10. pred[w] = v
11. end if
12. end for
13. end while
In the above iterative version of the DFS algorithm, the traversal starts from any randomly
selected node in the graph G. A stack S is used to store the adjacent nodes. An array named
“visited” is used to mark whether a node is visited or not. Initially all the nodes are marked as
not visited indicated by “False” or “F”. The node at the top of the stack S is popped out and
marked as traversed in the graph. The adjacent nodes of the popped out nodes are pushed into
the stack S in some random order and are marked as visited in the array “visited” indicated by
“T” or “True”. The array “pred” is used to store the predecessor node of the newly pushed
adjacent nodes (w). Here v the popped out node will be the predecessor node. This array helps
in marking the traversal route in the graph. The final output of DFS traversal will be an acyclic
graph (graph without cycles or closed regions). It will contain all the nodes of the graph.
Recursive algorithm
Algorithm DFS(G, v)
Input: Graph G and start vertex or node v
1. mark v as visited
2. for all neighbors w of v in Graph G
3. if w is not visited then
4. DFS(G, w)
5. end if
6. end for
Example:
The following example has been solved using iterative algorithm of DFS.
Let ‘B’ be the start vertex or node.
Stack S:
visited:
A B C D E F G H I J K L M N O P Q R
F T F F F F F F F F F F F F F F F F
pred:
A B C D E F G H I J K L M N O P Q R
Nil
Pop ‘B’ and push all adjacent node of ‘B’ that are not yet visited into stack and mark as visited.
Stack S:
C
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T F T T F F F F F F F F F F F F
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B B B
Pop ‘C’ and push all adjacent node of ‘C’ that are not yet visited into stack and mark as visited.
Stack S:
G
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T F T T T F F F F F F F F F F F
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B B B C
Pop ‘G’ and push all adjacent node of ‘G’ that are not yet visited into stack and mark as visited.
Stack S:
J
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T F F F F F F F F
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G
Pop ‘J’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
K
M
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T F T F F F F F
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J J
Pop ‘K’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
R
N
M
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T F T T F F F T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J J K K
Pop ‘R’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
N
M
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T F T T F F F T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J J K K
There are no adjacent vertices of node ‘R’ that are not yet visited.
Pop ‘N’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Note: Here the dashed arrow indicates backtracking. Backtracking is done by checking the
“pred” array for the predecessor of the popped out node.
Stack S:
M
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T F T T F F F T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J J K K
There are no adjacent vertices of node ‘N’ that are not yet visited.
Pop ‘M’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
Q
L
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T T T T F F T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J M J K M K
Pop ‘Q’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
P
L
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T T T T F T T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J M J K Q M K
Pop ‘P’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
O
L
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T T T T T T T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J M J K P Q M K
Pop ‘O’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
L
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F F T T T T T T T T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C G J M J K P Q M K
There are no adjacent vertices of node ‘O’ that are not yet visited.
Pop ‘L’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
I
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T F T T T T T T T T T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C L G J M J K P Q M K
Pop ‘I’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
H
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T T T T T T T T T T T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C I L G J M J K P Q M K
Pop ‘H’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
Stack S:
D
F
E
A
visited:
A B C D E F G H I J K L M N O P Q R
T T T T T T T T T T T T T T T T T T
pred:
A B C D E F G H I J K L M N O P Q R
B Nil B G B B C I L G J M J K P Q M K
There are no adjacent vertices of node ‘H’ that are not yet visited.
Pop ‘D’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
There are no adjacent vertices of node ‘D’ that are not yet visited.
Pop ‘F’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
There are no adjacent vertices of node ‘F’ that are not yet visited.
Pop ‘E’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
There are no adjacent vertices of node ‘E’ that are not yet visited.
Pop ‘A’ and push all its adjacent nodes that are not yet visited into stack and mark as visited.
There are no adjacent vertices of node ‘A’ that are not yet visited.
Final DFS result