Data Structures: R. K. Ghosh
Data Structures: R. K. Ghosh
R. K. Ghosh
IIT Kanpur
Graph Algorithms
R. K. Ghosh Graphs
Definition of a Graph
R. K. Ghosh Graphs
Definition of a Graph
R. K. Ghosh Graphs
Examples of Graphs
1 2 5 6
3 4 7 8
Undirected graph Directed graph
R. K. Ghosh Graphs
Graph Terminology
R. K. Ghosh Graphs
Examples of Subgraph
3
3 Subraph 1
1 2
8 1 2
8 1
9 4 Subraph 2
9 4
6 5
5 6
Subraph 3
7
7
Original graph
R. K. Ghosh Graphs
Adjacency Matrix Representation
1
1 2 3 4
1 0 1 1 1
2 4 2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
3
R. K. Ghosh Graphs
Adjacency List Representation
1 Adjacency list
2 3 4
1 3
2 4
1 2 4
1 3
3
R. K. Ghosh Graphs
Degrees of Vertices
I V = {a, b, c, d, e}
I E = {(a, b), (a, b), (a, d), (b, e),
a b (c, d), (c, e), (d, e)}
I Degree of a vertex v: # edges incident
c
on v.
d e I deg(a) = 3, deg(b) = 2, deg(c) = 3,
deg(d) = 3, deg(e) = 3,
I # of odd degree vertices is even.
R. K. Ghosh Graphs
Connectedness of Graphs
a b a b
c c
d e d e
R. K. Ghosh Graphs
Applications of Graph
R. K. Ghosh Graphs
Maze to Graph
Maze graph.
・Vertex = intersection.
・Edge = passage.
intersection passage
intersection passage
R. K. Ghosh Graphs
Depth First Search
// Initializations
index = 0;
for all ( v ∈ V ) {
mark [ v ] = ” u n v i s i t e d ” ;
T = Φ ; / / Tree edges
}
choose ( s ) ; / / S t a r t v e r t e x
DFS( s, G ) ;
R. K. Ghosh Graphs
Depth First Search
procedure DFS( G, v ) {
mark [ v ] = ” v i s i t e d ” ;
d f n [ v ] = ++ i n d e x ; / / DFS numbers
for all ( w ∈ ADJG ( v ) ) {
i f ( mark [ w ]== ” u n v i s i t e d ” ) {
T = T ∪ {(v, w) } ; / / Update T
DFS(G, w) ; / / Recursive c a l l
}
}
}
R. K. Ghosh Graphs
Depth First Search Example
R. K. Ghosh Graphs
Correctness of DFS
Lemma
DFS procedure is called exactly once for each vertex.
Proof.
I Once DFS is called for a particular vertex v, it is marked as
”visited”.
I DFS is never called out on ”visited” vertices.
R. K. Ghosh Graphs
Running Time of DFS
Lemma
Running time of DFS procedure is (V + E).
Proof.
I Also obvious from algorithm’s pseudo code.
I Procedure is called once for each vertex.
I But it traverses each edge exactly twice: once in forward
direction and once in reverse direction.
R. K. Ghosh Graphs
Classification of Edges
R. K. Ghosh Graphs
Edge Types in DFS of Undirected Graphs
i h
a b d e f
c g
10
i
1 2 3 4
a c b h
5 6 7 8
9
d e g f
R. K. Ghosh Graphs
DFS of Directed Graphs
R. K. Ghosh Graphs
Edge Types in DFS of Directed Graphs
a 1 a
cx
i b c 2 b c
9
back
cx
fwd 3 d h
7
h d
4 e i
8
g e
cx g
f
5 6
f
R. K. Ghosh Graphs
DFS of Disconnected Graphs
// Initializations
index = 0;
for all ( v ∈ V ) {
mark [ v ] = ” u n v i s i t e d ” ;
T = Φ ; / / Tree edges
}
for all ( v ∈ V ) {
i f ( mark [ v ] == ” u n v i s i t e d ” )
DFS( v, G ) ;
}
R. K. Ghosh Graphs
Iterative DFS
R. K. Ghosh Graphs
Iterative DFS
IterativeDFS (G , v ) {
// Initialization
index = 0;
T = Φ;
makeNull ( S ) ; / / D e f i n e an empty s t a c k
for all ( v ∈ V )
mark [ v ] = u n v i s i t e d ;
choose ( s ) ; / / S t a r t v e r t e x
S . push ( s ) ;
/ / Remaining p a r t i n n e x t s l i d e
}
R. K. Ghosh Graphs
Iterative DFS
while ( ! isEmpty ( S ) ) {
v = S . pop ( ) ;
i f ( marked [ v ] == ” u n v i s i t e d ” ) {
mark [ v ] = ” v i s i t e d ” ;
d f n [ v ] = ++ i n d e x ;
for all w ∈ ADJG ( v ) {
i f ( marked [ w ]== ” u n v i s i t e d ” ) {
S . push ( w ) ;
}
}
}
}
R. K. Ghosh Graphs
Iterative DFS
R. K. Ghosh Graphs
Breadth First Search
// Initialization
index = 0;
T = Φ;
Q = NULL ;
for all ( v ∈ V )
mark [ v ] = ” u n v i s i t e d ” ;
choose ( s ) ; / / S t a r t v e r t e x
b f n [ s ] = ++ i n d e x ;
ENQUEUE(Q, s ) ;
/ / Remaining p a r t i n n e x t s l i d e
R. K. Ghosh Graphs
Breadth First Search
R. K. Ghosh Graphs
Breadth First Search
v w Action Queue
- - bfn(S) = 1 {S}
S A bfn(A) = 1 {A}
F 4 B bfn(B) = 2 {A,B}
T A F bfn(F) = 4 {B,F}
C bfn(C) = 5 {B,F,C}
T E bfn(E) = 6 {B,F,C,E}
2 A C 5
T T B D BFN(D) = 7 {F,C,E,D}
E None {F,C,E,D}
1 S 6 E
S None {F,C,E,D}
T
F A None {C, E, D}
C A None {E, D}
T D None {E, D}
B D 7
E A None {D}
3
B None {D}
D C None {}
B None {}
R. K. Ghosh Graphs
Classification of Edges by BFS
R. K. Ghosh Graphs
Parenthesis Theory
R. K. Ghosh Graphs
Parenthesis Theory
i h
a b d e f
c g
[13,14]
i
[1,18] [2,17] [3,16] [4,15]
a c b h
[5,12]
d e g f
[6,11] [7,10] [8,9]
R. K. Ghosh Graphs
Parenthesis Theory
(a (c (b (h (i i) (d (e (g (f f) g) e) d) h) b) c) a)
R. K. Ghosh Graphs
Connected Components
R. K. Ghosh Graphs
Connected Components
index = 0;
count = 1 ; / / i n i t i a l i z e
for all ( v ∈ V ) {
mark [ v ] = ” u n v i s i t e d ” ;
}
for all ( v ∈ V ) {
i f ( mark [ v ]== ” u n v i s i t e d ” ) {
DFS(G, v ) ;
i n c r e m e n t ( count ) ; / / Component number
}
}
R. K. Ghosh Graphs
Depth First Search
DFS(G, v ) {
mark [ v ] = ” v i s i t e d ” ;
cID [ v ] = g e t ( count ) ; / / Component ID
d f n [ v ] = ++ i n d e x ; / / DFS number
for all (w ∈ ADJG ( v ) ) {
i f ( mark [ w]== ” u n v i s i t e d ” ) {
parent [w] = v ;
DFS(G, w) ;
}
}
}
R. K. Ghosh Graphs
Topological Sorting
Definition
A linear total ordering of the vertices of directed graph, such
that for each edge u → v, u appears before v in the list.
R. K. Ghosh Graphs
Topological Sorting
R. K. Ghosh Graphs
Topological Sorting
m n o p m n p
q r s x q r o
t u v w u y s
t
x y z v
p n o s m r u y v w z q t x
z
R. K. Ghosh Graphs
Topological Sorting
m n o p o
q r s q r s
t u v w t u v w
x y z x y z
List: m, n, p List: m, n, p, o, q
R. K. Ghosh Graphs
Topological Sorting
r s
t u v w
t u v w
x y z
List: m, n, p, o, q, s x y z
r List: m, n, p, o, q, s, r, u, y
t u v w t v w
x y z x z
List: m, n, p, o, q, s, r List: m, n, p, o, q, s, r, u, y, t, v
R. K. Ghosh Graphs
Topological Sorting
R. K. Ghosh Graphs
Weighted Graphs
R. K. Ghosh Graphs
Adjacency Matrix/List of Weighted Graphs
R. K. Ghosh Graphs
Example
2 1 b,2 c,10
10 a b
c 2
5 a,2 d,5 e,6 f,16
19
3
6 16
d 4
12 f 5
14 10
7
3 6
e
9 1
7
g h
4 8
R. K. Ghosh Graphs
Shortest Paths
R. K. Ghosh Graphs
Djikstra’s Algorithm
R. K. Ghosh Graphs
Example
14
R. K. Ghosh Graphs
Set Partitions
I2 5 6
R. K. Ghosh Graphs
Shortest Paths
Edge from S old d[v] new d[v] old p[v] new p[v]
2 ∞ 6 undef 1
3 ∞ 2 undef 1
4 ∞ 16 undef 1
7 ∞ 14 undef 1
R. K. Ghosh Graphs
Shortest Paths
2
S 1 3
6 16 7 8
5 3 14
5 5 1
I1 2 4 5 6 7
4
5
3
R. K. Ghosh Graphs
Shortest Paths
Now set S = {1, 3} and only edges with end points 1 and 3 are
considered for relaxation.
Edge from S old d[v] new d[v] old p[v] new p[v]
2 6 6 1 1
4 16 16 1 1
5 ∞ 5 undef 3
6 ∞ 10 undef 3
7 14 14 1 1
R. K. Ghosh Graphs
Shortest Paths
2 3
S 1 3 5
14
8
6 16 7 5 5 4
5
5 1
I1 2 4 6 7
R. K. Ghosh Graphs
Shortest Paths
Now set S = {1, 3, 5} and only edges with end points 1, 3 and 5
are considered for relaxation.
Edge from S old d[v] new d[v] old p[v] new p[v]
2 6 6 1 1
4 16 9 1 5
6 10 10 3 3
7 14 14 1 1
R. K. Ghosh Graphs
Shortest Paths
7
3
6 2
S 2 1 3 5
16 5
5 14 4
5
8
I1 4 6 7
1
R. K. Ghosh Graphs
Shortest Paths
R. K. Ghosh Graphs
Shortest Paths
5
5 3
7 16
6 2 5 5
S 2 1 3 4 5
8 3
14 4
I1 6 7
1
R. K. Ghosh Graphs
Shortest Paths
R. K. Ghosh Graphs
Shortest Paths
5
5 3 8
7 16
6 2 5 5
S 2 1 3 4 5 6
3 4
14 1
I1 7
R. K. Ghosh Graphs
Shortest Paths
R. K. Ghosh Graphs
Shortest Paths
14
5
5
3
7 16 10
5
S 2 1 3 4 5 6 7
6 2 5 1
3 8
R. K. Ghosh Graphs
Pseudo Code for Dijkstra’s Algorithm
// Initializations
for all ( v ∈ V ) {
d [ v ] = ∞ ; / / I n i t i a l i z e distances
p [ v ] = undef ; / / Predecessor i n t h e path
}
choose ( s ) ; / / Source
d [ s ] = 0; / / I n i t i a l i z e source d i s t a n c e
Q = V; / / I n i t i a l i z e queue w i t h v e r t e x s e t
R. K. Ghosh Graphs
Pseudo Code for Edge Relaxation
Relax ( u , v ) {
new d = min {d [ v ] , d [ u ] + w( u , v ) } ;
i f ( new d [ v ] < d [ v ] ) {
d [ v ] = new d ;
p[v] = u;
}
}
R. K. Ghosh Graphs
Optimal Substructure Property
R. K. Ghosh Graphs
Correctness of Dijkstra
R. K. Ghosh Graphs
Correctness of Dijkstra
R. K. Ghosh Graphs
Correctness of Dijkstra
R. K. Ghosh Graphs
Running Time of Dijkstra’s Algorithm
R. K. Ghosh Graphs
Running Time of Dijkstra’s Algorithm
R. K. Ghosh Graphs
Handling Negative Weight
R. K. Ghosh Graphs
Bellman-Ford Algorithm
e1 , e2 , . . . , em , e1 , e2 , . . . , em , · · · , e1 , e2 , . . . , em ,
relaxation relaxation relaxation
Repeat |V | − 1 times
R. K. Ghosh Graphs
Bellman Ford
Initialize () ;
f o r ( i =1; i < | V | ; i ++) {
foreach edge ( u , v ) {
Relax ( u , v , w) ;
}
foreach edge ( u , v ) {
i f ( d [ v ] > d [ u ] + w( u , v ) )
report negative cycle ;
}
}
Relax ( u , v , w) {
i f ( d [ v ] > d [ u ] +w( u , v ) ) {
d [ v ] = d [ u ] +w( u , v ) ;
pred [ v ] = u ;
}
}
R. K. Ghosh Graphs
Correctness of Bellman-Ford
Proof.
I Basis: Initially d[s] = 0 and d[v] = ∞ for all v ∈ V − {s}.
– The shortest path from s to itself is zero.
– At this point no edge is used for relaxation, consequently,
the shortest path to other vertices are not known.
– So, d[v] correctly gives shortest path where each path may
have at most 0 edges.
R. K. Ghosh Graphs
Correctness of Bellman-Ford
Proof.
I Hypothesis: Assume that after kth iteration of relaxation
step, d[v] is the shortest path from s by using at most k
edges.
I Induction step: Now consider k + 1th iteration of
relaxation step.
– Consider some arbitrary vertex v.
– In k + 1th iteration, relaxation is performed by each
incoming edge (u, v).
– At this point d[u] is the shortest path from s to u which used
upto k edges.
– After k + 1th iteration, since d[v] = min{d[v], d[u] + w(u, v)},
d[v] should have shortest path from source to v with at most
k + 1 edges.
R. K. Ghosh Graphs
Correctness of Bellman-Ford
R. K. Ghosh Graphs
Minimum Spanning Tree
R. K. Ghosh Graphs
Minimum Spanning Tree
R. K. Ghosh Graphs
Minimum Spanning Tree
Root vertex
2(1)
10 (7) a b
c
5(2)
19
6(3) 16
d
12 f
14 10
7 3(6)
e 1(4)
9
g h
4(5)
Edge weight(iteration#), Total weight = 30
R. K. Ghosh Graphs
Correctness of Prim’s Algorithm
R. K. Ghosh Graphs
Correctness of Prim’s Algorithm
R. K. Ghosh Graphs
Correctness of Prim’s Algorithm
R. K. Ghosh Graphs
Correctness of Prim’s Algorithm
R. K. Ghosh Graphs
Greedy Algorithm
R. K. Ghosh Graphs
Implementation Aspect
R. K. Ghosh Graphs
Implementation Aspect
R. K. Ghosh Graphs
Pseudo Code
foreach v ∈ V {
d[v] = ∞;
c o l o r [ v ] = ”W” ; / / No path a t a l l
pred [ v ] = ”U” ; / / u n d e f i n e d
}
d [ s ] = 0; / / source v e r t e x s
pred [ s ] = n i l ; / / so , s has no pred
Q = newPriorityQ ( ( v ,d [ v ] ) for a l l v ∈ V ) ;
while ( ! isEmpty (Q) ) {
u = Q. deleteMIN ( ) ; / / minimum d [ v ]
foreach v ∈ ADJ[u]
RelaxEdge ( G , (u, v) ) ;
c o l o r [ u ] = ”B” / / included i n S
}
R. K. Ghosh Graphs
Code for Relaxation
RelaxEdge ( G , ( u, v ) ) {
i f ( c o l o r [ v ] = ”W” && (w( u, v ) < d [ v ] ) ) {
d [ v ] = w( u, v ) ; / / new l i g h t e s t edge
Q. decreaseCost ( v , d [ v ] ) ; / / decrease d [ v ]
pred [ v ] = u ; / / C u r r e n t predecessor
}
}
R. K. Ghosh Graphs
Running Time
R. K. Ghosh Graphs
Kruskal’s Algorithm
R. K. Ghosh Graphs
Pseudocode for Kruskal’s Algorithm
Q = newPriorityQ ( a l l T r i p l e t s ) ; / / ( u, v, wuv )
T = Φ ; / / No edge i n i t i a l l y
foreach ( v ∈ V )
Sv = {v } ; / / Each v i s a s e t
while ( |T | < |V | − 1 ) {
( u, v, wuv ) = Q. deleteMIN ( ) ; / / minimum wuv
Sv = FIND ( v ) ;
Su = FIND ( u ) ;
i f ( Sv 6= Su ) {
UNION( Su , Sv ) ;
T = T ∪ { ( u, v ) } ;
}
}
R. K. Ghosh Graphs
Kruskal Example
9
a b Edge Weight
13
4 min −→ (d, e)
3
7 5 e (a, d) 4
3 (b, d) 5
c d
8 (a, c) 7
Original graph (a, b) 9
(b, d) 13
a b
Choosen edge
e
c 3
d
T = {(d,e)}
Initial forest
R. K. Ghosh Graphs
UNION and FIND
R. K. Ghosh Graphs
UNION and FIND
R. K. Ghosh Graphs
A Pathological Case
n UNION(1, 2, 2)
UNION(2, 3, 3)
n−1 ..
.
n−2 UNION(n − 1, n, n)
n−1
X
.. Cost of FINDs = i FIND(1)
. 0
FIND(2)
2 ..
.
1 FIND(n)
R. K. Ghosh Graphs
UNION and FIND
R. K. Ghosh Graphs
UNION and FIND
1 2 3 4 5 6 7 8
1 2 3 4 5 7
6 8
1 2 3 4 5
6 7
8
R. K. Ghosh Graphs
UNION and FIND
Lemma
Executing UNION makes the root of smaller tree a child of root
of larger tree then no tree in forest has a height ≥ h unless it
has 2h vertices.
Proof.
I Proof is by induction on height of the tree.
I For h = 0, every tree has 20 = 1 vertex .
I Assume it to be true for all values ≤ h − 1.
R. K. Ghosh Graphs
UNION and FIND
Proof.
I Now consider tree of T of height h with fewest number of
nodes.
I T must have been obtained by merging of two trees T1
(larger) and T2 (smaller) where,
– T1 has a height h − 1 and T2 has no more than number of
nodes in T1 .
– T1 by induction has 2h−1 nodes.
– T2 , therefore has 2h−1
I Hence, T has at least 2h nodes.
R. K. Ghosh Graphs
UNION and FIND
I Since the root of the smaller tree becomes the child of the
root of larger tree, no tree can have a height larger than
log n.
I So, O(n) UNION and FIND can cost at most O(n log n).
I The running time can be improved to O(G(n)n) using path
compression, where
– G(n) ≤ 5 for n ≤ 265536 .
I Analysis is involved and is not covered.
R. K. Ghosh Graphs
Path Compression
r r
w w u v
R. K. Ghosh Graphs
Running Time
R. K. Ghosh Graphs
Biconnectivity
R. K. Ghosh Graphs
Biconnected Components
g Articulation
points: e, f, i
e f
e e f
f
a b h i a b h i
c d j k c d j k
R. K. Ghosh Graphs
Alternative Definition of Biconnected Graphs
R. K. Ghosh Graphs
Block Cut Vertex Tree
R. K. Ghosh Graphs
Block Cut Vertex Tree
12
Blocks Vertices
10 11 9 b1 1, 2
5 13
15
18 b2 1, 3, 4
8
2 1 6 b3 1, 5, 6, 7
b4 8, 9, 10, 11, 12
7
16 b5 6, 8, 13, 14, 15
14 17
4 3 b6 15, 16
b7 15, 17, 18
Cut vertices (shown in red):
c1 = 1, c2 = 6, c2 = 8, c2 = 15
R. K. Ghosh Graphs
Block Cut Vertex Tree
b4
I c1 belongs to b1 , b2 , b3 .
c3
b1 c1 c4 I c2 belongs to b3 , b5 .
c2 I c3 belongs to b4 , b5 .
b3 b5 b7
I c4 belongs to b5 , b6 , b7 .
b2 b6
R. K. Ghosh Graphs
Central Idea of Algorithm
R. K. Ghosh Graphs
Low Point Function
R. K. Ghosh Graphs
Low Point Function
Definition
LOW point LOW[v] is the smallest of dfn[x], where x is a vertex
in G that can be reached by a sequence of zero or more tree
edges followed by at most one back edge.
R. K. Ghosh Graphs
Finding Articulation Point
R. K. Ghosh Graphs
Articulation Point
Theorem
Let G = (V, E) be a connected graph with DFS tree T and back
edges B. Then a ∈ V is an articulation point if and only if there
exists vertices v, w ∈ V such that (a, v) ∈ T and w is not a
descendant of v in T and LOW[v] ≥ dfn[a].
Proof.
I Let v and w exist as stated.
I Since (a, v) ∈ T and LOW[v] ≥ dfn[a], all paths from v not
passing through a must remain inside subtree Tv of v.
I As w is not descendant of v, w 6∈ Tv .
I So all paths from v to w must pass through a.
R. K. Ghosh Graphs
Articulation Point
Proof continues.
I Conversely, let a is an articulation point.
I If a is root, at least two tree edges are incident at a.
– Otherwise between any two vertices in V − {a} there is a
path avoiding a.
I If a is not the root, it has at least one ancestor w.
I One of the biconnected components containing a has all its
nodes as descendants of a in T .
I In fact, all of these nodes are descendant of some child v
of a in T .
I v and w can reach each other only through a.
R. K. Ghosh Graphs
Biconnectivity
R. K. Ghosh Graphs
Computation of LOW Point
R. K. Ghosh Graphs
Computation of LOW Point
i = 0; / / counter f o r dfn
S = newSTACK ( ) ;
for ( x ∈ V ) dfn [ v ] = 0
for x∈V
i f ( d f n [ x ] == 0 ) Bicon ( x, 0 ) ;
procedure Bicon ( v, u ) {
i = i + 1;
dfn [ v ] = i ;
LOW[ v ] = i ;
/ / main l o o p
}
R. K. Ghosh Graphs
Computation of LOW Point
f o r ( w ∈ ADJ(v) ) {
i f ( d f n [ w ] == 0 ) { / / ( v, w ) i s t r e e edge
S . push ( w, v ) ;
Bicon ( w, v ) ; / / Recursive c a l l
LOW[ v ] = min{LOW[ v ] , LOW[ w ] } ;
i f LOW[ w ] ≥ d f n [ v ] { / / A r t i c u l a t i o n p o i n t
d e l e t e a l l edges from S i n c l u d i n g
up t o and i n c l u d i n g edge ( v, w ) ;
}
} else i f ( d f n [ w ] < d f n [ v ] and w 6= u ) {
S . push ( v, w ) ; / / ( v, w ) i s back edge
LOW[ v ] = min{LOW[ v ] , d f n [ w ] } ;
}
}
R. K. Ghosh Graphs
Explanation of Algorithm
R. K. Ghosh Graphs
Explanation of Algorithm
R. K. Ghosh Graphs
Correctness of Algorithm
R. K. Ghosh Graphs
Correctness of Algorithm
R. K. Ghosh Graphs
Correctness of Algorithm
R. K. Ghosh Graphs
Notion of Connectedness in Directed Graphs
R. K. Ghosh Graphs
Strong Connected Components (SCC)
Definition (SCC)
Let G = (V, E) be directed graph. G is strongly connected iff for
every pair of vertices v, w, there is a directed path from v to w
and also a directed path from w to v.
R. K. Ghosh Graphs
Strong Connected Components
v
SC1 SC2
I There are four SCCs.
x I One SCC has just one
w
vertex.
u I Consider u, v ∈ SC2 .
SC4 I There exists pair of paths:
u → w → x → v and
v → w → u.
SC3
R. K. Ghosh Graphs
Strong Connected Components
R. K. Ghosh Graphs
Strong Connected Components
d 6/13
c 3/14 d 13
c
14
4/5 e
i 7/12 5 e
19/20 i 12
17/22 f g 20
22 f g
h j k
h j k
18/21 8/11 9/10
21 11 10
R. K. Ghosh Graphs
Strong Connected Components
15
16
a b
I Start vertices in
d 13 decreasing order of finish
c
14 time: f , a, b, and k.
I DFS from from these
5 e
i 12 vertices discover SCCs:
20 – f : {f, g, h}
22 f g – a: {a}
– b: {b, c, d, e}
– k: {i, j, k}
h j k
21 11 10
R. K. Ghosh Graphs
Correctness of SCC Algorithm
Lemma
Let S1 and S2 be two distinct SCCs. If ∃ an edge (u, v) ∈ E,
where u ∈ S1 and v ∈ S2 then f [S1 ] > f [S2 ]
R. K. Ghosh Graphs
Correctness of SCC Algorithm
Proof.
I Case 1 (d[S1 ] < d[S2 ]): Let x be first vertex in S1 to be
discovered.
I At this time none of the vertices in S1 and S2 have been
marked ”visited”
I For any vertex w ∈ S2 , ∃ a path from x to w.
I So, all vertices in S2 are descendants of x
I Therefore, f [x] = f [S2 ] < f [S1 ]
R. K. Ghosh Graphs
Correctness of SCC Algorithm
Proof continues.
I Case 2 (d[S1 ] > d[S2 ]): Let y be the first vertex discovered
in S2 .
I All vertices in S2 are descendant of y.
I Therefore, by definition f [y] = f [S2 ].
I Since, S1 and S2 are distinct SCC there cannot be path
from any vertex of S2 to a vertex of S1 .
I So all vertices in S1 are ”unvisited” when DFS of S2 is
complete.
I Therefore, f [S2 ] < f [S1 ].
R. K. Ghosh Graphs
Correctness of SCC Algorithm
Proof.
I Since every edge is reversed in E T , it implies (v, u) ∈ E.
I Therefore, f [S1 ] > f [S2 ] from the previous lemma.
R. K. Ghosh Graphs
Correctness of SCC Algorithm
R. K. Ghosh Graphs
Correctness of SCC Algorithm
R. K. Ghosh Graphs
Correctness of SCC Algorithm
I Consider case 2:
– Since d[w] < d[v] in DFS of G, w cannot be an ancestor of
v.
– Furthermore, there cannot be of a cross link on w v path,
because the finish time of a cross link’s source vertex is
greater than the finish time of its end vertex.
– So, case 2 is not possible, leaving case 3 as the only one
possibility.
R. K. Ghosh Graphs
Correctness of SCC Algorithm
I Consider case 3:
– When DFS enters a vertex v in GT , all the vertices in SCCs
with lower finish times remain unvisited.
– And if there is a path w v, it only means w ∈ Tv .
I So, proof is complete by noticing that for every two distinct
components S1 and S2 , if f [S1 ] > f [S2 ] there cannot be an
edge from S1 to S2 in GT .
I The above fact follows directly from the Corollary II proved
earlier.
I This implies w and v both belong to same SCC.
R. K. Ghosh Graphs
Summary of Graph Algorithms
R. K. Ghosh Graphs
Summary of Graph Algorithms
R. K. Ghosh Graphs