CS252 Algorithms Friday, 3 May 2024 + Ad hoc office hour, in this room after class through about 11:15 + Stuff I would think about while studying - What's a tree? - What's a spanning tree? - * What's a minimum spanning tree? - Interval scheduling - What problem is it trying to solve? - Write down the algorithm in pseudocode - What's the runtime O(?). Show why in the pseudocode. - Create an example/instance of the problem and run it by hand - What's the core of the correctness proof? - Dijkstra: same - * Kruskal: same - Prim: same - Reverse-Delete: same - What's a heap? What operations does a heap support, and how (and why) do we use them in Dijkstra? - What's a Union-Find data structure? What operations does it have? How and why do we use it in Kruskal? - Does Dijkstra work for undirected graphs, directed, or what? - Why do negative weights give Dijkstra trouble? - * If you add 10 to all the edge weights in G, do the MSTs for G change? - * If you add 10 to all the edge weights in G, do the shortest paths change? - Interval scheduling - If you sort the intervals by finish time and take them in order (eliminating conflicting intervals after each iteration), does that optimize the # of intervals in the resulting schedule? - * Same, but sort by start time - Same, but sort by interval length - Same three questions, but now try to optimize total interval length - What's a cut? What's the Cut Property? - What's a cycle? What's the Cycle Property? - Make yourself a weighted graph G - Find an edge that has to be in all MSTs. Explain why. - Find an edge that can't be in any MSTs. Explain why. - ...what other items in the style above should you think about?... ======= What's a minimum spanning tree? - "created from a connected graph, includes all the graph nodes" - "a spanning tree with the lowest possible total edge weight" Given a graph G = (V,E) with edge weights W a MST of G is a subgraph of G [the "of G" part] that is a tree [the "T" part] that includes all of the nodes in V [the "S" part] whose total edge weight is minimum among all the spanning trees of G Kruskal: - What's the problem we're trying to solve Given a connected graph, find an MST - What does the algorithm look like? Search for the edge e of lowest weight Add e to T if e doesn't cause T to have a cycle Keep going until you either have n-1 edges in T or you're out of edges T = (V, E') E' = empty set Sort edges in E by nondecreasing weight for e in E: if T = (V,E' U {e}) does not have a cycle: E' = E' U {e} return T - What's its runtime ======= Divide-and-conquer + Basic analysis plan - Be clear what the problem is - Articulate a recursive algorithm to solve the problem - Let T(n) count something relevant. ("Running time" is OK, but sometimes it's easier to be more concrete, like "additions" or "comparisons") - Derive from the algorithm a recurrence for T(n) Treat it as an equation if you want, or an inequality if you prefer - Solve the recurrence - Try unrolling first; see if you can guess a general form - Try proving your guess by induction - Are you old and jaded from too many induction proofs? See if the Master Theorem applies. + Example 1: sum of an array of integers (splitting in half) Problem: given an array of integers a[0...N-1], compute the sum of all N integers sum1(a, start_index, end_index) if end_index < start_index: return 0 elif end_index == start_index: return a[start_index] else: middle_index = (start_index + end_index) // 2 left_sum = sum(a, start_index, middle_index) right_sum = sum(a, middle_index + 1, end_index) return left_sum + right_sum Let T(n) = the worst-case running time of sum1(a, 0, n-1) Let T(n) = the worst-case number of additions in sum1(a, 0, n-1) Let T(n) = the worst-case number of ??? in sum1(a, 0, n-1) T(1) = C T(n) = ??? or T(1) <= C T(n) <= ???