You are on page 1of 36

Graph Traversal

BFT and DFT

R. Inkulu
http://www.iitg.ac.in/rinkulu/

(BFT and DFT)

1 / 18

Outline

Breadth-first traversal

Depth-first traversal

Comparison

(BFT and DFT)

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

BFT of a directed graph


c

tree edge

BFT(a) BFT(h)
algo convention: unexplored (white), discovered (grey), finished (black)
colors shown represent layers

takes O(|V| + |E|) time and O(|V| + |E|) space:


adjacency list representation of G(V, E)
queues (FIFO) for selecting the next node to explore

(BFT and DFT)

4 / 18

Invariant of BFT algorithm

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

(BFT and DFT)

5 / 18

Key properties of BFT1

There exists a path from s to t in G whenever t appears in some layer of

BFT(s).

unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)

6 / 18

Key properties of BFT1

There exists a path from s to t in G whenever t appears in some layer of

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

Key properties of BFT1

There exists a path from s to t in G whenever t appears in some layer of

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)

respectively for i j, and let e(x, y) be an edge of an undirected graph G.


Then 0 (j i) 1.

unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)

6 / 18

Key properties of BFT (cont)2

In any undirected graph G, each edge joining two nodes in the same

layer causes an odd lengthed cycle.

unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)

7 / 18

Key properties of BFT (cont)2

In any undirected graph G, each edge joining two nodes in the same

layer causes an odd lengthed cycle.


And, each odd lengthed cycle is due to an edge joining two nodes
belonging to the same layer.

unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)

7 / 18

Key properties of BFT (cont)2

In any undirected graph G, each edge joining two nodes in the same

layer causes an odd lengthed cycle.


And, each odd lengthed cycle is due to an edge joining two nodes
belonging to the same layer.
Suppose v Ts . Then the simple path from s to v in Ts is a shortest path

from s to v in G(V, E).


* hence, root to leaf paths are as short as possible

unless specfied otherwise, assume that G is a directed graph whose underlying undirected
(BFT and DFT)

7 / 18

Key properties of BFT (cont)2

In any undirected graph G, each edge joining two nodes in the same

layer causes an odd lengthed cycle.


And, each odd lengthed cycle is due to an edge joining two nodes
belonging to the same layer.
Suppose v Ts . Then the simple path from s to v in Ts is a shortest path

from s to v in G(V, E).


* hence, root to leaf paths are as short as possible
For a directed graph G, a node v belongs to the tree produced by

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

A simple application: checking bipartiteness

An undirected graph G is bipartite (or 2-colorable)


there is no odd cycle in G

(BFT and DFT)

8 / 18

A simple application: checking bipartiteness

An undirected graph G is bipartite (or 2-colorable)


there is no odd cycle in G
there does not exist an edge joining two nodes in the same layer of
BFT(s), for any node s of G.
leads to O(|V| + |E|) algo

(BFT and DFT)

8 / 18

Outline

Breadth-first traversal

Depth-first traversal

Comparison

(BFT and DFT)

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

DFT(a) DFT(b) DFT(c)


d/f denotes discovery-time/finish-time
Start from the given vertex and explore the most recently discovered vertex v
completely before backtracking to explore other edges leaving the
predecessor vertex of v.

(BFT and DFT)

10 / 18

DFT of directed graphs

3/6

4/5

2/9

1/10

7/8

h 11/16

14/15

12/13

takes O(|V| + |E|) time and O(|V| + |E|) space:


adjacency list representation of G(V, E)
stack (LIFO) for selecting the next node to explore

(BFT and DFT)

11 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.

(BFT and DFT)

12 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.
3/6

2/9

1/10

C
d

4/5

h 11/16

C
e

7/8

14/15

12/13

T: tree edge, B: back edge, F: forward edge, and C: cross edge

(u, v) is a

(BFT and DFT)

12 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.
3/6

2/9

1/10

C
d

4/5

h 11/16

C
e

7/8

14/15

12/13

T: tree edge, B: back edge, F: forward edge, and C: cross edge

