30 DFS Applications
30 DFS Applications
Graph Algorithms
DFS Applications
Types of Edges
● Undirected Graphs
● Directed Graphs
2
Applications
● Connected Components
● Strong Bridges*
*Giuseppe F. Italiano, Luigi Laura & Federico Santaroni, Finding Strong Bridges and Strong Articulation Points in Linear Time,
COCOA 2010.
3
Connected Components
b
d f
a
c e g
4
Connected Components
Complexity:
5
Articulation points and Biconnected comp
Articulation point of a connected graph is a vertex whose deletion disconnects it.
An undirected graph is biconnected if there are at least two vertex disjoint paths
between every pair of vertices, or it does not have any articulation point.
The maximum biconnected subgraphs of an undirected graph are its biconnected
components, whose boundary nodes are the articulation points.
b
d f
a
c e g
6
Articulation points and Biconnected comp
Properties: b
1- DFS forest has biCC as subtrees rooted d f
at articulation points a
c e g
2- No edge between child subtrees and ancestors
3- Root an articulation point if more than one subtree
d
e
f
c
7
Articulation points and Biconnected comp
Properties:
BiCC()
1- DFS forest has biCC as subtrees rooted for(forAll v in V){
at articulation points index = 1;
2- No edge between child subtrees and ancestors if(visit[v] == -1)
DFS(v);
3- Root an articulation point if more than }
one subtree a
DFS(vertex v){
b visit[v] = index++;
b low[v] = visit[v];
d f
a d forEach((v,w) in E){
c e g
e if(visit[w] == -1){
DFS(w);
low[v] = min(low[v],low[w]);
f if(visit[v] <= low[w])
Data Structures: c Mark v;
}
visit[x]: DFS visit time of x else
g
low[x]: Earliest DFS visit time reachable low[v] = min(low[v],visit[w]);
}
from subtree of x using a non-tree edge }
8
Articulation points and Biconnected comp
b
BiCC() d f
for(forAll v in V){
index = 1; a
c e g
if(visit[v] == -1)
DFS(v);
}
DFS(vertex v){
visit[v] = index++;
low[v] = visit[v]; a
forEach((v,w) in E){ b
if(visit[w] == -1){
DFS(w); d
low[v] = min(low[v],low[w]);
e
if(visit[v] <= low[w])
Mark v;
} f
else c
low[v] = min(low[v],visit[w]);
} g
}
9
Bridges and 2-edge Connected Comp
10
Bridges and 2-edge Connected Components
Properties: b
1- DFS forest has biCC as subtrees rooted d f
at bridge vertices a
c e g
2- No edge between subtree and ancestors
e
d
f b
11
Bridges and 2-edge Connected Components
2eCC()
Properties: for(forAll v in V){
index = 1;
1- DFS forest has biCC as subtrees rooted if(vis[v] == -1)
at bridge vertices DFS(v);
}
2- No edge between subtree and ancestors
DFS(vertex v){
a visit[v] = index++;
low[v] = visit[v];
c
forEach((v,w) in E){
b e if(visit[w] == -1){
d
d f DFS(w);
low[v] = min(low[v],low[w]);
a f b
c e g }
else
g low[v] = min(low[v],visit[w]);
}
Data Structures:
visit[x]: DFS visit time of x if(vis[v] == low[v])
Mark (v,par(v));
low[x]: Earliest DFS visit time reachable Report unmarked subtree of v
from subtree of x using a non-tree edge }
12
Bridges and 2-edge Connected Components
2eCC()
for(forAll v in V){ b
index = 1; d
if(vis[v] == -1) f
DFS(v); a
c e g
}
DFS(vertex v){
visit[v] = index++;
low[v] = visit[v];
forEach((v,w) in E){
if(visit[w] == -1){ a
DFS(w);
low[v] = min(low[v],low[w]);
} c
else
e
low[v] = min(low[v],visit[w]); d
}
f b
if(vis[v] == low[v])
Mark (v,par(v)); g
Report unmarked subtree of v
}
13
Relationship
14
Strongly Connected Components
An directed graph is strongly connected if for any pair of vertices there are
directed paths from each to the other.
The maximum strongly connected subgraphs of an undirected graph are its
strongly connected components.
b d
a f
c e
15
Strongly Connected Components
An directed graph is strongly connected if for any pair of vertices there are
directed paths from each to the other.
Brute Force:
g
Complexity:
16
Strongly Connected Components
SCC()
b d
for(forAll v in V){
index = 1;
if(visit[v] == -1) a f
c e
DFS(v);
}
g
DFS(vertex v){
Push v in S;
visit[v] = index++;
low[v] = visit[v];
b
forEach((v,w) in E){
if(visit[w] == -1){ d
DFS(w);
low[v] = min(low[v],low[w]); f
c
}
else if(w in S) g a
low[v] = min(low[v],visit[w]);
}
e
if(visit[v] == low[v])
Report SCC = Pop S upto v;
}
19
Topological Order
20