Unit 5 Notes
Unit 5 Notes
Graph Introduction
Graphs are non-linear data structures representing the "connections" between the
elements. These elements are known as the Vertices, and the lines or arcs that
connect any two vertices in the graph are known as the Edges. More formally, a
Graph comprises a set of Vertices (V) and a set of Edges (E). The Graph is denoted
by G(V, E).
Components of a Graph
1. Vertices: Vertices are the basic units of the
graph used to represent real-life objects,
persons, or entities. Sometimes, vertices
are also known as Nodes.
2. Edges: Edges are drawn or used to connect two vertices of the graph. Sometimes,
edges are also known as Arcs.
Types of Graphs
The Graphs can be categorized into two types:
1. Undirected Graph
2. Directed Graph Figure 3: A Simple Undirected Graph
2. Directed Graph: A Graph with edges with direction is termed a Directed Graph.
The edges of this graph imply a one-way relationship in which each edge can only
be traversed in a single direction. The following figure displays a simple directed
graph with four nodes and five edges.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 1 | 35
3. Null Graph - A graph is known as a null graph if there are no
edges in the graph.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 2 | 35
A Graph is said to be Weighted if each edge is
assigned a ‘weight’. The weight of an edge can
denote distance, time, or anything that models
the ‘connection’ between the pair of vertices it
connects. For instance, we can observe a blue
number next to each edge in the following figure
of the Weighted Graph. This number is utilized to
signify the weight of the corresponding edge.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 3 | 35
Comparison Graph Tree
Basic Operations on Graphs - Below are the basic operations on the graph:
Usage of graphs
• Maps can be represented using graphs and then can be used by computers to
provide various services like the shortest path between two cities.
• When various tasks depend on each other then this situation can be represented
using a Directed Acyclic graph and we can find the order in which tasks can
be performed using topological sort.
• State Transition Diagram represents what can be the legal moves from current
states. In-game of tic tac toe this can be used.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 4 | 35
8. Robot planning: Vertices represent states the robot can be in and the edges
the possible transitions between the states. Such graph plans are used, for
example, in planning paths for autonomous vehicles.
Advantages:
1. Graphs are a versatile data structure that can be used to represent a wide
range of relationships and data structures.
2. They can be used to model and solve a wide range of problems, including
pathfinding, data clustering, network analysis, and machine learning.
3. Graph algorithms are often very efficient and can be used to solve complex
problems quickly and effectively.
4. Graphs can be used to represent complex data structures in a simple and
intuitive way, making them easier to understand and analyze.
Disadvantages:
1. Graphs can be complex and difficult to understand, especially for people who
are not familiar with graph theory or related algorithms.
2. Creating and manipulating graphs can be computationally expensive,
especially for very large or complex graphs.
3. Graph algorithms can be difficult to design and implement correctly, and
can be prone to bugs and errors.
4. Graphs can be difficult to visualize and analyze, especially for very large or
complex graphs, which can make it challenging to extract meaningful insights
from the data.
Graph Representations
Graph is a data structure that consists of the following two components:
The pair is ordered because (u, v) is not the same as (v, u) in the case of a
directed graph(di-graph). The pair of the form (u, v) indicates that there is an edge
from vertex u to vertex v. The edges may contain weight/value/cost.
• Adjacency Matrix
• Adjacency List
In this method, the graph is stored in the form of the 2D matrix where rows and
columns denote vertices. Each entry in the matrix represents the weight of the
edge between those vertices. Adjacency Matrix is a 2D array of size V x V where V is
the number of vertices in a graph. Let the 2D array be adj[ ][ ], a slot adj[i][j] =
1 indicates that there is an edge from vertex i to vertex j. The adjacency matrix for
an undirected graph is always symmetric.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 5 | 35
Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then
there is an edge from vertex i to vertex j with weight w.
• In the case of an
undirected graph, we need
to show that there is an
edge from vertex i to
vertex j and vice versa. In
code, we assign adj[i][j] =
1 and adj[j][i] = 1.
Advantages of Adjacency
Matrix:
• Consumes more space O(V2). Even if the graph is sparse(contains less number
of edges), it consumes the same space.
• Adding a vertex takes O(V2) time. Computing all neighbors of a vertex takes
O(V) time (Not efficient).
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 6 | 35
An array of linked lists is used. The size of the array is equal to the number of
vertices. Let the array be an array[]. An entry array[i] represents the linked list of
vertices adjacent to the ith vertex. This representation can also be used to
represent a weighted
graph. The weights of
edges can be
represented as lists of
pairs. Consider the
following graph:
Example of undirected
graph with 5 vertices
Following is the
adjacency list
representation of the
above graph.
1. Saves space. Space taken is O(|V|+|E|). In the worst case, there can be C(V,
2) number of edges in a graph thus consuming O(V2) space.
2. Adding a vertex is easier.
3. Computing all neighbours of a vertex takes optimal time.
1. Queries like whether there is an edge from vertex u to vertex v are not efficient
and can be done O(V).
When the graph contains a large number of edges then it is good to store it as a
matrix because only some entries in the matrix will be empty. An algorithm such
as Prim’s and Dijkstra adjacency matrix is used to have less complexity.
Graph Traversals
The graph has two types of traversal algorithms. These are called the Breadth First
Search and Depth First Search.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 7 | 35
the vertices you are visiting. To implement such an order, you use a queue data
structure which First-in, First-out approach.
• Visited and
• Not visited.
Algorithm
1. Start putting anyone vertices from the graph at the back of the queue.
2. First, move the front queue item and add it to the list of the visited node.
3. Next, create nodes of the adjacent vertex of that list and add them which have not
been visited yet.
4. Keep repeating steps two and three until the queue is found to be empty.
Starting from the root, all the nodes at a particular level are visited first and then
the nodes of the next level are traversed till all the nodes are visited. To do this a
queue is used. All the adjacent unvisited nodes of the current level are pushed into
the queue and the nodes of the current level are marked visited and popped from
the queue.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 8 | 35
Remove node 0 from the front of queue and visited the unvisited neighbours and
push into queue.
Step 4: Remove node 1 from the front of queue and visit the unvisited neighbours
and push them into queue.
Step 5: Remove node 2 from the front of queue and visit the unvisited neighbours
and push them into queue.
Remove node 4 from the front of queue and visit the unvisited neighbours and push
them into queue. Now, Queue becomes empty, So, terminate these process of
iteration
Applications
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 9 | 35
Depth-first search - Depth First Traversal
(DFS) for a graph is similar to Depth
First Traversal of a tree. The only catch
here is, that, unlike trees, graphs may
contain cycles (a node may be visited
twice). To avoid processing a node more
than once, use a boolean visited array.
Algorithm
1. Start by putting one of the vertexes of the graph on the stack's top.
2. Put the top item of the stack and add it to the visited vertex list.
3. Create a list of all the adjacent nodes of the vertex and then add those nodes
to the unvisited at the top of the stack.
4. Keep repeating steps 2 and 3, and the stack becomes empty.
Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack
and put all of its adjacent nodes which are not visited in the stack.
Visit node 1
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 10 | 35
Step 4: Now, Node 2 at the top
of the stack, so visit node 2 and
pop it from the stack and put all
of its adjacent nodes which are
not visited (i.e, 3, 4) in the stack.
Visit node 4
Visit node 3, Now, Stack becomes empty, which means we have visited all the nodes
and our DFS traversal ends.
Applications
1. DFS finds its application when it comes to finding paths between two vertices and
detecting cycles.
2. Topological sorting can be done using the DFS algorithm easily.
3. DFS is also used for one-solution puzzles.
Topological Sorting
A topological sort or topological ordering of a directed graph is a linear ordering of
its vertices in which u occurs before v in the ordering for every directed edge uv
from vertex u to vertex v. For example, the graph's vertices could represent jobs to
be completed, and the edges could reflect requirements that one work must be
completed before another.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 11 | 35
Topological sorting has many applications,
particularly in ranking issues like the feedback arc
set. Even if the DAG includes disconnected
components, topological sorting is possible.
Topological sorting for Directed Acyclic Graph (DAG)
is a linear ordering of vertices such that for every
directed edge u v, vertex u comes before v in the
ordering. Topological Sorting for a graph is not
possible if the graph is not a DAG.
For example, a topological sorting of the following graph is “5 4 2 3 1 0”. There can
be more than one topological sorting for a graph. Another topological sorting of the
following graph is “4 5 2 3 1 0”. The first vertex in topological sorting is always a
vertex with an in-degree of 0 (a vertex with no incoming edges).
In DFS, we print a vertex and then recursively call DFS for its adjacent vertices.
In topological sorting, we need to print a vertex before its adjacent vertices.
For example, in the given graph, the vertex ‘5’ should be printed before vertex ‘0’,
but unlike DFS, the vertex ‘4’ should also be printed before vertex ‘0’. So Topological
sorting is different from DFS. For example, a DFS of the shown graph is “5 2 3 1 0
4”, but it is not a topological sorting.
Prerequisite: DFS, We can modify DFS to find the Topological Sorting of a graph.
A vertex is pushed to stack only when all of its adjacent vertices (and their adjacent
vertices and so on) are already in the stack
Approach:
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 12 | 35
5.1.1. Recursively call the topological sort function on the node
5.1.2. Push the current node in the stack.
6. Print all the elements in the stack.
Visited Nodes 0 1 2 3 4 5
Visit Status False False False False False False
Stack 0
Stack 0 1
Stack 0 1 3 2
Stack 0 1 3 2 4
Stack 0 1 3 2 4 5
Visited Nodes 0 1 2 3 4 5
Visit Status True True True True True True
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 13 | 35
2. Determining the order of compilation tasks to perform in make files, data
serialization, and resolving symbol dependencies in linker are all examples of
applications of this type in computer science.
3. Finding cycle in a graph: Only directed acyclic graphs may be ordered
topologically (DAG). It is impossible to arrange a circular graph topologically.
4. Operation System deadlock detection: A deadlock occurs when one process is
waiting while another holds the requested resource.
5. Dependency resolution: Topological Sorting has been proved to be very helpful
in Dependency resolution.
6. Sentence Ordering: The order of the sentences can be represented using a DAG.
Here the sentences (Si) represent the vertices, and the edges represent the
ordering between sentences. For example, if we have a directed edge between S1
to S2, then S1 must come before S2.
7. Critical Path Analysis: A project management approach known as critical route
analysis. It's used to figure out how long a project should take and how dependent
each action is on the others.
8. Course Schedule problem: Topological Sorting has been proved to be very
helpful in solving the Course Schedule problem.
1. It is connected, i.e. it is possible to reach every vertex from every other vertex,
by a simple path.
2. Even after removing any vertex the graph remains connected.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 14 | 35
2. There is not articulation point in graph.
We start from any vertex and do DFS traversal. In DFS traversal, we check if there
is any articulation point. If we don’t find any articulation point, then the graph is
Biconnected. Finally, we need to check whether all vertices were reachable in DFS
or not. If all vertices were not reachable, then the graph is not even connected
Biconnected components
The concept of biconnected components is based on the concept of the disc and low
values algorithms. This article aims to focus on the algorithm developed by Robert
Tarjan and John Hopcroft's view of biconnected components in a graph data
structure. The maximal sub-graph is known to be the concept behind Biconnected
Components.
Biconnected components are a part of the graph data structure created. When the
articulation points are found, the edges stored in the stack data structure will form
an entity of biconnected components. A biconnected component is a
maximal biconnected subgraph.
Algorithm is based on Disc and Low Values. Idea is to store visited edges in a stack
while DFS on a graph and keep
looking for Articulation
Points (highlighted in above
figure). As soon as
an Articulation Point u is
found, all edges visited while
DFS from node u onwards will
form one biconnected
component. When DFS
completes for one connected
component, all edges present in
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 15 | 35
stack will form a biconnected component. If there is no Articulation Point in graph,
then graph is biconnected and so there
will be one biconnected component which
is the graph itself.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 16 | 35
Identify Biconnected Components
How to find a biconnected component in a graph. For this we will use Tarjan's
algorithm for finding biconnected components in a graph. It was developed by Robert
Tarjan in 1972.
Algorithmic steps:
Articulation Point
A vertex V in a connected graph G is an articulation point
(cut point) if the deletion of vertex V along with all edges
incident to V disconnects the graph into two or more non-
empty components.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 17 | 35
Example: The above graph contains no articulation point
=> The graph is Biconnected Graph.
1. The DFS number of the vertex: This is the order in which the vertex was visited
during the DFS traversal.
2. The low-link value of the vertex: This is the minimum DFS number of any vertex
that can be reached from the current vertex by following zero or more DFS tree
edges and then at least one back edge.
To identify the articulation points in the graph, we can use the following steps:
1. Start by setting the DFS number and low-link value of each vertex to be the same.
2. For each vertex, do the following:
2.1. Consider all of the vertices that are reachable from the current vertex
by a single edge.
2.2. For each of these vertices, compute the low-link value using the
following rule:
2.2.1. If the low-link value of the current vertex is greater than the low-
link value of the reachable vertex, set the low-link value of the
current vertex to be the same as the low-link value of the reachable
vertex.
2.2.2. If the low-link value of the current vertex is equal to its DFS
number, then it is an articulation point.
3. Repeat this process for all vertices in the graph.
1. An articulation point (or cut vertex) in a graph is a vertex that, when removed
along with all the edges incident to it, increases the number of connected
components in the graph. In other words, an articulation point is a vertex that
plays a key role in keeping the graph connected.
2. Biconnected components, on the other hand, are subgraphs of a graph that are
themselves connected and do not have any articulation points.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 18 | 35
3. These concepts are important because they can be used to analyze the
connectivity and structural properties of a graph. For example, an articulation
point can be used to identify the most critical vertices in a network, such as
bridges in a road network or routers in a computer network. Biconnected
components can be used to identify the most robust parts of a graph, as they
remain connected even if one of their vertices is removed.
4. In addition, algorithms for finding articulation points and biconnected
components are important building blocks for other graph algorithms, such as
finding bridges and cycles in a graph, and for analyzing the structure of graphs
in general.
5. By the end of this article at OpenGenus, you must have complete knowledge
on biconnected components, lets test your knowledge with a question!
1. Dijkstra's Algorithm begins at the node we select (the source node), and it
examines the graph to find the shortest path between that node and all the
other nodes in the graph.
2. The Algorithm keeps records of the presently acknowledged shortest distance
from each node to the source node, and it updates these values if it finds any
shorter path.
3. Once the Algorithm has retrieved the shortest path between the source and
another node, that node is marked as 'visited' and included in the path.
4. The procedure continues until all the nodes in the graph have been included in
the path. In this manner, we have a path connecting the source node to all other
nodes, following the shortest possible path to reach each node.
A graph and source vertex are requirements for Dijkstra's Algorithm. This Algorithm
is established on Greedy Approach and thus finds the locally optimal choice (local
minima in this case) at each step of the Algorithm.
Each Vertex in this Algorithm will have two properties defined for it:
1. Visited Property
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 19 | 35
2. Path Property
Visited Property:
1. The 'visited' property signifies whether or not the node has been visited.
2. We are using this property so that we do not revisit any node.
3. A node is marked visited only when the shortest path has been found.
Path Property:
1. The 'path' property stores the value of the current minimum path to the node.
2. The current minimum path implies the shortest way we have reached this node
till now.
3. This property is revised when any neighbour of the node is visited.
4. This property is significant because it will store the final answer for each node.
Initially, we mark all the vertices, or nodes, unvisited as they have yet to be visited.
The path to all the nodes is also set to infinity apart from the source node. Moreover,
the path to the source node is set to zero (0). We then select the source node and
mark it as visited. After that, we access all the neighbouring nodes of the source node
and perform relaxation on every node. Relaxation is the process of lowering the
cost of reaching a node with the help of another node. In the process of relaxation,
the path of each node is revised to the minimum value amongst the node's current
path, the sum of the path to the previous node, and the path from the previous node
to the current node.
Let us suppose that p[n] is the value of the current path for node n, p[m] is the
value of the path up to the previously visited node m, and w is the weight of the
edge between the current node and previously visited one (edge weight between n and
m).In the mathematical sense, relaxation can be exemplified as:
We then mark an unvisited node with the least path as visited in every subsequent
step and update its neighbour’s paths. We repeat this procedure until all the nodes
in the graph are marked visited. Whenever we add a node to the visited set, the
path to all its neighbouring nodes also changes accordingly. If any node is left
unreachable (disconnected component), its path remains 'infinity'. In case the
source itself is a separate component, then the path to all other nodes remains
'infinity'.
Dijkstra's Algorithm
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 20 | 35
Example - Implementation of the algorithm with the
help of an example:
1. A=0
2. B = 4 (A -> B)
3. C = 5 (A -> C)
4. D = 4 + 9 = 13 (A -> B -> D)
5. E = 5 + 3 = 8 (A -> C -> E)
6. F = 5 + 3 + 6 = 14 (A -> C -> E -> F)
The working of the algorithm can be best understood using an example. Consider the
following graph having nodes marked from A to G, connected by weighted edges as
follows −
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 21 | 35
Pass 1 − We choose node A from Q since it
has the lowest dist[] value of 0 and put it in
S. The neighbouring nodes of A are B and C.
We update dist[] values corresponding to B
and C according to the algorithm. So the
values of the data structures become -
dist[7]={0,5,6,∞,∞,∞,∞}
Q={B,C,D,E,F,G}
S={A}
dist[7]={0,5,6,12,13,∞,∞}
Q={C,D,E,F,G}
S={A,B}
dist[7]={0,5,6,8,13,10,∞}
Q={D,E,F,G}
S={A,B,C}
dist[7]={0,5,6,8,10,10,18}
Q={E,F,G}
S={A,B,C,D}
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 22 | 35
dist[7]={0,5,6,8,10,10,13}
Q={F,G}
S={A,B,C,D,E}
dist[7]={0,5,6,8,10,10,13}
Q={G}
S={A,B,C,D,E,F}
dist[7]={0,5,6,8,10,10,13}
Q=∅
S={A,B,C,D,E,F,G}
1. The Time Complexity of Dijkstra's Algorithm is O(E log V), where E is the
number of edges and V is the number of vertices.
2. The Space Complexity of Dijkstra's Algorithm is O(V), where V is the number of
vertices.
Advantages
Disadvantages
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 23 | 35
3. Since this algorithm heads to the acyclic graph, it cannot calculate the exact
shortest path.
4. It also requires maintenance to keep a record of vertices that have been visited.
Algorithm - This algorithm follows the dynamic programming approach to find the
shortest paths.
Step 1 − Construct an adjacency matrix A with all the costs of edges present in the
graph. If there is no path between two vertices, mark the value as ∞.
Step 2 − Derive another adjacency matrix A1 from A keeping the first row and first
column of the original adjacency matrix intact in A1. And for the remaining values,
say A1[i,j], if A[i,j]>A[i,k]+A[k,j] then replace A1[i,j] with A[i,k]+A[k,j]. Otherwise, do
not change the values. Here, in this step, k = 1 (first vertex acting as pivot).
Step 3 − Repeat Step 2 for all the vertices in the graph by changing the k value for
every pivot vertex until the final matrix is achieved.
Step 4 − The final adjacency matrix obtained is the final solution with all the shortest
paths.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 24 | 35
How Floyd-Warshall Algorithm Works?
Follow the steps below to find the shortest path between all the
pairs of vertices.
2. Now, create a matrix A1 using matrix A0 . The elements in the first column and
the first row are left as they are. The remaining cells are filled in the following
way.
In this step, k is vertex 1. 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.
3. Similarly, A2 is created
using A . The elements in the
1
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the
same as in step 2. Calculate the distance from the source vertex to destination
vertex through this vertex 2
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 25 | 35
Calculate the distance from the
source vertex to destination
vertex through this vertex 4
Solution
Step 1 - Construct
an adjacency
matrix A with all
the distances as
values.
Step 3 - Considering
the above adjacency
matrix as the input,
derive another matrix
A0 by keeping only
first rows and
columns intact.
Take k = 1, and replace all the other values by A[i,k]+A[k,j].
Step 5 - Considering the above adjacency matrix as the input, derive another
matrix A0 by keeping only first rows and columns intact. Take k = 1, and replace all
the other values by A[i,k]+A[k,j].
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 26 | 35
Step 6 - Considering the
above adjacency matrix as
the input, derive another
matrix A0 by keeping only
first rows and columns
intact. Take k = 1, and
replace all the other values
by A[i,k]+A[k,j].
Minimum Spanning tree - Minimum spanning tree can be defined as the spanning
tree in which the sum of the weights of the edge is minimum. The weight of the
spanning tree is the sum of the weights given to the edges of the spanning tree.
Prim’s algorithm is also a Greedy algorithm. This algorithm always starts with a
single node and moves through several adjacent nodes, in order to explore all of
the connected edges along the way. The algorithm starts with an empty spanning
tree. The idea is to maintain two sets of vertices. The first set contains the vertices
already included in the MST, and the other set contains the vertices not yet
included. At every step, it considers all the edges that connect the two sets and
picks the minimum weight edge from these edges. After picking the edge, it moves
the other endpoint of the edge to the set containing MST.
A group of edges that connects two sets of vertices in a graph is called cut in graph
theory. So, at every step of Prim’s algorithm, find a cut, pick the minimum weight
edge from the cut, and include this vertex in MST Set (the set that contains already
included vertices).
How does Prim’s Algorithm Work? - The working of Prim’s algorithm can be
described by using the following steps:
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 27 | 35
The applications of prim's algorithm are –
o Prim's algorithm can be used in network designing.
o It can be used to make network cycles.
o It can also be used to lay down electrical wiring cables.
Step 4 - Now, select the edge CD, and add it to the MST.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 28 | 35
Step 1: Firstly, we select
an arbitrary vertex that
acts as the starting vertex
of the Minimum Spanning
Tree. Here we have selected
vertex 0 as the starting
vertex.
Selected node - 0 is
selected as starting vertex
Selected node – 0, 1 is
added to the MST
Selected node – 0, 1, 7 is
added in the MST
Selected node – 0, 1, 7, 6 is
added in the MST
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 29 | 35
Step 5: The connecting
edges now are {7, 8}, {1, 2},
{6, 8} and {6, 5}. Include
edge {6, 5} and vertex 5 in
the MST as the edge has the
minimum weight (i.e., 2)
among them.
Selected node – 0, 1, 7, 6,
5 Include vertex 5 in the
MST
Selected node – 0, 1, 7, 6,
5 , 2 Include vertex 2 in the
MST
Selected node – 0, 1, 7, 6, 5
, 2 , 8 Add vertex 8 in the
MST
Selected node – 0, 1, 7, 6, 5
, 2 , 8 , 3 Include vertex 3 in
MST
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 30 | 35
minimum weighted edge from the incomplete MST to 4 is {3, 4}.
Selected node – 0, 1, 7, 6,
5 , 2 , 8 , 3 , 4 Include
vertex 4 in the MST
Note: If we had selected the edge {1, 2} in the third step then the MST would look
like the following.
Prim’s Algorithm
• Create a set mstSet that keeps track of vertices already included in MST.
• Assign a key value to all vertices in the input graph. Initialize all key values as
INFINITE. Assign the key value as 0 for the first vertex so that it is picked first.
• While mstSet doesn’t include all vertices
• Pick a vertex u that is not there in mstSet and has a minimum key value.
• Include u in the mstSet.
• Update the key value of all adjacent vertices of u. To update the key values,
iterate through all adjacent vertices.
• For every adjacent vertex v, if the weight of edge u-v is less than the previous
key value of v, update the key value as the weight of u-v.
The idea of using key values is to pick the minimum weight edge from the cut.
The key values are used only for vertices that are not yet included in MST, the key
value for these vertices indicates the minimum weight edges connecting them to the
set of vertices included in MST.
Time Complexity: O(V2), If the input graph is represented using an adjacency list,
then the time complexity of Prim’s algorithm can be reduced to O(E * logV) with
the help of a binary heap. In this implementation, we are always considering the
spanning tree to start from the root of the graph.
A minimum spanning tree (MST) or minimum weight spanning tree for a weighted,
connected, undirected graph is a spanning tree with a weight less than or equal
to the weight of every other spanning tree. Kruskal’s algorithm is used to find the
MST of a given weighted graph. In Kruskal’s algorithm, sort all edges of the given
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 31 | 35
graph in increasing order. Then it keeps on adding new edges and nodes in the
MST if the newly added edge does not form a cycle. It picks the minimum
weighted edge at first at the maximum weighted edge at last. Thus we can say that
it makes a locally optimal choice in each step in order to find the optimal solution.
Hence this is a Greedy Algorithm.
How to find MST using Kruskal’s algorithm? - Below are the steps for finding
MST using Kruskal’s algorithm:
Example: Below is the illustration of the above approach - Input Graph: The graph
contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be
having (9 – 1) = 8 edges.
After sorting: we create table Now pick all edges one by one from the sorted list of
edges
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 32 | 35
Step 4: Pick edge 0-1. No cycle is
formed, include it. Add edge 0-1 in the
MST
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 33 | 35
Now, let's start constructing the minimum spanning tree.
So, the final minimum spanning tree obtained from the given weighted graph by using
Kruskal's algorithm is –
Now, the number of edges in the above tree equals the number of vertices minus 1.
So, the algorithm stops here.
Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the graph is a separa
te tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it
to the forest F (for combining two trees into one tree).
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 34 | 35
ELSE
Discard the edge
6. Step 6: END
Auxiliary Space: O(V + E), where V is the number of vertices and E is the number
of edges in the graph.
Sr.
No. Prim’s Algorithm Kruskal’s Algorithm
1 It starts to build the Minimum It starts to build the Minimum
Spanning Tree from any vertex in Spanning Tree from the vertex carrying
the graph. minimum weight in the graph.
2 It traverses one node more than one
It traverses one node only once.
time to get the minimum distance.
3 Prim’s algorithm has a time
complexity of O(V2), V being the Kruskal’s algorithm’s time complexity
number of vertices and can be is O(E log V), V being the number of
improved up to O(E log V) using vertices.
Fibonacci heaps.
4 Kruskal’s algorithm can generate
Prim’s algorithm gives connected
forest(disconnected components) at
component as well as it works only
any instant as well as it can work on
on connected graph.
disconnected components
5 Prim’s algorithm runs faster in Kruskal’s algorithm runs faster in
dense graphs. sparse graphs.
6 It generates the minimum spanning
It generates the minimum spanning
tree starting from the least weighted
tree starting from the root vertex.
edge.
7 Applications of prim’s algorithm are
Travelling Salesman Problem, Applications of Kruskal algorithm are
Network for roads and Rail tracks LAN connection, TV Network etc.
connecting all the cities etc.
8 Prim’s algorithm prefer list data Kruskal’s algorithm prefer heap data
structures. structures.
D r. D h e r e s h S o n i , A s s t . P r o f. V I T B h o p a l U n i v e r s i t y P a g e 35 | 35