Challenge - 5 Problems
Dijkstra Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dijkstra's Algorithm on a Simple Graph
What is the shortest distance array output after running Dijkstra's algorithm from source node 0 on the following graph?
Graph edges with weights:
0 -> 1 (4), 0 -> 2 (1), 2 -> 1 (2), 1 -> 3 (1), 2 -> 3 (5)
Graph edges with weights:
0 -> 1 (4), 0 -> 2 (1), 2 -> 1 (2), 1 -> 3 (1), 2 -> 3 (5)
DSA C#
int V = 4; int source = 0; List<(int, int)>[] graph = new List<(int, int)>[V]; for (int i = 0; i < V; i++) graph[i] = new List<(int, int)>(); graph[0].Add((1, 4)); graph[0].Add((2, 1)); graph[2].Add((1, 2)); graph[1].Add((3, 1)); graph[2].Add((3, 5)); int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; dist[source] = 0; var pq = new SortedSet<(int dist, int node)>(Comparer<(int dist, int node)>.Create((a, b) => a.dist != b.dist ? a.dist - b.dist : a.node - b.node)); pq.Add((0, source)); while (pq.Count > 0) { var current = pq.Min; pq.Remove(current); int u = current.node; foreach (var (v, w) in graph[u]) { if (dist[u] + w < dist[v]) { if (dist[v] != int.MaxValue) pq.Remove((dist[v], v)); dist[v] = dist[u] + w; pq.Add((dist[v], v)); } } } Console.WriteLine(string.Join(",", dist));
Attempts:
2 left
💡 Hint
Trace the shortest paths step-by-step from source 0 to all nodes.
✗ Incorrect
Starting from node 0, the shortest path to node 2 is 1, to node 1 is via node 2 with total cost 3, and to node 3 is via node 1 with total cost 4.
🧠 Conceptual
intermediateUnderstanding Priority Queue Role in Dijkstra's Algorithm
Why is a priority queue used in Dijkstra's algorithm?
Attempts:
2 left
💡 Hint
Think about how the algorithm decides which node to explore next.
✗ Incorrect
The priority queue helps select the next node with the smallest tentative distance to ensure the shortest paths are found efficiently.
🔧 Debug
advancedIdentify the Bug in Dijkstra's Algorithm Implementation
What error will the following code produce when running Dijkstra's algorithm on a graph with 3 nodes and edges 0->1 (2), 1->2 (3)?
Code snippet:
int[] dist = new int[3]; for (int i = 0; i < 3; i++) dist[i] = int.MaxValue; dist[0] = 0; var pq = new SortedSet<(int dist, int node)>(); pq.Add((0, 0)); while (pq.Count > 0) { var current = pq.Min; pq.Remove(current); int u = current.node; foreach (var (v, w) in graph[u]) { if (dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.Add((dist[v], v)); } } }
Code snippet:
int[] dist = new int[3]; for (int i = 0; i < 3; i++) dist[i] = int.MaxValue; dist[0] = 0; var pq = new SortedSet<(int dist, int node)>(); pq.Add((0, 0)); while (pq.Count > 0) { var current = pq.Min; pq.Remove(current); int u = current.node; foreach (var (v, w) in graph[u]) { if (dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.Add((dist[v], v)); } } }
Attempts:
2 left
💡 Hint
Consider how SortedSet handles duplicate keys with same distance and node.
✗ Incorrect
The code does not remove old entries from the priority queue before adding updated distances, causing duplicates and runtime errors.
🚀 Application
advancedApplying Dijkstra's Algorithm to Find Shortest Path Count
Given a graph with edges:
0->1 (1), 0->2 (1), 1->3 (1), 2->3 (1)
How many shortest paths exist from node 0 to node 3?
0->1 (1), 0->2 (1), 1->3 (1), 2->3 (1)
How many shortest paths exist from node 0 to node 3?
Attempts:
2 left
💡 Hint
Count all distinct shortest routes from 0 to 3.
✗ Incorrect
There are two shortest paths: 0->1->3 and 0->2->3, both with total cost 2.
🧠 Conceptual
expertWhy Dijkstra's Algorithm Fails with Negative Edge Weights
Why does Dijkstra's algorithm not work correctly if the graph contains negative edge weights?
Attempts:
2 left
💡 Hint
Think about the algorithm's assumption about shortest distances.
✗ Incorrect
Dijkstra's algorithm assumes that once a node is visited with the smallest distance, no shorter path to it exists, which is false if negative edges exist.
