=========================================================================== CSC 373H / L0101 Lecture Summary for Week 2 Winter 2005 =========================================================================== Activity Scheduling (cont'd). - Recall greedy algorithm for Activity Scheduling: sort by finish time. - S_0, S_1, ..., S_n = partial schedules constructed by algo. - Prove by induction on i (# iterations) that S_i is "promising" with respect to {A_(i+1),....,A_n}, i.e., there is optimal solution S'_i that "extends" S_i using {A_(i+1),...,A_n} (S_i subset of S'_i and S'_i subset of S_i U {A_(i+1),...,A_n}). . Base case: S_0 = {} so any optimal solution S'_0 extends S_0 using {A_1,...,A_n}. . Ind. Hyp.: For some i >= 0, assume there is optimal S'_i that extends S_i using {A_(i+1),...,A)n}. . Ind. Step: To prove: S_(i+1) is promising w.r.t. {A_(i+2),...,A_n}. Case 1: S_(i+1) = S_i This means A_(i+1) not compatible with S_i, so S'_(i+1) = S'_i extends S_(i+1) using {A_(i+2),...,A_n}. Case 2: S_(i+1) = S_i U {A_(i+1)} Subcase a: A_(i+1) in S'_i Then S'_(i+1) = S'_i extends S_(i+1) using {A_(i+2),...,A_n}. Subcase b: A_(i+1) not in S'_i Then there must be A_j in S'_i that overlaps A_(i+1) (otherwise, S'_i U A_(i+1) would be better than optimal S'_i). Also, j > i+1 because A_(i+1) is compatible with S_i. S'_(i+1) = S'_i U {A_(i+1)} - {A_j} extends S_(i+1) using {A_(i+2),...,A_n}: it contains same number of activities as S'_i, and no overlap introduced because f(i+1) <= f(j) (by sorting order). In all cases, there is optimal S'_(i+1) that extends S_(i+1) using {A_(i+2),...,A_n}. - So each S_i is promising. In particular, S_n is promising w.r.t. {}, i.e., there is optimal S'_n that "extends" S_n using activities from {}. In other words, S_n must be optimal itself. Minimum Spanning Tree. Input: Connected undirected graph G=(V,E) with positive cost c(e) > 0 for each edge e in E. Output: A spanning tree T subset of E such that cost(T) (sum of the costs of edges in T) is minimal. - Terminology: . "Spanning tree": acyclic connected subset of edges. . "Acyclic": does not contain any cycle. . "Connected": contains a path between any two vertices. A. Brute force: consider each possible subset of edges. Runtime? Exponential, even if we limit search to spanning trees of G. B. Kruskal's algorithm: // let m = |E| (# edges) and n = |V| (# vertices) sort edges by cost, i.e., c(e_1) <= c(e_2) <= ... <= c(e_m) T := {} // partial spanning tree for each v in V: MakeSet(v) // initialize disjoint sets for i := 1 to m: let (u,v) := e_i if FindSet(u) != FindSet(v): // u,v not already connected T := T U {e_i} Union(u,v) return T Runtime? Theta(m log m) for sorting; main loop involves sequence of m Union and FindSet operations on n elements which is Theta(m log n). Total is Theta(m log n) since log m is Theta(log n). Correctness? Let T_0, T_1, ..., T_m be partial solutions created by algo. Prove that T_i is promising for each i, by induction. . Base case: Any MST T'_0 extends T_0 = {}. . Ind. Hyp.: For some i >= 0, assume there is some MST T'_i that extends T_i using {e_(i+1),...,e_m}. . Ind. Step: To prove T_(i+1) is promising, consider cases. Case 1: T_(i+1) = T_i This means endpoints of e_(i+1) already connected in T_i so T'_i does not contain e_(i+1). Then T'_(i+1) = T'_i extends T_(i+1) using {e_(i+2),...,e_m}. Case 2: T_(i+1) = T_i U {e_(i+1)} Subcase a: e_(i+1) in T'_i Then T'_(i+1) = T'_i extends T_(i+1) using {e_(i+2),...,e_m}. Subcase b: e_(i+1) not in T'_i Let (u,v) = e_(i+1). T'_i contains a path from u to v; this path must contain at least one edge e_j not in T_i (otherwise e_(i+1) would not be added by algo), for some j >= i+2. Let T'_(i+1) = T'_i U {e_(i+1)} - {e_j}. T'_(i+1) is a spanning tree: adding edge e_(i+1) to T'_i creates exactly one cycle, broken by removing edge e_j. cost(T'_(i+1)) <= cost(T'_i) because c(e_(i+1)) <= c(e_j) by sorting order, so T'_(i+1) is a MST. In all cases, there is a MST T'_(i+1) that extends T_(i+1) using {e_(i+2),...,e_m}. At the end, "T_m is promising" equivalent to "T_m is a MST", i.e., algorithm returns a MST. C. Prim's algorithm: Idea: start with some vertex s in V (pick arbitrarily) and at each step, add lowest-cost edge that connects a new vertex. Proof: similar to Kruskal's.