Challenge - 5 Problems
Prim's MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prim's Algorithm on a Small Graph
What is the printed MST edges and their weights after running Prim's algorithm starting from vertex 0 on this graph?
DSA C#
int[,] graph = { {0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, {6, 8, 0, 0, 9}, {0, 5, 7, 9, 0} }; // Prim's algorithm implementation int V = 5; int[] parent = new int[V]; int[] key = new int[V]; bool[] mstSet = new bool[V]; for (int i = 0; i < V; i++) { key[i] = int.MaxValue; mstSet[i] = false; } key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = -1; int minKey = int.MaxValue; for (int v = 0; v < V; v++) { if (!mstSet[v] && key[v] < minKey) { minKey = key[v]; u = v; } } mstSet[u] = true; for (int v = 0; v < V; v++) { if (graph[u, v] != 0 && !mstSet[v] && graph[u, v] < key[v]) { parent[v] = u; key[v] = graph[u, v]; } } } for (int i = 1; i < V; i++) { Console.WriteLine($"{parent[i]} - {i} : {graph[i, parent[i]]}"); }
Attempts:
2 left
💡 Hint
Trace the algorithm step-by-step, always picking the smallest edge connecting MST to a new vertex.
✗ Incorrect
Prim's algorithm starts from vertex 0, picks edge 0-1 with weight 2, then 1-2 with weight 3, then 0-3 with weight 6, and finally 1-4 with weight 5, forming the MST.
🧠 Conceptual
intermediateUnderstanding Prim's Algorithm Data Structures
Which data structure is primarily used in Prim's algorithm to efficiently select the next edge with the smallest weight?
Attempts:
2 left
💡 Hint
Think about how to always pick the smallest edge quickly.
✗ Incorrect
Prim's algorithm uses a priority queue (min-heap) to pick the edge with the smallest weight efficiently at each step.
❓ Predict Output
advancedPrim's Algorithm Output on a Disconnected Graph
What will be the output or error when running Prim's algorithm on this disconnected graph starting from vertex 0?
DSA C#
int[,] graph = { {0, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 2}, {0, 0, 2, 0} }; int V = 4; int[] parent = new int[V]; int[] key = new int[V]; bool[] mstSet = new bool[V]; for (int i = 0; i < V; i++) { key[i] = int.MaxValue; mstSet[i] = false; } key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = -1; int minKey = int.MaxValue; for (int v = 0; v < V; v++) { if (!mstSet[v] && key[v] < minKey) { minKey = key[v]; u = v; } } if (u == -1) break; mstSet[u] = true; for (int v = 0; v < V; v++) { if (graph[u, v] != 0 && !mstSet[v] && graph[u, v] < key[v]) { parent[v] = u; key[v] = graph[u, v]; } } } for (int i = 1; i < V; i++) { if (parent[i] == 0 || parent[i] > 0) { Console.WriteLine($"{parent[i]} - {i} : {graph[i, parent[i]]}"); } else { Console.WriteLine($"{i} is not connected to MST"); } }
Attempts:
2 left
💡 Hint
Disconnected vertices won't get included in MST and parent remains default.
✗ Incorrect
Vertices 2 and 3 are disconnected from vertex 0's component, so they are not included in MST and print as not connected.
🔧 Debug
advancedIdentify the Bug in Prim's Algorithm Implementation
What is the bug in this Prim's algorithm code snippet that causes incorrect MST construction?
DSA C#
int V = 4; int[] parent = new int[V]; int[] key = new int[V]; bool[] mstSet = new bool[V]; for (int i = 0; i < V; i++) { key[i] = int.MaxValue; mstSet[i] = false; parent[i] = -1; } key[0] = 0; for (int count = 0; count < V - 1; count++) { int u = -1; int minKey = int.MaxValue; for (int v = 0; v < V; v++) { if (!mstSet[v] && key[v] < minKey) { minKey = key[v]; u = v; } } mstSet[u] = true; for (int v = 0; v < V; v++) { if (graph[u, v] != 0 && !mstSet[v] && graph[u, v] < key[v]) { key[v] = graph[u, v]; // Missing parent update here } } }
Attempts:
2 left
💡 Hint
Check if the parent array tracks the MST edges correctly.
✗ Incorrect
Without updating parent[v] when key[v] changes, the MST edges are not recorded correctly.
🚀 Application
expertMinimum Spanning Tree Weight Calculation
Given the following weighted graph, what is the total weight of the MST found by Prim's algorithm starting from vertex 0?
DSA C#
int[,] graph = { {0, 10, 6, 5}, {10, 0, 0, 15}, {6, 0, 0, 4}, {5, 15, 4, 0} }; int V = 4; int[] parent = new int[V]; int[] key = new int[V]; bool[] mstSet = new bool[V]; for (int i = 0; i < V; i++) { key[i] = int.MaxValue; mstSet[i] = false; } key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = -1; int minKey = int.MaxValue; for (int v = 0; v < V; v++) { if (!mstSet[v] && key[v] < minKey) { minKey = key[v]; u = v; } } mstSet[u] = true; for (int v = 0; v < V; v++) { if (graph[u, v] != 0 && !mstSet[v] && graph[u, v] < key[v]) { parent[v] = u; key[v] = graph[u, v]; } } } int totalWeight = 0; for (int i = 1; i < V; i++) { totalWeight += graph[i, parent[i]]; } Console.WriteLine(totalWeight);
Attempts:
2 left
💡 Hint
Sum the weights of edges selected in MST after running Prim's algorithm.
✗ Incorrect
The MST edges are 0-3 (5), 3-2 (4), and 0-1 (10), total weight = 5 + 4 + 10 = 19.
