Challenge - 5 Problems
Prim's Algorithm 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 total weight of the minimum spanning tree after running Prim's algorithm starting from node 0 on the given graph?
DSA Python
graph = {
0: {1: 2, 3: 6},
1: {0: 2, 2: 3, 3: 8, 4: 5},
2: {1: 3, 4: 7},
3: {0: 6, 1: 8, 4: 9},
4: {1: 5, 2: 7, 3: 9}
}
import heapq
def prim(graph, start):
visited = set()
min_heap = [(0, start)]
total_weight = 0
while min_heap:
weight, node = heapq.heappop(min_heap)
if node in visited:
continue
visited.add(node)
total_weight += weight
for adj, w in graph[node].items():
if adj not in visited:
heapq.heappush(min_heap, (w, adj))
return total_weight
result = prim(graph, 0)
print(result)Attempts:
2 left
💡 Hint
Trace the edges chosen by Prim's algorithm starting from node 0 and sum their weights.
✗ Incorrect
Prim's algorithm starts at node 0 and picks edges with the smallest weight connecting to unvisited nodes. The edges chosen are (0-1:2), (1-2:3), (1-4:5), and (0-3:6). Their sum is 2 + 3 + 5 + 6 = 16. However, the edge (3-4:9) is not chosen because 4 is already visited. The total weight is 16.
🧠 Conceptual
intermediateUnderstanding Prim's Algorithm Behavior
Which of the following statements about Prim's algorithm is correct?
Attempts:
2 left
💡 Hint
Think about how Prim's algorithm chooses edges to add to the tree.
✗ Incorrect
Prim's algorithm starts from any node and grows the tree by adding the smallest edge that connects the tree to a new node. It works on undirected graphs and produces a minimum spanning tree, not maximum.
🔧 Debug
advancedIdentify the Error in Prim's Algorithm Implementation
What error will the following Prim's algorithm code produce when run on a connected graph?
DSA Python
def prim_bug(graph, start): visited = set() min_heap = [(0, start)] total_weight = 0 while min_heap: weight, node = min_heap.pop() if node in visited: continue visited.add(node) total_weight += weight for adj, w in graph[node].items(): if adj not in visited: min_heap.append((w, adj)) return total_weight # graph is a dictionary of dictionaries
Attempts:
2 left
💡 Hint
Consider how the edges are selected from the min_heap list.
✗ Incorrect
The code uses a list and pops from the end, which does not guarantee the smallest edge is chosen next. This breaks Prim's algorithm logic and results in an incorrect total weight.
❓ Predict Output
advancedPrim's Algorithm Output on a Disconnected Graph
What will be the output of the following code when Prim's algorithm is run starting from node 0 on a disconnected graph?
DSA Python
graph = {
0: {1: 4},
1: {0: 4},
2: {3: 5},
3: {2: 5}
}
import heapq
def prim(graph, start):
visited = set()
min_heap = [(0, start)]
total_weight = 0
while min_heap:
weight, node = heapq.heappop(min_heap)
if node in visited:
continue
visited.add(node)
total_weight += weight
for adj, w in graph.get(node, {}).items():
if adj not in visited:
heapq.heappush(min_heap, (w, adj))
return total_weight
print(prim(graph, 0))Attempts:
2 left
💡 Hint
Prim's algorithm only visits nodes connected to the start node.
✗ Incorrect
The graph has two disconnected parts: nodes 0-1 and nodes 2-3. Starting from node 0, only nodes 0 and 1 are visited. The edge weight between them is 4, so total weight is 4.
🧠 Conceptual
expertTime Complexity of Prim's Algorithm with Different Data Structures
Which of the following statements about the time complexity of Prim's algorithm is correct when using a binary heap and adjacency list representation?
Attempts:
2 left
💡 Hint
Consider how many times edges are added to the heap and how heap operations cost.
✗ Incorrect
Using a binary heap and adjacency list, each edge is pushed into the heap at most once, and each heap operation costs O(log V). Thus, total time is O(E log V).
