Lec25 DFS I
Lec25 DFS I
(ESO207)
Lecture 25
• A data structure problem for graphs.
• Depth First Search (DFS) Traversal
• Novel application: computing biconnected components of a graph
1
BFS Traversal
in Undirected Graphs
x b
w
f u
h v d
g r
y
s c
Theorem:
BFS Traversal from x visits all vertices reachable from x in the given graph.
2
Connectivity problem in a Graph
Problem:
Build an O() size data structure for a given undirected graph s.t.
the following query can be answered in O(1) time.
Is vertex reachable from vertex ?
Connectivity problem in a Graph
Problem:
Build an O() size data structure for a given undirected graph s.t.
the following query can be answered in O(1) time.
Is vertex reachable from vertex ?
Connectivity problem in a Graph
Problem:
Build an O() size data structure for a given undirected graph s.t.
the following query can be answered in O(1) time.
Is vertex reachable from vertex ?
Connectivity problem in a Graph
Problem:
Build an O() size data structure for a given undirected graph s.t.
the following query can be answered in O(1) time.
Is vertex reachable from vertex ?
Connectivity problem in a Graph
BFS(x)
CreateEmptyQueue(Q);
Visited(x) true; Label[x] x;
Enqueue(x,Q);
While(Not IsEmptyQueue(Q))
{ v Dequeue(Q);
For each neighbor w of v
{ if (Visited(w) = false)
{ Visited(w) true ; Label[w] x ;
Enqueue(w, Q);
}
}
}
Connectivity(G)
{ For each vertex x Visited(x) false;
For each vertex v in V
Create an array Label;
If (Visited(v) = false) BFS(x);
return Label;
} 7
Analysis of the algorithm
Output of the algorithm:
Array Label[] of size O() such that
Label[x]=Label[y] if and only if x and y belong to same connected component.
Theorem:
An undirected graph can be processed in O() time
to build an O() size data structure
which can answer any connectivity query in O(1) time.
8
Is there alternate way to traverse a graph ?
Yes
More Natural
More Powerful
9
Try to get inspiration from your
Suppose “human
you go executable method”
to Paris.
You wish to design
travel to various
“a machine executablemonuments.
algorithm” for traversing a graph.
• Avoiding loop:
How to avoid visiting a vertex multiple times ?
(keeping track of vertices already visited)
• Completeness :
We must visit all vertices reachable from the start vertex x.
11
A recursive
natural way to traverse a graph
We need a mechanism to
y v
• Avoid visiting a vertex multiple times
w
f We can solve it by keeping a label
“Visited” for each vertex
d like in BFS traversal.
h g
• Trace back in case we reach a dead end.
u r
Recursion takes care of it
z
s
c
12
DFS traversal of G
DFS(v)
{ Visited(v) true;
For each neighbor w of v
{ if (Visited(w) = false)
{ DFS(w) ;
……..;
Add a few extra statements here
}
to get an efficient algorithm
……..; for a new problem
}
}
DFS-traversal(G)
{ For each vertex vϵ V { Visited(v) false; }
For each vertex v ϵ V {
If (Visited(v ) = false) DFS(v);
}
}
13
DFS traversal
a milestone in the area of graph algorithms
14
DFS traversal
a milestone in the area of graph algorithms
Applications:
• Connected components of a graph.
• Biconnected components of a graph.
(Is the connectivity of a graph robust to failure of any node ?)
• Finding bridges in a graph.
(Is the connectivity of a graph robust to failure of any edge)
• Planarity testing of a graph
(Can a given graph be embedded on a plane so that no two edges intersect ?)
• Strongly connected components of a directed graph.
(the extension of connectivity in case of directed graphs)
15
Insight into DFS through an example
DFS(v) begins
v v visits y
y
DFS(y) begins
w y visits f
f DFS(f) begins
b f visits b
h d DFS(b) begins
g
all neighbors of b are already visited
u r DFS(b) ends
control returns to DFS(f)
z f visits h
DFS(h) begins
s
…. and so on ….
c
After visiting z, control returns to
rcsuhfyv
v visits w
DFS(w) begins
…. and so on ….
16
Insight into DFS through an example
y v
w
f
Observation1: (Recursive nature of DFS)
b
h d If DFS(v) invokes DFS(w), then
g
DFS(w) finishes before
? DFS(v).
u r
z
s
c
17
Insight into DFS through an example
y v
w
f Question :
b When DFS reaches a vertex u, what is the role
h d of vertices already visited ?
g
u r
18
Insight into DFS through an example
X
y v
w
f Observation 2:
Let X be the set of vertices visited before DFS traversal
b reaches vertex u for the first time.
h d
g The DFS(u) pursued now is like
fresh DFS(u) executed in graph G\X.
u r
NOTE:
z G\X is the graph G after removal of all vertices X along
s with their edges.
c
19
Proving that
DFS(v) visits all vertices reachable from v
By induction on the
size of connected component of v
Can you figure out the inductive assertion now?
Think over it. It is given on the following slide…
20
Inductive assertion
A():
If a connected component has size = , then DFS from any of its vertices will visit all its vertices.
PROOF:
Base case: =1.
The component is {v} and the first statement of DFS(v) marks it visited.
So A(1) holds.
Induction hypothesis:
If a connected component has size < , then DFS from any of its vertices will visit all its vertices.
Induction step:
We have to prove that A() holds.
Consider any connected component of size .
Let V* be the set of its vertices. |V*|= .
Let v be any vertex in the connected component.
21
DFS(v)
Let y be the first neighbor visited by v.
v B= the set of vertices such that v is present on every path
y
from y to them.
w C= V*\B.
f
b B= {v, g, w, d} |B|< i since y B
h d C= {y, b, f, h, u, s, c, r, z}
g |C|< i since v C
Question: What is DFS(y) like ?
u r Answer: DFS(y) in G\{v}.
Question: What is the connected component of y in G\{v} ?
z
s
c
22
DFS(v)
Let y be the first neighbor visited by v.
v B= the set of vertices such that every path from
y
y to them passes through v.
w C= V*\B.
f
b B= {v, g, w, d} |B|< i since y B
h d C= {y, b, f, h, u, s, c, r, z}
g |C|< i since v C
Question: What is DFS(y) like ?
u r Answer: DFS(y) in G\{v}.
Question: What is the connected component of y in G\{v} ?
z Answer: C.
s |C|< i, so by I.H., DFS(y) visits entire set C & we return to v.
c Question: What is DFS(v) like when DFS(y) finishes ?
Answer: DFS(v) in G\C.
Question: What is the connected component of v in G\C ?
23
DFS(v)
Let y be the first neighbor visited by v.
v B= the set of vertices such that every path from
y
y to them passes through v.
w C= V*\B.
f
b B= {v, g, w, d} |B|< i since y B
h d C= {y, b, f, h, u, s, c, r, z}
g |C|< i since v C
Question: What is DFS(y) like ?
u r Answer: DFS(y) in G\{v}.
Question: What is the connected component of y in G\{v} ?
z Answer: C.
s |C|< i, so by I.H., DFS(y) visits entire set C & we return to v.
c Question: What is DFS(v) like when DFS(y) finishes ?
Answer: DFS(v) in G\C.
Hence entire component Question: What is the connected component of v in G\C ?
of v gets visited Answer: B.
|B|< i, so by I.H., DFS(v) pursued after finishing DFS(y) visits
24
entire set B.
Theorem: DFS(v) visits all vertices of the connected component of v.
Homework:
Use DFS traversal to compute all connected components of a given G
in time O().
25