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

cs lunch
SMART_READER_LITE
LIVE PREVIEW

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:


slide-1
SLIDE 1

CS Lunch

Learn about the CS Department and CS major Today, 12:15 to 1:00

1

Midterm

No homework this week Midterm on Feb. 25 Covers chapters 1-3 Will post some sample questions

2 3 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-2
SLIDE 2

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

4

Interval Partitioning: Greedy Solution

Sort intervals by starting time so that s1 ≤ s2 ≤ ... ≤ sn. 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?

5

Scheduling to Minimize Lateness

Single computer processes one job at a time. Job j requires tj units of processing time Job j has a deadline dj by which it must be done If j starts at time sj, it finishes at time fj = sj + tj. Lateness: lj = max { 0, fj - dj }. Goal: schedule all jobs to minimize maximum lateness L = max lj.

1 ≤ j ≤ n

6 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-3
SLIDE 3

Scheduling Example

dj 6 tj 3 1 8 2 2 9 2 3 9 3 4 14 3 5 15 2 6

7

Scheduling Example: Earliest Deadline First

dj 6 tj 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

d4 = 9 d3 = 9

lateness = 1

Max lateness: 1

d1 = 6 d2 = 8 d5 = 14 d6 = 15

8

Minimizing Lateness: No Idle Time

  • Observation. There exists an optimal schedule with

no idle time.

1 2 3 4 5 6

d = 4 d = 6

7 8 9 10 11

d = 12

1 2 3 4 5 6

d = 4 d = 6

7 8 9 10 11

d = 12

9-1 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-4
SLIDE 4

Minimizing Lateness: No Idle Time

  • Observation. There exists an optimal schedule with

no idle time.

1 2 3 4 5 6

d = 4 d = 6

7 8 9 10 11

d = 12

1 2 3 4 5 6

d = 4 d = 6

7 8 9 10 11

d = 12

  • Observation. The greedy schedule has no idle time.

9-2

Minimizing Lateness: Inversions

An inversion in schedule S is a pair of jobs i and j such that di < dj but j is scheduled before i.

dj 6 tj 3 1 8 2 2 9 1 3 9 4 4 14 3 5 15 2 6

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

d4 = 9 d3 = 9 d1 = 6 d2 = 8 d5 = 14 d6 = 15

10

Minimizing Lateness: Inversions

  • Claim. Swapping two adjacent, inverted jobs i and j, where di <

dj, reduces the number of inversions by one and does not increase the max lateness.

i j i j

before swap after swap f'j fi inversion

11-1 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-5
SLIDE 5

Minimizing Lateness: Inversions

  • Claim. Swapping two adjacent, inverted jobs i and j, where di <

dj, reduces the number of inversions by one and does not increase the max lateness.

i j i j

before swap after swap f'j fi inversion

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?

11-2

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.

12

Shortest Path Problem

Shortest path network. Directed graph G = (V , E). Source s, destination t. Length le = length of edge e. le > 0 for all edges e. Shortest path problem: (the Google Maps problem!) find shortest directed path from s to t.

a e b d c f 1 1 1 1 1 2 3 4

13 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-6
SLIDE 6

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

a e b d c f 1 1 1 1 1 2 3 4

S dist=3 dist=2 dist=4

14 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 }

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 }

How do we implement this efficiently?

16 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-7
SLIDE 7

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 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)) } 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) }

Cost?

17 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.

18 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

19 Slides09 - GreedyAlgorithms.key - February 20, 2019

slide-8
SLIDE 8

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.

s u v x y

l(P) ≥ l(P’) + l(x,y) ≥ d(x) + l(x,y) ≥ d(y) ≥ d(v) P’

20

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

21 22 Slides09 - GreedyAlgorithms.key - February 20, 2019