Dijikstra's Algorithm for Shortest Path
Dijkstra’s Algorithm — Concept
Purpose:
To find the shortest path from a source vertex to all other vertices in a weighted graph (with non-negative edge weights).
Key Idea:
We repeatedly pick the vertex with the minimum tentative distance (using a priority queue or min-heap), and then update (relax) the distances to its neighboring vertices.
⚙️ Data Structures Used
-
Array / Min-heap → to store and find the vertex with the minimum distance.
-
Adjacency Matrix or Adjacency List → to represent the graph.
-
Visited Set → to keep track of vertices whose shortest distance is finalized.
🧠 Algorithm Steps
-
Initialize the distance of all vertices to ∞, and the source vertex to 0.
-
Mark all vertices as unvisited.
-
Choose the unvisited vertex with the smallest distance (call it
u). -
For each neighbor
vofu, check if the path throughugives a shorter distance: -
Mark
uas visited. -
Repeat until all vertices are visited.
🧮 Example
Let’s take a weighted graph with 6 vertices (A, B, C, D, E,F):
| Edge | Weight |
|---|---|
| A–B | 4 |
| A–C | 2 |
| B–C | 5 |
| B–D | 10 |
| C–E | 3 |
| E–D | 4 |
| D–F | 11 |
We will find shortest paths from A.
Step 1: Initialization
| Vertex | Distance from A | Previous | Visited |
|---|---|---|---|
| A | 0 | — | No |
| B | ∞ | — | No |
| C | ∞ | — | No |
| D | ∞ | — | No |
| E | ∞ | — | No |
| F | ∞ | — | No |
Step 2: Start with A
From A:
-
B = min(∞, 0 + 4) = 4
-
C = min(∞, 0 + 2) = 2
| Vertex | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | ∞ | — |
| E | ∞ | — |
| F | ∞ | — |
Mark A as visited.
Step 3: Pick the next vertex with smallest distance → C (2)
From C:
-
E = min(∞, 2 + 3) = 5
-
B = min(4, 2 + 5) = 4 (no change)
| Vertex | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | ∞ | — |
| E | 5 | C |
| F | ∞ | — |
Mark C as visited.
Step 4: Next smallest → B (4)
From B:
-
D = min(∞, 4 + 10) = 14
-
C = already visited
| Vertex | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | 14 | B |
| E | 5 | C |
| F | ∞ | — |
Mark B as visited.
Step 5: Next → E (5)
From E:
-
D = min(14, 5 + 4) = 9
-
F = min(∞, 5 + 11) = 16
| Vertex | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | 9 | E |
| E | 5 | C |
| F | 16 | E |
Mark E as visited.
Step 6: Next smallest → D (9)
From D:
-
F = min(16, 9 + 11) = 16 (no change)
Mark D as visited.
Finally mark F as visited.
✅ Final Shortest Distances from A
| Vertex | Distance | Path |
|---|---|---|
| A | 0 | A |
| B | 4 | A → B |
| C | 2 | A → C |
| D | 9 | A → C → E → D |
| E | 5 | A → C → E |
| F | 16 | A → C → E → F |
🧩 Summary
-
Dijkstra’s algorithm ensures the shortest path from the source to every other vertex.
-
It is a greedy algorithm because it makes the locally optimal choice (minimum distance) at each step.
-
Time complexity:
-
With adjacency matrix: O(V²)
-
With min-heap + adjacency list: O((V + E) log V)


Comments
Post a Comment