290 likes | 430 Views
Minimum Spanning Trees. Section 8.3. Definition. For a undirected graph G=(V,E), a spanning tree T is a sub-graph that is a tree covering all vertices in V. In an undirected weighted, a Minimum Spanning Tree (MST) is the spanning tree with the min sum of weights.
E N D
Minimum Spanning Trees Section 8.3
Definition • For a undirected graph G=(V,E), a spanning tree T is a sub-graph that is a tree covering all vertices in V. • In an undirected weighted, a Minimum Spanning Tree (MST) is the spanning tree with the min sum of weights. • We shall assume that |V|=n and G is connected. • Note that MST may not be unique • And there is n-1 vertices in MST.
Problem • Given a weighted undirected graph, find a MST. • Solution • Kruskal’s Algorithm • Prim’s Algorithm
Kruskal’s Algorithm • Sort the edges non-decreasingly • add the edge with the min weight to the tree if it doesn’t make a cycle, otherwise skip it. • Repeat • Note:We can’t just take the first n-1 edges with the min weights. Choose as long as it’s a tree.
Kruskal’s Algorithm Input: weighted connected undirected G=(V,E) Output: MST T • Sort the edges in E non-decreasingly • For each vertex v V makeset({v}) End for • T { } • While (|T| < n-1) • let (x,y) be the next edge in E • if find(x) find(y) then • add(x,y) to T • union(x,y) • end if • End while
Analysis • Running Time= (m log n) • Because we need (m log m) for sorting the edges and (m log* n) for the find union operations
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Prim’s Algorithm • Divide V into disjoint X & Y • Find xX and yY such that (x,y) has the min weight among all such pairs • Add the edge to the tree and move y to X • Repeat
Prim’s Algorithm Input: weighted connected undirected G=(V,E) Output: MST T T { }; X {1 }; Y V - {1 }; While Y { } let (x,y) be the edge of min weight such that xX & yY X {y } X; Y Y - {y }; T T {(x,y)} End while
More Details X Y • N[y] is closest vertex adjacent to y in X • C[y]=c[N[y],y] cost of incident edge of y y C[y] N[y]
Prim’s Algorithm Input: weighted connected undirected G=(V,E) Output: MST T T { }; X {1 }; Y V - {1 }; For y 2 to n if y adjacent to 1 then N[y] 1; C[y] c[1,y] else C[y] end if End for For j 1 to n-1 let yY be s.t. C[y] is min T T {y, N[y] }; X X {y }; Y Y - {y }; for each vertex w Y if c[y,w] C[w] then N[w] y; C[w] c[y,w] end if end for End for
Analysis • Running Time = (m+ n2) • But can be improved by using the min-heap to find min C[y]. • Improved running time = (m log n) which is good if m=o(n2/log n)
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4
Example 7 2 4 1 3 5 4 2 6 1 7 1 3 2 3 5 4