📖 Scenario: You are working as a city planner. You have a map of intersections connected by roads with distances. You want to find the shortest path from a starting intersection to all other intersections.
🎯 Goal: Build a program that uses Dijkstra's algorithm to find the shortest distance from a given start intersection to every other intersection in the city.
📋 What You'll Learn
Create a graph using an adjacency matrix with exact distances
Set a starting intersection index
Implement Dijkstra's algorithm to find shortest paths
Print the shortest distances from the start to all intersections
💡 Why This Matters
🌍 Real World
City planners and GPS systems use shortest path algorithms like Dijkstra's to find quickest routes between locations.
💼 Career
Understanding Dijkstra's algorithm is essential for software engineers working on navigation, mapping, and network routing applications.
Progress0 / 4 steps
1
Create the graph as an adjacency matrix
Create a 2D integer array called graph with 4 rows and 4 columns representing the distances between intersections. Use these exact values: row 0: 0, 5, 0, 10; row 1: 5, 0, 3, 0; row 2: 0, 3, 0, 1; row 3: 10, 0, 1, 0.
DSA C
Hint
Use a 2D array with the exact distances for each row as shown.
2
Set the starting intersection index
Create an integer variable called start and set it to 0 to represent the starting intersection.
DSA C
Hint
Declare an integer variable named start and assign it the value 0.
3
Implement Dijkstra's algorithm to find shortest paths
Write the core logic of Dijkstra's algorithm inside main. Use arrays dist[4] to store shortest distances and visited[4] to track visited intersections. Initialize dist with a large number (e.g., 10000) except for dist[start] which is 0. Loop 4 times to pick the unvisited intersection with the smallest distance, mark it visited, and update distances to neighbors if shorter paths are found.
DSA C
Hint
Initialize distances to a large number except the start. Then repeat picking the closest unvisited node and update neighbors.
4
Print the shortest distances from the start
Add a for loop to print the shortest distances from start to each intersection using printf. Print exactly this format for each intersection i: Distance from 0 to i is dist[i] with a newline.
DSA C
Hint
Use a for loop and printf to show each distance in the exact format.
Practice
(1/5)
1. What is the main purpose of Dijkstra's Algorithm in graph theory?
easy
A. To sort the vertices of a graph topologically
B. To find the minimum spanning tree of a graph
C. To detect cycles in a graph
D. To find the shortest path from a single source to all other vertices
Solution
Step 1: Understand the goal of Dijkstra's Algorithm
Dijkstra's Algorithm is designed to find the shortest distance from one starting point (source) to every other point in a graph with non-negative edge weights.
Step 2: Compare with other graph algorithms
Minimum spanning tree algorithms find a tree connecting all vertices with minimum total edge weight, cycle detection finds loops, and topological sort orders vertices in directed acyclic graphs. These are different goals.
Final Answer:
To find the shortest path from a single source to all other vertices -> Option D
Quick Check:
Dijkstra's Algorithm = shortest paths from source [OK]
Hint: Dijkstra finds shortest paths from one start point [OK]
Common Mistakes:
Confusing with minimum spanning tree algorithms
Thinking it detects cycles
Assuming it sorts vertices
2. Which of the following is the correct way to initialize the distance array in Dijkstra's Algorithm in C?
easy
A. int dist[V]; for(int i=0; i
B. int dist[V] = {INT_MAX};
C. int dist[V] = {0};
D. int dist[V] = NULL;
Solution
Step 1: Understand distance initialization
Distances should start as infinity (or a very large number) except the source vertex, to represent unknown shortest paths.
Step 2: Check each option
int dist[V] = {0}; initializes all distances to 0, which is incorrect. int dist[V]; for(int i=0; i
Final Answer:
int dist[V]; for(int i=0; i -> Option A
Quick Check:
Initialize distances to INT_MAX with loop [OK]
Hint: Use loop to set all distances to INT_MAX [OK]
Common Mistakes:
Using {INT_MAX} initializes only first element
Setting all distances to 0
Assigning NULL to int array
3. What will be the output distances array after running Dijkstra's Algorithm from source vertex 0 on the following graph represented by adjacency matrix?
Hint: Relax edges stepwise, pick smallest distance vertex next [OK]
Common Mistakes:
Not updating distances after relaxation
Choosing wrong next vertex
Ignoring shorter paths through intermediate vertices
4. Identify the error in the following snippet of Dijkstra's Algorithm in C:
for (int count = 0; count < V-1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
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];
}
}
}
medium
A. The loop should run from 1 to V instead of 0 to V-1
B. The condition 'graph[u][v]' should be 'graph[u][v] != 0' to check edge existence
C. The sptSet array should be initialized with 1 instead of 0
D. The dist[u] check should be 'dist[u] == INT_MAX' instead of '!='
Solution
Step 1: Analyze edge existence condition
The condition 'graph[u][v]' relies on implicit truthiness of non-zero values. In C, this works but is less clear. Using 'graph[u][v] != 0' explicitly checks for edge presence and improves readability.
Step 2: Check other conditions
Loop range from 0 to V-1 is correct for V vertices. sptSet should start with 0 (false). The dist[u] check must be '!= INT_MAX' to ensure u is reachable before relaxing edges.
Final Answer:
The condition 'graph[u][v]' should be 'graph[u][v] != 0' to check edge existence -> Option B
Quick Check:
Explicit edge check improves clarity [OK]
Hint: Use explicit '!= 0' to check edges in adjacency matrix [OK]
Common Mistakes:
Assuming loop range is wrong
Initializing sptSet incorrectly
Reversing dist[u] check logic
5. Given a weighted graph with vertices 0 to 5 and edges: