Professional Documents
Culture Documents
R. Inkulu
http://www.iitg.ac.in/rinkulu/
1 / 18
Outline
Breadth-first traversal
Depth-first traversal
Comparison
2 / 18
Strategy
BFT(1)
Initiate an expanding wave from a given node, say 1, and flood the graph with
that wave until it grows enough to visit all the nodes that the wave can reach
by adding nodes layer by layer, while keeping track of the predecessor vertex
of each discovered vertex.
(BFT and DFT)
3 / 18
tree edge
BFT(a) BFT(h)
algo convention: unexplored (white), discovered (grey), finished (black)
colors shown represent layers
4 / 18
At any time, the queue contains vertices corresponding to only two distinct
layer ids, in specific, layerid(tail) layerid(head) + 1 and the layer ids
increase monotonically from head to tail.
- induction on the number of enqueue and dequeue operations
5 / 18
BFT(s).
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
6 / 18
BFT(s).
The predecessor graph corresponding to BFT(s) is an arborescence Ts
rooted at s.
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
6 / 18
BFT(s).
The predecessor graph corresponding to BFT(s) is an arborescence Ts
rooted at s.
Let x and y be nodes in Ts belonging to layers Li and Lj of BFT(s)
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
6 / 18
In any undirected graph G, each edge joining two nodes in the same
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
7 / 18
In any undirected graph G, each edge joining two nodes in the same
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
7 / 18
In any undirected graph G, each edge joining two nodes in the same
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
7 / 18
In any undirected graph G, each edge joining two nodes in the same
BFTG (s) if and only if s belongs to the tree produced by BFTGT (v).
unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)
7 / 18
8 / 18
8 / 18
Outline
Breadth-first traversal
Depth-first traversal
Comparison
9 / 18
Strategy
8/11
1/16
a
22/23
b 17/20
2/15
7/12
c
9/10
3/14
21/28
18/19
4/13
24/27
25/26
5/6
10 / 18
3/6
4/5
2/9
1/10
7/8
h 11/16
14/15
12/13
11 / 18
12 / 18
2/9
1/10
C
d
4/5
h 11/16
C
e
7/8
14/15
12/13
(u, v) is a
12 / 18
2/9
1/10
C
d
4/5
h 11/16
C
e
7/8
14/15
12/13
(u, v) is a
tree edge if v is discovered via (u, v)
12 / 18
2/9
1/10
C
d
4/5
h 11/16
C
e
7/8
14/15
12/13
(u, v) is a
tree edge if v is discovered via (u, v)
back edge if v is an ancestor of u (self loops are considered as back edges)
12 / 18
2/9
1/10
C
d
4/5
h 11/16
C
e
7/8
14/15
12/13
(u, v) is a
tree edge if v is discovered via (u, v)
back edge if v is an ancestor of u (self loops are considered as back edges)
forward edge if v is a descendent of u
12 / 18
2/9
1/10
C
d
4/5
h 11/16
C
e
7/8
14/15
12/13
(u, v) is a
tree edge if v is discovered via (u, v)
back edge if v is an ancestor of u (self loops are considered as back edges)
forward edge if v is a descendent of u
cross edge otherwise;
12 / 18
2/9
1/10
C
d
4/5
h 11/16
C
e
7/8
14/15
12/13
(u, v) is a
tree edge if v is discovered via (u, v)
back edge if v is an ancestor of u (self loops are considered as back edges)
forward edge if v is a descendent of u
cross edge otherwise; it is
(i) between vertices in the same depth-first tree, as long as one vertex is not
an ancestor of the other, or
(ii) between vertices in different depth-first trees
(BFT and DFT)
12 / 18
3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)
13 / 18
3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)
13 / 18
3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)
13 / 18
no back edges.
3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)
13 / 18
4
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)
14 / 18
if s is a descendent of v in DFTGT .
4
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)
14 / 18
Outline
Breadth-first traversal
Depth-first traversal
Comparison
15 / 18
16 / 18
Efficiency
17 / 18
Typical applications
BFT:
nodes to be searched are known to be near to root
finding shortest paths/short path trees
18 / 18
Typical applications
BFT:
nodes to be searched are known to be near to root
finding shortest paths/short path trees
DFT:
nodes to be searched are deep from the root
cycle detection in directed graphs
strongly connected components
topological sorting
articulation points
18 / 18
Typical applications
BFT:
nodes to be searched are known to be near to root
finding shortest paths/short path trees
DFT:
nodes to be searched are deep from the root
cycle detection in directed graphs
strongly connected components
topological sorting
articulation points
Either of BFT or DFT 5 :
connected components
finding a path between two specified vertices
cycle detection in unirected graphs
testing whether a graph is bipartite
5
18 / 18