170 likes | 320 Vues
Minimum Spanning Trees. Definition Algorithms Prim Kruskal Proofs of correctness. Problem Definition. Input Weighted, connected undirected graph G=(V,E) Weight (length) function w on each edge e in E Task Compute a spanning tree of G of minimum total weight Spanning tree
E N D
Minimum Spanning Trees • Definition • Algorithms • Prim • Kruskal • Proofs of correctness
Problem Definition • Input • Weighted, connected undirected graph G=(V,E) • Weight (length) function w on each edge e in E • Task • Compute a spanning tree of G of minimum total weight • Spanning tree • If there are n nodes in G, a spanning tree consists of n-1 nodes such that no cycles are formed
Kruskal’s algorithm • Greedy approach to edge selection • Select the minimum weight edge that does not form a cycle • Implementation • Sort edges by increasing lengths • Alternatively use heap to store edges • Either way: O(E log E) time • Include minimum weight edge if it does not form a cycle. • How do we test if edge forms a cycle?
1 4 2 A B C D 5 6 3 10 2 5 E F G Disjoint-set Data Structure • Use disjoint-set data structure (Chapter 21) • Each vertex belongs to a set containing the vertices in its current tree (initially only itself) • Find-set(A) = Find-set(B) = Find-set(E) = A • Find-set(C) = Find-set(D) = C • Find-set(F) = F • Find-set(G) = G
1 4 2 A B C D 5 6 3 10 2 5 E F G Cycle Detection • Take minimum weight edge and test both endpoints to see if they are in same set • Min weight edge: (A,E). Same Set. Cycle. • Min weight edge: (B,C): Different Sets. No cycle.
Update Disjoint-set Data Structure • Merge sets corresponding to node B and node C. • We need the result to now be: • Find-set(A) = Find-set(B) = Find-set(E) = Find-set(C) = Find-set(D) = A (or C) • Find-set(F) = F • Find-set(G) = G 1 4 2 A B C D 5 6 3 10 2 5 E F G
Time for Disjoint-set Data Structure Operations • Initialization: • Makeset(v) for all vertices v: O(V) time • Merges and Find-set Operations • O(E) of them • Each can be implemented in amortized a(V) time where a is a very slow growing function • O(E a(V)) time overall which is essentially O(E) for all practical purposes
Overall Running Time • Initialization: • Makeset(v) for all vertices v: O(V) time • Merges and Find-set Operations • O(E) of them • Each can be implemented in amortized a(V) time where a is a very slow growing function • O(E a(V)) time overall for cycle detection • O(E log E) time for sorting edges by weight dominates
Prim’s algorithm • Different greedy approach to edge selection • Initialize connected component N to be any node v • Select the minimum weight edge connecting N to V-N • Implementation • Maintain a priority queue for the nodes in V-N based on how close they are to any node in N • When a new node v is added to N, we need to update the weight of the neighbors of v in V-N
1 4 2 A B C D 5 6 3 10 2 5 E F G Priority Queue • Maintain priority queue of nodes in V-N • If we started with node D, N is now {C,D} • Priority Queue values of other nodes: • A, E, F: infinity • B: 4 • G: 6
1 4 2 A B C D 5 6 3 10 2 5 E F G Updating Priority Queue • Node B is added to N; edge (B,C) is added to T • Need to update priority queue values of A, E, F • Decrease-key operation • Priority Queue values of other nodes: • A: 1 • E: 2 • F: 5 • G: 6
Updating Priority Queue • Node A is added to N; edge (A,B) is added to T • Need to update priority queue values of E • Decrease-key operation • Priority Queue values of other nodes: • E: 2 (unchanged because 2 is smaller than 3) • F: 5 • G: 6 1 4 2 A B C D 5 6 3 10 2 5 E F G
Running time Analysis • Assume binary heap implementation • Build initial heap takes O(v) time • V extract-min operations for O(V log V) time • For each edge, potentially 1 decrease-key operation, so O(E log V) time • Overall: O(E log V) time which is asymptotically equivalent to our implementation of Kruskal’s algorithm • Use of fibonacci heap can improve running time to O(E + V log V) time • Decrease-key drops to O(1) amortized time
Proofs of Correctness (Kruskal) • Let T’ be a minimum spanning tree for G • Let T be the tree formed by Kruskal’s algorithm that utilizes edges in T’ whenever a tie needs to be broken • Assumption: T has more weight than T’ • Otherwise Kruskal’s algorithm has produced an optimal tree
Proofs of Correctness (Kruskal) • Let e be the smallest weight edge in T that is not in T’ • Add e to T’ and consider the cycle Y that is formed • There must be some edge e’ on Y that has weight greater than e. • Explain why? • What do we know about T and T’ up to this point?
Proofs of Correctness (Kruskal) • Replace e’ by e in T’ • We have a new spanning tree T’’ such that the weight of T’’ is smaller than the weight of T’ • This contradicts the fact that T’ was optimal. • This implies no such edge e can be found, and thus T must be optimal.
Proofs of Correctness (Prim) • Let T’ be a minimum spanning tree for G • Let T be the tree formed by Prim’s algorithm that utilizes edges in T’ whenever a tie needs to be broken • Let e be the first edge added to T that is not in T’ • Finish this argument