Unit 1 Graph Algorithms-Ii: Structure Page No
Unit 1 Graph Algorithms-Ii: Structure Page No
Structure Page No
1.1 Introduction 1
1.1.1 Objectives 1
1.1.2 Graph Basics 1
1.1.3 Representation of Graph 3
1.2 Minimum Cost Spanning Tree (MCST) 4
1.2.1 Generic MST algorithm 4
1.2.2 Kruskal’s Algorithm 6
1.2.3 Prim’s Algorithm 8
1.3 Single Source Shortest Path 12
1.3.1 Dijkstra’s Algorithm 12
1.3.2 Bellman-Ford Algorithm 19
1.4 Maximum Bipartite Matching 22
1.5 Solution / Answers 28
1.6 Further Readings 31
1.1 INTRODUCTION
Graph is a non-linear data structure just like tree that consist of set of vertices and
edges and it has lot of applications in computer science and in real world scenarios. In
a real-world, we can see graph problem in road networks, computer networks, and in
social networks. What if you want to find the cheapest and shortest path to reach from
one place to other places? What will be the cheapest way to connect among computer
networks? What efficient algorithm will you use to find out communities (friends or
friend of friend) in Facebook? The answer to all these problems is one and only one
Graph Algorithm. Using graph algorithm you can find the shortest path, cheapest path
and predicted outputs. Graph is used to model and represents a variety of systems and
it is useful in both computer-science and real world.
Maps: You can think of map as a graph where intersections of roads are vertices and
connecting roads are edges.
Social Networks: It is another example of graph structure where peoples are
connected based on the friendships, or some relationships.
Internet: You can think of internet as graph structures, where there are certain
webpages and each webpages is connected through some link
1.1.1 Objectives:
After completion of this unit, you will be able to:
1
Graph Algorithms-II
Apply minimum cost spanning tree algorithm on the graph and apply
single source shortest path to find the shortest path from source vertex.
1.1.2 Graph Basics
A graph is just like tree is a collection of vertices and edges where each edge
is connected with a pair of vertices. But in a tree there are certain constraints
such as tree should be acyclic, always connected and directed graph and there
must be a root element. While in graph there is no-root element, it can be
connected or disconnected graph, can be directed or undirected, there may or
not be cycle in the graph.
In a tree if there are n nodes, it can have maximum n-1 edges.
Each edges shows parent-child relationship and each node except the
root node have a parent.
In a tree all nodes must be reachable from root and there is exactly
one path from root to child.
While in a Graph:
1 1
2 3 2 3
(a) (b
)
Figure 2 (a) Represent undirected graph with 3 edges and 3 vertices. (b) Represents directed
graph with 3 edges and 3 vertices.
3
Graph Algorithms-II
Adjacency List
Adjacency Matrix
Adjacency Matrix:
An adjacency matrix of a Graph 𝑮 (𝑽, 𝑬) is a 2-D array of size
𝐕 ∗ 𝐕 represented as:
(a)
(b) (c)
Figure3: (a) An undirected graph G with 5 vertices and 5 edges. (b) The adjacency matrix
representation of graph G. (c) The adjacency list of a graph G.
Adjacency matrix of the graph needs 𝑶(𝑽)𝟐memory to store the data.
Figure 3 (c) shows the adjacency matrix of the corresponding graph.
Adjacency List:
An adjacency list of graph 𝑮(𝑽, 𝑬)is an array of list.Size of the array isthe no of
vertexin the graph and elements of the array are vertices. Element of
𝑨𝒅𝒋[𝑽]represents the index for each vertex and corresponding list represent the
number of vertices connected to that vertex. Alist nodes corresponding to vertex𝒗𝒊 in
array 𝐀[𝒗𝒊 ]represents the vertices adjacent to 𝒗i. Figure 3(c) represents the adjacency
list of the graph G. For each𝒖 ∈ 𝑽, the adjacency list 𝑨𝒅𝒋[𝒖]contains all the vertices
𝒗such that there is an edge(𝒖, 𝒗) ∈ 𝑬. If E is a number of edges in a graph. For each
vertex we need to create a list that contains number of edges adjacent to that vertex.
Thus space complexity will be 𝑶(𝑽 + 𝑬). If the graph is a complete graph, then in
worst case space complexity will be𝑶(𝑽𝟐 ).
A connected sub graph S of Graph𝐆(𝐕, 𝐄)is said to be spanning tree if and only ifit
contains all the vertices of the graph G and have minimum total weight of the edges of
G. Spanning tree must be acyclic. Spanning tree are used to solve real-world problems
such as finding the shortest route, optimizing airline routes, finding least costly path in
a network etc.
4
Introduction to Algorithms
Consider a residential area that contains multiple roads and apartments. Two apartment
𝑢 and 𝑣are connected by single/multiple roads has a cost 𝒘(𝒖, 𝒗). Now you need to
find the minimum path from one apartment to all other part such that:
All apartments are connected to each other.
Total cost is minimum.
To optimize the airline route by finding the minimum miles path with no cycles. The
vertices of the graph will be cities and edges will be the path from one city to other city.
Miles could be the weight of those edges.
In mathematical term given an undirected graph 𝑮(𝑽, 𝑬) and weight 𝒘(𝒖, 𝒗)on each
edge (𝒖, 𝒗) ∈ 𝑬. The problem is to find 𝑆 ∈ 𝐸 such that:
1. S connects all vertices (S is a spanning tree), and It contains all the vertices of
a Graph 𝑮
2. 𝒘(𝑺) = ∑(𝒖,𝒗)∈𝑺 𝒘(𝒖, 𝒗)is minimized.
A Spanning tree whose weight is minimum over all spanning trees is called a
minimum spanning tree or MST. Some important properties of MST are as follows:
A MST has |V|-1 edges.
MST has no cycles.
It might not be unique.
1.2.1 Generic MST Algorithm
The generic algorithm for finding MSTs maintains a subset A of the set of
edges E. At each step, an edge (u, v)∈E is added to A if it is not already in A and
its addition to the set does not violate the condition that there may not be cycles
in A. The algorithm also considers edges according to their weights in non-
decreasing order. Generic MST algorithm is:
We build a set of edges A.
Initially A has no edges.
As we add edges to A, we maintain a loop invariant: A is a subset
of some MST for G.
Define an edge (u,v) to be safe for A iff A ∪ {(u,v)} is also a subset of some MST
for G. If we only add safe edges to A, once |V| − 1 edges have been added we have a
MST for G.
Generic-MST(G, w)
1. A= { }
2. while A is not a
spanning tree
3. do find an edge (u, v)
that is a safe for set A
4. A = 𝐴 ∪ (𝑢, 𝑣)
5. return A
5
Graph Algorithms-II
Example1: Find the MST of the following graph:
In the above example, there is more than one MST. Replace edge (e, f) by (c, e). We
get a different spanning tree with same weights.
Two find the minimum spanning tree of a graph G, two algorithm has been proposed:
3. Kruskal’s Algorithm
4. Prim’s Algorithm
1.2.2Kruskal’s Algorithm
The minimum spanning tree algorithm first given by Kruskal’s in 1956. Kruskal’s
algorithm finds a minimum weight edge (safe edge) from a graph G and add to the
new sub-graph S. Then again find a next minimum weight edge and add it to the sub-
graph. Keep doing it until you get the minimum spanning tree. Intermediate steps of
kruskal’s algorithm may generate a forest.
6
Introduction to Algorithms
Pseudo code of Kruskal’s Algorithm:
MCST_Kruskal(V, E, w)
1. 𝐾{ }
2. for each vertex 𝑣 ∈ 𝑉
3. MAKE − SET(v)
4. Sort the edge 𝐸 of 𝐺 in a non-decreasing order by weight 𝑤
5. for the edge (𝑢, 𝑣) ∈ 𝐸, taken from the sorted-list
6. if 𝐹𝐼𝑁𝐷 − 𝑆𝐸𝑇(𝑢) ≠ 𝐹𝐼𝑁𝐷 − 𝑆𝐸𝑇(𝑣)
7. then 𝐾 ← 𝐾 ∪ {(𝑢, 𝑣)}
8. 𝑈𝑁𝐼𝑂𝑁(𝑢, 𝑣)
9. return 𝐾
(a)
In the graph, the edge (a, b)and edge (d,
g)is the next smallest weight edge. Both
have similar weight, you can choose any
one edge. So we select the edge (a, b) in
the graph G.
K = {(a, c), (a, b)}
(b)
Now edge (d, g) is the minimum weight
edge among all the non-highlighted edge.
So we select edge (d, g) in the graph G.
K = {(a, c), (a, b), (d, g)}
(c)
Now edge (b, e) is the minimum weight
edge among all the non-highlighted edge.
So we select edge (b, e) in the graph G.
K = {(a, c), (a, b), (d, g), (b, e)}
(d)
Now edge (b, d) is the minimum weight
edge among all the non-highlighted edge.
So we select edge (b, d) in the graph G.
K = {(a, c), (a, b), (d, g), (b, e), (b, d)}
(e)
8
Introduction to Algorithms
Now edge (e, d) have the minimum
weight among all the non-selected edge.
But when we select that edge it will
create a cycle (e→b→d) in the graph. So
we discard the edge (e, d) and select next
minimum weight edge (𝐜, 𝐟)among all
the non-highlighted edge. So we select
edge (c, f) in the graph G. It completes
the spanning tree that have exactly |V|-1
edges and having minimum cost.
K
(f) = {(a, c), (a, b), (d, g), (b, e), (b, d), (c, f)}
In the pseudo code𝒓is the root of the minimum spanning tree to be grown.
𝑺𝑬is the set of selected edges.
𝑺𝑽is the set of vertices already in the spanning tree. Initially 𝑺𝑽 contains the
root vertexr.
𝑽 𝑎𝑛𝑑 𝑬are vertices and edges of the Graph G.
MCST_Prim’s( V, E, w, r )
1. 𝑆𝐸 ← {}
2. 𝑆𝑉 ← {𝑟}
3. while 𝐸 ≠ ∅&& size |𝑆𝐸| ≠ |𝑉| − 1
4. Select minimum weight edge (𝑢, 𝑣) from G
5. If ~( 𝑢 ∈ 𝑆𝑉 and 𝑣𝑆𝑉)
6. break;
7. 𝐸 = 𝐸 − {(𝑢, 𝑣)}
8. 𝑆𝐸 = 𝑆𝐸 ∪ {(𝑢, 𝑣)}
9. 𝑆𝑉 = 𝑆𝑉 ∪ {𝑢}
10. if (|𝑆𝐸 | = = |𝑉| − 1)
11. 𝑆𝐸 is a minimum spanning tree that contains |𝑉| − 1 vertices
12. else
13. Graph is disconnected
Example: Let’s run the Prim’s algorithm on the following graph. Assume that root
vertex 𝒓 = 𝒂
10
Introduction to Algorithms
In the tree highlighted vertex are the root vertex.
There is no edge selected in the tree. Initially
𝑆𝐸 = {}
𝑆𝑉 = {𝑎}
(a)
Now select the minimum weight edge vertex
from G that start with source vertex 𝒂.(𝒂, 𝒄)
have minimum weight among all the edge start
with vertex 𝒂 and add this edge to the tree
𝑆𝐸 = {(𝑎, 𝑐)}
𝑆𝑉 = {𝑎, 𝑐}
(b)
Now select the minimum weight edge vertex
from G that start with vertices𝒂 and vertexc.
(𝒂, 𝒃) have minimum weight among all the edge
start with vertex 𝒂 and add this edge to the tree
12
Introduction to Algorithms
Q3. How many edges are in minimum cost spanning tree with 𝑛 vertices and 𝑒 edges.
a) 𝑛 − 1 b) 𝑛 + 1 c) 𝑛2 d) 𝑒 − 1
Q4. Derive the complexity of kruskal’s algorithm and prim’s algorithm for worst and
best case.
Q5. Assume that Prim’s and Kruskal’s algorithm runs on a Graph G. The spanning
tree created from both the algorithm are𝑆1 ,𝑆2 respectively and 𝑆1 , 𝑆2 are not
equal. Select the true statement about MCST:
a) There is an edge that have minimum weight and included in both the spanning
tree.
b) There is an edge that have maximum weight and excluded in both the
spanning tree.
c) Some pair of edges that have similar weight in the Graph G.
d) All edges have same weight in the Graph.
Q6. Apply the kruskal’s and prim’s algorithm on the following graph.
Q7. Apply kruskal’s and prim’s algorithm on the following graph and find the total
minimum weight in the spanning tree.
13
Graph Algorithms-II
In real world life graph can be used to represent the cities and their connections
between each city. Vertices represents the cities and edges representing roads that
connects these vertices. The edges can have weights which may be the miles from
one city to other city. Suppose a person wants to drive from a city P to city Q. He may
be interested to know the following queries:
Is there any path exist from P to Q?
If there are multiple paths from P to Q, then which is the
shortest path?
The above discussed problem is considered in finding the shortest path problem. In
the given weighted graph G (V, E), we want to find a shortest path from given vertex
to each other vertex on G. The shortest path weight from a vertex 𝒖 ∈ 𝑽 to a vertex
𝒗 ∈ 𝑽 in the weighted graph is the minimum cost of all paths from 𝒖to 𝒗.
There are two algorithm to solve the single-source-shortest path problem.
1. Dijkstra’s Algorithm
2. Bellman Ford Algorithm
1.3.1 Dijkstra’s Algorithm
Dijkstra’s algorithm solves the single-source shortest path problem when all edges
have non-negative weights. It is a greedy algorithm’s similar to Prim’s algorithm and
always choose the path that are optimal right now not for future consequences.
15
Graph Algorithms-II
𝐷𝑖𝑗𝑘𝑠𝑡𝑟𝑎′𝑠(𝐺, 𝑉, 𝑠)
a b c d e f g
𝑑𝑖𝑠𝑡[𝑉] 0 ∞ ∞ ∞ ∞ ∞ ∞
=
𝑝𝑟𝑒𝑣[𝑉] - - - - - - -
=
Vertex𝒄 and 𝒃 are reachable from source
vertex𝒂. Update the distance of 𝒄 and 𝒃 from ∞
to 1 and 2 respectively. Red edges shows the
relaxed edge and updated weight on the vertex
head. Vertex in green color are added in the tree
T.
𝑑𝑖𝑠𝑡[𝑉] 0 1 2 ∞ ∞ ∞ ∞
=
𝑝𝑟𝑒𝑣[𝑉] - a a - - - -
=
16
Introduction to Algorithms
Vertex 𝒄 having minimum distance is selected
and added in the tree T. 𝒄 is also called as a
closest vertex to source 𝒂. Update the distance
of all the vertex adjacent to 𝒄 except the vertex
already added in the tree T.
𝑑𝑖𝑠𝑡[𝑉] 0 1 2 8 ∞ 7 ∞
=
𝑝𝑟𝑒𝑣[𝑉] - a a c - c -
=
Vertex 𝒃 having minimum distance is selected
and added in the tree T.Update the distance of
all the vertex adjacent to b except the vertex
already added in the tree T. Here 𝒅𝒊𝒔𝒕[𝒅] is
updated from 8 to 6 because 𝒅𝒊𝒔𝒕[𝒅] via 𝒄is
higher than 𝒅𝒊𝒔𝒕[𝒅]via b.
𝑑𝑖𝑠𝑡[𝑉] 0 1 2 6 5 7 ∞
=
𝑝𝑟𝑒𝑣[𝑉] - a a b b c -
=
Vertex 𝒆having minimum distance among all the
remaining vertex is selected and added in the
tree T.
There is only one vertex 𝒅 reachable from 𝒆
whose distance is 6 via vertex 𝒃 less than the
distance 10via 𝒆. Thus distance of𝒅 will not be
updated.
𝑑𝑖𝑠𝑡[𝑉] 0 1 2 6 5 7 ∞
=
𝑝𝑟𝑒𝑣[𝑉] - a a b b c -
=
𝑝𝑟𝑒𝑣[𝑉] - a a b b c d
=
𝑑𝑖𝑠𝑡[𝑉] 0 1 2 6 5 7 8
=
17
Graph Algorithms-II
𝑝𝑟𝑒𝑣[𝑉] - a a b b c d
=
𝒈is only vertex left. 𝒈is selected and added in
tree T. Now all the vertex are selected and path
from source vertex to all other vertex has been
found as shown in graph highlighted in red
color.
𝑑𝑖𝑠𝑡[𝑉] 0 1 2 6 5 7 8
=
𝑝𝑟𝑒𝑣[𝑉] - a a b b c d
=
Figure 8:Shows the step by step execution of Dijakstra’s algorithm
From the above graph answer the following questions:
What is the shortest path from vertex 𝒂 to vertex 𝒈and distance of the path.
Answer: 𝒂 → 𝒃 → 𝒅 → 𝒈 distance= 8 from the 𝑝𝑟𝑒𝑣[𝑉]you can find the path
checking in backward direction
𝑝𝑟𝑒𝑣[𝑉] = - a a b b c d
Method 1:
Selected a b c d e
vertex
a 0 ∞ ∞ ∞ ∞
c 10 5 ∞ ∞
e 8 14 7
b 8 13
d 9
You can apply the dijakstra’s using the above table. In each row distance array is
created and updated according to the minimum distance. Left column represents the
selected vertex in the tree or order of the vertex. To find the path from vertex 𝒂to 𝒅.
In first row only source has 0 weight and all other vertices have ∞ weight. Now,
vertex a is chosen and weight of all adjacent vertex to a is relaxed if needed. Thus,
weight of b and c is relaxed. Now next minimum vertex is c is chosen and weights of
all adjacent vertices is relaxed if needed and so forth. To find the path from vertex 𝒂to
𝒅.You need to check in backward direction. First check for distance of 𝒅updated due
to which vertex that is 𝒃markd by arrow in table. Now check for distance of b updated
due to which vertex that is 𝒄. Now value of 𝒄 updated due to 𝒂. Thus path from 𝒂 to
𝒅 is: 𝒂 → 𝒄 → 𝒃 → 𝒅.
18
Introduction to Algorithms
Method 2: Apply dijakstra’s as explain in Example 1.
(a) (b)
(a)
(c) (d)
19
Graph Algorithms-II
(e) (f)
Figure 10: The execution of dijakstra’s algorithm in negative edge weight. (a) The
source𝒂 is the leftmost vertex. (b) Assigned 0 to vertex a and∞ to the remaining
vertex. (c)- (e) apply dijakstra’s algorithm to update the distance and select the vertex
in tree T. (f) vertex 𝒄 is selected and distance from 𝒄 → 𝒃 is 𝟐 that can’t be updated
because vertex𝒃 is already added in the tree.
In Figure 10 (f) we can see that here dijakstra’s algorithm fails. Because 𝒃 is already
traversed and added in the tree T. Distance from 𝒂 → 𝒃 is 10 while distance 𝒂 → 𝒄 →
𝒃 is 2 that can’t be updated. When there is a negative edge weight, dijakstra’s may not
work because relaxation can be done only one time. In the graph there is negative
edge but no negative weight cycle. Because 𝒄 → 𝒃there is a minimum distance but
𝒃 → 𝒄 distance is 32 that is not less than 20. Hence there is no cycle no negative
weight cycle. If weight on edge (c, b) is -5 then dijakstra’s algorithm works perfectly
fine. Relaxation many time on any edge is called Bellman Ford algorithm.
Dijakstra’s failure for negative edge weight cycle graph:
(a) (b)
(c) (d)
(e) (f)
Figure 11: (a) is a actual graph. (b) 0 assigned to the source vertex and ∞ assigned to
the remaining vertex. (c)- (d) apply dijakstra’s algorithm and edges are relaxed and
distance are updated. (e)- (f) when distance from vertex 𝒄 to all the reachable vertex
20
Introduction to Algorithms
calculated. Here distance from 𝒄 → 𝒃is 5 less than actual distance 10. - - arrow
shows that there is minimum distance but vertex 𝒃 already relaxed and selected it
can’t be updated now. Here dijakstra’s fails.
In the above graph there is negative edge weight cycle. As we can see in the Figure
12that distance from 𝒃 → 𝒄 and 𝒄 → 𝒃always reducing and keeps on reducing. This is
called a negative edge weight cycle and dijakstra’s always fails for negative weight
cycle.
Figure 12: shows example of negative edge weight cycle and dijakstra’s always fail
for negative edge weight cycle.
Time Complexity of Dijakstra’s Algorithm:
The running time of dijakstra’s algorithm depends on how we implements the min-
priority queue.
Line 1-4 takes 𝑂(𝑉) times.
Line 5 constant time 𝑂(1)
Line 6-7: While loop iterates V times. In priority queue Q total V-1 vertex are there.
𝐸𝑋𝑇𝑅𝐴𝐶𝑇_𝑀𝐼𝑁takes𝑂(1) time to extract the minimum and for V vertex time will be
O(V).
Line 8-12 is a relaxation steps. For loop iterates for E times.
So total time = 𝑂(𝑉) + 𝑂(𝑉 2 + 𝐸) = 𝑶(𝑽𝟐 )
If binary min-heap is used to create the priority queue then 𝐸𝑋𝑇𝑅𝐴𝐶𝑇_𝑀𝐼𝑁 take
log(𝑉) times and relaxation steps also takes log(𝑉) times.
The total running time is therefore 𝑂((𝑉 + 𝐸 )𝑙𝑜𝑔𝑉) = 𝑶(𝑬𝒍𝒐𝒈𝑽)
21
Graph Algorithms-II
After all the vertices have their path check for the negative edge weight cycle
in the graph.
𝐵𝐸𝐿𝐿𝑀𝐴𝑁 − 𝐹𝑂𝑅𝐷(𝐺, 𝑉, 𝐸, 𝑤, 𝑠)
1. Initialize dist[s]0
2. for each vertex 𝑣 ∈ 𝑉-s
3. dist[𝑉] ← ∞
4. for each vertex i=1 to 𝑉 − 1
5. for each edge (𝑢, 𝑣) in E[G]
6. RELAX(𝑢, 𝑣, 𝑤)
7. for each edge edge(𝑢, 𝑣) in E[G]
8. if 𝑑𝑖𝑠𝑡[𝑢] + 𝑤(𝑢 , 𝑣) < 𝑑𝑖𝑠𝑡[𝑣]
9. return FALSE
10. return TRUE
a b c d
0 ∞ ∞ ∞
a b c d
0 ∞ ∞ ∞
0 10 ∞ ∞
a b c d
0 ∞ ∞ ∞
0 10 ∞ ∞
0 10 20 ∞
22
Introduction to Algorithms
a b c d
0 ∞ ∞ ∞
0 10 20 ∞
0 10 20 15
a b c d
0 ∞ ∞ ∞
0 10 20 ∞
0 10 20 15
0 2 20 15
a b c d
0 ∞ ∞ ∞
0 10 20 ∞
0 10 20 15
0 2 20 15
0 2 20 7
.
23
Graph Algorithms-II
Q2. What are the disadvantages of using Dijakstra’s algorithm? List out any three.
Q3. Suppose we have a graph that have no weight on the edges. Each vertex have
weight/cost. Can dijakstra’s algorithm applicable on such graph. If so, give proper
justification and if not then why?
Q5. Apply Bellman-ford on the following graph. List out the order of execution of
vertices.
24
Introduction to Algorithms
25
Graph Algorithms-II
Ford-Fulkerson algorithm is the most efficient method to find a solution for the flow
network. It relies on the Breadth First Search (BFS) to pick a path with minimum
number of edges. Ford-Fulkerson, computes a maximum flow in a network by
applying the greedy method to the augmenting path approach used to prove max-
flow. The intuition behind it that keep augmenting flow among an augmenting path
until there is no augmenting path.
Figure 14: Flow network corresponding to the bipartite graph. Right most graph shows the
solution for a maximum flow problem (the final flow network of (b)). Each edge have unit
26
Introduction to Algorithms
capacity, and shaded edge from L to R corresponds to those in the maximum matching from
(b).
Ford − Fulkerson(G, s, t)
Inputs Given a Network𝐺 ′ = (𝑉 ′ , 𝐸 ′ ) with flow capacity c, a source node s, and a sink
node t
Output Compute a flow f from s to t of maximum value
1. 𝑓(𝑢, 𝑣) ← 0for all edges(𝑢, 𝑣)
2. While there is a path p from s to t in 𝐺𝑓 , such that 𝑐𝑓 (𝑢, 𝑣)> 0 for all
edges (𝑢, 𝑣) ∈ p
3. Find 𝑐𝑓 (𝑝) = min{𝑐𝑓 (𝑢, 𝑣) ∶ (𝑢, 𝑣) ∈ p}
4. For each edge (𝑢, 𝑣) ∈ p
5. 𝑓(𝑢, 𝑣) ← 𝑓(𝑢, 𝑣) + 𝑐𝑓 (𝑝)(Send flow along the path)
6. 𝑓(𝑢, 𝑣) ← 𝑓(𝑢, 𝑣) − 𝑐𝑓 (𝑝) (The flow might be "returned" later)
Q2. Let K be a flow network with v vertices and e edges. Show how to compute an
augmenting path with the largest residual capacity in 𝑂(𝑣(𝑣 + 𝑒)𝑙𝑜𝑔 𝑣) time.
Q4. InFord-Fulerson algorithm which graph traversal algorithm is used to pick a path
with minimum number of edges.
Q2. b)
Q3. a)
Q4. Complexity of Prim’s Algorithm:
Line 1 and Line 2 takes a constant time O(1)
In Line 3 while loop executes until all edges traversed or |𝑉| − 1 edges have
been selected. While loop executes 𝑂(𝐸)times.
Line 10-14 take constant time. O(1)
Adding all the time= O(1)+ 𝑂(𝐸) + O(1) = 𝑂(𝐸)
28
Introduction to Algorithms
Now in worst case when graph is a complete graph, you need to traverse all the
edges. The no of edges
|𝑉|(|𝑉| − 1)
𝐸= = 𝑂(|𝑉|2 ) = 𝑂(𝑉 2 )
2
Now in the average or best case to find spanning tree graph must be connected.
Means there must be at least |𝑉| − 1 edges in the graph. Thus in best or average
case complexity will be = 𝑂(𝐸)
Q5. c)
Q6.
Q7.
29
Graph Algorithms-II
Q5.
a b c d e
0 ∞ ∞ ∞ ∞
0 -2 ∞ ∞ ∞
0 -2 5 ∞ ∞
0 -2 2 ∞ ∞
0 -2 2 ∞ 1
0 -2 2 1 1
0 -2 2 -3 1
0 -2 2 -3 1
Q6. Bellman-Ford calculates the shortest distance to all vertices in the graph from the
source vertex.Dijkstra's algorithm is a greedy algorithm that selects the nearest vertex
that has not been processed. Bellman-Ford, on the other hand, relaxes all of the edges.
30
Introduction to Algorithms
Q7.
a b c d
0 ∞ ∞ ∞
0 10 ∞ ∞
0 10 30 ∞
0 5 30 ∞
0 5 25 70
0 0 20 65
Q8. Bellman-ford algorithm can be used in to route packets of data on network used in
distance vector routing protocol.
Q3. Maximum flow must use the maximum number of unitary capacity edges across
the cut (L, R). Consider unitary capacity because no maximum flow is given on the
edges.
31
Graph Algorithms-II
6. Fundamentals of Computer Algorithms, E. Horowitz & S. Sahni: (Galgotia
Publications).
7. The Design and Analysis of Algorithms, AnanyLevitin: (Pearson Education, 2003).
8. Programming Languages (Second Edition) ─ Concepts and Constructs, Ravi Sethi:
(Pearson Education, Asia, 1996).
32