(Graph Algorithms) : Chapter: Graph Algorithms Design and Analysis of Algorithms
(Graph Algorithms) : Chapter: Graph Algorithms Design and Analysis of Algorithms
[Graph Algorithms]
Samujjwal Bhandari 1
Chapter: Graph Algorithms Design and Analysis of Algorithms
Graph is a pair G = (V,E) where V denotes a set of vertices and E denotes the set of edges
connecting two vertices. Many natural problems can be explained using graph for
example modeling road network, electronic circuits, etc. The example below shows the
road network.
Kalanki
Kirtipur
Balkhu
kalimati
TU gate
Representing Graphs
Generally we represent graph in two ways namely adjacency lists and adjacency matrix.
Both ways can be applied to represent any kind of graph i.e. directed and undirected.
An adjacency matrix is an n×n matrix M where M[i,j] = 1 if there is an edge from vertex
i to vertex j and M[i,j]=0 if there is not. Adjacency matrices are the simplest way to
represent graphs. This representation takes O(n2) space regardless of the structure of the
graph. So, if we have larger number of nodes say 100000 then we must have spcae for
1000002 = 10,000,000,000 and this is quite a lot space to work on. The adjacency matrix
representation of a graph for the above given road network graph is given below. Take
the order {Kirtipur,TU gate, Balkhu, Kalanki, Kalimati}
0 1 0 0 0
1 0 1 0 0
0 1 0 1 1
0 0 1 0 1
0 0 1 1 0
It is very easy to see whether there is edge from a vertex to another vertex (O(1) time),
what about space? Especially when the graph is sparse or undirected.
If adjacency list representation of a graph contains and array of size n such that every
Samujjwal Bhandari 2
Chapter: Graph Algorithms Design and Analysis of Algorithms
vertex that has edge between the vertex denoted by the vertex with array position is
added as a list with the corresponding array element. The example below gives the
adjacency list representation of the above road network graph.
Searching for some edge (i,j) required O(d) time where d is the degree of i vertex.
Some points:
To test if (x, y) is in graph adjacency matrices are faster.
To find the degree of a vertex adjacency list is good
For edge insertion and deletion adjacency matrix takes O(1) time where as adjacency list
takes O(d) time.
Algorithm:
BFS(G,s) //s is start vertex
{
T = {s};
L =∅; //an empty queue
Enqueue(L,s);
Samujjwal Bhandari 3
Chapter: Graph Algorithms Design and Analysis of Algorithms
while (L != ∅ )
{
v = dequeue(L);
for each neighbor w to v
if ( w∉ L and w∉ T )
{
enqueue( L,w);
T = T ∪ {w}; //put edge {v,w} also
}
}
}
Example:
Use breadth first search to find a BFS tree of the following graph.
b c
a g d
f e
Solution:
Choose a as initial vertex then we have
b c
b c
a g d
a g d
f e
f e
Samujjwal Bhandari 4
Chapter: Graph Algorithms Design and Analysis of Algorithms
Order the vertices of level 1 i.e. {b, c, g, e, f}. Say order be {e, f, g, b, c}.
b c
b c
a g d
a g d
f e
f e
Analysis:
From the algorithm above all the vertices are put once in the queue and they are accessed.
For each accessed vertex from the queue their adjacent vertices are looked for and this
can be done in O(n) time(for the worst case the graph is complete). This computation for
all the possible vertices that may be in the queue i.e. n, produce complexity of an
algorithm as O(n2) . Also we can write the complexity as O(E+V).
Samujjwal Bhandari 5
Chapter: Graph Algorithms Design and Analysis of Algorithms
Algorithm:
DFS(G,s)
{
T = {s};
Traverse(s);
}
Traverse(v)
{
for each w adjacent to v and not yet in T
{
T = T ∪ {w}; //put edge {v,w} also
Traverse (w);
}
}
Example:
Use depth first search to find a spanning tree of the following graph.
b c
a g d
f e
Solution:
Choose a as initial vertex then we have
b c b c
a g d a g d
f e f e
Samujjwal Bhandari 6
Chapter: Graph Algorithms Design and Analysis of Algorithms
b c
b c
a g d
a g d
f e
f e
b c b c
a g d a g d
f e f e
b c b c
a g d a g d
f e
f e
b c
b c
a g d
a g d
f e
f e
Analysis:
The complexity of the algorithm is greatly affected by Traverse function we can write its
running time in terms of the relation T(n) = T(n-1) + O(n), here O(n) is for each vertex at
most all the vertices are checked (for loop). At each recursive call a vertex is decreased.
Solving this we can find that the complexity of an algorithm is O(n2). Also we can write
the complexity as O(E+V).
Samujjwal Bhandari 7
Chapter: Graph Algorithms Design and Analysis of Algorithms
Kruskal’s Algorithm
The problem of finding MST can be solved by using Kruskal’s algorithm. The idea
behind this algorithm is that you put the set of edges form the given graph G = (V,E) in
nondecreasing order of their weights. The selection of each edge in sequence then
guarantees that the total cost that would from will be the minimum. Note that we have G
as a graph, V as a set of n vertices and E as set of edges of graph G.
Algorithm:
KruskalMST(G)
{
T = {V} // forest of n nodes
S = set of edges sorted in nondecreasing order of weight
while(|T| < n-1 and E !=∅)
{
Select (u,v) from S in order
Remove (u,v) from E
if((u,v) doesnot create a cycle in T))
T = T ∪ {(u,v)}
}
} 1 18
2
Example: 7 10
9
Find the MST and its weight of the graph. 3
6 7
15 11
17 8
5
13 4
Samujjwal Bhandari 8
Chapter: Graph Algorithms Design and Analysis of Algorithms
Solution:
1
1 2 1
2 7 2
7 7 9
3
3 6 7 3
6 7 6 7
8
8
5
5 5
4
4 4
Edge with weight 11 forms Edge with weight 15 forms
cycle so discard it cycle so discard it
1 1 1
2 2 2
7 9 10 7 9 10 7 9 10
3 3 3
6 7 6 7 6 7
8 8 17 8
5 5 5
4 13 4 13 4
Samujjwal Bhandari 9
Chapter: Graph Algorithms Design and Analysis of Algorithms
we can say that both tree T and T’ have the same cost thus the cost is minimum since T’
is MST. If E(T) ≠ E(T’), then take an minimum cost edge e ∈ E(T) and e ∉ E(T’). Such
an edge must exist. Now if we include the edge e in tree T’ it will form a cycle. Let the
cycle be e, e1, …, ek. Then it is clear that at least one of the edges from tree T’ is not in
E(T) otherwise T also would contain cycle e, e1, …, ek. Take any edge ej from the cycle
such that ej ∉ E(T). If ej is of the lower cost than e then algorithm will consider ej before
e and include ej in T. So we have cost of ej ≥ cost of e since ej ∉ E(T). Consider a graph
with edge set E(T’) + {e}, where there is a cycle e, e1, …, ek. If we remove any edge from
the cycle another tree will be formed say T’’. If the removed edge is ej, then it is
guaranteed that the resulting tree T’’ will have no more cost than that of T’, hence T’’ is
also MST. If we take as many as required edges from the T’ that is not in T and remove
the cycle as described above then the tree so formed after all transformations will be
transformation from T’ to T with the cost no more than T’.
Prim’s Algorithm
This is another algorithm for finding MST. The idea behind this algorithm is just take any
arbitrary vertex and choose the edge with minimum weight incident on the chosen vertex.
Add the vertex and continue the above process taking all the vertices added. Remember
the cycle must be avoided.
Algorithm:
PrimMST(G)
{ T = ∅; // T is a set of edges of MST
S = {s}; //s is randomly chosen vertex and S is set of vertices
while(S != V)
{
e = (u,v) an edge of minimum weight incident to vertices in T and not forming a
simple circuit in T if added to T i.e. u ∈ S and v∈ V-S
T = T ∪ {(u,v)};
S = S ∪ {v};
}}
Samujjwal Bhandari 10
Chapter: Graph Algorithms Design and Analysis of Algorithms
Example:
1 18
Find the minimum spanning tree of the following graph. 2
7 10
9
3
6 7
15 11
17 8
5
13 4
Solution: note: dotted edge is chosen.
18 3 18 3
2 2
1 7 1 7
6 4 7 4
7 6
5 5
17
S V-S S V-S
18
3 18
1 2 1 2
7 7 7
6 15 15 7
6
4
17 17 11 3
5 13
5
13 4 8
V-S
V-S
S
S
18 1
1
2
7 9 7
7 10 7 6
6 2
15 17 15
17 10
5 3 11
5 3 11
8 13 8
13 4
4 V-S
V-S
S
S
Samujjwal Bhandari 11
Chapter: Graph Algorithms Design and Analysis of Algorithms
1 7
1 7
7 9
7 9 6
6 2
2 17
17 10
10
5 3
5 3
13 8
13 8 4
4
V-S
MST
S
The total weight of MST is 64.
Analysis:
In the above algorithm while loop execute O(V). The edge of minimum weight incident
on a vertex can be found in O(E), so the total time is O(EV). We can improve the
performance of the above algorithm by choosing better data structures as priority queue
and normally it will be seen that the running time of prim’s algorithm is O(ElogV)!.
Correctness: See Assignment 4
-10 5
b
As a matter of fact even the positive weight cycle doesn’t constitute shortest path but
there will be shortest path. Some of the variations of shortest path problem include:
Single Source: This type of problem asks us to find the shortest path from the given
vertex (source) to all other vertices in a connected graph.
Single Destination: This type of problem asks us to find the shortest path to the given
Samujjwal Bhandari 12
Chapter: Graph Algorithms Design and Analysis of Algorithms
Samujjwal Bhandari 13
Chapter: Graph Algorithms Design and Analysis of Algorithms
p[v] = u
for each edge (u,v) ∈ E
do if d[v] > d[u] + w(u,v)
then return FALSE
return TRUE
}
Example:
Find the shortest path using Bellman Ford algorithm, from the source a to all other
vertices in the following graph.
2 b
4
a 6
5
9 -2 g 5 c
5
1 i 4 -2
f h 1
3
6 d
e 1
Solution:
Samujjwal Bhandari 14
Chapter: Graph Algorithms Design and Analysis of Algorithms
∞ 2
2 b 2 b
0 4 0 4
a 6 ∞ a 6 ∞
5 5
g ∞ 5
5
9 -2 c 9 -2 g 5 c
5 5
1 i -2 1 i -2
∞ 4 ∞ 4
f h 1 f h 1
3 ∞ 3 ∞
∞ ∞ 9 ∞
6 d 6 d
e ∞ 1 e ∞ 1
2
2 b
2 b 0 2
0 4 4
6 a 6 6
a 6 5
5 5
5 9 -2 g 5 c
9 -2 g 5 c
5
5 1 i
1 i 4 -2
4 -2 f 3 h 1
f 3 h 1 3 7
3 10 9 4
9 ∞ 6 d
6 d
e 15 1 e 6 1
For other 5 iterations there are no changes. The returned value will be true for this
example.
Analysis:
Execution of first for loop block takes O(V) time. The execution of second for loop block
takes O(EV) time and the execution of third for loop block takes O(E) time. So the total
running time for above algorithm is (EV).
Correctness:
s u1 u2 u
Let u be any vertex. Assume any shortest path from s to u. All vertices on this path must
be distinct otherwise we can get another shorter path so that the above assumption
becomes invalid. When the first iteration of the second for loop in the above algorithm
completes, we have d[u1] = δ(s,u1), similarly after the second iteration we have d[u2] =
δ(s,u2), and so on. If we have the path from s to u containing all the vertices of the graph
Samujjwal Bhandari 15
Chapter: Graph Algorithms Design and Analysis of Algorithms
G (in worst case), then d[u] will be the value after |V|-1 iterations. So, if there are no
negative weight cycles, array d[] will contain stable values after the termination of the
loop (when all vertices are visited) and the returned value will be TRUE. Conversely, if
we get the correct output, i.e. the value returned is TRUE and there is a negative weight
cycle, say wn =<v0, v1, …, vk> reachable from s and v0 = vk then sum of weights of all the
k
edges in a cycle must be negative i.e. w(vi −1 , vi ) < 0 . We have d[vi] ≤ d[vi-1] + w(vi-1,
i =1
vi) (since we have return value as TRUE this condition must be true), summing up
inequalities d[vi] ≤ d[vi-1] + w(vi-1, vi) around the cycle produces,
k k k k
d [vi ] ≤ (d [vi ] + w(vi −1 , vi )) = d [vi −1 ] + w(vi −1 , vi ) ------------(1)
i =1 i =1 i =1 i =1
Since we have v0 = vk each vertex in c appears exactly once in each of the summations
k k k k
d [vi ] and d [vi −1 ] , hence d [vi ] = d [vi −1 ] so we have from (1)
i =1 i =1 i =1 i =1
k
0≤ w(vi −1 , vi ) = w(c), this is a contradiction that there is a negative weight cycle.
i =1
Samujjwal Bhandari 16
Chapter: Graph Algorithms Design and Analysis of Algorithms
∞ 0 1 2 ∞ ∞ ∞ ∞
1 9 6 1 1
f c a b e h d 2 g
2 2 5 3
2 2
From (c) 1
∞ 0 1 2 3 ∞ ∞ ∞
1 9 6 1 1
f c a b e h d 2 g
2 2 5 3
From (a) 2 2
1
∞ 0 1 2 3 7 4 ∞
1 9 6 1 1
f c a b e h d 2 g
2 2 5 3
2 2
From (b) 1
Samujjwal Bhandari 17
Chapter: Graph Algorithms Design and Analysis of Algorithms
∞ 0 1 2 3 4 4 6
1 9 6 1 1
f c a b e h d 2 g
2 2 2 5 3
2
From (e) 1
∞ 0 1 2 3 4 4 6
1 1
f c a b e h d g
2 2 3
2
From (h) (d) and (g) no change. So above is the shortest path tree.
Analysis:
In the above algorithm, the topological sort can be done in O(V+E) time (Since this is
similar to DFS! see book.).The first for loop block takes O(V) time. In case of second for
loop it executes in O(V2) Time so the total running time is O(V2). Aggregate analysis
gives us the running time O(E+V).
Correctness:
Lemma 2: (path relaxation property)
If p =< v0, v1, …, vk> is a shortest path from s = v0 to vk, and the edges of p are relaxed in
the order (v0, v1), (v1, v2), …, (vk-1, vk), then d[vk] = δ(s, vk).
Lemma 3: (Predecessor subgraph property)
Once d[v] = δ(s,v) for all v∈ V, the predecessor subgraph is a shortest paths tree rooted at
s.
Theorem 2:
For a DAG G = (V,E) with source vertex s, at the termination of DagSP algorithm, d[v] =
δ(s,v) for all vertices v∈ V, and the predecessor subgraph Gπ is a shortest paths tree.
Proof:
If v is unreachable from s, then there is no path from s to v so we have d[v] = δ(s,v) = ∞.
Assume that v is reachable from s, then there is some shortest path p = <v0, v1, …, vk>,
where v0 = s and vk = v. Now from the above algorithm it is guaranteed that relaxation of
edges on the path are done in a particular order as (v0, v1), (v1, v2), …, (vk-1, vk) due to the
topological ordering. So from the above lemmas 2 and 3 we complete the proof.
Samujjwal Bhandari 18
Chapter: Graph Algorithms Design and Analysis of Algorithms
Dijkstra’s Algorithm
This is another approach of getting single source shortest paths. In this algorithm it is
assumed that there is no negative weight edge. Dijkstra’s algorithm works using greedy
approach, as we will see later.
Algorithm:
Dijkstra(G,w,s)
{
for each vertex v∈ V
do d[v] = ∞
p[v] = Nil
d[s] = 0
S=∅
Q=V
While(Q!= ∅)
{
u = Take minimum from Q and delete.
S = S ∪ {u}
for each vertex v adjacent to u
do if d[v] > d[u] + w(u,v)
then d[v] = d[u] + w(u,v)
}
}
Example:
Find the shortest paths from the source g to all other vertices using Dijkstra’s algorithm.
2 b
4
a 6
5
9 2 g 5 c
5
1 i 4 2
f h 1
3
6 d
e 1
Samujjwal Bhandari 19
Chapter: Graph Algorithms Design and Analysis of Algorithms
6
∞ 2 b
2 b 5 4
∞ 4
a 6 ∞
a 6 ∞ 5
5 0
0 9 2 g 5 c
9 2 g 5 c
5
5 1 i 4 2
1 i 2 2 h 1
f ∞ 4 h 1
f
5
3 ∞ ∞ 3 ∞
∞ ∞ 6 d
6 d
e ∞ 1
e ∞ 1
6 6
2 b
2 b 5 4
5 4
6 a 6 ∞
a ∞ 5
5 0
0 9 2 g 5 c
9 2 g 5 c
5 5
1 1 i 4 2
i 4 2
f 2 h 1 f 2 h 1
3 5
3 5 3 ∞
3 ∞ 6 d
6 d
e 5 1
e 5 1
6 6
2 b 2 b
5 4 5 4
a 6 ∞ a 6 ∞
5 0 5 0
9 2 g 5 c 9 2 g 5 c
5 5
1 i 4 2 1 i 4 2
f 2 h 1 f 2 h 1
3 5 3 5
3 ∞ 3 6
6 d 6 d
e 5 1 e 5 1
6 6
b 2 b
5 2 5 4
4
a 6 5
a 6 5 5 0
5 0 9 2 g 5 c
9 2 g 5 c
5
5 1 i 4 2
1 i 4 2 f 2 h 1
f 2 h 1 3 5
3 5 3 6
3 6 6 d
6 d
e 5 1
e 5 1
There will be no change for vertices b and d. continue above steps for b and d to
complete. The tree is shown as dark connection.
Samujjwal Bhandari 20
Chapter: Graph Algorithms Design and Analysis of Algorithms
Analysis:
In the above algorithm, the first for loop block takes O(V) time. Initialization of priority
queue Q takes O(V) time. The while loop executes for O(V), where for each execution
the block inside the loop takes O(V) times . Hence the total running time is O(V2).
Correctness:
Let S be the set of vertices to which the shortest paths from the source is already known.
x
S
Let w be the node that is not in the set S such that the length of the path connecting s to w
and passing only through vertices in S is the shortest among all choices of w. Say this
path as special path. Let us argue that any other path connecting s to w and passing
through vertices outside of S cannot be shorter than the special path. Suppose that a path
connecting s to w passes through a vertex x outside of S and is shorter than the special
path. Then the length of the path from the s to x is shorter than the special path, this is a
contradiction special path is shortest. Hence Dijkstra’s algorithm is correct.
[Remark: You can prove for the correctness taking d[v] = δ(s,v) for each vertex v∈S]
Samujjwal Bhandari 21
Chapter: Graph Algorithms Design and Analysis of Algorithms
using only vertices from 1,2,…,k as intermediate vertices in the path. If we consider
shortest path with intermediate vertices as above then computing the path contains two
cases. Dk(i,j) does not contain k as intermediate vertex and .Dk(i,j) contains k as
intermediate vertex. Then we have the following relations
Dk(i,j) = Dk-1(i,j), when k is not an intermediate vertex, and
k
i j
k-1 k-1
D (i,k) D (k,j)
11 3
Solution:
Samujjwal Bhandari 22
Chapter: Graph Algorithms Design and Analysis of Algorithms
Adjacency Matrix
Remember we are not showing Dk(i,i), since there
W 1 2 3 will be no change i.e. shortest path is zero.
D1(1,2) = min{D0(1,2), D0(1,1)+ D0(1,2)}
1 0 4 11 = min{4, 0+ 4} = 4
1
D (1,3) = min{D0(1,3), D0(1,1)+ D0(1,3)}
2 6 0 2 = min{11, 0+ 11} = 11
1
D (2,1) = min{D0(2,1), D0(2,1)+ D0(1,1)}
3 3 ∞ 0
= min{6, 6+ 0} = 6
1
D (2,3) = min{D0(2,3), D0(2,1)+ D0(1,3)}
= min{2, 6+ 11} = 2
D1 1 2 3 D1(3,1) = min{D0(3,1), D0(3,1)+ D0(1,1)}
= min{3, 3+ 0} = 3
1 0 4 11
D1(3,2) = min{D0(3,2), D0(3,1)+ D0(1,2)}
2 6 0 2 = min{∞∞, 3+ 4} = 7
Samujjwal Bhandari 23
Chapter: Graph Algorithms Design and Analysis of Algorithms
Exercises
Samujjwal Bhandari 24