Professional Documents
Culture Documents
12 Graph Traversal
12 Graph Traversal
12 Graph Traversal
BFS, DFS
• Graph Traversal Algorithms
• Breadth First Search
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
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=∞ 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=∞ d=∞
f=∞ D E f=∞
d=∞ F
f=∞
Topological sort - 4
Call DFS(G) to compute the
Time = 3
finishing times f[v]
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=4 F
f=∞
Topological sort – 6
Call DFS(G) to compute the
Time = 4
finishing times f[v]
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=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=4 F
f=4
D F
Topological sort – 9
Call DFS(G) to compute the
Time = 6
finishing times f[v]
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=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=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=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=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=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