0% found this document useful (0 votes)
23 views

Module 5 Graph DFS

DFS begins with a source node (A) and explores as far as possible along each branch before backtracking. It first visits nodes A then B then C then E then D, marking each visited node and recording start and end times. DFS fully explores each branch from the source node before moving to the next unvisited node.

Uploaded by

AYUSH OM MISHRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Module 5 Graph DFS

DFS begins with a source node (A) and explores as far as possible along each branch before backtracking. It first visits nodes A then B then C then E then D, marking each visited node and recording start and end times. DFS fully explores each branch from the source node before moving to the next unvisited node.

Uploaded by

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

Data Structure and Algorithm: (BCSE202L)

Module-5: Graph
Graph Traversal
- Breadth First Search (BFS)
- Depth First Search (DFS)

DSA:
Dr. Durgesh Kumar
Assistant Professor (Senior),
S C O P E , V I T Ve l l o r e

Durgesh Kumar (VIT Vellore) Week 1: Lecture 01 1 / 19


Lecture Outline

Graph Traversal Algorithm


 Depth First Search (DFS)
 Running DFS on given graph and a source node
 Practice problems of DFS
 Pseudocode of DFS
 Time complexity analysis of DFS

Durgesh Kumar (VIT Vellore) Week 1: Lecture 01 2/


Traversing/Searching a Graph
Traverse all nodes of a graph :
• Find the path from source vertex v to desired vertex.
• visit all the vertex reachable from source vertex v .

Durgesh Kumar (VIT Vellore) DSA : BCSE202 L Week 1: Lecture 03 2/


Traversing/Searching a Graph
Application of Graph traversal/search:

• web crawling (how Google finds pages)

• social networking (Facebook/Instagram/LinkedIn friend finder)

• network broadcast routing

• garbage collection

• model checking (finite state machine)

• checking mathematical conjectures


• solving puzzles and games
Durgesh Kumar (VIT Vellore) DSA : BCSE202 L Week 1: Lecture 03 2/
Traversing/Searching a Graph
Two common Graph traversal algorithm:

1. Breadth First Search (BFS)

2. Depth First Search (DFS)

Durgesh Kumar (VIT Vellore) DSA : BCSE202 L Week 1: Lecture 03 2/


Application of DFS
1. Finding Connected components.

Fig-1: has 3
components
2. Finding Cycle in undirected/directed graph

Fig-2: No Cycle Fig-3: has Cycle

Durgesh Kumar (VIT Vellore) DSA : BCSE202 L Week 1: Lecture 03 2/


Application of DFS
3. Topological sorting in a Directed Acyclic graph. – finding order of task/job to be
completed.

4. Finding path in maze

Fig-4: path detection in a maze

Durgesh Kumar (VIT Vellore) DSA : BCSE202 L Week 1: Lecture 03 2/


Depth First Search (DFS) Pseudocode-
DFS
part_1
(G, s)//Where G-> graph with N node, s->source node,
// Initialization

1. Initialize an empty STACK S.


2. visited[N] = {0} // Initialize an visited array default value 0
3. parent[N] = {-1} // Initialize parent array with default value -1
4. start[N] = {-1} // Initialize start time array with default value -1
5. end[N] = {-1} // start end time array with default value -1
6. push(S, A)
7. visited[s] = 1 // mark s as visited.
8. time = 0
9. start[s] = 0
Depth First Search (DFS) Pseudocode-
DFS part_2
(G, s)//Where G is the graph with N nodes and s is the source node

10. while (S is not empty)


11. u = check_top(S)// Don’t delete from top
11. processed_u = True
12. for v in neighbour(u):
13. if visited[v] == 0: // v is not visited
14. processed_u = False
15. time +=1
16. push (S, v)
17. visited[v] =1
18. parent[v] = u
19. start[v] = time
20. break
Depth First Search (DFS) Pseudocode-
part_2
DFS (G, s)//Where G is the graph with N nodes and s is the source node

