Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Prim proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Grow a minimum spanning tree by repeatedly choosing the cheapest edge leaving the tree. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Prim when the prompt's constraints and required operations match this shape: Grow a minimum spanning tree by repeatedly choosing the cheapest edge leaving the tree.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Prim starts from one vertex and grows a connected visited set. At every step it chooses the cheapest edge crossing from the tree to an unvisited vertex.
When a vertex joins, push its outgoing edges whose other endpoint is not yet visited. The heap may contain obsolete entries as the cut changes.
import heapq
def prim_trace(adjacency,start):
heap=[(0,start)];visited=set();total=0;trace=[]
while heap:
weight,node=heapq.heappop(heap)
if node in visited:continue
visited.add(node);total+=weight;trace.append([node,weight,total])
for neighbor,cost in adjacency[node]:
if neighbor not in visited:heapq.heappush(heap,(cost,neighbor))
return traceImplement prim_trace(adjacency,start). Edges are [neighbor,weight]; return [node,weight,total] for accepted heap entries.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
A popped edge to an already visited vertex would create a cycle, so skip it. The first accepted heap edge for an unvisited vertex is safe by the cut property.
import heapq
def prim_weight(adjacency):
if not adjacency:return 0
heap=[(0,0)];visited=set();total=0
while heap:
weight,node=heapq.heappop(heap)
if node in visited:continue
visited.add(node);total+=weight
for neighbor,cost in adjacency[node]:
if neighbor not in visited:heapq.heappush(heap,(cost,neighbor))
return total if len(visited)==len(adjacency) else NoneImplement prim_weight(adjacency). Return MST weight from node zero or None if disconnected.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Choose Prim for adjacency-list graphs and connected growth, especially dense graphs with suitable priority structures. Choose Kruskal when edges are already sorted or sparse and union-find is natural.
Say: “Visited vertices define a cut. The heap’s cheapest live crossing edge is safe, and an already visited endpoint would create a cycle.” Check visited count for disconnection and state O(E log V) heap time.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Prim proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Prim complete workflow | O(E log E) | O(E log E) | Grow from node zero using the lightest edge crossing from visited to unvisited vertices. |
O(V + E) for the focused Grow a Spanning Tree with Prim implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: The weighted graph is undirected, and the required output connects each component without cycles.
Avoid
def prim_spanning_weight(node_count, edges):
passUse instead
def prim_spanning_weight(node_count, edges):
import heapq
if node_count <= 1:
return 0
graph = [[] for _ in range(node_count)]
for left, right, weight in edges:
graph[left].append((weight, right)); graph[right].append((weight, left))
visited = set()
frontier = [(0, 0)]
total = 0
while frontier and len(visited) < node_count:
weight, node = heapq.heappop(frontier)
if node in visited:
continue
visited.add(node); total += weight
for next_weight, neighbor in graph[node]:
if neighbor not in visited:
heapq.heappush(frontier, (next_weight, neighbor))
return total if len(visited) == node_count else -1Where you will hit this: Grow a Spanning Tree with Prim(opens in a new tab)
Returns the first edge weight instead of growing through every vertex.
Prevent it: Preserve this proof obligation: The cut property makes each chosen minimum crossing edge compatible with some minimum spanning tree.
Avoid
def prim_spanning_weight(n,edges):
return min((w for _,_,w in edges),default=0)Use instead
def prim_spanning_weight(node_count, edges):
import heapq
if node_count <= 1:
return 0
graph = [[] for _ in range(node_count)]
for left, right, weight in edges:
graph[left].append((weight, right)); graph[right].append((weight, left))
visited = set()
frontier = [(0, 0)]
total = 0
while frontier and len(visited) < node_count:
weight, node = heapq.heappop(frontier)
if node in visited:
continue
visited.add(node); total += weight
for next_weight, neighbor in graph[node]:
if neighbor not in visited:
heapq.heappush(frontier, (next_weight, neighbor))
return total if len(visited) == node_count else -1Where you will hit this: Grow a Spanning Tree with Prim(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(E log E).
Avoid
def prim_spanning_weight(n,edges):
return min((w for _,_,w in edges),default=0)Use instead
def prim_spanning_weight(node_count, edges):
import heapq
if node_count <= 1:
return 0
graph = [[] for _ in range(node_count)]
for left, right, weight in edges:
graph[left].append((weight, right)); graph[right].append((weight, left))
visited = set()
frontier = [(0, 0)]
total = 0
while frontier and len(visited) < node_count:
weight, node = heapq.heappop(frontier)
if node in visited:
continue
visited.add(node); total += weight
for next_weight, neighbor in graph[node]:
if neighbor not in visited:
heapq.heappush(frontier, (next_weight, neighbor))
return total if len(visited) == node_count else -1Where you will hit this: Maximum Container Area(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27