(u, v) is a
tree edge if v is discovered via (u, v)

(BFT and DFT)

12 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.
3/6

2/9

1/10

C
d

4/5

h 11/16

C
e

7/8

14/15

12/13

T: tree edge, B: back edge, F: forward edge, and C: cross edge

(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)

(BFT and DFT)

12 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.
3/6

2/9

1/10

C
d

4/5

h 11/16

C
e

7/8

14/15

12/13

T: tree edge, B: back edge, F: forward edge, and C: cross edge

(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

(BFT and DFT)

12 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.
3/6

2/9

1/10

C
d

4/5

h 11/16

C
e

7/8

14/15

12/13

T: tree edge, B: back edge, F: forward edge, and C: cross edge

(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;

(BFT and DFT)

12 / 18

Categorizing digraph arcs


The predecessor graph of DFT is a collection F of arborescences.
3/6

2/9

1/10

C
d

4/5

h 11/16

C
e

7/8

14/15

12/13

T: tree edge, B: back edge, F: forward edge, and C: cross edge

(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

Key properties of DFT 3

3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)

13 / 18

Key properties of DFT 3


Parenthesis theorem: In DFT of G, for any two vertices u and v of G,

either of the following is true


(i) the discovery and finish time intervals of u and v are disjoint; and neither u
nor v is a descendent of the other in the DFS forest
(ii) the discovery and finish time interval of u is entirely contained in the
interval of v; and u is a descendent of v
(iii) the discovery and finish time interval of v is entirely contained in the
interval of u; and v is a descendent of u

3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)

13 / 18

Key properties of DFT 3


Parenthesis theorem: In DFT of G, for any two vertices u and v of G,

either of the following is true


(i) the discovery and finish time intervals of u and v are disjoint; and neither u
nor v is a descendent of the other in the DFS forest
(ii) the discovery and finish time interval of u is entirely contained in the
interval of v; and u is a descendent of v
(iii) the discovery and finish time interval of v is entirely contained in the
interval of u; and v is a descendent of u
White-path theorem: In F, vertex v is a proper descendent of vertex u if

and only if by the time u is discovered, there exists an unexplored simple


path connecting u to v.

3
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)

13 / 18

Key properties of DFT 3


Parenthesis theorem: In DFT of G, for any two vertices u and v of G,

either of the following is true


(i) the discovery and finish time intervals of u and v are disjoint; and neither u
nor v is a descendent of the other in the DFS forest
(ii) the discovery and finish time interval of u is entirely contained in the
interval of v; and u is a descendent of v
(iii) the discovery and finish time interval of v is entirely contained in the
interval of u; and v is a descendent of u
White-path theorem: In F, vertex v is a proper descendent of vertex u if

and only if by the time u is discovered, there exists an unexplored simple


path connecting u to v.
A directed graph G is acyclic if and only if depth-first search of G yields

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

Key properties of DFT (cont)4

For an undirected graph G (unlike directed graph), every edge of G is

either a tree edge or a back edge.

4
unless specified otherwise, assume that G is a directed graph whose underlying undirected
graph is connected
(BFT and DFT)

14 / 18

Key properties of DFT (cont)4

For an undirected graph G (unlike directed graph), every edge of G is

either a tree edge or a back edge.


For a directed graph G, a node v is a descendent of s in DFTG if and only

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

(BFT and DFT)

15 / 18

Structure of trees produced

trees produced by BFT are wide and shallow


trees produced by DFT are narrow and deep

(BFT and DFT)

16 / 18

Efficiency

DFT is efficient as compared with BFT:


in recursive implementations, no need of a separate data structure (stack)

to store; hence, smaller memory footprint


easy to implement
in practice, performs better

(BFT and DFT)

17 / 18

Typical applications
BFT:
nodes to be searched are known to be near to root
finding shortest paths/short path trees

DFT is preferred due to efficiency over BFT


(BFT and DFT)

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

DFT is preferred due to efficiency over BFT


(BFT and DFT)

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

DFT is preferred due to efficiency over BFT


(BFT and DFT)

18 / 18

You might also like