Graph G=(V,E)
Subgraph G'=(V',E'): V'
If G' contains no cycles then G'=T is a tree.
T= (V',E') is a spanning tree of G if V'= V
The right two are spanning trees of the graph on the left:
The cost of a spanning tree is the sum of the costs (weights) of the edges in
the spanning tree.
A minimum-cost spanning tree is a spanning tree of least cost.
Cost on edges C: E -> R+
(i.e., is a function from the edges to the real numbers)
(3,6)-- 15
(4,6)-- 20
(2,6)-- 25
(1,4)-- 30
(3,5)-- 35 ...
See also Figure 18.11
Use Union and Find to convert a forest of n isolated nodes to a spanning tree
note: link between nodes in Union-Find data structure not necessarily same as
edges in spanning tree. We want no cycles in this construction.
Time: (page 730)
e= |E|, n= |V|
Sorting: eloge
The rest of the algorithm then takes at most 2e Finds and (n-1) Unions. ( time)
Adding up all parts get O(n + eloge). If each node has even one edge then e > n. Hence bounded mostly by the sort -- O(eloge)
Theorem: Procedure Kruskal produces a minimum cost spanning tree.
Proof: (page 729 also) Uses transformation and contradiction
Let T be tree constructed by algorithm
If T=T' than Done ... we will transform to make it so
Let e' be an edge on this cycle such that e'
then, at some stage in the construction of T, Kruskal must have considered e' for inclusion (i.e., T<e U e' or not)
e' must have been rejected because it caused a cycle with edges in T<e. Contradiction of theirs being minimum. Done .
Consider T'' = T' U {e} - {e'}
c(T'') < c(T')
See also Figure 18.14
Outer loop n-1 (basically n to get all vertices) times - inside loop ("over all edges") O(n) time)
Prim 0(n2)
Again, note that if # of e is close to n2 -- almost complete graph
Kruskal O(e log e)
e<< n2
or e ~= n - a sparse graph
note: if a lot of edges Kruscal's looks like n2 log n2 This, or even,
n2 log n is worse than just n2
If # edges << n2 (i.e. sparse)
use Kruskal 0(e log e)
otherwise use Prim's 0(n2)
Use Figure 18.17 to try things.
And check out java demos for Prim and
Kruscal
Minimum Cost Spanning Trees
(1,2)-- 10
Before
After
Step 1
Step 2
Step 3
Step 4
Text notes that "not essential to sort...so long as the next edge can be determined easily" - suggest a heap (O(log e) for next to consider and O(e) to build the heap.)
so this part of the algorithm is sightly more than 0(n + e) Proof:
Let T' be a min cost spanning tree
Let e be an edge of min. cost in T - T'. (ones Kruscal used but they didn't)
visual
Let T<e denote the edges in T of cost < c(e) (anybody smaller not used?)
If all edges in T<e contained in T' then T' U {e} creates a unique cycle.
Claim: c(e') > c(e)Proof: suppose c(e') < c (e)
but T<e
therefore T' contains a cyclePrim
Edges included so far form a tree
Before
After
Also in text at Figure 18.13 and discussed on page 731
Time
O(n2)
inside the loop --
could check all edges => bounded by e
but ways to implement to get 0(n2)
but e, if graph is complete, could be up n2 => time for algorithm of O(n3)
(A (6,10) graph means 6 vertices and 10 edges)