Greedy Algorithms A greedy algorithm always makes the

  • Slides: 29
Download presentation
Greedy Algorithms: § A greedy algorithm always makes the choice that looks best at

Greedy Algorithms: § A greedy algorithm always makes the choice that looks best at the moment. § It makes a local optimal choice in the hope that this choice will lead to a globally optimal solution. § Greedy algorithms yield optimal solutions for many (but not all) problems. 1 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

The 0 -1 Knapsack problem: § The 0 -1 knapsack problem: § N items,

The 0 -1 Knapsack problem: § The 0 -1 knapsack problem: § N items, where the i-th item is worth vi dollars and weight wi pounds. 11 p 3 p 4 p 58 p 8 p 88 p l vi and wi are integers. 3$ 6$ 35$ 8$ § We can carry at most W (integer) pounds. § How to take as valuable a load as possible. l 66$ An item cannot be divided into pieces. § The fractional knapsack problem: § § 28$ The same setting, but the thief can take fractions of items. W may not be integer. W 2 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Solve the fractional Knapsack problem: § Greedy on the value per pound vi/wi. l

Solve the fractional Knapsack problem: § Greedy on the value per pound vi/wi. l Each time, take the item with maximum vi/wi. l § § If exceeds W, take fractions of the item. Example: (1, 5$), (2, 9$), (3, 12$), (3, 11$) and w=4. vi/wi : 5 4. 0 3. 667 First: (1, 5$), Second: (2, 9$), Third: 1/3 (3, 12$) Total W: 1, 3, 4. Can only take part of item 3 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

X Proof of correctness: (The hard part) Let X = i 1, i 2,

X Proof of correctness: (The hard part) Let X = i 1, i 2, …ik be the optimal items taken. § Consider the item j : (vj, wj) with the highest v /w. § if j is not used in X (the optimal solution), get rid of some items with total weight wj (possibly fractional items) and add item j. (since fractional items are allowed, we can do it. ) l Total value is increased. Why? i 1 w 1 i 2 wj. . . ip wp. One more item selected by greedy is added to X Repeat the process, X is changed to contain all items selected by greedy WITHOUT decreasing the total value taken by the thief. l § . . i kw k 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng 4

The 0 -1 knapsack problem cannot be solved optimally by greedy § Counter example:

The 0 -1 knapsack problem cannot be solved optimally by greedy § Counter example: (moderate part) § W=10 2 1. 8 § Items found (6 pounds, 12 dollars), (5 pounds, 9 dollar), 1. 8 1. 1 (5 pounds, 9 dollars), (3 pounds, 3 dollars), (3 pounds, 3 dollars) § If we first take (6, 12) according to greedy algorithm, then solution is (6, 12), (3, 3) (total value is 12+3=15). § However, a better solution is (5, 9), (5, 9) with total value 18. To show that a statement does not hold, we only have to give an example. 5 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

A subset of mutually compatibles jobs: {c, f} 6 2021/6/16 CS 4335 Design and

A subset of mutually compatibles jobs: {c, f} 6 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

7 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

7 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

8 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

8 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Sorting the n jobs based on fi needs O(nlog n) time 9 2021/6/16 CS

Sorting the n jobs based on fi needs O(nlog n) time 9 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Example: § Jobs (s, f): (0, 10), (3, 4), (2, 8), (1, 5), (4,

Example: § Jobs (s, f): (0, 10), (3, 4), (2, 8), (1, 5), (4, 8), (5, 6) (7, 9). Sorting based on fi: (3, 4) (1, 5), (4, 5) (5, 6) (4, 8) (2, 8) (7, 9)(0, 10). Selecting jobs: (3, 4) (4, 5) (5, 6) (7, 9) 10 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

11 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

11 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

12 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

12 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Sort ob finish time: b, c, a, e, d, f, g, h. Greedy algorithm

Sort ob finish time: b, c, a, e, d, f, g, h. Greedy algorithm Selects: b, e, h. 13 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Depth: The maximum No. of jobs required at any time. Depth: 3 14 2021/6/16

Depth: The maximum No. of jobs required at any time. Depth: 3 14 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

15 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

15 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Depth: The maximum No. of jobs required at any time. 16 2021/6/16 CS 4335

Depth: The maximum No. of jobs required at any time. 16 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Greedy on start time 17 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Greedy on start time 17 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Greedy Algorithm: c d b g j f i Depth: a. The maximum No.

Greedy Algorithm: c d b g j f i Depth: a. The maximum No. ofe jobs required at any h time. Depth: 3 18 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

d depth 19 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

d depth 19 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

20 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

20 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

21 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

21 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

l 1=0, l 2=1 l 2=0, l 1=0 0 10 11 l 1=9, l

l 1=0, l 2=1 l 2=0, l 1=0 0 10 11 l 1=9, l 2=0 l 1=0, l 2=1 0 1 11 22 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

23 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

23 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

24 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

24 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

di<dj n 1 n 2 … nk Example: 1, 2, 3, 4, 5, 6,

di<dj n 1 n 2 … nk Example: 1, 2, 3, 4, 5, 6, 7, 8, 9 1, 2, 6, 7, 3, 4, 5, 8, 9 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng We check every pair of consecutive numbers, if there is not inversion, then the whole sequenc has no inversion. 25

di<dj 26 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

di<dj 26 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

27 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

27 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

Job 1 2 3 4 5 j 1 2021/6/16 2 j 2 Example: di

Job 1 2 3 4 5 j 1 2021/6/16 2 j 2 Example: di 2 4 6 8 10 ti 2 3 4 4 6 5 j 3 9 j 4 13 j 5 19 CS 4335 Design and Analysis of Algorithms/WANG Lusheng 28

29 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng

29 2021/6/16 CS 4335 Design and Analysis of Algorithms/WANG Lusheng