Dijkstra’s
Algorithm and
Prim’s Algorithm
Presented by:-
Shouvic Banik
Contents
Dijkstras algorithm
Basic concepts of Dijkstras algorithm
Working principle of Dijkstras algorithm
Applications
Merits
Demerits
What is Prim’s algorithm
Basic concepts of Prim’s algorithm
Working Principle of Prim’s algorithm
Applications
Merits
Demerits
What is Dijkstras
Algorithm?
What if you are provided with a graph of nodes where every node is linked to several
other nodes with varying distance. Now, if you begin from one of the nodes in the
graph, what is the shortest path to every other node in the graph?
Well simply explained, an algorithm that is used for finding the shortest distance, or
path, from starting node to target node in a weighted graph is known as Dijkstra’s
Algorithm.
It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published
three years later.
This algorithm makes a tree of the shortest path from the starting node, the source, to
all other nodes (points) in the graph.
Dijkstra's algorithm makes use of weights of the edges for finding the path that
minimizes the total distance (weight) among the source node and all other nodes. This
algorithm is also known as the single-source shortest path algorithm.
Dijkstra’s algorithm is the iterative algorithmic process to provide us with the shortest
path from one specific starting node to all other nodes of a graph.
Generally, Dijkstra’s algorithm works on the principle of relaxation where an
approximation of the accurate distance is steadily displaced by more suitable values
until the shortest distance is achieved.
Also, the estimated distance to every node is always an overvalue of the true distance
and is generally substituted by the least of its previous value with the distance of a
recently determined path.
It uses a priority queue to greedily choose the nearest node that has not been visited
yet and executes the relaxation process on all of its edges.
The following are the basic concepts of
Dijkstra's Algorithm:
1. Dijkstra's Algorithm basically starts at the node that you choose (the
source node) and it analyzes the graph to find the shortest path between
that node and all the other nodes in the graph.
2. The algorithm keeps track of the currently known shortest distance from
each node to the source node and it updates these values if it finds a
shorter path.
3. Once the algorithm has found the shortest path between the source node
and another node, that node is marked as "visited" and added to the path.
4. The process continues until all the nodes in the graph have been added to
the path. This way, we have a path that connects the source node to all
other nodes following the shortest path possible to reach each node.
Working principles of Dijkstras algorithm
Let’s apply Dijkstra’s Algorithm for the graph given below, and find the shortest
path from node A to node C:
Solution:
1.All the distances from node A to the rest of the nodes is ∞.
2.Calculating the distance between node A and the immediate nodes (node B &
node D): For node B,
Node A to Node B = 3 For node D,
Node A to Node D = 8
3.Choose the node with the shortest distance to be the current node from unvisited
nodes, i.e., node B. Calculating the distance between node B and the immediate
nodes:
For node D,
Node B to Node D = 3+5 = 8
For node E,
Node B to Node E = 3+6 = 9
4.Choose the node with the shortest distance to be the current node from unvisited
nodes, i.e., node D. Calculating the distance between node D and the immediate
nodes:
For node E,
Node D to Node E = 8+3 = 11 ( [9 < 11] > TRUE: So, No Change) For node F,
Node D to Node F = 8+2 = 10
5.Choose the node with the shortest distance to be the current node from unvisited
nodes, i.e., node E. Calculating the distance between node E and the immediate
nodes:
For node C,
Node E to Node C = 9+9 = 18 For node F,
Node E to Node F = 9+1 = 10
6.Choose the node with the shortest distance to be the current node from unvisited
nodes, i.e., node F. Calculating the distance between node F and the immediate
nodes:
For node C,
Node F to Node C = 10+3 = 13 ([18 < 13] FALSE: So, Change the previous value)
So, after performing all the steps, we have the shortest path from node A to node C,
i.e., a value of 13 units.
Applications:
1.Routing Protocols:- Dijkstra's algorithm is widely used in computer networks for
finding the shortest path between routers or nodes.
2.Maps and Navigation Systems:- It is used in mapping and navigation applications
to find the shortest route between two locations.
3.Robotics:- Dijkstra's algorithm is employed in robotics for path planning to help
robots navigate efficiently.
4.Transportation Networks:- It can be used in modeling transportation networks to
optimize routes for vehicles.
5.Telecommunications:- The algorithm is used in designing communication networks
for efficient data transmission.
Merits:
1.Optimality:-Dijkstra's algorithm guarantees the shortest path in a graph with non-
negative weights.
2.Versatility:- It can be applied to a variety of scenarios, making it a versatile
algorithm for pathfinding.
3.Ease of Implementation:- The algorithm is relatively easy to understand and
implement.
Demerits:
4.Non-Negative Weights:- Dijkstra's algorithm assumes non-negative weights on the
edges. If negative weights are present, it can give incorrect results.
5.Limited Applicability:- It may not be suitable for large-scale networks due to its
time complexity, especially when more efficient algorithms like A* are available for
certain scenarios.
6.Memory Usage:- The algorithm may use a lot of memory, especially in scenarios
where there are many nodes.
7.Not Distributed:- Dijkstra's algorithm is not well-suited for distributed systems
where nodes have limited information about the entire network.
What is Prims Algorithm
In Computer science, Prim's algorithm (also known as Jarník's algorithm) is a greedy
algorithm that finds a minimum spanning tree for a weighted undirected graph. This
means it finds a subset of the edges that forms a tree that includes every vertex, where
the total weight of all the edges in the tree is minimized. The algorithm operates
by building this tree one vertex at a time, from an arbitrary starting vertex, at each step
adding the cheapest possible connection from the tree to another vertex.
The algorithm was developed in 1930 by Czech mathematician Vojtech Jarnik and
later rediscovered and republished by computer scientist Robert C. Prim in
1957 and Edsger W. Dijkstra in 1959. Therefore, it is also sometimes called
the Jarník's algorithm, Prim–Jarník algorithm, Prim–Dijkstra algorithm or the
DJP algorithm
Algorithm:
The algorithm starts with an arbitrary node and grows the minimum spanning tree one
vertex at a time. Here are the steps:
1.Initialization: Select an arbitrary node as the starting point.
2.Select Minimum Edge:Choose the edge with the smallest weight that connects a
vertex in the current MST to a vertex outside the MST.
3.Add to MST:Add the selected edge and its connected vertex to the MST.
4.Repeat: Repeat steps 2-3 until all vertices are included in the MST.
Working example of Prim’s algorithm
Suppose, a weighted graph is -
Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are
two edges from vertex B that are B to C with weight 10 and edge B to D with weight
4. Among the edges, the edge BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other
edges. In this case, the edges DE and CD are such edges. Add them to MST
and explore the adjacent of C, i.e., E and A. So, select the edge DE and add it to the
MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would
create a cycle to the graph. So, choose the edge CA and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given graph. The
cost of the MST is given below -
Cost of MST = 4 + 2 + 1 + 3 = 10 units.
Applications-
1. Network Design:- Prim's algorithm is used in designing networks to minimize the
cost of connecting different locations.
2.Circuit Design:- It can be applied in designing electronic circuits to minimize the
total wire length.
3.Robotics:- Prim's algorithm is used in robotics for tasks such as sensor placement
to cover an area with the minimum number of sensors.
4.Cluster Analysis:- It is used in clustering analysis to identify groups of closely
related data points.
Merits-
1.Guaranteed Optimality:- Prim's algorithm guarantees the construction of a
minimum spanning tree, making it an optimal solution.
2.Efficiency:- The algorithm is relatively efficient, especially for dense graphs.
3.Simplicity:-Prim's algorithm is easy to understand and implement, making it
suitable for educational purposes.
Demerits-
4.Dependence on Starting Vertex:-The algorithm's output can vary depending on
the starting vertex. If different starting vertices are chosen, the algorithm may produce
different minimum spanning trees.
5.Inefficiency for Dense Graphs:-Prim's algorithm can be less efficient for dense
graphs, where the number of edges is close to the square of the number of vertices.
This is because it involves a priority queue and repeatedly updating the key values of
vertices, leading to a higher time complexity in dense graphs compared to other
algorithms like Kruskal's.
6.Not Suitable for Dynamic Graphs:-If the graph is dynamic and edges are added or
removed frequently, recalculating the minimum spanning tree using Prim's algorithm
from scratch every time can be computationally expensive. Other algorithms, such as
Boruvka's algorithm or certain modifications of Prim's, might be more suitable for
dynamic graphs.
7.Not Distributed:-Prim's algorithm is not as naturally parallelizable or distributable
as some other algorithms. This can be a disadvantage in distributed computing
environments where parallelization is essential for efficient processing.
Thank You

