Grow a spanning tree by finding the edges that are safe iteratively.
A cut
An edge crosses the cut
An light edge crossing the cut
safe edge = adding this edge to Amaintains the property that
A⊆T, e.g. no cycle, low weight.
When the algorithm is running GA will be a forest, in which each
connected component is a tree. As the algorithm runs, light edges crossing
each connected components are picked to form the spanning tree.
def MST(G, w) A = set() # some set while A is not spanning tree: find an edge (u, v) that is safe for A A.add( (u, v) ) return A
Based on the generic method, algorithms of Kruskal and
Prim each uses a specific rule to determine a safe edge.
Kruskal: A is a forest with all vertices, safe edge is a lowest-weight
edge in the graph that connects two distinct components.
Prim: A is a single tree, safe edge is a lowest-weight edge connecting the
tree to a vertex not in the tree.