Cs3401 Algorithm Unit2
Cs3401 Algorithm Unit2
UNIT II
GRAPH ALGORITHMS
Graph algorithms: Representations of graphs - Graph traversal: DFS – BFS - applications
- Connectivity, strong connectivity, bi-connectivity - Minimum spanning tree: Kruskal’s
and Prim’s algorithm- Shortest path: Bellman-Ford algorithm - Dijkstra’s algorithm -
Floyd-Warshall algorithm Network flow: Flow networks - Ford-Fulkerson method –
Matching: Maximum bipartite matching
In general, A graph is a collection of nodes (also called vertices) and edges (also called
arcs or links) each connecting a pair of nodes.
V1 V2
Example:
V3
2. What are the different types of Graph?
There are two types of Graphs. They are:
1. Directed Graphs.
2. Undirected Graphs.
Example for Directed Graph: Example for undirected Graph:
V V
Example: V2
V1 V2
V3 V3
Example: V1 V2
V3 V4
6. What are directed graphs?
Directed graph is a graph which consists of directed edges, where each edge in E is
unidirectional. It is also referred as Digraph. If (v,w) is a directed edge then (v,w) ≠
(w,v
V1 V2
(v1,v2) ≠ (v2,v1)
V3
V V
(v1,v2) = (v2,v1)
V
8. Define cycle.
A cycle in a graph is a path in which first and last vertex are the same.
B C
D E
Ex:
1 1
1 2
1 2
2 2
2 2
3 3
Diagram: V1 V2
V4 V5
V5
Diagram: V5 V1 V2
V4 V3
Example: A C
B D
Degree (A) = 0
Degree (C) = 1
Degree (D) = 3
The out degree is the numbers of edges that are exiting or leaving from the vertex V.
Indegree(V1) = 1,Outdegree(V1)=2
V1 V,
V3
Example:
A C
B D
18. Prove that the number of odd degree vertices in a connected graph should be even.
(May/June 2007)
Consider a graph with odd degree vertices.
Degree =1
This implies that the no. of vertices is even for odd degree graphs.
Example:
V V
aV V V V V z
V V V
The paths from a to z are as below:
Example:
2 3
Example:
1
2 3
Example
In the given example, after removing edge E1 the graph does not become disconnected.
Given an undirected and connected graph G=(V,E), a spanning tree of the graph G is a
tree that spans G (that is, it includes every vertex of G) and is a subgraph of G (every
edge in the tree belongs to G)
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree
from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with all
the connecting edges at every step. The edges with the minimal weights causing no cycles
in the graph got selected.
Kruskal's Algorithm is used to find the minimum spanning tree for a connected
weighted graph. The main target of the algorithm is to find the subset of edges by using
which we can traverse every vertex of the graph. It follows the greedy approach that finds
an optimum solution at every stage instead of focusing on a global optimum.
Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices
of a weighted graph.It can work with graphs in which edges can have negative weights.
Dijkstra's Algorithm finds the shortest path between a given node (which is called the
"source node") and all other nodes in a graph.This algorithm uses the weights of the
edges to find the path that minimizes the total distance (weight) between the source node
and all other nodes.
The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this
algorithm, it will generate a matrix, which will represent the minimum distance from any
node to all other nodes in the graph.
At first the output matrix is same as given cost matrix of the graph. After that the output
matrix will be updated with all vertices k as the intermediate vertex.
PREPARED BY D.SUJATHA, AP/CSE Page 9
SRRCET/CSE/IV SEMESTER/CS3401-ALGORITHM
33. Write down the optimization technique used for Warshalls algorithm. State
the rules and assumptions which are implied behind that.
Warshalls algorithm constructs the transitive closure of given digraph with n
vertices through a series of n-by-n Boolean matrices. The computations in warshalls
algorithm are given by following sequences,
R(0),…,R(k-1),…,R(k),…,R(n)
Rules:
1. Start with computation of R (0). In R(0) any path with intermediate veritices is not
allowed. That means only direct edges towards the vertices are considered. In other words
the path length of one edge is allowed in R (0). Thus R(0) is adjacency matrix for the
digraph.
2. Construct R(1) in which first vertex is used as intermediate vertex and a path length of
two edge is allowed. Note that R(1) is build using R(0)which is already computed.
3. Go on building R(k) by adding one intermediate vertex each time and with more path
length. Each R(k) has to be built from R(k-1)
4. The last matrix in this series is R (1), in this R(n) all the n vertices are used as
intermediate vertices. And the R(n) which is obtained is nothing but the transitive closure
of given digraph.
34. Define the single source shortest path problem. (MAY\JUNE 2016)
Dijkstra’s algorithm solves the single source shortest path problem of finding
shortest paths from a given vertex( the source), to all the other vertices of a weighted
graph or digraph.
Dijkstra’s algorithm provides a correct solution for a graph with non-negative weights.
set to −uij; if there is no edge between vertices i and j , both these elements are set to
zero. Outline a simple algorithm for identifying a source and a sink in a network
presented by sucha matrix and indicate its time efficiency.
of edges.
42. What do you meant by perfect matching in bipartite graph? (APR/MAY 2017)
When there are an equal number of nodes on each side of a bipartite graph, a
perfect matching is an assignment of nodes on the left to nodes on the right, in such a way
that
i) each node is connected by an edge to the node it is assigned to, and
ii) no two nodes on the left are assigned to the same node on the right
It contains only one vertex with no entering edges called as source and assumed to be 1.
It contains only one vertex at end with no leaving edges called as sink and assumed as n.
The weight uij of each directed edge (i, j) is a positive integer, called as edge capacity.
45. Define the constraint in the context of maximum flow problem. (APR/MAY 2019)
It represents the maximum amount of flow that can pass through an edge. ... , for each
(capacity constraint: the flow of an edge cannot exceed its capacity); , for each
(conservation of flows: the sum of the flows entering a node must equal the sum of
the flows exiting a node, except for the source and the sink nodes).
PART-B
BFS algorithm
A standard BFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the back of the
queue.
The graph might have two different disconnected parts so to make sure that we cover every vertex, we can
also run the BFS algorithm on every node
BFS example
Let's see how the Breadth First Search algorithm works with an example. We use an undirected graph with
5 vertices.
Visit last remaining item in the queue to check if it has unvisited neighbors
Since the queue is empty, we have completed the Breadth First Traversal of the graph.
The time complexity of the BFS algorithm is represented in the form of O(V + E), where V is the number
of nodes and E is the number of edges.
The space complexity of the algorithm is O(V).
Depth first Search or Depth first traversal is a recursive algorithm for searching all the vertices of a graph or
tree data structure. Traversal means visiting all the nodes of a graph.
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top of the
stack.
Let's see how the Depth First Search algorithm works with an example. We use an undirected graph with 5
vertices.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.
After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have completed the
Depth First Traversal of the graph.
After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have completed the
Depth First Traversal of the graph.
The time complexity of the DFS algorithm is represented in the form of O(V + E), where V is the number
of nodes and E is the number of edges.
The space complexity of the algorithm is O(V).
Application of DFS Algorithm
1. For finding the path
Dijkstra's Algorithm:
It differs from the minimum spanning tree because the shortest distance between two vertices might
Dijkstra's Algorithm works on the basis that any subpath B -> D of the shortest path A -> D between
vertices A and D is also the shortest path between vertices B and D.
The algorithm uses a greedy approach in the sense that we find the next best solution hoping that the end
result is the best solution for the whole problem.
It is easier to start with an example and then think about the algorithm.
Choose a starting vertex and assign infinity path values to all other devices
If the path length of the adjacent vertex is lesser than new path length, don't update it
After each iteration, we pick the unvisited vertex with the least path length. So we choose 5 before 7
Notice how the rightmost vertex has its path length updated twice
We need to maintain the path distance of every vertex. We can store that in an array of size v, where v is the
number of vertices.
We also want to be able to get the shortest path, not only know the length of the shortest path. For this, we
map each vertex to the vertex that last updated its path length.
Once the algorithm is over, we can backtrack from the destination vertex to the source vertex to find the
path.
A minimum priority queue can be used to efficiently receive the vertex with least path distance.
4. Write floyd’s algorithm to find all pair shortest path and derive its complexity.
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices in a
weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it does not
work for the graphs with negative cycles (where the sum of the edges in a cycle is negative).
A weighted graph is a graph in which each edge has a numerical value associated with it.
This algorithm follows the dynamic programming approach to find the shortest paths.
Initial graph
Follow the steps below to find the shortest path between all the pairs of vertices.
Let k be the intermediate vertex in the shortest path from source to destination. In this step, k is the first
vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
That is, if the direct distance from the source to the destination is greater than the path through the vertex k,
then the cell is filled with A[i][k] + A[k][j].
In this step, k is vertex 1. We calculate the distance from source vertex to destination vertex through this
vertex k.
4. Calculate the distance from the source vertex to destination vertex through this vertex k
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the distance from
vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4) is 7. Since 4 < 7, A0[2, 4] is
filled with 4.
5. Similarly, A2 is created using A1 . The elements in the second column and the second row are left as they
are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step 2.
6. Calculate the distance from the source vertex to destination vertex through this vertex 2
7. Similarly, A3 and A4 is also created.
8. Calculate the distance from the source vertex to destination vertex through this vertex
3
9. Calculate the distance from the source vertex to destination vertex through this vertex 4
10. A4 gives the shortest path between each pair of vertices.
Floyd-Warshall Algorithm
n = no of vertices
for k = 1 to n
for i = 1 to n
for j = 1 to n
Time Complexity
There are three loops. Each loop has constant complexities. So, the time complexity of the Floyd-Warshall
algorithm is O(n3).
Space Complexity
Kruskal's Algorithm
Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of
the edges of that graph which
form a tree that includes every vertex
has the minimum sum of weights among all the trees that can be formed from the graph
It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of
finding a global optimum.
We start from the edges with the lowest weight and keep adding edges until we reach our goal.
2. Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle,
then reject this edge.
Choose the edge with the least weight, if there are more than 1, choose anyone
Choose the next shortest edge that doesn't create a cycle and add it
Choose the next shortest edge that doesn't create a cycle and add it
Any minimum spanning tree algorithm revolves around checking if adding an edge creates a loop or not.
The most common way to find this out is an algorithm called Union Find. The Union-Find algorithm
divides the vertices into clusters and allows us to check if two vertices belong to the same cluster or not and
hence decide whether adding an edge creates a cycle.
KRUSKAL(G):
A=∅
For each vertex v ∈ G.V:
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A
Prim's algorithm is another popular minimum spanning tree algorithm that uses a different logic to find the
MST of a graph. Instead of starting from an edge, Prim's algorithm starts from a vertex and keeps adding
lowest-weight edges which aren't in the tree, until all vertices have been covered.
Ford-Fulkerson Algorithm
Ford-Fulkerson algorithm is a greedy approach for calculating the maximum possible flow in a network or a
graph.
A term, flow network, is used to describe a network of vertices and edges with a source (S) and a sink (T).
Each vertex, except S and T, can receive and send an equal amount of stuff through it. S can only send
and T can only receive stuff.
We can visualize the understanding of the algorithm using a flow of liquid inside a network of pipes of
different capacities. Each pipe has a certain capacity of liquid it can transfer at an instance. For this
algorithm, we are going to find how much liquid can be flowed from the source to the sink at an instance
using the network.
Terminologies Used
Augmenting Path
Residual Graph
Residual Capacity
It is the capacity of the edge after subtracting the flow from the maximum capacity.
2. While there is an augmenting path between the source and the sink, add this path to the flow.
We can also consider reverse-path if required because if we do not consider them, we may never find a
maximum flow.
Ford-Fulkerson Example
The minimum capacity among the three edges is 2 ( B-T). Based on this, update the flow/capacity for each
path.
2. Select another path S-D-C-T. The minimum capacity among these edges is 3 ( S-D).
3. Now, let us consider the reverse-path B-D as well. Selecting path S-A-B-D-C-T. The minimum residual
The capacity for forward and reverse paths are considered separately.
4. Adding all the flows = 2 + 3 + 1 = 6, which is the maximum possible flow on the flow network.Note that if
the capacity for any edge is full, then that path cannot be used.
Ford-Fulkerson Applications
Review questions
1. Write an algorithm for all pairs shortest path algorithm and what are the time and space
complexity of the algorithm. (APIRL/MAY2009) (APRIL/MAY 2012)
2. Explain Floyd algorithm with example. Write down and explain the algorithm to solve all
pairs shortest paths problem. (APRIL/MAY 2010)(MAY/JUNE 2013).
5. Discuss about the algorithm and pseudocode to find the Minimum Spanning Tree using
Prim’s Algorithm. Find the Minimum Spanning Tree for the graph. Discuss about the
efficiency of the algorithm.(MAY\JUNE 2016) (Apr 18)
6.Solve the all-Pairs shortest-path problem for the diagraph with the following weight
7.Apply Kruskal’s algorithm to find a minimum spanning tree of the following graph.
(NOV/DEC 2016)
8.Write suitable ADT operation for shortest path problem. Show the simulation of shortest path
with an example graph.
10.Explain Dijkstra’s algorithm using the following graph. Find the shortest path
between v1, v2, v3, v4, v5, v6 & v7. 2
(May/June 2007) V2
V1
4 1 3 10
2 7
V4
V3 V5
5 8 4 6
1 V6
V7
x y
13.State and Prove Maximum Flow Min cut Theorem. (MAY\JUNE 2016)
14.Apply the shortest Augmenting Path algorithm to the network shown
below.
15.Explain KMP string matching algorithm for finding a pattern on the text, a analysis
the algorithm.(APR/MAY 2017)
16.Determine the max-flow in the following network. (APR/MAY 2019)