Depth First Search
Depth First Search
Depth First Search
S.Bharathiraja,
Associate Professor,
School of Computer Science and Engineering,
VIT – University - Chennai
Graph Traversal
Types:
C
A
E
H
G D
B F
C
A
E
H
G D
Depth First Search
B F
C
E A
H A
G D
Result : A
Result : A B
Select the least vertex out of B,G & D, say ‘B’ and mark it visited
Result : A B E
Select the least vertex out of E & F, say ‘E’ and mark it visited &
Result : A B E G
Mark ‘G’ as visited & Push it on to stack and update the result.
Depth First Search
B F B F
G
X
C C
E A A E
E B
H H
G D G D
A
Result : A B E G
Result : A B E G
Result : A B E G F
Result : A B E G F C
Vertex ‘B’ is already visited. Out of ‘D’ & ‘C’ mark ‘C’ as marked.
Result : A B E G F C H
Result : A B E G F C H
Pop ‘H’ from the stack, so that top pointer points to ‘C’.
Depth First Search
B F B F
C C
E A A F
E B
H H
G D G D
A
Result : A B E G F C H
Pop ‘C’ from the stack, so that top pointer points to ‘F’.
Depth First Search
B F B F
D
C C
E A A F
E B
H H
G D G D
A
Result : A B E G F C H D
Vertex ‘B’ & ‘C’ are already visited, So mark ‘D’ as visited.
Result : A B E G F C H D
Pop ‘D’ from the stack, so that the top pointer points to ‘F’.
Depth First Search
B F B F
C C
E A A
E B
H H
G D G D
A
Result : A B E G F C H D
Pop ‘F’ from the stack, so that the top pointer points to ‘B’.
Depth First Search
B F B F
C C
E A A
E
H H
G D G D
A
Result : A B E G F C H D
Pop ‘B’ from the stack, so that the top pointer points to ‘A’.
Depth First Search
B F B F
C C
E A A
E
H H
G D G D
Result : A B E G F C H D
Pop ‘A’ from the stack, if stack is empty stop the process. Traversal
3
1
4 5
2 2
4
3
Solution: a b c f e d g