21 . if (processed_u = True)
22. u = pop(S)// Delete from the stack S
23. time +=1
24. visited[u] = 2
25. end[u] = time
26. [ END of While loop]
Depth First Search (DFS)
Q. Find the DFS of the following graph using A as source node.

Nodes Adjacency list


A B->D->E
B A->C->E
C B->E->F->G
D A->E
E A->B->C->D->F
F C->E->G
G C->F
Depth First Search (DFS)STEP-0 (Initi alizati on ) , ti m e = 0
top A B C
Stack A G
D E F
index A B C D E F G

Visited 1 0 0 0 0 0 0
Parent -1 -1 -1 -1 -1 -1 -1 A
Start time 0 -1 -1 -1 -1 -1 -1
End time -1 -1 -1 -1 -1 -1 -1

⚫ Choose the Starting Vertex as A


⚫ VISIT A
⚫ Insert A into the Queue
Depth First Search (DFS) STEP-1, ti me =1

A B C
G
Stack A B
D E F

index A B C D E F G

Visited 1 1 0 0 0 0 0 A
Parent -1 A -1 -1 -1 -1 -1
Start time 0 1 -1 -1 -1 -1 -1 B
End time -1 -1 -1 -1 -1 -1 -1

⚫ S[top] = A
⚫ Adj[A] = B, D, E
⚫ Push(S, B), start[B] = 1, visited[B] = 1, parent [B] = A
Depth First Search (DFS) STEP-2

A B C
ti me =2
G
Stack A B C
D E F

index A B C D E F G

Visited 1 1 1 0 0 0 0 A
Parent -1 A B -1 -1 -1 -1
Start time 0 1 2 -1 -1 -1 -1 B
End time -1 -1 -1 -1 -1 -1 -1

⚫ S[top] = B C
⚫ Adj[B] = A, C, E [ visited[C] =0, so we will process C]
⚫ Push(S, C), start[C] = 2, visited[C] = 1, parent[C] = B
Depth First Search (DFS) STEP-3

ti me =3 A B C
G
Stack A B C E
D E F

index A B C D E F G

Visited 1 1 1 0 1 0 0 A
Parent -1 A B -1 C -1 -1
0 1 2 -1 3 -1 -1
B
Start time
End time -1 -1 -1 -1 -1 -1 -1
C
⚫ S[top] = C
⚫ Adj[C] = B, E, F, G [ visited[E] =0, so we will process E] E
⚫ Push(S, E), start[E] = 3, visited[E] = 1, parent[E] = C
Depth First Search (DFS) STEP-4

ti me =4 A B C
G
Stack A B C E D
D E F

index A B C D E F G

Visited 1 1 1 1 1 0 0 A
Parent -1 A B E C -1 -1
0 1 2 4 3 -1 -1
B
Start time
End time -1 -1 -1 -1 -1 -1 -1
C
⚫ S[top] = E
⚫ Adj[E] = A, B, C, D, F [ visited[D] =0, so we will process D] E
⚫ Push(S, D), start[D] = 4, visited[D] = 1, parent[D] = E
D
Depth First Search (DFS) STEP-5

ti me =5 A B C
G
Stack A B C E D
D E F

index A B C D E F G

Visited 1 1 1 2 1 0 0 A
Parent -1 A B E C -1 -1
0 1 2 4 3 -1 -1
B
Start time
End time -1 -1 -1 5 -1 -1 -1
C
⚫ S[top] = D
⚫ Adj[D] = A, E [ visited[A] !=0, visited[E] !=0, so we will pop D] E
⚫ D = Pop(S), visited[D] = 2, end[D] = 5
D
Depth First Search (DFS) STEP-6

ti me =6 A B C
G
Stack A B C E F
D E F

index A B C D E F G

Visited 1 1 1 2 1 1 0 A
Parent -1 A B E C E -1
0 1 2 4 3 6 -1
B
Start time
End time -1 -1 -1 5 -1 -1 -1
C
⚫ S[top] = E
⚫ Adj[E] = A, B, C, D, F [ visited[F] =0, so we will push F] E
⚫ Push(S, E), start[F] = 6, visited[F] = 1, parent[F] = E
D F
Depth First Search (DFS)STEP-7
A B C
ti me =7 G