Dijkstra’s Algorithm and Prim’s Algorithm in Graph Theory and Combinatorics

  • 1.
  • 2.
    Contents Dijkstras algorithm Basic conceptsof Dijkstras algorithm Working principle of Dijkstras algorithm Applications Merits Demerits What is Prim’s algorithm Basic concepts of Prim’s algorithm Working Principle of Prim’s algorithm Applications Merits Demerits
  • 3.
    What is Dijkstras Algorithm? Whatif you are provided with a graph of nodes where every node is linked to several other nodes with varying distance. Now, if you begin from one of the nodes in the graph, what is the shortest path to every other node in the graph? Well simply explained, an algorithm that is used for finding the shortest distance, or path, from starting node to target node in a weighted graph is known as Dijkstra’s Algorithm. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later. This algorithm makes a tree of the shortest path from the starting node, the source, to all other nodes (points) in the graph. Dijkstra's algorithm makes use of weights of the edges for finding the path that minimizes the total distance (weight) among the source node and all other nodes. This algorithm is also known as the single-source shortest path algorithm. Dijkstra’s algorithm is the iterative algorithmic process to provide us with the shortest path from one specific starting node to all other nodes of a graph. Generally, Dijkstra’s algorithm works on the principle of relaxation where an approximation of the accurate distance is steadily displaced by more suitable values until the shortest distance is achieved.
  • 4.
    Also, the estimateddistance to every node is always an overvalue of the true distance and is generally substituted by the least of its previous value with the distance of a recently determined path. It uses a priority queue to greedily choose the nearest node that has not been visited yet and executes the relaxation process on all of its edges. The following are the basic concepts of Dijkstra's Algorithm: 1. Dijkstra's Algorithm basically starts at the node that you choose (the source node) and it analyzes the graph to find the shortest path between that node and all the other nodes in the graph. 2. The algorithm keeps track of the currently known shortest distance from each node to the source node and it updates these values if it finds a shorter path. 3. Once the algorithm has found the shortest path between the source node and another node, that node is marked as "visited" and added to the path. 4. The process continues until all the nodes in the graph have been added to the path. This way, we have a path that connects the source node to all other nodes following the shortest path possible to reach each node.
  • 5.
    Working principles ofDijkstras algorithm Let’s apply Dijkstra’s Algorithm for the graph given below, and find the shortest path from node A to node C: Solution: 1.All the distances from node A to the rest of the nodes is ∞. 2.Calculating the distance between node A and the immediate nodes (node B & node D): For node B, Node A to Node B = 3 For node D, Node A to Node D = 8 3.Choose the node with the shortest distance to be the current node from unvisited nodes, i.e., node B. Calculating the distance between node B and the immediate nodes: For node D,
  • 6.
    Node B toNode D = 3+5 = 8 For node E, Node B to Node E = 3+6 = 9 4.Choose the node with the shortest distance to be the current node from unvisited nodes, i.e., node D. Calculating the distance between node D and the immediate nodes: For node E, Node D to Node E = 8+3 = 11 ( [9 < 11] > TRUE: So, No Change) For node F, Node D to Node F = 8+2 = 10 5.Choose the node with the shortest distance to be the current node from unvisited nodes, i.e., node E. Calculating the distance between node E and the immediate nodes: For node C, Node E to Node C = 9+9 = 18 For node F, Node E to Node F = 9+1 = 10 6.Choose the node with the shortest distance to be the current node from unvisited nodes, i.e., node F. Calculating the distance between node F and the immediate nodes: For node C, Node F to Node C = 10+3 = 13 ([18 < 13] FALSE: So, Change the previous value) So, after performing all the steps, we have the shortest path from node A to node C, i.e., a value of 13 units.
  • 7.
    Applications: 1.Routing Protocols:- Dijkstra'salgorithm is widely used in computer networks for finding the shortest path between routers or nodes. 2.Maps and Navigation Systems:- It is used in mapping and navigation applications to find the shortest route between two locations. 3.Robotics:- Dijkstra's algorithm is employed in robotics for path planning to help robots navigate efficiently. 4.Transportation Networks:- It can be used in modeling transportation networks to optimize routes for vehicles. 5.Telecommunications:- The algorithm is used in designing communication networks for efficient data transmission.
  • 8.
    Merits: 1.Optimality:-Dijkstra's algorithm guaranteesthe shortest path in a graph with non- negative weights. 2.Versatility:- It can be applied to a variety of scenarios, making it a versatile algorithm for pathfinding. 3.Ease of Implementation:- The algorithm is relatively easy to understand and implement. Demerits: 4.Non-Negative Weights:- Dijkstra's algorithm assumes non-negative weights on the edges. If negative weights are present, it can give incorrect results. 5.Limited Applicability:- It may not be suitable for large-scale networks due to its time complexity, especially when more efficient algorithms like A* are available for certain scenarios. 6.Memory Usage:- The algorithm may use a lot of memory, especially in scenarios where there are many nodes. 7.Not Distributed:- Dijkstra's algorithm is not well-suited for distributed systems where nodes have limited information about the entire network.
  • 9.
    What is PrimsAlgorithm In Computer science, Prim's algorithm (also known as Jarník's algorithm) is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. The algorithm was developed in 1930 by Czech mathematician Vojtech Jarnik and later rediscovered and republished by computer scientist Robert C. Prim in 1957 and Edsger W. Dijkstra in 1959. Therefore, it is also sometimes called the Jarník's algorithm, Prim–Jarník algorithm, Prim–Dijkstra algorithm or the DJP algorithm Algorithm: The algorithm starts with an arbitrary node and grows the minimum spanning tree one vertex at a time. Here are the steps: 1.Initialization: Select an arbitrary node as the starting point. 2.Select Minimum Edge:Choose the edge with the smallest weight that connects a vertex in the current MST to a vertex outside the MST. 3.Add to MST:Add the selected edge and its connected vertex to the MST. 4.Repeat: Repeat steps 2-3 until all vertices are included in the MST.
  • 10.
    Working example ofPrim’s algorithm Suppose, a weighted graph is - Step 1 - First, we have to choose a vertex from the above graph. Let's choose B. Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges from vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges, the edge BD has the minimum weight. So, add it to the MST.
  • 11.
    Step 3 -Now, again, choose the edge with the minimum weight among all the other edges. In this case, the edges DE and CD are such edges. Add them to MST and explore the adjacent of C, i.e., E and A. So, select the edge DE and add it to the MST. Step 4 - Now, select the edge CD, and add it to the MST.
  • 12.
    Step 5 -Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle to the graph. So, choose the edge CA and add it to the MST. So, the graph produced in step 5 is the minimum spanning tree of the given graph. The cost of the MST is given below - Cost of MST = 4 + 2 + 1 + 3 = 10 units.
  • 13.
    Applications- 1. Network Design:-Prim's algorithm is used in designing networks to minimize the cost of connecting different locations. 2.Circuit Design:- It can be applied in designing electronic circuits to minimize the total wire length. 3.Robotics:- Prim's algorithm is used in robotics for tasks such as sensor placement to cover an area with the minimum number of sensors. 4.Cluster Analysis:- It is used in clustering analysis to identify groups of closely related data points.
  • 14.
    Merits- 1.Guaranteed Optimality:- Prim'salgorithm guarantees the construction of a minimum spanning tree, making it an optimal solution. 2.Efficiency:- The algorithm is relatively efficient, especially for dense graphs. 3.Simplicity:-Prim's algorithm is easy to understand and implement, making it suitable for educational purposes. Demerits- 4.Dependence on Starting Vertex:-The algorithm's output can vary depending on the starting vertex. If different starting vertices are chosen, the algorithm may produce different minimum spanning trees. 5.Inefficiency for Dense Graphs:-Prim's algorithm can be less efficient for dense graphs, where the number of edges is close to the square of the number of vertices. This is because it involves a priority queue and repeatedly updating the key values of vertices, leading to a higher time complexity in dense graphs compared to other algorithms like Kruskal's. 6.Not Suitable for Dynamic Graphs:-If the graph is dynamic and edges are added or removed frequently, recalculating the minimum spanning tree using Prim's algorithm from scratch every time can be computationally expensive. Other algorithms, such as Boruvka's algorithm or certain modifications of Prim's, might be more suitable for dynamic graphs. 7.Not Distributed:-Prim's algorithm is not as naturally parallelizable or distributable as some other algorithms. This can be a disadvantage in distributed computing environments where parallelization is essential for efficient processing.
  • 15.