Dijkstra's Algorithm: Finding
theShortest Path
Named after Edsger W. Dijkstra, a groundbreaking Dutch computer
scientist. Published in 1959, it remains vital in graph theory. Widely used
in GPS, network routing, and many modern technologies.
by Ghazala Anjum
2.
What is Dijkstra'sAlgorithm?
Purpose
Finds shortest paths between
nodes in a graph.
Source Node
Calculates shortest paths from
a single source to all other
nodes.
Constraints
Works only with non-negative edge weights (distances).
3.
How Dijkstra's AlgorithmWorks: Step-by-Step
Initialization
Set source distance to 0, others to infinity.
Select Node
Pick unvisited node with smallest tentative distance.
Update Neighbors
Calculate tentative distances via current node.
Mark Visited
Mark current node as visited.
Repeat
Continue until all nodes are visited or target found.
4.
Dijkstra's Algorithm: A
VisualExample
Graph Setup
Nodes A to E connected with weighted edges.
Source Node
Starting point set at node A.
Distance Updates
Stepwise update of shortest distances to each node.
Shortest Paths
Final paths highlight shortest routes from A to others.
5.
Pseudocode Implementation
Function Dijkstra(Graph,source):
Create vertex set Q
for each vertex v in Graph:
dist[v] ← INFINITY; prev[v] ← UNDEFINED; add v to Q
dist[source] ← 0
while Q is not empty:
u ← vertex in Q with min dist[u]
remove u from Q
for each neighbor v of u:
alt ← dist[u] + length(u, v)
if alt < dist[v]:
dist[v] ← alt
prev[v] ← u
return dist[], prev[]
6.
Code Example (Python)
Usesdictionaries for graph and distances.
Employs priority queue for efficient node selection.
Finds shortest path to a specific target node.
Demonstrates key logic in a compact form.
Real-World Applications
GPS Navigation
Calculatesshortest routes between locations.
Network Routing
Protocols like OSPF use it for optimal data paths.
Robotics
Path planning in known environments.
Social Networks
Calculates degrees of separation between users.
9.
Limitations and
Considerations
Negative Weights
Doesn'thandle negative edge
weights; use Bellman-Ford
instead.
Large Graphs
Can be inefficient; consider A*
for better performance.
Edge Weight Assumptions
Edge weights must be non-negative and constant.
10.
Conclusion
Dijkstra is fundamentaland
widely adopted for pathfinding.
Efficient for shortest paths in
non-negative weighted graphs.
Key in GPS, routing, robotics,
and many critical systems.