Greed Greed is good Greed is right Greed

  • Slides: 24
Download presentation
Greed "Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and

Greed "Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. " Gordon Gecko (Michael Douglas) Some of these lecture slides are adapted from Kleinberg and Tardos.

Greedy Algorithms Some possibly familiar examples: n Gale-Shapley stable matching algorithm. n Dijkstra's shortest

Greedy Algorithms Some possibly familiar examples: n Gale-Shapley stable matching algorithm. n Dijkstra's shortest path algorithm. n Prim and Kruskal MST algorithms. n Huffman codes. n . . . 2

Selecting Breakpoints Minimizing breakpoints. n Truck driver going from Princeton to Palo Alto along

Selecting Breakpoints Minimizing breakpoints. n Truck driver going from Princeton to Palo Alto along predetermined route. n Refueling stations at certain points along the way. n Truck fuel capacity = C. Greedy algorithm. n Go as far as you can before refueling. C 1 Princeton C 2 C 3 C C 4 5 C 6 C 7 Palo Alto 3

Selecting Breakpoints: Greedy Algorithm Greedy Breakpoint Selection Algorithm Sort breakpoints by increasing value: 0

Selecting Breakpoints: Greedy Algorithm Greedy Breakpoint Selection Algorithm Sort breakpoints by increasing value: 0 = b 0 < b 1 < b 2 <. . . < bn. S {0} S = breakpoints selected. x = 0 while (x bn) let p be largest integer such that bp x + C if (bp = x) return "no solution" x bp S S {p} return S 4

Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let

Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let 0 = g 0 < g 1 <. . . < gp = L denote set of breakpoints chosen by greedy and assume it is not optimal. Let 0 = f 0 < f 1 <. . . < fq = L denote set of breakpoints in optimal solution with f 0 = g 0, f 1= g 1 , . . . , fr = gr for largest possible value of r. Note: q < p. g 0 g 1 g 2 gr Greedy: 1 2 3 4 OPT: 1 2 3 4 f 0 f 1 f 2 gp 5 5 fr 6 6 7 8 9 7 fq r=4 5

Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let

Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let 0 = g 0 < g 1 <. . . < gp = L denote set of breakpoints chosen by greedy and assume it is not optimal. Let 0 = f 0 < f 1 <. . . < fq = L denote set of breakpoints in optimal solution with f 0 = g 0, f 1= g 1 , . . . , fr = gr for largest possible value of r. Note: q < p. g 0 g 1 g 2 gp Greedy: 1 2 3 4 5 OPT: 1 2 3 4 55 f 0 f 1 6 6 f 2 7 8 9 7 fq r=4 r=5 6

Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): n n Let 0

Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): n n Let 0 = g 0 < g 1 <. . . < gp = L denote set of breakpoints chosen by greedy and assume it is not optimal. Let 0 = f 0 < f 1 <. . . < fq = L denote set of breakpoints in optimal solution with f 0 = g 0, f 1= g 1 , . . . , fr = gr for largest possible value of r. Note: q < p. Thus, f 0 = g 0, f 1= g 1 , . . . , fq = gq g 0 g 1 gq g 2 Greedy: 1 2 3 4 5 OPT: 1 2 3 4 5 f 0 f 1 f 2 gp 6 7 fq r=q=5 7

Activity Selection Activity selection problem (CLR 17. 1). n n Job requests 1, 2,

Activity Selection Activity selection problem (CLR 17. 1). n n Job requests 1, 2, … , n. Job j starts at sj and finishes at fj. n Two jobs compatible if they don't overlap. n Goal: find maximum subset of mutually compatible jobs. A B C D E F G H 0 1 2 3 4 5 6 7 8 9 10 11 Time 8

Activity Selection: Greedy Algorithm Greedy Activity Selection Algorithm Sort jobs by increasing finish times

Activity Selection: Greedy Algorithm Greedy Activity Selection Algorithm Sort jobs by increasing finish times so that f 1 f 2 . . . fn. S = jobs selected. S = for j = 1 to n if (job j compatible with A) S S {j} return S 9

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let g 1, g 2, . . . gp denote set of jobs selected by greedy and assume it is not optimal. Let f 1, f 2, . . . fq denote set of jobs selected by optimal solution with f 1 = g 1, f 2= g 2, . . . , fr = gr for largest possible value of r. Note: r < q. p=6 Greedy: 1 f 1 = g 1 5 f 2 = g 2 8 f 3 = g 3 OPT: 1 5 8 r=3 9 13 11 21 15 17 21 q=7 10

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let g 1, g 2, . . . gp denote set of jobs selected by greedy and assume it is not optimal. Let f 1, f 2, . . . fq denote set of jobs selected by optimal solution with f 1 = g 1, f 2= g 2, . . . , fr = gr for largest possible value of r. Note: r < q. p=6 Greedy: 1 f 1 = g 1 5 f 2 = g 2 8 f 3 = g 3 OPT: 1 5 8 9 9 11 13 21 15 r=3 17 21 q=7 Replace 11 with 9 11

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let g 1, g 2, . . . gp denote set of jobs selected by greedy and assume it is not optimal. Let f 1, f 2, . . . fq denote set of jobs selected by optimal solution with f 1 = g 1, f 2= g 2, . . . , fr = gr for largest possible value of r. Note: r < q. p=6 Greedy: 1 f 1 = g 1 5 f 2 = g 2 8 f 3 = g 3 9 OPT: 1 5 8 9 13 21 15 r=3 17 21 q=7 Replace 11 with 9 12

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let