Stack A B C E F G D E F

A B C D E F G
A
index
Visited 1 1 1 2 1 1 1
B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time -1 -1 -1 5 -1 -1 -1
E
⚫ S[top] = F
F
⚫ Adj[F] = C, E, G [ visited[G] = 0, so we will push G] D
⚫ Push(S, G), start[G] = 7, visited[G] = 1, parent[F] = E G
Depth First Search (DFS)STEP-8
A B C
ti me =8 G

Stack A B C E F G D E F

A
index A B C D E F G

Visited 1 1 1 2 1 1 2
B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time -1 -1 -1 5 -1 -1 8
E
⚫ S[top] = G
F
⚫ Adj[G] = C, F [ visited[C] != 0, visited[F] != 0, so we will pop G] D
⚫ G = Pop(S), visited[G] = 2, end[G] = 8 G
Depth First Search (DFS)STEP-9
A B C
ti me =9 G

Stack A B C E F D E F

A
index A B C D E F G

Visited 1 1 1 2 1 2 2
B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time -1 -1 -1 5 -1 9 8
E
⚫ S[top] = F
F
⚫ Adj[F] = C, E, G [ visited[C, E, G] != 0, so we will pop F] D
⚫ F = Pop(S), visited[F] = 2, end[G] = 9 G
Depth First Search (DFS)STEP-10
A B C
ti me =10 G

Stack A B C E D E F

A
index A B C D E F G

Visited 1 1 1 2 2 2 2
B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time -1 -1 -1 5 10 9 8
E
⚫ S[top] = E
F
⚫ Adj[E] = A, B, C, D, F [ visited[A, B, C, D, F] != 0, so we will pop E] D
⚫ E = Pop(S), visited[E] = 2, end[E] = 10 G
Depth First Search (DFS)
STEP-11 A B C
ti me =11 G

Stack A B C D E F

A
index A B C D E F G

Visited 1 1 2 2 2 2 2
B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time -1 -1 11 5 10 9 8
E
⚫ S[top] = C
F
⚫ Adj[C] = B, E, F, G [ visited[B, E, F, G] != 0, so we will pop C] D
⚫ C = Pop(S), visited[C] = 2, end[C] = 11 G
Depth First Search
(DFS) STEP-12 A B C
ti me =12 G

Stack A B D E F

A
index A B C D E F G

Visited 1 2 2 2 2 2 2
B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time -1 12 11 5 10 9 8
E
⚫ S[top] = B
F
⚫ Adj[B] = A, C, E, [ visited[A, C, E] != 0, so we will pop B] D
⚫ B = Pop(S), visited[B] = 2, end[B] = 12 G
Depth First Search
(DFS) STEP-13 A B C
ti me =13 G

Stack A D E F

A
index A B C D E F G

Visited 2 2 2 2 2 2 2 B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time 13 12 11 5 10 9 8
E
⚫ S[top] = A
F
⚫ Adj[B] = B, D, E, [ visited[B, D, E] != 0, so we will pop A] D
⚫ A = Pop(S), visited[A] = 2, end[A] = 13 G
Depth First Search
(DFS) STEP-14 A B C
ti me =14
G

Stack D E F

A
index A B C D E F G

Visited 2 2 2 2 2 2 2 B
Parent -1 A B E C E F
Start time 0 1 2 4 3 6 7 C
End time 13 12 11 5 10 9 8
E
⚫ Stack Empty
F
⚫ DFA algorithm terminated. D
G
Time complexity of DFS
• Time complexity - O( V +E)
• Space complexity – to store STACK – O(V)
Practice Questions – Write down DFS and BFS for the Graphs

Graph-1
Graph-2
Graph-3
Practice Questions – Write down DFS and BFS for the Graphs

Graph-5

You might also like