Let be a connected, undirected graph. Let denote the weight of each edge .
A spanning tree is an acyclic subset that connects all the vertices of . A minimum spanning tree is a spanning tree with , defined as follows, minimized:
Kruskal's Algorithm
Let be an undirected graph and be a weight function. Kruskal's algorithm is a greedy algorithm for finding an MST in . Assuming is represented as an adjacency-list:
Create a sorted list of the edges in in increasing order.
For each edge in the sorted list, check if the sets containing and are disjoint. If so, join the disjoint sets and set .
Upon termination, is the desired MST.
Prim's Algorithm
Let be an undirected graph and be a weight function. Prim's algorithm is a greedy algorithm for finding an MST in . Assume is represented as an adjacency-list. Given an empty min priority queue and an arbitrary vertex :