### Lecture 6 - BFS and DFS

```MCA 202: Discrete Structures
Instructor
Neelima Gupta
[email protected]
Graph Traversal
Breadth First Search, DFS and their Applications
Thanks: Meghna 22 and Mrityunjaya 23
(MCA 202)
(Undirected Graph)
u1
u2
u3
u4
u6
u5
Red Edges: Tree Edge
Green Edges: Cross Edge
Thanks Nicky Bagaria (29), Prerna Mishra (30)
MCA(2012)
Note: There
are no
forward and
backward
edges in an
undirected
BFS Tree.
Rooted Tree
A tree is said to be a rooted tree if:• One of the vertices can be designated as root say r.
• For every (u,v) edge in the tree
u=parent(v),v=child(u),parent(r)=r.
A Breadth First Search Tree is also a rooted tree.
Thanks Nicky Bagaria (29), Prerna Mishra (30)
MCA(2012)
Tree Edge and Cross Edge
• Ancestor(u): Ancestor of a vertex ‘u’ is any vertex
that lies above ‘u’ on the path between ‘u’ and the
root(including the root).
• Descendant(u): Descendant of a vertex ‘u’ is any
vertex that lies below ‘u’ on a path from ‘u’ down to a
leaf.
• Tree Edge: (u,v) is a tree edge if ‘v’ is visited first
time while scanning u’s neighbours. We set pr(v) = u
• Cross Edge: (u,v) is a cross edge if neither ‘v’ is a
descendant of ‘u’ nor ‘u’ is a descendant of ‘v’.
Thanks Nicky Bagaria (29), Prerna Mishra (30)
MCA(2012)
(Directed Graph)
u1
u2
u3
u4
u6
u5
Red Edges are all Tree edges here.
Green Edges are all Cross Edges.
Black Edges are all Back Edges.
Thanks Nicky Bagaria (29), Prerna Mishra (30)
MCA(2012)
Note: We
can never
have a
forward
edge in a
BFS tree.
Back Edge and Forward Edge
• Back Edge: (u,v) is a Back Edge if ‘v’ is an ancestor of
‘u’.
• Forward Edge: (u,v) is a forward edge if ‘v’ is a
descendant of ‘u’, but ‘v’ is not in child(u).
Thanks Nicky Bagaria (29), Prerna Mishra (30)
MCA(2012)
Complexity
• For undirected graph, very node is scanned twice.
complexity = 2|E| = O(E)
where, E – number of edges
• For directed graph,
complexity = |E|
• If graph is disconnected,
complexity = no. of vertices = V
Therefore,
Overall Complexity = O(max{V,E})
Implementation of BFS
Application of BFS
• Shortest Path
• Bipartite Graphs
Shortest Path
Claim: Let T be a BFS tree with u as root. Let v be a node r hops away from u (i..e the
length of the shortest path between u and v is r) , then the BFS path between ‘u’ & ‘v’ is
of length r.
Proof: Let r = 1, then by the very definition of BFS, it will visit every node in its
neighbourhood so path would be of distance 1.
Suppose the claim holds for all nodes r hops away from u.
Let ‘v’ be node (r+1) hops away from ‘u’, then Э a path
(u=) u0 – u1 – u2 . . . . .ur- ur+1 = v in G.
Then Э a path
(u=) u0 – u’1 – u’2 . . . . .u’r = ur of length r in T ……..by IH
When u’r = ur is visited in the BFS, we should visit v, if it has not already been visited and
hence we get a path of length r+1 between u and v in T.
If v was already visited, then let Let that path be (u=) u0 – u”1 – u”2 . . . . .u”k-1- u”k = v be
the BFS path. Then k < r+1, since r+1 is the length of the shortest path between u and v in
G. This implies that shortest path between u and u”k-1 is < r. Thus by IH, the BFS path
namely (u=) u0 – u”1 – u”2 . . . . .u”k-1 between u and u”k-1 is < r i.e. k – 1 < r i.e k < r + 1
Hence proved.
Thanks: Rakesh 31, Riya 32,
Sanju 33, Saroj 34 (MCA 2012)
Bipartite Graphs
• A graph is said to be bipartite if its vertex set
can be partitioned into two sets u , v and edge
set E={(u , v):u  U and v V }.
Thanks: Rakesh 31, Riya 32,
Sanju 33, Saroj 34 (MCA 2012)
Exercise
• Give a linear time algorithm to determine
whether a graph is bipartite or not.
Thanks: Rakesh 31, Riya 32,
Sanju 33, Saroj 34 (MCA 2012)
Depth first search on an
undirected graph
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Depth First Search
• Depth first search means to search deeper in
the graph as much as possible.
• In the graph numbers representing start and
finish times are from 1 to 2n, where n is the
number of vertices.
• There are tree edges and back edges in depth
first tree.
• There are no cross edges in depth first tree.
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
u1
u2
u11
u3
u4
u10
u5
u7
u12
u6
Thanks: Rakesh 31, Riya 32,
Sanju 33, Saroj 34 (MCA 2012)
u8
u13
u9
u1
u2
(1,
26)
u11
(2, 19)
(5,14)
u3
u4
(4, 15)
(3,
18)
u5
(20, 25)
(21, 24)
u12
u8
(9,12)
u10
u7
u6
u13
u9
(16, 17)
(8, 13)
(6, 7)
(22, 23)
(10, 11)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Back edge (u4, u1)
u1
u2
(1,26)
u11
(2,19)
(5,14)
u3
u4
(4,15)
(3,18)
u5
(20,25)
(21,24)
u12
u8
(9,12)
u10
u7
u6
u13
u9
(16,17)
(8,13)
(6,7)
(22,23)
(10,11)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Depth first search on a
directed graph
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
u1
u2
u11
u3
u4
u10
u5
u7
u12
u6
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
u8
u13
u9
u1
(1, 26)
(16, 25)
(2, 15)
u2
u9
(9,14)
(3, 8)
u4
(4, 5)
u3
u6
u5
(6, 7)
u7
(10,11)
(21, 24)
(17, 20)
u10
u12
u8
u11
u13
(12,13)
(18, 19)
(22, 23)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Cross edges (u6, u4) and (u12, u7)
Forward edge (u1, u4)
Back edge (u10, u1)
u1
(1,26)
(16,25)
(2,15)
u2
u9
(9,14)
(3,8)
u4
(4,5)
u3
u6
u5
(6,7)
u7
(10,11)
(21,24)
(17,20)
u10
u12
u8
u11
u13
(12,13)
(18,19)
(22,23)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Time Complexity
Time Complexity of DFS is <= 2E+V which is O(V+E)
For a connected graph E >= V-1
Thus
O(V+E)=O(E)
Hence,
Time Complexity=O(E)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
: Topological Sort: An Application of
DFS
 Definition: Given a directed graph G=(V,E) and {u,v}
∈ , label the vertices in such a manner that ℓ  <
ℓ  , where ℓ:  → ℝ+, whenever there is a directed
path from u to v in G.
• Considering a cyclic graph:
v
w
u
x
• Does there exist a labeling for this graph?
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
• Corrected Definition: Given a directed acyclic graph
G=(V,E) and {u,v} ∈ , label the vertices in such a
manner that ℓ  < ℓ  , where ℓ:  → ℝ+,
whenever there is a directed path from u to v in G.
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Algorithm
1. Perform DFS on any vertex with in-degree 0
2. Label the vertices in decreasing order of finishing
times
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Example
(1,14)
a
b
c
e
(11,12)
(7, 8)
(3, 4)
(5, 10)
(2,13)
d
f
(6, 9)
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
g
Applications
• Designing Parallel Algorithms
x=a
print x
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Application of DFS
STRONGLY CONNECTED COMPONENTS
Steps to compute Strongly Connected Components of a
graph :1. Perform DFS on the original graph.
2. Compute transpose of a graph GT
i.e. reverse the direction of the edges.
3. Perform DFS on GT in decreasing order of finishing time
f(u) of step 1.
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
EXAMPLE :-
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
(2, 19)
STEP 1:-
(12, 17)
u2
u8
(13, 16)
(1, 20)
u1
(3, 18)
u4
u5
(7, 8)
u7
u9
u3
u10
(4, 11)
(5, 10)
u6
(6, 9)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
(14, 15)
(2,19)
u2
(12,17)
u8
(13,16)
(1,20)
u1
(3,18)
u4
u5
(7,8)
u7
u9
u3
u10
(4,11)
(5,10)
u6
(6,9)
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
(14,15)
STEP 2:-
u2
GT
u1
u8
u9
u3
u10
u4
u5
u7
u6
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
u2
STEP 3:-
u1
u8
u9
u3
u10
u4
u5
u7
u6
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
u2
STEP 3:-
u1
u8
u9
u3
u10
u4
u5
u7
u6
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
The graph is divided into two strongly
connected components :{u1 , u2 ,u3 , u4 , u8 ,u9 ,u10 }
and
{u5 , u6 , u7 }
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Two strongly connected
components
u2
u1
u8
u9
u3
u10
u4
u5
u7
u6
Thanks: Rakesh 31, Riya 32, Sanju 33,
Saroj 34 (MCA 2012)
Implementation
Topological Sort from Saumya, Saurabh
```