Problem Introduction
For an undirected graph, if in some subgraph any two vertices are mutually connected and it is a tree, then this tree is called a spanning tree. If the edges carry weights, then the spanning tree that minimizes the total edge weight is called the Minimum Spanning Tree.
Common Algorithms
Prim’s Algorithm
Pick a starting vertex and add it to the tree, compute for every vertex its distance to the tree, choose the smallest one and add it to the tree, then update the distances again, find the smallest among them, and repeat until all vertices have been added to the tree.
Kruskal’s Algorithm
Sort the weights, then examine them from smallest to largest: if adding this edge does not form a cycle, it is an edge of the desired tree.
(combined with Union-Find (Disjoint Set Union))
Reference articles:
Introduction to Algorithm Competition Classic
Programming Contest Challenge Book
Minimum Spanning Tree - Prim’s Algorithm and Kruskal’s Algorithm