Dijkstra’s Algorithm :
Finding Single – Source
Shortest Paths
The Single Source-Shortest Path Algorithm:Overview
The Single-Source Shortest Path problem focuses on finding the shortest
path from one designated source vertex to every other vertex in a
weighted graph. Unlike the All-Pairs Shortest Path (APSP) problem which
finds routes between all node pairs, SSSP is ideal for "point A to
everywhere else" scenarios.
Why Do We Need It?
• Many real-world problems don't require knowing the shortest path
between every possible pair of locations. Instead, they need the most
efficient routes from a single starting point. Applications like GPS
navigation, network broadcasting, and finding the closest available
resources are all based on the SSSP model.
Limitations of Simpler
Algorithms
While algorithms like Breadth-First Search (BFS) can find the shortest
path in unweighted graphs, they fail when edges have different
"costs" or weights. Dijkstra's algorithm was developed to solve this by
systematically handling weighted edges to find the truly optimal
path, not just the one with the fewest steps.
Introducing the Dijkstra Algorithm
Finds the Shortest Path
Its main purpose is to find the most
efficient path from a single starting
point (the "source") to every other
node in a weighted network or graph.
Uses a Greedy Strategy
The algorithm works by always
choosing the nearest unvisited node.
It then explores paths from that node,
continuously updating its list of
shortest distances until all nodes have
been visited.
Requires Non-
Negative Weights
A critical rule for Dijkstra's is that it
only functions correctly when the
"cost" of traveling between any two
nodes (the edge weight) is zero or
positive. It cannot handle negative
weights.
Key Advantages of Dijkstra Algorithm
Efficient and Reliable
Dijkstra's algorithm is a highly efficient and reliable method
for finding the absolute shortest path from a single source in
graphs with non-negative weights.
Graph Versatility
The algorithm is compatible with both directed and
undirected graphs, providing broad applicability.
Optimized for Sparse
Graphs
In real-world scenarios like road or computer networks,
graphs are often "sparse" (meaning they have far fewer
connections than the maximum possible).
Predictable
Performance
The time complexity is consistently O(V^2), where V
represents the number of vertices, making its performance
predictable even with increasing graph size.
Core Idea: Greedy Approach with
Iterative Refinement
The fundamental principle of Dijkstra's algorithm is to iteratively build a
set of nodes with known shortest paths. It always "greedily" selects the
unvisited node with the smallest known distance from the source and
uses it to update, or "relax," the distances of its neighbors.
Step-by-Step Implementation of Dijkstra
Algorithm
Initialize
First, set up your data structures. Create a distances table or
array to store the shortest known distance from the source
node to every other node. Set the distance to the source
node itself as 0 and all other distances to infinity ( ). All
∞
nodes start in the "unvisited" set.
Select the Closest Node
From the set of unvisited nodes, identify the node with the
smallest known distance from the source. This becomes your
"current" node for this iteration. In the very first step, this will
always be the source node itself.
Update Neighbor Distances
For the current node, examine all of its unvisited neighbors.
For each neighbor, calculate the distance to it by adding the
current node's distance to the weight of the edge connecting
them. If this new calculated distance is shorter than the
neighbor's currently recorded distance, update it in your
distances table. This process is often called "relaxing the
Mark as Visited and
Repeat
After checking all of the current node's neighbors, move it
from the "unvisited" set to the "visited" set. This means its
shortest path has been finalized. Repeat steps 2 and 3 until
all reachable nodes have been visited or the unvisited set is
empty. The final distances table will then contain the shortest
path from the source to all other nodes.
Example: Finding the Shortest Path from
Node 1
Let's apply Dijkstra's algorithm to the following graph, starting from Node 1. Our goal is to find
‍
♂️
‍
️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️ ‍
♂️
the shortest distance from Node 1 to all other nodes. We'll use a table to track the shortest distance
found so far to each node and which nodes have been visited.
1 2 3 4
1 0 3 ∞ 7
2 8 0 2 ∞
3 5 ∞ 0 1
=
4 2 ∞ ∞ 0
1 2 3 4
Iteration 1
1 0 3 ∞ 7
2 8 0 2 15
3 5 8 0 1
=
STEPS :
1. Keep the first row and first column the same as in .
2. Update the remaining entries by checking if going through
vertex 1 provides a shorter path.
3. For each pair , compare the current distance with the distance .
4. Record the smaller of the two values.
4 2 5 ∞ 0
1 2 3 4
Iteration 2
1 0 3 5 7
2 8 0 2 15
STEPS :
1.Keep the second row and second column the same as in .
2.Update the remaining entries by checking if going through
vertex 2 provides a shorter path.
3.For each pair , compare the current distance with .
4.Record the smaller value.
5.You may also use previously updated paths through vertex 1 if
that yields an even shorter distance.
3 5 8 0 1
=
4 2 5 7 0
1 2 3 4
Iteration 3
1 0 3 5 6
2 7 0 2 3
STEPS:
1. Keep the third row and third column the same as in .
2. Update the remaining entries by checking if going through
vertex 3 provides a shorter path.
3. Compare with .
4. Record the smaller value.
5. Paths that already use vertices 1 or 2 can also contribute to a
shorter distance.
3 5 8 0 1
=
4 2 5 7 0
1 2 3 4
Iteration 4
1 0 3 5 6
2 5 0 2 3
3 3 6 0 1
=
STEPS:
1. Keep the fourth row and column the same as in A3.
2. Update the remaining entries by checking if going through
vertex 4 provides a shorter path.
3. Compare with .
4. Record the smaller value.
5. Paths that include vertices 1, 2, or 3 may also combine to yield
the shortest final distances.
4 2 5 7 0
Final Shortest Distances
Matrix
After the Floyd-Warshall algorithm completes its iterations,
the matrix reflects the shortest distances between all pairs:
1 2 3 4
1 0 3 5 6
2 5 0 2 3
3 3 6 0 1
4 2 5 7 0
Detecting Negative
Cycles
The Floyd-Warshall algorithm is not only useful for finding shortest paths but also
for detecting the presence of negative cycles within a graph.
How to Detect
If, at any point during or after the iterations, any of the diagonal elements in the
distance matrix (i.e., dist[i][i]) becomes negative, it indicates the existence of a
negative cycle reachable from vertex i.
Implication of Negative
Cycles
When a negative cycle is present, the concept of a "shortest path" becomes
undefined. This is because traversing a negative cycle repeatedly can indefinitely
decrease the total path distance, leading to infinite loops and no true shortest path.
Algorithm's Role
While Floyd-Warshall can detect negative cycles, it cannot provide a meaningful
shortest path solution in their presence. Its output will simply reflect the infinitely
decreasing nature of paths involving such cycles.
Real-World Applications of Dijkstra
Algorithm
GPS and Mapping Services
The most common application is in GPS navigation.
When you ask for the fastest route from your
location to a destination, services like Google Maps
or Waze use Dijkstra's algorithm (or a variation like
A*) to model the road network as a graph.
Network Routing
n computer networking, Dijkstra's is used in routing
protocols like OSPF (Open Shortest Path First).
Routers use the algorithm to find the shortest path to
send data packets between different points in a
network.
Social Network Analysis
Helps in understanding connectivity and
relationships within social graphs, determining the
shortest "degrees of separation" between individuals.
Flight Itineraries
Airlines and travel websites use Dijkstra's to find the
cheapest or fastest flight routes. Airports are treated
as nodes and flights as edges, with weights
representing cost or travel time.

