Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Kruskal proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Build a minimum spanning tree by taking sorted edges that join different components. 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 Kruskal when the prompt's constraints and required operations match this shape: Build a minimum spanning tree by taking sorted edges that join different components.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Kruskal considers undirected edges globally from lightest to heaviest. Accepted edges form a forest that gradually joins components.
An edge whose endpoints have different roots connects two trees and is safe by the cut property. Equal roots mean the edge closes a cycle and must be rejected.
def kruskal_trace(node_count,edges):
parent=list(range(node_count));trace=[]
def find(node):
if parent[node]!=node:parent[node]=find(parent[node])
return parent[node]
for edge in sorted(edges):
weight,left,right=edge;a,b=find(left),find(right);accepted=a!=b
if accepted:parent[b]=a
trace.append([edge.copy(),accepted])
return traceImplement kruskal_trace(node_count,edges). Edges are [weight,u,v]; return [edge,accepted] in sorted order.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Union-find supplies near-constant amortized component checks. Path compression and union by size keep its forest shallow while accepted-edge count tracks completion.
def kruskal_weight(node_count,edges):
parent=list(range(node_count));size=[1]*node_count;total=used=0
def find(node):
if parent[node]!=node:parent[node]=find(parent[node])
return parent[node]
for weight,left,right in sorted(edges):
a,b=find(left),find(right)
if a==b:continue
if size[a]<size[b]:a,b=b,a
parent[b]=a;size[a]+=size[b];total+=weight;used+=1
return total if used==node_count-1 else NoneImplement kruskal_weight(node_count,edges). Return MST weight or None when disconnected.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Choose Kruskal for sparse edge lists, pre-sorted edges, or minimum spanning forests. Choose Prim when an adjacency representation and one connected growth frontier are more convenient. Sorting makes Kruskal O(E log E).
Say: “The next lightest edge crossing two current components is safe; union-find rejects cycle edges.” Stop after V minus one accepted edges and report failure or a forest when components remain.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Kruskal 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 |
|---|---|---|---|
| Kruskal complete workflow | O(E log E) | O(E log E) | Normalize endpoints, sort by the documented tuple, and union distinct components. |
O(V + E) for the focused Select Kruskal Tree Edges 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 kruskal_edges(node_count, edges):
passUse instead
def kruskal_edges(node_count, edges):
if node_count <= 1:
return []
parent = list(range(node_count))
def find(node):
while node != parent[node]:
parent[node] = parent[parent[node]]
node = parent[node]
return node
chosen = []
for weight, left, right in sorted((weight, min(left,right), max(left,right)) for left,right,weight in edges):
a, b = find(left), find(right)
if a == b:
continue
parent[b] = a
chosen.append([left, right, weight])
if len(chosen) == node_count - 1:
break
return chosen if len(chosen) == node_count - 1 else []Where you will hit this: Select Kruskal Tree Edges(opens in a new tab)
Returns the first V-1 edges and includes a cycle.
Prevent it: Preserve this proof obligation: The cut property makes each chosen minimum crossing edge compatible with some minimum spanning tree.
Avoid
def kruskal_edges(n,edges):
return [[min(u,v),max(u,v),w] for u,v,w in edges[:n-1]]Use instead
def kruskal_edges(node_count, edges):
if node_count <= 1:
return []
parent = list(range(node_count))
def find(node):
while node != parent[node]:
parent[node] = parent[parent[node]]
node = parent[node]
return node
chosen = []
for weight, left, right in sorted((weight, min(left,right), max(left,right)) for left,right,weight in edges):
a, b = find(left), find(right)
if a == b:
continue
parent[b] = a
chosen.append([left, right, weight])
if len(chosen) == node_count - 1:
break
return chosen if len(chosen) == node_count - 1 else []Where you will hit this: Select Kruskal Tree Edges(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 kruskal_edges(n,edges):
return [[min(u,v),max(u,v),w] for u,v,w in edges[:n-1]]Use instead
def kruskal_edges(node_count, edges):
if node_count <= 1:
return []
parent = list(range(node_count))
def find(node):
while node != parent[node]:
parent[node] = parent[parent[node]]
node = parent[node]
return node
chosen = []
for weight, left, right in sorted((weight, min(left,right), max(left,right)) for left,right,weight in edges):
a, b = find(left), find(right)
if a == b:
continue
parent[b] = a
chosen.append([left, right, weight])
if len(chosen) == node_count - 1:
break
return chosen if len(chosen) == node_count - 1 else []Where 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