Find minimum spanning trees for weighted graphs using Prim's or Kruskal's algorithm. Visualize graphs and MSTs with interactive tools.
A minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight.
Formal Definition:
Given a connected, undirected graph G = (V, E) with weight function w: E → ℝ, a minimum spanning tree T = (V, E') where E' ⊆ E such that:
Initialization: Start with an arbitrary vertex as the initial MST. Initialize a set to track visited vertices.
Greedy Selection: Repeatedly add the cheapest edge that connects a vertex in the MST to a vertex not yet in the MST.
Termination: Continue until all vertices are included in the MST.
Time Complexity: O(E log V) using a binary heap, where V is vertices and E is edges.
Sort Edges: Sort all edges in non-decreasing order of their weight.
Initialize Forest: Start with each vertex as a separate tree (using disjoint-set data structure).
Greedy Selection: Iterate through sorted edges, adding an edge to the MST if it doesn't form a cycle (connects different trees).
Termination: Stop when V-1 edges have been added to the MST.
Time Complexity: O(E log E) or O(E log V) using union-find with path compression.
Key Properties:
V
Set of vertices
E
Set of edges
w(e)
Weight of edge e
|V|
Number of vertices
|E|
Number of edges
MST
Minimum Spanning Tree
| Algorithm | Time | Space |
|---|---|---|
| Prim (Binary Heap) | O(E log V) | O(V) |
| Kruskal (Sorting) | O(E log E) | O(E) |
| Prim (Matrix) | O(V²) | O(V²) |