Dijkstra Algorithm Computer science engineering

  • 1.
    Dijkstra’s Algorithm : FindingSingle – Source Shortest Paths
  • 2.
    The Single Source-ShortestPath Algorithm:Overview The Single-Source Shortest Path problem focuses on finding the shortest path from one designated source vertex to every other vertex in a weighted graph. Unlike the All-Pairs Shortest Path (APSP) problem which finds routes between all node pairs, SSSP is ideal for "point A to everywhere else" scenarios. Why Do We Need It? • Many real-world problems don't require knowing the shortest path between every possible pair of locations. Instead, they need the most efficient routes from a single starting point. Applications like GPS navigation, network broadcasting, and finding the closest available resources are all based on the SSSP model. Limitations of Simpler Algorithms While algorithms like Breadth-First Search (BFS) can find the shortest path in unweighted graphs, they fail when edges have different "costs" or weights. Dijkstra's algorithm was developed to solve this by systematically handling weighted edges to find the truly optimal path, not just the one with the fewest steps.
  • 3.
    Introducing the DijkstraAlgorithm Finds the Shortest Path Its main purpose is to find the most efficient path from a single starting point (the "source") to every other node in a weighted network or graph. Uses a Greedy Strategy The algorithm works by always choosing the nearest unvisited node. It then explores paths from that node, continuously updating its list of shortest distances until all nodes have been visited. Requires Non- Negative Weights A critical rule for Dijkstra's is that it only functions correctly when the "cost" of traveling between any two nodes (the edge weight) is zero or positive. It cannot handle negative weights.
  • 4.
    Key Advantages ofDijkstra Algorithm Efficient and Reliable Dijkstra's algorithm is a highly efficient and reliable method for finding the absolute shortest path from a single source in graphs with non-negative weights. Graph Versatility The algorithm is compatible with both directed and undirected graphs, providing broad applicability. Optimized for Sparse Graphs In real-world scenarios like road or computer networks, graphs are often "sparse" (meaning they have far fewer connections than the maximum possible). Predictable Performance The time complexity is consistently O(V^2), where V represents the number of vertices, making its performance predictable even with increasing graph size.
  • 5.
    Core Idea: GreedyApproach with Iterative Refinement The fundamental principle of Dijkstra's algorithm is to iteratively build a set of nodes with known shortest paths. It always "greedily" selects the unvisited node with the smallest known distance from the source and uses it to update, or "relax," the distances of its neighbors.
  • 6.
    Step-by-Step Implementation ofDijkstra Algorithm Initialize First, set up your data structures. Create a distances table or array to store the shortest known distance from the source node to every other node. Set the distance to the source node itself as 0 and all other distances to infinity ( ). All ∞ nodes start in the "unvisited" set. Select the Closest Node From the set of unvisited nodes, identify the node with the smallest known distance from the source. This becomes your "current" node for this iteration. In the very first step, this will always be the source node itself. Update Neighbor Distances For the current node, examine all of its unvisited neighbors. For each neighbor, calculate the distance to it by adding the current node's distance to the weight of the edge connecting them. If this new calculated distance is shorter than the neighbor's currently recorded distance, update it in your distances table. This process is often called "relaxing the Mark as Visited and Repeat After checking all of the current node's neighbors, move it from the "unvisited" set to the "visited" set. This means its shortest path has been finalized. Repeat steps 2 and 3 until all reachable nodes have been visited or the unvisited set is empty. The final distances table will then contain the shortest path from the source to all other nodes.
  • 7.
    Example: Finding theShortest Path from Node 1 Let's apply Dijkstra's algorithm to the following graph, starting from Node 1. Our goal is to find ‍ ♂️ ‍ ️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ the shortest distance from Node 1 to all other nodes. We'll use a table to track the shortest distance found so far to each node and which nodes have been visited.
  • 8.
    1 2 34 1 0 3 ∞ 7 2 8 0 2 ∞ 3 5 ∞ 0 1 = 4 2 ∞ ∞ 0
  • 9.
    1 2 34 Iteration 1 1 0 3 ∞ 7 2 8 0 2 15 3 5 8 0 1 = STEPS : 1. Keep the first row and first column the same as in . 2. Update the remaining entries by checking if going through vertex 1 provides a shorter path. 3. For each pair , compare the current distance with the distance . 4. Record the smaller of the two values. 4 2 5 ∞ 0
  • 10.
    1 2 34 Iteration 2 1 0 3 5 7 2 8 0 2 15 STEPS : 1.Keep the second row and second column the same as in . 2.Update the remaining entries by checking if going through vertex 2 provides a shorter path. 3.For each pair , compare the current distance with . 4.Record the smaller value. 5.You may also use previously updated paths through vertex 1 if that yields an even shorter distance. 3 5 8 0 1 = 4 2 5 7 0
  • 11.
    1 2 34 Iteration 3 1 0 3 5 6 2 7 0 2 3 STEPS: 1. Keep the third row and third column the same as in . 2. Update the remaining entries by checking if going through vertex 3 provides a shorter path. 3. Compare with . 4. Record the smaller value. 5. Paths that already use vertices 1 or 2 can also contribute to a shorter distance. 3 5 8 0 1 = 4 2 5 7 0
  • 12.
    1 2 34 Iteration 4 1 0 3 5 6 2 5 0 2 3 3 3 6 0 1 = STEPS: 1. Keep the fourth row and column the same as in A3. 2. Update the remaining entries by checking if going through vertex 4 provides a shorter path. 3. Compare with . 4. Record the smaller value. 5. Paths that include vertices 1, 2, or 3 may also combine to yield the shortest final distances. 4 2 5 7 0
  • 13.
    Final Shortest Distances Matrix Afterthe Floyd-Warshall algorithm completes its iterations, the matrix reflects the shortest distances between all pairs: 1 2 3 4 1 0 3 5 6 2 5 0 2 3 3 3 6 0 1 4 2 5 7 0
  • 14.
    Detecting Negative Cycles The Floyd-Warshallalgorithm is not only useful for finding shortest paths but also for detecting the presence of negative cycles within a graph. How to Detect If, at any point during or after the iterations, any of the diagonal elements in the distance matrix (i.e., dist[i][i]) becomes negative, it indicates the existence of a negative cycle reachable from vertex i. Implication of Negative Cycles When a negative cycle is present, the concept of a "shortest path" becomes undefined. This is because traversing a negative cycle repeatedly can indefinitely decrease the total path distance, leading to infinite loops and no true shortest path. Algorithm's Role While Floyd-Warshall can detect negative cycles, it cannot provide a meaningful shortest path solution in their presence. Its output will simply reflect the infinitely decreasing nature of paths involving such cycles.
  • 15.
    Real-World Applications ofDijkstra Algorithm GPS and Mapping Services The most common application is in GPS navigation. When you ask for the fastest route from your location to a destination, services like Google Maps or Waze use Dijkstra's algorithm (or a variation like A*) to model the road network as a graph. Network Routing n computer networking, Dijkstra's is used in routing protocols like OSPF (Open Shortest Path First). Routers use the algorithm to find the shortest path to send data packets between different points in a network. Social Network Analysis Helps in understanding connectivity and relationships within social graphs, determining the shortest "degrees of separation" between individuals. Flight Itineraries Airlines and travel websites use Dijkstra's to find the cheapest or fastest flight routes. Airports are treated as nodes and flights as edges, with weights representing cost or travel time.