Skip to content
Graph Intelligence Hub
Menu

Algorithm

Kruskal's minimum spanning tree

activeNeeds verification

Builds a minimum-weight spanning tree by adding edges in increasing weight order whenever they join two different components.

Intuition

Greedily take the cheapest remaining edge that does not create a cycle; a union–find structure tracks components.

Pseudocode

Kruskal(G):
  T = ∅; make_set(v) for all v
  for (u, v) in edges sorted by weight:
    if find(u) != find(v): T.add((u, v)); union(u, v)
  return T

Complexity

TimeO(E log E)
SpaceO(V)

Python

import networkx as nx
T = nx.minimum_spanning_tree(G, algorithm='kruskal', weight='weight')

An interactive step-by-step visualizer for this algorithm is planned for phase 3.

Profile

Time complexityO(E log E)
Space complexityO(V)

Relationships