Single source all destinations (Dijkstra)
Dijkstra's algorithm: the shortest distance from one source to every vertex, with a step-by-step table.
Syllabus says: “Applications of Graphs - Single Source All Destination”
On this page · 11 sections
Pick the CLOSEST unvisited vertex, LOCK it, RELAX its roads. Repeat. (Greedy — never goes back.)
Spilling water on the floor at the source: it reaches the nearest spots first, and each spot is reached by the quickest route before the water moves further out.
Dijkstra's algorithm repeatedly selects the unvisited vertex with the smallest tentative distance, marks it final and relaxes its outgoing edges (dist[v] = min(dist[v], dist[u] + w(u, v))); it requires non-negative edge weights.
What you will be able to do
- Write Dijkstra's algorithm.
- Trace it in a table and give the shortest paths (May 2026 Q13b, model paper Q14a).
- Explain why weights must be non-negative.
Worked example
Directed graph: A→B (4), A→C (1), C→B (2), B→D (1), C→D (5), D→E (3). Source A.
| Step | Pick (lock) | A | B | C | D | E |
|---|---|---|---|---|---|---|
| init | — | 0 | ∞ | ∞ | ∞ | ∞ |
| 1 | A (0) | 0 | 4 | 1 | ∞ | ∞ |
| 2 | C (1) | 0 | 3 (via C) | 1 | 6 | ∞ |
| 3 | B (3) | 0 | 3 | 1 | 4 (via B) | ∞ |
| 4 | D (4) | 0 | 3 | 1 | 4 | 7 |
| 5 | E (7) | 0 | 3 | 1 | 4 | 7 |
| Vertex | Distance | Path |
|---|---|---|
| B | 3 | A → C → B |
| C | 1 | A → C |
| D | 4 | A → C → B → D |
| E | 7 | A → C → B → D → E |
If two vertices have the same smallest distance, follow the rule the question gives (e.g. the model paper fixes which shortest path is reported). Without a rule, pick the alphabetically or numerically smaller vertex and say so.
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithm
Algorithm Dijkstra(G, s)
1. for each vertex v: dist[v] ← ∞; pred[v] ← none; done[v] ← false
2. dist[s] ← 0
3. repeat n times
4. u ← the vertex with done[u] = false and the smallest dist[u]
5. done[u] ← true
6. for each edge (u, v) with weight w do
7. if not done[v] and dist[u] + w < dist[v] then
8. dist[v] ← dist[u] + w; pred[v] ← u // relax
9. return dist, pred // follow pred back from v to read a pathC implementation
dijkstra.ctested ✓ 250/250 cases/* Dijkstra's single-source shortest paths — KTU PCCST303, Module 3 (May 2026 Q13b, model Q14a)
* Repeatedly lock the unfinished vertex with the smallest tentative distance (ties: the
* smaller vertex number), then relax its outgoing edges. Needs non-negative weights.
* Input : n m directed(0|1) then m edges "u v w" then the source.
* Output: "v dist pred" for every vertex (INF / -1 when unreachable). */
#include <stdio.h>
#define MAXV 20
#define INF 1000000000
int cost[MAXV][MAXV], hasEdge[MAXV][MAXV];
int main(void) {
int n, m, directed, u, v, w, s, i, k;
int dist[MAXV], pred[MAXV], done[MAXV];
if (scanf("%d %d %d", &n, &m, &directed) != 3 || n < 1 || n > MAXV) { printf("BAD INPUT\n"); return 1; }
for (i = 0; i < m; i++) {
if (scanf("%d %d %d", &u, &v, &w) != 3 || u < 0 || v < 0 || u >= n || v >= n || w < 0) { printf("BAD INPUT\n"); return 1; }
cost[u][v] = w; hasEdge[u][v] = 1;
if (!directed) { cost[v][u] = w; hasEdge[v][u] = 1; }
}
if (scanf("%d", &s) != 1 || s < 0 || s >= n) { printf("BAD INPUT\n"); return 1; }
for (i = 0; i < n; i++) { dist[i] = INF; pred[i] = -1; done[i] = 0; }
dist[s] = 0;
for (k = 0; k < n; k++) {
u = -1;
for (i = 0; i < n; i++) /* choose: smallest dist, not done */
if (!done[i] && dist[i] < INF && (u == -1 || dist[i] < dist[u])) u = i;
if (u == -1) break; /* the rest is unreachable */
done[u] = 1;
for (v = 0; v < n; v++) /* relax every edge u -> v */
if (hasEdge[u][v] && !done[v] && dist[u] + cost[u][v] < dist[v]) {
dist[v] = dist[u] + cost[u][v];
pred[v] = u;
}
}
for (i = 0; i < n; i++) {
if (dist[i] == INF) printf("%d INF -1\n", i);
else printf("%d %d %d\n", i, dist[i], pred[i]);
}
return 0;
}
Sample run · A..E = 0..4, source A · real output from gcc
Input (stdin)
5 6 1 0 1 4 0 2 1 2 1 2 1 3 1 2 3 5 3 4 3 0
Output (stdout)
0 0 -1 1 3 2 2 1 0 3 4 1 4 7 3
Complexity
| Implementation | Time |
|---|---|
| Adjacency matrix + linear scan | O(n²) |
| Adjacency list + binary min-heap | O((n + e) log n) |
Asked in KTU exams (2)
- May 2026 · Q13b4 marksFind the shortest path from source vertex A to all remaining vertices.
- — 2024 · Q14a5 marksConsider the directed graph shown in the figure below. There are multiple shortest paths between vertices S and T. Which one will be reported by Dijkstra's shortest path algorithm? Assume that, in any iteration, the shortest path to a vertex v is updated only when a strictly shorter path to v is discovered.
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Greedy: lock the closest unvisited vertex, relax its edges.
- dist[v] = min(dist[v], dist[u] + w).
- Non-negative weights only.
- Show a table with one row per locked vertex, then list the paths.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §6.4.1 (Single source all destinations)
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §24.3
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)