520 likes | 750 Views
Design and Analysis of Computer Algorithm Lecture 5-2. Pradondet Nilagupta Department of Computer Engineering. This lecture note has been modified from lecture note for 23250 by Prof. Francis Chin , CS332 by David Luekbe. Greedy Method (Cont.). TREE.
E N D
Design and Analysis of Computer AlgorithmLecture 5-2 Pradondet Nilagupta Department of Computer Engineering This lecture note has been modified from lecture note for 23250 by Prof. Francis Chin , CS332 by David Luekbe
TREE • A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G • G (V,E) is connected. • G (V,E) is acyclic. • |E| =|V| -1
Spanning Trees • A spanning tree in an undirected graph G(V,E) is a subset of edges that are acyclic and connect all the vertices in V. • It follows from the above conditions that a spanning tree must consist of exactly n-1 edges. • Now suppose that each edge has a weight associated with it: Say that the weight of a tree Tis the sum of the weights of its edges;
1 1 1 28 28 28 10 10 2 2 2 16 16 16 14 14 14 6 6 6 3 3 3 7 7 7 24 24 25 24 18 18 5 5 5 12 22 22 4 4 4 connected graph Example: Spanning Tree spanning tree cost = 129 spanning tree cost = 110
Definition • The minimum spanning tree in a weighted graph G(V,E) is one which has the smallest weight among all spanning trees in G(V,E).
A Greedy Strategy Generic-MST(G, w) A = {} // Invariant: A is a subset of some MST whileA does not form a spanning tree do find an edge (u, v) that is safefor A A = A + {(u, v)} // A remains a subset of some MST return A How to find a safe edge?
Example • Example: spanning tree T {(a,b), (a,c), (b,d), (d,e), (d,f)} • w(T) = 4+8+8+7+3 = 30 • T* = {(a,b), (b,e), (c,e), (c,d), (d,f)} • w(T*) = 4+7+1+2+3 = 17 (minimum ?, why ? see note 1)
Method 1: Brute force • Enumerate all spanning trees, evaluate their costs and find the minimum. • Drawback - the number of spanning trees must be very large, how large? • Let Ns be the number of spanning trees for an n-node graph. • lower bound, (the least value of Ns): n!/2 for a chain, why? • upper bound, (the largest value of Ns): Ce,n-1, where e = n(n-1)/2, i.e. O(n (n-1) )
Method 2: Removing redundant edges • Edge of the largest weight is removed if the resultant graph remains connected. Repeat the edge removal until not possible. • Correctness: If the largest-weight edge was not removed, it can always be replaced by an edge of lower weight because of the redundancy. • Time complexity: • sorting the edges - O(n2 logn) or O(elogn) • testing connectivity - O(n2 ) or O(e) • connectivity test for all edges - O(n4 ) or O(e2 )
Minimum Spanning Trees • TheoremGiven a spanning forest which is a subset of the MST, the minimum weight edge (u, v) emerging from a tree in the forest must be "safe", i.e., an edge in the MST.
Proof: • Assume that the minimum weight edge (u, v) is not in MST. Let us add edge(u, v) to the MST to result a unique cycle which must contain another edge (u', v'), where u and u' belong to the some tree. As edge (u, v) is shorter than edge (u', v'), the weight of the new MST containing (u, v) and not (u', v') should be lower than the weight of the original MST. This contradicts our assumption that (u, v) is not in the MST.
Method 3: Ordered edge insertion (Krushal) • The edge with minimum weight is added to the "forest" as long as it is not redundant, i.e., it connects two trees in the forest. Repeat the edge insertion until not possible. • Correctness: The proof follows directly from the theorem and can be proved by induction. Initially, the forest has n trees each of which contains a single node of G. The edge with minimum weight edge which is not redundant, i.e., emerging from some trees in the forest, must be a minimum weight edge in the MST.
Time Complexcity • Time Complexity: sorting the edges - O(n 2 logn) or O(elogn)
Minimum Spanning Tree (MST) 7 a d 2 2 5 4 b 5 f g 1 1 4 3 c 4 7 e Problem Select edges in a connected graph to Connect all the vertices Minimize the total edge weight // must be a tree
a 7 d 2 2 5 4 b 5 f g 1 1 4 3 c 4 7 e Cut A cut of G = (V, E) is a partition (S, V-S) of V. Ex. S = {a, b, c, f}, V-S = {e, d, g} Edges {b, d}, {a, d}, {b, e}, {c, e} all cross the cut. The remaining edges do not cross the cut.
a 7 d 2 2 5 4 b 5 f g 1 1 4 3 c 4 7 e Respected by a Cut A1 = {(a, b), (d, g), (f, b), (a, f)} A2 = A1 + {(b, d)} Ex. S = {a, b, c, d} The cut (S, V-S) respectsA1 but not A2.
Light Edge The edge that crosses a cut with the minimum weight. Ex. S = {a, b, c, d} a 7 d 2 2 5 4 b 5 f g 1 1 3 4 light edge c 4 7 e Edge {b, e} has weight 3, “lighter” than the other edges {a, d}, {b, d}, and {b, e} that cross the cut.
Light Edge Is Safe! Theorem Let (S, V-S) be any cut of G(V, E) that respects a subset A of E, which is included in some MST for G. Let (u, v) be a light edge crossing (S, V-S). Then (u, v) is safe for A; that is, A + {(u, v)} is also included in some MST. A includes all red edges. 4 v 2 S V- S 6 u
v 4 7 u C Corollary Let A be a subset of E that is included in some MST of G, and C be a connected component in the forest G = <V, A>. If (u, v) is a light edge connecting C to some other component in G , then (u, v) is safe for A. A A
Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph: 6 4 5 9 14 2 10 15 3 8
Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 6 4 5 9 14 2 10 15 3 8
Minimum Spanning Tree • Which edges form the minimum spanning tree (MST) of the below graph? A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F
Minimum Spanning Tree • Answer: A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F
Minimum Spanning Tree • MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees • Let T be an MST of G with an edge (u,v) in the middle • Removing (u,v) partitions T into two trees T1 and T2 • Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2) (Do V1 and V2 share vertices? Why?) • Proof: w(T) = w(u,v) + w(T1) + w(T2)(There can’t be a better tree than T1 or T2, or T would be suboptimal)
Minimum Spanning Tree • Thm: • Let T be MST of G, and let A T be subtree of T • Let (u,v) be min-weight edge connecting A to V-A • Then (u,v) T
Minimum Spanning Tree • Thm: • Let T be MST of G, and let A T be subtree of T • Let (u,v) be min-weight edge connecting A to V-A • Then (u,v) T • Proof: in book (see Thm 24.1)
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 r 0 3 8 Pick a start vertex r
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 u 0 3 8 Red vertices have been removed from Q
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 u 0 3 8 3 Red arrows indicate parent pointers
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 u 0 3 8 3
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 0 3 8 3 u
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 0 8 15 3 8 3 u
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 10 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 u 14 2 10 15 0 8 15 3 8 3
Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?
Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));