You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Dijkstra's algorithm finds the shortest path from a single source vertex to every other vertex in a weighted graph with non-negative edge weights. It is one of the most important algorithms in discrete mathematics and computer science.
Given a weighted graph and a starting vertex S, find the shortest (minimum weight) path from S to every other vertex.
Create a table with columns:
Consider a graph with vertices A, B, C, D, E and edges:
| Edge | Weight |
|---|---|
| AB | 4 |
| AC | 2 |
| BC | 1 |
| BD | 5 |
| CD | 8 |
| CE | 10 |
| DE | 2 |
Find the shortest path from A to all other vertices.
| Order | Vertex | Permanent | From |
|---|---|---|---|
| 1 | A | 0 | — |
Update from A: B = 4, C = 2.
| Order | Vertex | Permanent | From |
|---|---|---|---|
| 1 | A | 0 | — |
| 2 | C | 2 | A |
Update from C: B = min(4, 2+1) = 3, D = min(∞, 2+8) = 10, E = min(∞, 2+10) = 12.
| Order | Vertex | Permanent | From |
|---|---|---|---|
| 1 | A | 0 | — |
| 2 | C | 2 | A |
| 3 | B | 3 | C |
Update from B: D = min(10, 3+5) = 8.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.