cs lunch
play

CS Lunch Learn about the CS Department and CS major Today, 12:15 to - PowerPoint PPT Presentation

1 CS Lunch Learn about the CS Department and CS major Today, 12:15 to 1:00 2 Midterm No homework this week Midterm on Feb. 25 Covers chapters 1-3 Will post some sample questions 3 Slides09 - GreedyAlgorithms.key - February 20, 2019 4 Review:


  1. 1 CS Lunch Learn about the CS Department and CS major Today, 12:15 to 1:00 2 Midterm No homework this week Midterm on Feb. 25 Covers chapters 1-3 Will post some sample questions 3 Slides09 - GreedyAlgorithms.key - February 20, 2019

  2. 4 Review: Hallmark of a Greedy Algorithm Sort data according to some criteria Consider each piece of data in sorted order and make a local decision Result is globally optimal (if this problem is amenable to a greedy solution!) Complexity is generally no better than O(n log n) due to the sort Important to prove that the solution is optimal 5 Interval Partitioning: Greedy Solution Sort intervals by starting time so that s 1 ≤ s 2 ≤ ... ≤ s n . d ← 0 / / Number of classrooms for j = 1 to n { if (lecture j is compatible with some classroom k) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d ← d + 1 } Complexity? 6 Scheduling to Minimize Lateness Single computer processes one job at a time. Job j requires t j units of processing time Job j has a deadline d j by which it must be done If j starts at time s j , it finishes at time f j = s j + t j . Lateness: l j = max { 0, f j - d j }. Goal: schedule all jobs to minimize maximum lateness L = max l j . 1 ≤ j ≤ n Slides09 - GreedyAlgorithms.key - February 20, 2019

  3. 7 Scheduling Example 1 2 3 4 5 6 t j 3 2 2 3 3 2 d j 6 8 9 9 14 15 8 Scheduling Example: Earliest Deadline First 1 2 3 4 5 6 t j 3 2 1 4 3 2 d j 6 8 9 9 14 15 lateness = 1 d 1 = 6 d 2 = 8 d 3 = 9 d 4 = 9 d 5 = 14 d 6 = 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Max lateness: 1 9-1 Minimizing Lateness: No Idle Time Observation. There exists an optimal schedule with no idle time. d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 Slides09 - GreedyAlgorithms.key - February 20, 2019

  4. 9-2 Minimizing Lateness: No Idle Time Observation. There exists an optimal schedule with no idle time. d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 Observation. The greedy schedule has no idle time. 10 Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i and j such that d i < d j but j is scheduled before i. 1 2 3 4 5 6 t j 3 2 1 4 3 2 d j 6 8 9 9 14 15 d 1 = 6 d 3 = 9 d 2 = 8 d 4 = 9 d 5 = 14 d 6 = 15 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 11-1 Minimizing Lateness: Inversions Claim. Swapping two adjacent, inverted jobs i and j, where d i < d j , reduces the number of inversions by one and does not increase the max lateness. inversion f i before swap j i after swap i j f' j Slides09 - GreedyAlgorithms.key - February 20, 2019

  5. 11-2 Minimizing Lateness: Inversions Claim. Swapping two adjacent, inverted jobs i and j, where d i < d j , reduces the number of inversions by one and does not increase the max lateness. inversion f i before swap j i after swap i j f' j Observation: i gets earlier, so its lateness can only decrease. Observation: The lateness of requests other than i and j do not change. What about j’ s lateness? 12 Proof Strategies for Greedy Algorithms Greedy algorithm stays ahead. Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithm's. Exchange argument. Gradually transform any solution to the one found by the greedy algorithm without hurting its quality. 13 Shortest Path Problem Shortest path network. Directed graph G = (V , E). Source s, destination t. Length l e = length of edge e. l e > 0 for all edges e. Shortest path problem: (the Google Maps problem!) find shortest directed path from s to t. 1 a c 2 3 1 1 1 e f 4 b d 1 Slides09 - GreedyAlgorithms.key - February 20, 2019

  6. 14 Dijkstra’ s Algorithm Dijkstra's algorithm. Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0. Repeatedly choose unexplored node v which is the minimum distance from s S = S ∪ {v} d(v) = minimum distance from s to v 1 dist=3 a c 2 3 1 dist=4 S 1 1 e f 4 b d 1 dist=2 15 Dijkstra’ s Algorithm: Implementation Dijkstra’ s Algorithm (G, s) { S = {s} / / S is the set of explored nodes d(s) = 0 / / d is the distance to the node from s while S != V { / / there are unexplored nodes select a node v from V-S with an edge from S for which the distance from s to v is the minimum of all paths to any node in V-S add v to S d(v) = minimum distance from s to v } 16 Dijkstra’ s Algorithm: Implementation Dijkstra’ s Algorithm (G, s) { S = {s} / / S is the set of explored nodes d(s) = 0 / / d is the distance to the node from s while S != V { / / there are unexplored nodes select a node v from V-S with an edge from S for which the distance from s to v is the minimum of all paths to any node in V-S add v to S d(v) = minimum distance from s to v } How do we implement this efficiently? Slides09 - GreedyAlgorithms.key - February 20, 2019

  7. Dijkstra’ s Algorithm (G, s) { 17 S = {s} / / S is the set of explored nodes d(s) = 0 / / d is the distance to the node from s lastNode = s while S != V { / / there are unexplored nodes for each edge (lastNode, v) where v is in V-S { dist_to_v = d(lastNode) + l(lastNode, v) if d’(v) is unknown { d’(v) = dist_to_v heap.addElement (v, d’(v)) } Cost? else if dist_to_v < d’(v) { d’(v) = dist_to_v heap.changeKey(v, d’(v)) } } lastNode = heap.extractMin() add lastNode to S d(lastNode) = d’(lastNode) } 18 Dijkstra’ s Algorithm: Proof of Correctness Invariant. For each node u ∈ S, d(u) is the length of the shortest s-u path. Proof (by induction on |S|) Base case: |S| = 1 is trivial. 19 Dijkstra’ s Algorithm: Proof of Correctness Inductive hypothesis: Assume true for |S| = k ≥ 1. Let v be next node added to S, and let u-v be the chosen edge. The shortest s-u path plus (u, v) is an s-v path of length 
 d(u)+l(u,v). s u v Slides09 - GreedyAlgorithms.key - February 20, 2019

  8. 20 Dijkstra’ s Algorithm: Proof of Correctness Inductive hypothesis (cont.): Consider any s-v path P (the yellow edge). We'll see that it's no shorter than d(u)+l(u,v). Let x-y be the first edge in P that leaves S, 
 and let P' be the subpath to x. P’ x y s u v l(P) ≥ l(P’) + l(x,y) ≥ d(x) + l(x,y) ≥ d(y) ≥ d(v) 21 Edsger W . Dijkstra The question of whether computers can think is like the question of whether submarines can swim. A. Nico Habermann First dean of the School of Computer Science at CMU CRA A. Nico Habermann Award given to a person making outstanding contributions to increasing participation of underrepresented minorities in CS 22 Slides09 - GreedyAlgorithms.key - February 20, 2019

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend