You are on page 1of 138

Data Structures

R. K. Ghosh

IIT Kanpur

Graph Algorithms

R. K. Ghosh Graphs
Definition of a Graph

I A graph G consists of a pair of sets V, E denoted by


G = (V, E).
I V : vertex set.
– Each vertex v ∈ V may represent some records, objects or
a piece of information.
I E: edge set.
– Each edge e ∈ E links (relates) one pair of distinct vertices
u 6= v ∈ V .
– There is at most one edge which relates two distinct
vertices.

R. K. Ghosh Graphs
Definition of a Graph

I G is undirected if each edge represents an unordered pair,


i.e., e = (u, v) = (v, u).

I In an undirected graph, E may define upto |V2 | relations
among vertices.
I If (u, v) 6= (v, u), then the edges are said to be directed:
– The edge (u, v) is oriented from u to v.
– The edge (v, u) is oriented from v to u.
I When edges in a graph G are directed, G is known as
directed.
I A directed graph may have upto |V |(|V | − 1) edges.

R. K. Ghosh Graphs
Examples of Graphs

1 2 5 6

3 4 7 8
Undirected graph Directed graph

V = {1, 2, 3, 4} and V = {1, 2, 3, 4} and


E = {(1, 2), (1, 3), (1, 4), (2, 3), (3, 4)} E = {(5, 6), (5, 7), (5, 8), (6, 8), (8, 6)}

R. K. Ghosh Graphs
Graph Terminology

I A graph H = (VH , EH ) is a subgraph of G = (VG , EG ), if


VH ⊆ VG and EH ⊆ EG .
I A simple path in a graph is a sequence of distinct vertices
v1 , v2 , . . . , vk where (vi , vi + 1) ∈ E, for 1 ≤ i ≤ k − 1.
I A cycle is a simple path in which the start and end vertices
are same, i.e., v1 = vk .
I G is connected if there is a path between any two pair of
distinct vertices in G.
I A connected component of a graph G is a maximally
connected subgraph of G
I A graph which does not have any cycle is called acyclic.
I An acyclic undirected graph is a tree.

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

I Adjancy matrix is a |V | × |V | matrix in which each row and


each column represents a vertex. For a undirected graph
A[i, j] = A[j, i].
(
1, if (i, j) ∈ E
A[i, j] =
0, otherwise

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

I Each list represents adjacency relations corresponding to


a vertex.

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

Connected graph Disconnected graph

I Connected graphs: ∃ a path between any two vertices.


I Disconnected graphs: Having more than one connected
subgraphs.

R. K. Ghosh Graphs
Applications of Graph

I Tremaux was obsessed with problem of finding path out of


a maze.
I He came up with technique as follows:
– Unroll a ball of thread to trace of path that is already
traversed.
– Mark each intersection by putting a mark (color).
– Retrace back to recent most intersection when no new visit
options are present.

R. K. Ghosh Graphs
Maze to Graph
Maze graph.
・Vertex = intersection.
・Edge = passage.

intersection passage
intersection passage

From Chapter 4 of Robert Sedgewick and Kevin Wayne’s


Algorithm book.
Goal. Explore every intersection in the maze.
2

R. K. Ghosh Graphs
Depth First Search

I Basic form of processing graphs is traversal.


I DFS and BFS are two important traversal techniques.

// 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

I DFS gives an orientation to edges of an undirected graph.


I Traversing some edges lead to unvisited vertices.
I While the remaining edges lead to visited vertices.
I If a vertex w is found visited during DFS(v), then w must be
an ancestor of v in the DFS tree.
– DFS(v) must have been called during the time DFS(w) call
itself.
– In other words, DFS(w) is still incomplete when DFS(v) was
called.
I So edges are classified into two types: tree edges, and
back 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

I DFS of directed graphs must explore the edges by


respecting the direction of orientation of the edges.
I As usual, tree edges are those edges that always lead to
new (unvisited) vertices.
I Remaining edges are partitioned into three other types.
– Back edges: which lead from a descendant to an ancestor.
– Forward edges: which lead from a proper ancestor to a
descendant
– Cross edges: connects two unrelated vertices w and v. If
the orientation is v → w, then v is visited aftr w.

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

I Algorithm we have presented works for connected graph.


I For DFS of disconnected graphs, we need to change initial
calling of procedure a bit.

// 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

I Use a stack to allow for backtracking during DFS.


I Initialize stack by placing a start vertex v.
I As long as stack is nonempty pop the last vertex and mark
it visited if it s not visited.
I Then push all the other end vertices of the edges incident
on the current vertex.

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

I There may be multiple copies of vertices on the stack.


I But the total number of iterations of stack loop cannot
exceed number edges.
I Thus the size of the stack cannot exceed |E|.
I Try out how you can avoid having multiple copies a vertex
in the stack.

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

while ( ! isEmpty (Q) ) {


v = DEQUEUE(Q) ;
mark [ v ] = ” v i s i t e d ” ;
for all ( w ∈ ADJG ( v ) ) {
i f ( mark [ w ] == ” u n v i s i t e d ” ) {
mark [ w ] = ” v i s i t e d ” ;
T = T ∪ {(v, w) } ;
b f n [ v ] = ++ i n d e x ;
ENQUEUE(Q, w ) ;
}
}
}

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

I There can be no back edges or forward edges in BFS of


undirected graphs.
I For each tree edge (u, v), dist[v] = dist[u] + 1
I For each cross edge (u, v), dist[u] = dist[v] or dist[v] =
dist[u] + 1

R. K. Ghosh Graphs
Parenthesis Theory

I Consider DFS numbers and reverse DFS numbers


generated during DFS of a graph.
I Let u and v be any two vertices in the graph and let d[v]
and f[u] respectively denote discovery time and finishing
time of DFS then exactly one of the following three
conditions hold.
– If the intervals [d[u],f[u]] and [d[v],f[v]] are disjoint neither u
nor v is a descendant of the other in DFS tree/forest.
– If [d[v],f[v]] completely enclosed within [d[u],f[u]] then v is a
descendant of u.
– If [d[u],f[u]] completely enclosed within [d[v],f[v]] then u is a
descendant of v.

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)

I Opening parenthesis corresponds to discovery time d.


I Closing parenthesis corresponds to finish time f .
I Resulting expression is a valid parenthetical matching
string.

R. K. Ghosh Graphs
Connected Components

I How to obtain connected components?


– Using DFS/BFS it is possible.
I Outline of the algorithm is as follows:
– Initialize a connected component number to 0.
– Inside the second for-loop increment connected component
number each time before calling DFS procedure.

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.

I Scheduling constraints between lectures.


I Pre-requisites of your B. Tech degree.
I Various stages or tasks related to completion of projects.

R. K. Ghosh Graphs
Topological Sorting

I Just call DFS to compute reverse DFS number.


I As numbering to a vertex get assigned insert it to the front
of an initally empty linked list.
I Linked list gives the topological sorted sequence in
decreasing order of finish time of the task.

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

Topological Sort: all edges from left to right w

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

I Another simple way to get topological sort is as follows:


– List out all vertices with no incoming edges.
– Remove these vertices and keep repeating two step until all
vertices as listed.

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

w I After deleting w and x only


z is left out.
x z I Just append z to the list.
List: m, n, p, o, q, s, r, u, y, t, v, w, x I As we can check the list
orders that nodes such
Final List: that edges alway directed
m, n, p, o, q, s, r, u, y, t, v, w, x, z from left to right.

R. K. Ghosh Graphs
Weighted Graphs

In introducing graph, as cost is implicitly assumed on links or


edges. The cost is usually given as a numerical value.
Formally, a weighted graph is defined as follows:
Definition (Weighted Graph)
If each edge of a undirected graph is associated with a weight
then, it is known as a weighted graph. A weighted graph is
defined in terms of a triple G = (V, E, w), where w : E → val. is
a function that maps edges/directed edges to their associated
values.
Typically, the value val is a real number.

R. K. Ghosh Graphs
Adjacency Matrix/List of Weighted Graphs

I A natural extension of adjacency matrix for unweighted


graph:
(
wij weight of edge if (i, j) ∈ E
A[i, j] =
undef, if (i, j) 6∈ E

I In adjacency list representation, we store weight along with


adjcency relation inside the list.

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

I Two possible variations in problems:


1 Single source shortest paths
2 All pairs of shortest paths
I Single source shortest path can be executed n times each
with a different source vertex for all pairs shortest path.
I So, let us first examine how single source shortest path
can be solved.

R. K. Ghosh Graphs
Djikstra’s Algorithm

I Dijkstra’s algorithm partitions the set of vertices logically


into three partitions.
– Set S: consists of vertices u ∈ V such that dist[v] (from
source s) already known.
– Set I1 : consists of vertices v ∈ V − S such that each v ∈ I1
is directly connected to a vertex x ∈ S.
– Set I2 : consists of vertices w ∈ V − S − I1 .
I Dijkstra’s algorithm iteratively expands set S to include all
vertices in V .

R. K. Ghosh Graphs
Example

Source vertex: d[1]=0


8
2 6
1 3 3
16 7
5 1
6 5 10
4
3
2 4 7
5

14

R. K. Ghosh Graphs
Set Partitions

Source vertex 1: d[1]=0


S 1
I Initially, set S consists of
6 2 16 14 just the source vertex {1}
5 I I1 consists of vertices
adjacent to 1.
3
I1 2 3 4 7 I I2 consists of remaining
7
vertices, i.e.
8 4 I2 = V − I1 ∪ S.
3
5 1

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

Select vertex 3 (minimum d value) for inclusion into set S. So,


S = {1, 3} and p[3] = 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

I After 1st iteration S = {1, 3}, I1 = {2, 4, 5, 6, 7} and I2 = Φ.

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

Select vertex 5 for inclusion into set S.


So S = {1, 3, 5}, and p[3] = 1, p[5] = 3

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

I After 2nd iteration S = {1, 3, 5}, 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

Select vertex 2 for inclusion into set S. So S = {1, 2, 3, 5}.


p[3] = 1, p[5] = 3, p[2] = 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

I After 3rd iteration S = {1, 2, 3, 5}, I1 = {4, 6, 7}.

R. K. Ghosh Graphs
Shortest Paths

Now set S = {1, 2, 3, 5} and only edges with end points 1, 2, 3


and 5 are considered for relaxation.
Edge from S old d[v] new d[v] old p[v] new p[v]
4 16 9 5 5
6 10 10 3 3
7 14 14 1 1

Select vertex 4 for inclusion into set S. So S = {1, 2, 3, 5}.


p[3] = 1, p[5] = 3, p[2] = 1, p[4] = 5

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

I After 4th iteration S = {1, 2, 3, 4, 5}, I1 = {6, 7}.

R. K. Ghosh Graphs
Shortest Paths

Now set S = {1, 2, 3, 4, 5} and only edges with end points in S


are considered for relaxation.
Edge from S old d[v] new d[v] old p[v] new p[v]
6 10 10 3 3
7 14 14 1 1

Select vertex 6 for inclusion into set S. So S = {1, 2, 3, 4, 5}.


p[3] = 1, p[5] = 3, p[2] = 1, p[4] = 5, p[6] = 3

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

I After 5th iteration S = {1, 2, 3, 4, 5, 6}, I1 = {7}.

R. K. Ghosh Graphs
Shortest Paths

Now set S = {1, 2, 3, 4, 5} and only edges with end points in S


are considered for relaxation.
Edge from S old d[v] new d[v] old p[v] new p[v]
7 14 11 1 6

I Include 7 into set S. So S = {1, 2, 3, 4, 5, 6, 7}.


p[3] = 1, p[5] = 3, p[2] = 1, p[4] = 5, p[6] = 3, p[7] = 6.
I After including the remaining vertex 7 in S in the last
iteration.

R. K. Ghosh Graphs
Shortest Paths

Shortest paths is recovered using


p[3] = 1, p[5] = 3, p[2] = 1, p[4] = 5, p[6] = 3, p[7] = 6.
Figure below highlights the shortest paths from node 1.

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

while ( ! isEmpty (Q) ) {


u = min ( d [ u ] ) ; / / Add new v e r t e x
Q = Q − {u } ; / / Update Q
f o r each ( v ∈ ADJG ( u ) )
Relax ( u , v ) ;
}

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

I Consider a shortest path from s to t.


I Let v be an intermediate vertex on the path.
I Consider subpath s v.
I If the subpath s v is not a shortest path, then ∃ a shorter
path from s to v.
I It implies that that using the above shorter path we can
reduce the cost of shortest path from s to t.

R. K. Ghosh Graphs
Correctness of Dijkstra

I Let v be the first vertex added to S such that d[v] 6= dist[v].


I v cannot be s, because d[s] = 0.
I There must be path from s to v, otherwise d[v] would be ∞.
I Since, there is a finite path , a shortest path also exists.
I Furthermore the shortest path to s v does lie completely
inside S.

R. K. Ghosh Graphs
Correctness of Dijkstra

I Consider shortest path P : s v.


Path P2 I Consider two partitions of
Set S
P = P1 ∪ P2 ,
s v
where P1 completely belongs to
S.
Path P1 x
y I Let y be first vertex on shortest
path from s to v that does not
belong to S.

R. K. Ghosh Graphs
Correctness of Dijkstra

I When x is included in S, d[x] = dist[x] and edge x → y


must have been used to update
d[y] = dist[y] ≤ dist[v] ≤ d[v] =⇒ d[y] ≤ d[v].
I Since both y and v are in V − S when v was chosen,
d[v] ≤ d[y].
I The above two set of inequalities imply
d[y] = dist[y] = dist[v] = d[v].
I So, d[v] = dist[v] is a contradiction.

R. K. Ghosh Graphs
Running Time of Dijkstra’s Algorithm

I Once Q is empty the algorithm terminates.


I On each iteration one vertex (with minimum d value) is
added to S. Finding minimum requires |V | − 1 time.
– So, with |V | − 1 iterations, it gives running time |V |2 .
I After a vertex is included in S it performs updates for d[v]s.
I However, each edge is used exactly only once during the
execution of the algorithm.
– So the cost of update cannot exceed |E|.
I Therefore, the running time is O(|E| + |V |2 ).

R. K. Ghosh Graphs
Running Time of Dijkstra’s Algorithm

I If graph is dense, |E| = Θ(|V |2 ).


I But we can do better than this by keeping d[.] in form of a
heap.
I We create a heap all d[s].
I Use DeleteMIN to select the vertex to be included next.
– It requires total of |V | log |V | unit of time.
I Relaxation step is equivalent to a decrease key operation,
which requires |E| log |V | units of time.
I So, total time: O(|E| log |V | + |V | log |V |).

R. K. Ghosh Graphs
Handling Negative Weight

I Dijkstra’s algorithm cannot handle


negative edge weight even if there is
no negative cycle?
4→24
I The simplest solution could be to
1/11 3/13 turn negative edge weight to zero by
adding a big number, say M .
5/15
-10/0
1/11 I So cost of each path P becomes
M × (hop length of P ) + weight(P ).
-4→26
I So hop length begins to dominate
due to M .

R. K. Ghosh Graphs
Bellman-Ford Algorithm

I Relaxation is the most important part of the operation.


I Label edges as: e1 , e2 , . . . em , where m = |E|.
I Relaxation is repeated |V | − 1 and carried out for each
edge every time.

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

Lemma (Main lemma)


After k iterations of the edge relaxations, d[v] contains correct
shortest path from the source s to every vertes v ∈ V using at
most k edges.

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

I A corollary of the above induction proof leads to the


following conclusion:
– After |V | − 1 iterations of the edge relaxation step, d[v] for
every v ∈ V , contains the weight of the shortest path from s
to v with at most |V | − 1 edges.
– If there is no negative cycle, then every cycle has a
nonnegative cost.
– So, gnoring or deleting the cycle from the path can only
lead to lower cost.
– Therefore, d[v] after completing |V | − 1 relaxation steps will
give correct shortest path from s to v.

R. K. Ghosh Graphs
Minimum Spanning Tree

Definition (Spanning Tree)


A spanning tree T of an undirected connected graph G is a
connected acyclic graph containing all vertices of G.

Definition (Minimum Spanning Tree)


Given a weighted graph find a spanning tree that has minimum
weight.

R. K. Ghosh Graphs
Minimum Spanning Tree

I Uses a strategy similar to Dijkstra’s algorithm.


I Initially, only one vertex v0 is included in VT .
I Then grow VT by including exactly one vertex u ∈ V − VT
by choosing cheapest edge (v, u) ∈ E such that v ∈ VT .
I Repeat previous step until VT = V or V − VT = Φ. in VT

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

Cut line I Let VT be the subset of


vertices forming current T .
a c
I Each edge joining one end
d h
vertex in VT and another in
V − VT is known as a cut
Min edge.
e b f g I So, the edges across the
cut line are cut-edges and
denoted by set C.

C = {(u, v)|u ∈ VT and v ∈ V − VT }.

R. K. Ghosh Graphs
Correctness of Prim’s Algorithm

I Suppose T is the spanning tree found by Prim’s algorithm.


I Let Tmin be the MST of G.
I Assume Tmin 6= T , but we have VT = VTmin = V
I Consider one edge (u, v) ∈ T − Tmin .
I When (u, v) is added to T it was the minimum cost edge
between (VT , V − VT )
I Since (u, v) 6∈ Tmin there must be a path P : u v in Tmin .

R. K. Ghosh Graphs
Correctness of Prim’s Algorithm

I P begins in u ∈ VT and ends in V − VT .


I So, there must be an edge (x, y) connecting a vertex
x ∈ VT and y ∈ V − VT .
0
I Let Tmin = Tmin ∪ {(u, v)} − {(x, y)}.
I Clearly, cycle formed by addition of (u, v) to Tmin is
removed by deleting (x, y).
0
I So, Tmin is a tree and its cost is lower than Tmin .

R. K. Ghosh Graphs
Correctness of Prim’s Algorithm

(u,v) list cost edge


belongs to S
belongs to V − S

R. K. Ghosh Graphs
Greedy Algorithm

MST of induced subgraph

Delete this edge


MST of induced subgraph

I Deleting edge (u, v) results in two MSTs T1 and T2 of


induced subgraphs G1 and G2 respectively.
I If lower cost MSTs T10 or T20 were there then
– w(T ) > w(T10 ) + w(u, v) + w(T2 ) and
– Similarly, w(T ) > w(T1 ) + w(u, v) + w(T20 ).

R. K. Ghosh Graphs
Implementation Aspect

I Use a priority queue to store tuple (v, d[v])


I d[v] is the weight of the lightest edge connecting v to some
vertex in S.
I pred[v] = u, where u ∈ S is the end point of the lightest
edge connecting v.
I So, the collection of pred[.] yields the MST.
I d[u] should be used as a key to extract the triple from the
priority queue.

R. K. Ghosh Graphs
Implementation Aspect

Root vertex 2(1)


10 a b
c 5(2) v d[v] pred[v]
19
6(3) 16
c 10 a
d
f
f 10 e
14 12
10
7
3
g 7 d
e
9 1 min −→ h
1 e
g h
4

I The lightest edge connecting a v ∈ S to a w ∈ V − S: (e, h).


I After deleting this triple, update d[v] and pred[v] in
relaxation step.

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

I O(log |V |) to extract vertex out of queue.


I Done once for each vertex, total time O(|V | log |V |)
I O(log n) to decrease d[v] of neighboring vertex.
I Done at most once for each edge: O(|E| log |V |)
I Total cost O((|V | + |E|) log |V |).

R. K. Ghosh Graphs
Kruskal’s Algorithm

1 Sort all the edges in non-decreasing order of edge weights.


2 Pick the lightest edge and add to an initially empty tree,
insert the edge if no cycle is formed.
3 Otherwise discard the edges and return back to Step 2
until |V | − 1 edges are included in the tree.

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

I Operation on disjoint set is a motivation for Kruskal’s


algorithm.
I It is one of the simplest algorithms but have an involved
proof for running time.
I The simplest representation is a vector R where R[i], for
1 ≤ i ≤ n contains the name of the set to which i belongs.
I So, initially R[i] = i because each set {i} is a set by itself.

R. K. Ghosh Graphs
UNION and FIND

I For UNION(A, B, C), scan R change entries having names


A and B to C.
I The cost for this is O(n).
I A better way is to use trees, where each set as a rooted
tree.
I The root of the tree contains the name of the set.
I Every vertex including the root assume to represent an
element.
I We also maintain a count of the number of nodes in each
tree.

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

I Cost of merging tree is O(1) as it just requires the root of


one tree become a child of the root of other tree.
I So UNION can be done in O(1) time.
I FIND can take at most time of O(n) time, as we need to
climb up the tree from the vertex (a element) to the root of
the tree (name of the set) where it belongs.
I Merging is performed by keeping track of size of each tree.
I Then making the root of the smaller tree as child of the root
of the larger tree.

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

I Effect of path compression during FIND(v).


I The cost is amortised across all future FIND operations.

R. K. Ghosh Graphs
Running Time

I Creating disjoint sets: O(|V |).


I Building priority queue: O(|E|)
I For queue manipulation: O(|E| log |V |).
I Total time O(|E| log |E|).

R. K. Ghosh Graphs
Biconnectivity

Definition (Articulation Point)


A vertex a is called articulation point of G if there exists a pair of
vertices v and w such that a, v and w are distinct and every
path between v to w passes through a. A graph is said to be
biconnected if it does not have any articulation point.

I Removal of a will split G into two or more parts.


I In other words, G is biconnected if G contains no
articulation point.

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

I Three articulation points split graph into four biconnected


components.

R. K. Ghosh Graphs
Alternative Definition of Biconnected Graphs

I A pair of edges e1 and e2 are related if either e1 = e2 , or


there is a cycle containing both edges.
I Above relation defines an equivalence relation and splits
edge set into equivalence classes E1 , E2 , . . . , Ek .
I Let Vi be vertex set of Ei .
I Each graph Gi = (Vi , Ei ) is called a biconnected
component of G.

R. K. Ghosh Graphs
Block Cut Vertex Tree

I A biconnected component is also known as a block.


I Let B be the set of vertices in which each vertext
correspond to a block.
I Let the set of articulation points in G be represented by C.
I Construct a graph of B ∪ C vertices as follows:
– Join b ∈ B to c ∈ C if c belongs to block corresponding to b.
I The resulting graph G = B ∪ C, EB∪C is a 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

I Block and cut vertices are adjacent.


I No two block or no two cut vertices are adjacent.

R. K. Ghosh Graphs
Central Idea of Algorithm

I Central Idea behind the algorithm comes from block and


cut vertx tree
I Suppose DFS enters a block B2 from a block B1 , then it
has to be through an edge (c, w), where c is a common cut
vertex between blocks B1 and B2 and w ∈ B2 .
I Backtracking to c is possible only after completing the
exploration of B2 .
I Implies that all the edges of B2 must be on the top of the
edge (c, w) in STACK.

R. K. Ghosh Graphs
Low Point Function

I After removing edges of B2 , DFS can explore another


block in a similar fashion.
I In other words, DFS works on induced graph G − B2 in
identical way.
I To identify an articulation point we use a function called
LOW point.
– It is based on idea of the oldest reachable ancestor in a
DFST using tree edges and at most one back edge.
I Let T represent DFS tree, and B its set of back edges.

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.

LOW [v] =M IN ({df n[v]} ∪ {LOW [x]|(v, x) ∈ T }


∪ {df n[x]|(v, x) ∈ B})

R. K. Ghosh Graphs
Finding Articulation Point

I Let us use following notations:



– A tree path from v to w: v → w
– A back edge from v to w: v− →w
 

I Then LOW[v] = MIN {v} ∪ {w|v → x and x− →w} ,
I So, LOW[v] is minimum of the following three values:
1 v− →w.
2 DFS number of v.
3 LOW[c] where c is child of v in DFS tree T .

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

From the above theorem, if we have values of DFS numbers


and LOW points then a is an articulation point iff
1 Either a is the root of a DFST with more than 1 children, or
2 a is not the root and there is at least one child c of a such
that no back edge leads from any descendant of c
(including c) to a proper ancestor of a.

R. K. Ghosh Graphs
Computation of LOW Point

I Computation requires a single pass of


1 1 DFS.
I Initially LOW[8] = dfn[8] = 8, but LOW[9]
2 2 8 1
= 1 due to a back edge (9, 1).
2 3 9 1 I LOW is updated after call Bicon(9,8)
completed:
4 4 7 10 8 LOW[8] = MIN {8} ∪ {LOW[9]} = 1.
2
I LOW[10] = 8, since (10, 8) ∈ B.
4 5
I But as LOW[10] < 9, 9 cannot be an
4 6
articulation 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

I DFS numbers of all vertices initialized to 0.


– It serves as vertices marked ”unvisited”
– Assigning DFS number to a vertes implies it is marked
”visited”
I It then selects an arbitrary vertex v = x, builds a DFST,
and computes LOW[v] in one single pass.
I An edge leading to a new vertex is a tree edge.
I The tree edge is pushed on to the stack before making the
recursive call.

R. K. Ghosh Graphs
Explanation of Algorithm

I On return from call LOW[v] is computed.


I If v is an articulation point then its DFS number must be
greater than or equal to LOW point of the child.
I At this point, all the edges up to and including v, w are
output as a biconnected component.
I If w is a old vertex (DFS number is nonzero) then it is a
back edge.
I Only back edge to a proper ancestor of the parent (w 6= u)
would lower the LOW point.

R. K. Ghosh Graphs
Correctness of Algorithm

Lemma (Correctness of LOW point computation)


When Search(w) procedure completes, the edges in the stack
above (v, w) are the edges in the same biconnected
components as (v, w).

I To prove it, we use induction on the number of biconnected


components, b.
I If there is just one biconnected components, i.e., b = 1, it is
trivial.
– In this case, there is no articulation point.
– So all the edges of G will be on the stack.

R. K. Ghosh Graphs
Correctness of Algorithm

I Induction hypothesis: assume this to be true for all graphs


having b biconnected components.
I Let G have b + 1 biconnected components.
I Consider the first Bicon(w, v) call that ends with LOW[w] ≥
dfn[v] for a tree edge (v, w).
I No edge has been removed yet from the STACK.
I Since LOW[w] ≥ dfn[v], all the set of edges above (v, w)
are incident on the descendants of w and first block B1 has
been detected.
I So the edges on the STACK above (v, w) are exactly the
edges in the same biconnected component as (v, w).

R. K. Ghosh Graphs
Correctness of Algorithm

I Now after removing edges of B1 , the algorithm works on


induced graph G0 = G − B1 , in exactly the same way as it
had worked on graph G.
I But G0 has b biconnected components.
I By induction, algorithm should correctly obtain all b
biconnected components of G0 .

R. K. Ghosh Graphs
Notion of Connectedness in Directed Graphs

I For every pair of vertices u, v there exists a path.


I But path here means directed path.
I Connectivity in directed graph implies both u v and
v u.
I In general, it is possible that path u v may exist but
v 6 u.
I Or even u and v are not reachable from each other.
I There is also a notion of weak connectivity: it possible to
reach any vertex from any other vertex by traversing edges
in some direction (ignoring direction).
I It essentially means every vertex has either indegree or
outdegree of at least 1.

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.

I SCC is an equivalence relation.


I If u, v are in same SCC then uRv and vRu where R: there
exists a directed path.
I If uRv and vRw then obviously, uRw.
I Collapsing each SCC to a vertex we get a condensation
graph which is a DAG.

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

I Compute Finish time f[u] for u ∈ V .


I Reverse each edge of G to obtain GT .
I Call DFS on GT but apply it decreasing order of finish time
f[u] as computed in first DFS.
I Output the vertices of each tree in DFS forest of GT as a
separate SCC.

R. K. Ghosh Graphs
Strong Connected Components

Original Graph G Transformed Graph GT (by


reversing edge directions).
2/15
1/16
b 15
a 16
a b

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

I Let d[v]: DFS discovery time, and


I Let f [v]: DFS finish time.
I Let S be subset of V .
d[S] = min d[u], and f [S] = max f [u]
u∈S u∈S

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

A corollary of the above lemma is on GT where the inequalities


are reversed is as follows:
Lemma (Corollary I)
Let S1 and S2 be two distinct SCCs. If ∃ an edge (u, v) ∈ E T
where u ∈ S1 and v ∈ S2 then f [S2 ] < f [S1 ].

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

Another corollary that follows from the above result is:


Lemma (Corollary II)
If f [S2 ] > f [S1 ], then there cannot be any edge (u, v), where
u ∈ S2 to v ∈ S1 in GT .

I Use of decreasing finish time in exploring GT is the


intuition behind the algorithm’s correctness.

R. K. Ghosh Graphs
Correctness of SCC Algorithm

I Consider a pair of vertices v and w.


I Let finish time of w is smaller than v: f [w] < f [v].
I Now we consider three cases:
– Case 1: w 6 v (v is not reachable from w) then v and w are
in different SCCs, and correctness of algorithm is not
affected.
– Case 2: w v (v is reachable from w) and w is not in DFS
subtree of v in GT .
– Case 3: w is in DFS subtree of v.
I Therefore, we consider last two cases in more details.

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

I Graph terminology and representation were introduced.


I BFS, and DFS search were explained.
I We learnt about a number of applications of DFS,
particularly in computing:
– Connected components, biconnected components of a
undirected graphs, and
– Strong connected components of directed graph and
topological sorting of a DAG

R. K. Ghosh Graphs
Summary of Graph Algorithms

I With reference to weighted graphs we talked about the


problems of determining shortest paths, namely,
– Dijkstra and Bellman-Ford algorithms using edge relaxation
operation.
– Prim and Kruskal’s algorithms for finding MSTs.
I We also looked at almost linear time UNION and FIND
operations on disjoint sets in the context of Kruskal’s
algorithm.
I However, a detailed analysis of UNION-FIND algorithms
was not discussed recognizing involved mathematical
complications.

R. K. Ghosh Graphs

You might also like