Challenge - 5 Problems
Prim's Algorithm Mastery
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 total weight of the minimum spanning tree after running Prim's algorithm on this graph?
DSA Java
import java.util.*; class Edge { int to, weight; Edge(int to, int weight) { this.to = to; this.weight = weight; } } public class MST { public static int prim(int n, List<List<Edge>> graph) { boolean[] visited = new boolean[n]; PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight)); pq.add(new Edge(0, 0)); int totalWeight = 0; while (!pq.isEmpty()) { Edge current = pq.poll(); if (visited[current.to]) continue; visited[current.to] = true; totalWeight += current.weight; for (Edge e : graph.get(current.to)) { if (!visited[e.to]) pq.add(e); } } return totalWeight; } public static void main(String[] args) { int n = 4; List<List<Edge>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); graph.get(0).add(new Edge(1, 1)); graph.get(1).add(new Edge(0, 1)); graph.get(0).add(new Edge(2, 4)); graph.get(2).add(new Edge(0, 4)); graph.get(1).add(new Edge(2, 2)); graph.get(2).add(new Edge(1, 2)); graph.get(1).add(new Edge(3, 5)); graph.get(3).add(new Edge(1, 5)); graph.get(2).add(new Edge(3, 3)); graph.get(3).add(new Edge(2, 3)); System.out.println(prim(n, graph)); } }
Attempts:
2 left
💡 Hint
Think about choosing edges with the smallest weights connecting new vertices.
✗ Incorrect
Prim's algorithm starts from node 0, picks edge (0-1) with weight 1, then edge (1-2) with weight 2, then edge (2-3) with weight 3. Total weight is 1+2+3=6.
🧠 Conceptual
intermediateUnderstanding Prim's Algorithm Data Structures
Which data structure is best suited to efficiently select the next edge with the smallest weight in Prim's algorithm?
Attempts:
2 left
💡 Hint
You want to always pick the smallest edge quickly.
✗ Incorrect
A priority queue (min-heap) allows quick access to the smallest edge weight, which is essential for Prim's algorithm efficiency.
🔧 Debug
advancedIdentify the Bug in Prim's Algorithm Implementation
What error will this code produce when running Prim's algorithm on a connected graph?
DSA Java
import java.util.*; class Edge { int to, weight; Edge(int to, int weight) { this.to = to; this.weight = weight; } } public class MST { public static int prim(int n, List<List<Edge>> graph) { boolean[] visited = new boolean[n]; PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight)); pq.add(new Edge(0, 0)); int totalWeight = 0; while (!pq.isEmpty()) { Edge current = pq.poll(); if (visited[current.to]) continue; visited[current.to] = true; totalWeight += current.weight; for (Edge e : graph.get(current.to)) { if (visited[e.to]) pq.add(e); } } return totalWeight; } }
Attempts:
2 left
💡 Hint
Check the condition inside the for loop when adding edges to the priority queue.
✗ Incorrect
The code adds edges to already visited nodes back into the priority queue, causing repeated processing and an infinite loop.
❓ Predict Output
advancedPrim's Algorithm Output on Disconnected Graph
What will be the output of the Prim's algorithm function when run on this disconnected graph?
DSA Java
import java.util.*; class Edge { int to, weight; Edge(int to, int weight) { this.to = to; this.weight = weight; } } public class MST { public static int prim(int n, List<List<Edge>> graph) { boolean[] visited = new boolean[n]; PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight)); pq.add(new Edge(0, 0)); int totalWeight = 0; while (!pq.isEmpty()) { Edge current = pq.poll(); if (visited[current.to]) continue; visited[current.to] = true; totalWeight += current.weight; for (Edge e : graph.get(current.to)) { if (!visited[e.to]) pq.add(e); } } return totalWeight; } public static void main(String[] args) { int n = 5; List<List<Edge>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); graph.get(0).add(new Edge(1, 2)); graph.get(1).add(new Edge(0, 2)); graph.get(1).add(new Edge(2, 3)); graph.get(2).add(new Edge(1, 3)); // Node 3 and 4 are disconnected System.out.println(prim(n, graph)); } }
Attempts:
2 left
💡 Hint
Prim's algorithm only covers the connected component starting from node 0.
✗ Incorrect
The algorithm covers nodes 0,1,2 with edges 2 and 3, total 5. Nodes 3 and 4 are disconnected and not included.
🧠 Conceptual
expertPrim's Algorithm Time Complexity Analysis
What is the time complexity of Prim's algorithm when implemented using an adjacency list and a binary heap priority queue for a graph with V vertices and E edges?
Attempts:
2 left
💡 Hint
Consider the cost of extracting minimum edges and updating the priority queue.
✗ Incorrect
Using a binary heap, each edge can be added to the priority queue once, and extracting minimum takes O(log V). So total is O(E log V).
