12 Graph Traversal

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Graph Traversal

BFS, DFS
• Graph Traversal Algorithms
• Breadth First Search

• Depth First Search


BFS, DFS
• Graph Traversal Algorithms
• Breadth First Search
– Uses Queue Data Structure
– Application: Calculate Distances from Source
• Depth First Search
– Uses Stack, or, Recursive functions
– Application: Reachability from Source
Graph 1

B C

D E

F
Graph 1

Start Node: A
B
BFS Sequence:
C

D E
DFS Sequence:

F
Adjacency Matrix

S\D A B C D E F
A 0 1 1 0 0 0
B 1 0 0 1 0 0
C 1 0 0 1 1 0
D 0 1 1 0 1 1
E 0 0 1 0 0 1
F 0 0 0 1 1 0
Adjacency List

A B C

B A D

C A D E

D B C E F

E C D F

F D E
Graph 2

BFS Sequence:
B C

DFS Sequence:
D E

F
Topological Sort
• Input: Directed graph G = (V, E) with Vertices
V and Edges E
• Output: Ordered list of vertices 1, ... |V|, such
that, for every edge (u  v), u appears before v
in the ordered list
• This list is a Topological Sort of G
• Note: multiple topological sorts are possible for same
graph
Topological Sort
• Is it possible to prepare such a list for all
Directed graphs?
Topological Sort
• Is it possible to prepare such a list for all
Directed graphs?
• The answer is "yes" if and only if the directed
graph G has no cycle!
(otherwise we have a deadlock)
Topological Sort
• Is it possible to prepare such a list for all
Directed graphs?
• The answer is "yes" if and only if the directed
graph G has no cycle!
(otherwise we have a deadlock)
• Such a Graph is called a Directed Acyclic
Graph, or just a DAG
Approach for Topological Sort
• TOPOLOGICAL-SORT(G):
1) Use DFS Algorithm
2) call DFS(G) to compute finishing times f[v] for
each vertex v
3) as each vertex is finished, insert it onto the front
of a linked list
4) return the linked list of vertices

• Note that the result is just a list of vertices in


order of decreasing finish times f[]
DFS Algorithm
Input: Graph G with V Vertices and E edges
Output: Depth First Traversal of G

Procedure DFS(G) Procedure DFS-Visit(u)


Begin Begin
Repeat for u = 1 to V step 1 Set time := time + 1
Set visited [u] := 1 Set d[u] := time
End Repeat Set visited [u] := 2
Set time := 0 Repeat for each adjacent
Repeat for u = 1 to V step 1 vertex v of u
If visited [u] = 1 If visited [v] = 1
DFS-Visit (u) DFS-Visit (v)
End If End If
End Repeat End Repeat
End Set visited [u] := 3
Set f[u] := time
End

14
Topological sort - 1
Call DFS(G) to compute the
Time = 0
finishing times f[v]

d=∞
f=∞ A

d=∞ d=∞
f=∞ B C f=∞

d=∞ d=∞
f=∞ D E f=∞

d=∞ F
f=∞
Topological sort - 2
Call DFS(G) to compute the
Time = 1
finishing times f[v]

d=1 Start Vertex: A


f=∞ A

d=∞ d=∞
f=∞ B C f=∞

d=∞ d=∞
f=∞ D E f=∞

d=∞ F
f=∞
Topological sort - 3
Call DFS(G) to compute the
Time = 2
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: B
d=2 d=∞
f=∞ B C f=∞

d=∞ d=∞
f=∞ D E f=∞

d=∞ F
f=∞
Topological sort - 4
Call DFS(G) to compute the
Time = 3
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: B
d=2 d=∞
f=∞ B C f=∞ Next Vertex: D

d=3 d=∞
f=∞ D
d E f=∞

d=∞ F
f=∞
Topological sort - 5
Call DFS(G) to compute the
Time = 4
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: B
d=2 d=∞
f=∞ B C f=∞ Next Vertex: D

d=3 d=∞ Next Vertex: F


f=∞ D
d E f=∞

d=4 F
f=∞
Topological sort – 6
Call DFS(G) to compute the
Time = 4
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: B
d=2 d=∞
f=∞ B C f=∞ Next Vertex: D

d=3 d=∞ F has no next Vertex, move


D
d E f=∞
f=∞ back to D

d=4 F
f=4 As each vertex is finished,
insert it onto the front of a
linked list
Topological sort – 7
Call DFS(G) to compute the
Time = 4
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: B
d=2 d=∞
f=∞ B C f=∞ Next Vertex: D

d=3 d=∞ F has no next Vertex, move


D
d E f=∞
f=∞ back to D

d=4 F
f=4 As each vertex is finished,
insert it onto the front of a
F linked list
Topological sort – 8
Call DFS(G) to compute the
Time = 5
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: B
d=2 d=∞
f=∞ B C f=∞ D has no next Vertex, move
back to B
d=3 d=∞
f=5 D
d E f=∞

d=4 F
f=4

D F
Topological sort – 9
Call DFS(G) to compute the
Time = 6
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
B done, move back to A
d=2 d=∞
f=6 B C f=∞

d=3 d=∞
f=5 D
d E f=∞

d=4 F
f=4

B D F
Topological sort – 10
Call DFS(G) to compute the
Time = 7
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: C
d=2 d=7
f=6 B C f=∞

d=3 d=∞
f=5 D
d E f=∞

d=4 F
f=4

B D F
Topological sort – 11
Call DFS(G) to compute the
Time = 8
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: C
d=2 d=7
f=6 B C f=∞ Next Vertex: E

d=3 d=8
f=5 D
d E f=∞

d=4 F
f=4

B D F
Topological sort – 12
Call DFS(G) to compute the
Time = 8
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
Next Vertex: C
d=2 d=7
f=6 B C f=∞ E is done, move back to C

d=3 d=8
f=5 D
d E f=8

d=4 F
f=4

E B D F
Topological sort – 13
Call DFS(G) to compute the
Time = 9
finishing times f[v]

d=1 Start Vertex: A


f=∞ A
C is done, back to A
d=2 d=7
f=6 B Cc f=9

d=3 d=8
f=5 D
d E f=8

d=4 F
f=4

C E B D F
Topological sort – 14
Call DFS(G) to compute the
Time = 10
finishing times f[v]

d=1 A is also done, DFS


f = 10 A completed

d=2 d=7
f=6 B Cc f=9

d=3 d=8
f=5 D
d E f=8

d=4 F
f=4

A C E B D F
Topological sort – 14
Call DFS(G) to compute the
Time = 10
finishing times f[v]

d=1
f = 10 A

d=2 d=7
f=6 B Cc f=9

d=3 d=8
f=5 D
d E f=8

d=4 F
f=4 This list shows Topological
Sort of G
A C E B D F
Analysis of Traversal Algorithms

• Time Complexity of Traversal


Algorithms:
– With Adjacency Matrix, Θ(V2)
– With Adjacency List, Θ(V+E)
V is the number of Vertices
E is the number of Edges
The End

You might also like