Activity Selection Theorem: greedy algorithm is optimal. Proof (by contradiction): n n n Let g 1, g 2, . . . gp denote set of jobs selected by greedy and assume it is not optimal. Let f 1, f 2, . . . fq denote set of jobs selected by optimal solution with f 1 = g 1, f 2= g 2, . . . , fr = gr for largest possible value of r. Note: r < q. p=6 Greedy: 1 f 1 = g 1 5 f 2 = g 2 8 f 3 = g 3 9 OPT: 1 5 8 9 r=3 r=4 13 21 15 17 21 q=7 13

Making Change Given currency denominations: 1, 5, 10, 25, 100, devise a method to

Making Change Given currency denominations: 1, 5, 10, 25, 100, devise a method to pay amount to customer using fewest number of coins. n Ex. 34¢. Greedy algorithm. n n At each iteration, add coin of the largest value that does not take us past the amount to be paid. Ex. $2. 89. 14

Coin-Changing: Greedy Algorithm Greedy Coin-Changing Algorithm Sort coins denominations by increasing value: c 1

Coin-Changing: Greedy Algorithm Greedy Coin-Changing Algorithm Sort coins denominations by increasing value: c 1 < c 2 <. . . < cn. S = coins selected. S while (x 0) let p be largest integer such that cp x if (p = 0) return "no solution found" x x - cp S S {p} return S 15

Is Greedy Optimal for Coin-Changing Problem? Yes, for U. S. coinage: {c 1, c

Is Greedy Optimal for Coin-Changing Problem? Yes, for U. S. coinage: {c 1, c 2, c 3, c 4, c 5 } = {1, 5, 10, 25, 100}. Ad hoc proof. n n n Consider optimal way to change amount ck x < ck+1. Greedy takes coin k. Suppose optimal solution does not take coin k. – it must take enough coins of type c 1, c 2, . . . , ck-1 to add up to x. Max # taken by Max value of coins optimal solution 1, 2, . . . , k in any OPT k ck 1 1 4 4 2 5 1 4+5=9 3 10 2 20 + 4 = 24 4 25 3 75 + 24 = 99 5 100 no limit 2 dimes no nickels 16

Does Greedy Always Work? US postal denominations: 1, 10, 21, 34, 70, 100, 350,

Does Greedy Always Work? US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, 1500. n Ex. 140¢. n Greedy: 100, 34, 1, 1, 1. n Optimal: 70, 70. 17

Characteristics of Greedy Algorithms Greedy choice property. n n Globally optimal solution can be

Characteristics of Greedy Algorithms Greedy choice property. n n Globally optimal solution can be arrived at by making locally optimal (greedy) choice. At each step, choose most "promising" candidate, without worrying whether it will prove to be a sound decision in long run. Optimal substructure property. n Optimal solution to the problem contains optimal solutions to subproblems. – if best way to change 34¢ is {25, 5, 1, 1} then best way to change 29¢ is {25, 1, 1}. Objective function does not explicitly appear in greedy algorithm! Hard, if not impossible, to precisely define "greedy algorithm. " n See matroids (CLR 17. 4), greedoids for very general frameworks. 18

Minimizing Lateness Minimizing lateness problem. Single resource can process one job at a time.

Minimizing Lateness Minimizing lateness problem. Single resource can process one job at a time. n n jobs to be processed. – job j requires pj units of processing time. – job j has due date dj. n n If we assign job j to start at time sj, it finishes at time fj = sj + pj. n Lateness: j = max { 0, fj - dj }. n Goal: schedule all jobs to minimize maximum lateness L = max j. d=9 Lateness = 3 d=9 0 1 d=8 2 d=8 d=2 3 d = 15 4 d=6 5 6 7 d = 11 d=9 d = 11 8 9 10 11 d=9 12 13 14 15 19

Minimizing Lateness: Greedy Algorithm Greedy Activity Selection Algorithm Sort jobs by increasing deadline so

Minimizing Lateness: Greedy Algorithm Greedy Activity Selection Algorithm Sort jobs by increasing deadline so that d 1 d 2 … dn. t = 0 for j = 1 to n Assign job j to interval [t, t + pj] sj t, fj t + pj t t + pj output intervals [sj, fj] max lateness = 2 d 1 = 6 0 1 2 d 2 = 8 3 4 d 4 = 9 d 3 = 9 5 6 7 8 9 10 d 5 = 11 d 6 = 15 11 12 13 14 15 20

Minimizing Lateness: No Idle Time Fact 1: there exists an optimal schedule with no

Minimizing Lateness: No Idle Time Fact 1: there exists an optimal schedule with no idle time. d=4 0 1 d=6 2 d=4 0 1 3 4 5 d=6 2 3 4 d = 12 6 7 8 9 10 11 d = 12 5 6 7 Fact 2: the greedy schedule has no idle time. 21

Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i

Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i and j such that: n i<j n j scheduled before i inversion d 5 = 8 d 2 = 4 Fact 3: greedy schedule no inversions. Fact 4: if a schedule (with no idle time) has an inversion, it has one whose with a pair of inverted jobs scheduled consecutively. 22

Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i

Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i and j such that: n i<j n j scheduled before i inversion d 5 = 8 d 2 = 4 d 5 = 8 Fact 3: greedy schedule no inversions. Fact 4: if a schedule (with no idle time) has an inversion, it has one whose with a pair of inverted jobs scheduled consecutively. Fact 5: swapping two adjacent, inverted jobs: n Reduces the number of inversions by one. n Does not increase the maximum lateness. Theorem: greedy schedule is optimal. 23

Minimizing Lateness: Proof of Fact 5 An inversion in schedule S is a pair

Minimizing Lateness: Proof of Fact 5 An inversion in schedule S is a pair of jobs i and j such that: n i<j n j scheduled before i fi j i i j f'j Swapping two adjacent, inverted jobs does not increase max lateness. n 'k = k for all k i, j n 'i li n If job j is late: 24