Skip to main content

Minimum Spanning Tree

We define a saft edge for a subset AA of some minimum spanning tree if adding it to AA still is a part of a minimum spanning tree.

  1. We define a cut (S, V - S) of an undirected graph GG is a partition of VV.
  2. We say a edge (u,v)E(u,v)\in E crosses a cut (S, V - S) if uSu\in S and vVSv\in V-S.
  3. We say that a cut respects a set AA of edges if no edge in AA crosses the cut.
  4. An edge (u,v)E(u,v)\in E is light edge crossing a cut (S, V - S) if its weight is the minimum weight of all edges crossing the cut.

Let G=(V,E)G = (V,E) be a connected undirected graph with a real-valued weight function ww defined on EE. Let A4beasubsetofA4 be a subset of Ethatisincludedinsomeminimumspanningtreeforthat is included in some minimum spanning tree forG,let, let (S, V-S)beanycutofbe any cut ofGthatrespectthat respectA,andlet, and let (u,v)bealightedgecrossingbe a light edge crossing(S, V-S).Then. Then (u,v)isasafeedgeforis a safe edge forA$.

Let G=(V,E)G = (V, E) be a connected, undirected graph with a real-valued weight function ww defined on EE. Let AA be a subset of EE that is included in some minimum spanning tree for GG, and let C=(VC,VVC)C = (V_C, V - V_C) be a connected component (tree) in the forest GA=(V,A)G_A = (V, A). If (u,v)(u,v) is a light edge connecting CC to some other component in GAG_A, then (u,v)(u,v) is safe for AA.

Kruskal's Algorithm

Kruskal’s algorithm finds a safe edge to add to the growing forest by finding, of all the edges that connect any two trees in the forest, an edge (u,v)(u,v) of least weight. One implementation of Kruskal’s algorithm is to use a disjoint-set data structure to keep track if two vertices are in the same tree.

The basic idea:

  1. Make a set for each vertex.
  2. sort the edges in nondecreasing order of weight.
  3. Union sorted edges if they are not in the same set.
  4. return the set of edges that are in the same set.

The running time of Kruskal’s algorithm is O(ElogV)O(|E| \log |V|).

Prim's Algorithm

Prim’s algorithm finds a safe edge to add to the growing forest by finding, of all the edges that connect any vertex in the forest to any vertex not in the forest, an edge (u,v)(u,v) of least weight. One implementation of Prim’s algorithm is to use a priority queue to keep track of the light edges that connect the forest to the rest of the graph.

The basic idea:

  1. set up all keys and parents.
  2. set up a min-prority heap by vertex.
  3. set root key to 0 and then do while loop
  4. modify the adj vertices' keys and parents by the light edges.

The running time of Prim’s algorithm is O(E+VlogV)O(|E| + |V| \log |V|).