0% found this document useful (0 votes)
62 views20 pages

Depth First Search

Depth First Search (DFS) is a graph traversal algorithm that explores all nodes reachable from a starting node by going as deep as possible along each branch before backtracking. It can be implemented iteratively using a stack, or recursively by calling DFS on each child node before returning. The example shows DFS performed iteratively on a graph starting at node B, with the stack and visited/predecessor arrays updated at each step.

Uploaded by

VISHAL MUKUNDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views20 pages

Depth First Search

Depth First Search (DFS) is a graph traversal algorithm that explores all nodes reachable from a starting node by going as deep as possible along each branch before backtracking. It can be implemented iteratively using a stack, or recursively by calling DFS on each child node before returning. The example shows DFS performed iteratively on a graph starting at node B, with the stack and visited/predecessor arrays updated at each step.

Uploaded by

VISHAL MUKUNDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Depth First Search or DFS

 It is a graph traversal technique


 It involves exhaustive searches of all the nodes by going ahead, if possible, else by
backtracking.
[the word backtrack means that when you are moving forward and there are no more
nodes along the current path, you move backwards on the same path to find nodes to
traverse]
 In can be done using either a iterative or recursive algorithm

Iterative Algorithm

Algorithm DFS(G, v): //Where G is graph and s is source vertex


Input: Graph G and start vertex or node v
1. let S be stack, S:={} //start with an empty stack
2. S.push( v ) //Inserting v in stack
3. mark v as visited, visited[v] = true
4. while S is not empty do

//Pop a vertex from stack to visit next


5. v = S.pop( ), this node is marked in the graph as traversed

//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.

Following is the recursive version of DFS algorithm,

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.

Push ‘B’ on to the stack S and mark as visited

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

You might also like