Challenge - 5 Problems
Prim's MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Prim's Algorithm on a Small Graph
What is the printed state of the minimum spanning tree edges after running Prim's algorithm starting from node 0 on this graph?
DSA Typescript
const 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] ]; function primMST(graph: number[][]): string { const V = graph.length; const key = Array(V).fill(Infinity); const parent = Array(V).fill(-1); const mstSet = Array(V).fill(false); key[0] = 0; for (let count = 0; count < V - 1; count++) { let min = Infinity; let u = -1; for (let v = 0; v < V; v++) { if (!mstSet[v] && key[v] < min) { min = key[v]; u = v; } } mstSet[u] = true; for (let v = 0; v < V; v++) { if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) { parent[v] = u; key[v] = graph[u][v]; } } } let result = ''; for (let i = 1; i < V; i++) { result += `${parent[i]} - ${i} | `; } return result.trim(); } console.log(primMST(graph));
Attempts:
2 left
💡 Hint
Trace the algorithm step-by-step, always picking the smallest edge connecting the MST set to a new vertex.
✗ Incorrect
Prim's algorithm starts from node 0, picks edges with minimum weight connecting to new nodes. The edges chosen are 0-1 (2), 1-2 (3), 0-3 (6), and 1-4 (5).
🧠 Conceptual
intermediate1:00remaining
Understanding 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.
🔧 Debug
advanced2:00remaining
Identify the Bug in Prim's Algorithm Implementation
What error will this code produce when running Prim's algorithm on a graph with disconnected components?
DSA Typescript
function primMST(graph: number[][]): number[] {
const V = graph.length;
const key = Array(V).fill(Infinity);
const parent = Array(V).fill(-1);
const mstSet = Array(V).fill(false);
key[0] = 0;
for (let count = 0; count < V - 1; count++) {
let min = Infinity;
let u = -1;
for (let v = 0; v < V; v++) {
if (!mstSet[v] && key[v] < min) {
min = key[v];
u = v;
}
}
mstSet[u] = true;
for (let v = 0; v < V; v++) {
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
return parent;
}Attempts:
2 left
💡 Hint
Consider what happens if no vertex is reachable from the current MST set.
✗ Incorrect
If the graph has disconnected components, the inner loop may not find a vertex u with key less than Infinity, leaving u = -1. Then mstSet[-1] = true causes logical error or infinite loop.
❓ Predict Output
advanced2:00remaining
Prim's Algorithm Output with Edge Weights and Parent Array
What is the final parent array after running Prim's algorithm on this graph starting from node 0?
DSA Typescript
const graph = [ [0, 1, 4, 0, 0, 0], [1, 0, 4, 2, 7, 0], [4, 4, 0, 3, 5, 0], [0, 2, 3, 0, 4, 6], [0, 7, 5, 4, 0, 7], [0, 0, 0, 6, 7, 0] ]; function primMST(graph: number[][]): number[] { const V = graph.length; const key = Array(V).fill(Infinity); const parent = Array(V).fill(-1); const mstSet = Array(V).fill(false); key[0] = 0; for (let count = 0; count < V - 1; count++) { let min = Infinity; let u = -1; for (let v = 0; v < V; v++) { if (!mstSet[v] && key[v] < min) { min = key[v]; u = v; } } mstSet[u] = true; for (let v = 0; v < V; v++) { if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) { parent[v] = u; key[v] = graph[u][v]; } } } return parent; } console.log(primMST(graph));
Attempts:
2 left
💡 Hint
Follow the selection of edges with minimum weights step-by-step.
✗ Incorrect
The MST edges connect nodes as: 0-1, 1-3, 1-2, 3-4, 3-5. So parents are: 1's parent 0, 3's parent 1, 2's parent 1, 4's parent 3, 5's parent 3.
🧠 Conceptual
expert1:00remaining
Time Complexity of Prim's Algorithm with Different Data Structures
What is the time complexity of Prim's algorithm when implemented using an adjacency matrix and a simple array to find the minimum key?
Attempts:
2 left
💡 Hint
Consider how many times you scan all vertices to find the minimum key.
✗ Incorrect
Using an adjacency matrix and scanning all vertices to find the minimum key each time results in O(V^2) time complexity.