Find the shortest path from one starting point to all other points by always choosing the closest unvisited point next.
Analogy: Imagine you are in a city and want to find the shortest way to every other city by always traveling to the nearest city you haven't visited yet.
Start: S
Graph:
S --2--> A
S --5--> B
A --1--> B
A --3--> C
B --2--> C
Distances:
S: 0
A: ∞
B: ∞
C: ∞
Visited: none
Dry Run Walkthrough
Input: Graph with nodes S, A, B, C and edges: S-A(2), S-B(5), A-B(1), A-C(3), B-C(2); start node S
Goal: Find shortest distances from S to all nodes
Step 1: Initialize distances: S=0, others=∞; mark all unvisited
Distances: S[0], A[∞], B[∞], C[∞]; Visited: none
Why: Set starting point distance to zero and others unknown
Step 2: Pick unvisited node with smallest distance: S(0); visit S
Visited: S; Distances: S[0], A[∞], B[∞], C[∞]
Why: Start from source node
Step 3: Update neighbors of S: A=2, B=5
Distances: S[0], A[2], B[5], C[∞]; Visited: S
Why: Check direct paths from S
Step 4: Pick unvisited node with smallest distance: A(2); visit A
Visited: S, A; Distances: S[0], A[2], B[5], C[∞]
Why: Next closest node to explore
Step 5: Update neighbors of A: B=min(5, 2+1=3)=3, C=2+3=5
Distances: S[0], A[2], B[3], C[5]; Visited: S, A
Why: Found shorter path to B and new path to C
Step 6: Pick unvisited node with smallest distance: B(3); visit B
Visited: S, A, B; Distances: S[0], A[2], B[3], C[5]
Why: Next closest node to explore
Step 7: Update neighbors of B: C=min(5, 3+2=5)=5 (no change)
Distances: S[0], A[2], B[3], C[5]; Visited: S, A, B
Why: No shorter path to C found
Step 8: Pick unvisited node with smallest distance: C(5); visit C
Visited: S, A, B, C; Distances: S[0], A[2], B[3], C[5]
Why: All nodes visited, algorithm ends
Result:
Final distances: S[0] -> A[2] -> B[3] -> C[5]
Annotated Code
DSA C
#include <stdio.h>#include <limits.h>#include <stdbool.h>#define V 4int minDistance(int dist[], bool sptSet[]) {
intmin = INT_MAX, min_index = -1;
for (int v = 0; v < V; v++) {
if (!sptSet[v] && dist[v] < min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = false;
}
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet); // pick closest unvisited
sptSet[u] = true; // mark visited
for (int v = 0; v < V; v++) {
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v]; // update distance
}
}
}
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++) {
printf("%c -> %d\n", 'S' + i, dist[i]);
}
}
int main() {
int graph[V][V] = {
{0, 2, 5, 0},
{0, 0, 1, 3},
{0, 0, 0, 2},
{0, 0, 0, 0}
};
dijkstra(graph, 0); // source is S (index 0)
return0;
}
int u = minDistance(dist, sptSet); // pick closest unvisited
select the unvisited node with the smallest current distance
update distance to neighbor v with shorter path found
OutputSuccess
Vertex Distance from Source
S -> 0
A -> 2
B -> 3
C -> 5
Complexity Analysis
Time: O(V^2) because we check all vertices to find the minimum distance and update neighbors in nested loops
Space: O(V) for distance and visited arrays to store shortest path info
vs Alternative: Compared to using a priority queue (min-heap), this simple approach is slower (O(V^2) vs O(E log V)) but easier to understand and implement for small graphs
Edge Cases
Graph with no edges
Distances remain infinite except source which is zero
DSA C
dist[src] = 0;
Graph with single node
Distance to itself is zero, algorithm ends immediately
DSA C
for (int count = 0; count < V - 1; count++) {
Disconnected nodes
Distances to unreachable nodes remain infinite (INT_MAX)
When you need shortest paths from one point to all others in a weighted graph without negative edges, reach for Dijkstra's algorithm because it efficiently finds minimum distances step-by-step.
Common Mistakes
Mistake: Not marking nodes as visited after selecting them Fix: Set sptSet[u] = true immediately after picking the node with minimum distance
Mistake: Updating distances without checking if current distance is INT_MAX Fix: Add condition dist[u] != INT_MAX before updating neighbors to avoid overflow
Summary
Finds shortest paths from a single source to all nodes in a weighted graph with non-negative edges.
Use when you want minimum travel cost or distance from one point to many points.
Always pick the closest unvisited node next and update neighbors to find shortest paths stepwise.