GENERIC-MSTEvidently, the gist of the MST algorithm is finding an edge that has the minimum weight and that adding this edge to does not result in a cycle in . To describe how this is done, the following concepts are introduced (see Figure 1):
1
2 while the edges in do not form an MST
3 do find an edge that has the
. minimum weight and this edge, along with the
. edges in , does not form a cycle.
4
5 return
KRUSKAL-MSTThe sorting of edges will take time. Next for each edge, disjoint-set functions are called. This requires time. Since is at most and , . The running time for Kruskal's algorithm is thus . One heuristic that can be used to improve Kruskal's runtime is to maintain a count of the number of edges in . The reason for this is that a tree spanning all the vertices in will have edges. Thus, if , the algorithm can safely terminate. This heuristic will tend to be more effective the denser a graph gets. Figure 2 illustrates the running of Kruskal's algorithm.
1
2 for each vertex
3 do MAKE-SET
4 Sort the edges of by nondecreasing weight
5 for each edge in order by nondecreading weight
6 do if FIND-SET FIND-SET
7 then
8 UNION
9 return
PRIM-MSTThe performance of Prim's algorithm depends on how the priority queue is implemented.
1
2 for each
3 do
4
5 NIL
6 while
7 do EXTRACT-MIN
8 for each
9 do if and
10 then
11