Presentation is loading. Please wait.

Presentation is loading. Please wait.

Greedy Algorithms: Maximizing Loot

Similar presentations


Presentation on theme: "Greedy Algorithms: Maximizing Loot"— Presentation transcript:

1 Greedy Algorithms: Maximizing Loot
Algorithms and Data Structures Greedy Algorithms: Maximizing Loot Artem A. Golubnichiy Department of Software of Computer Facilities and Automated Systems Katanov Khakass State University

2 STRUCTURE OF THE CLASS Maximizing Loot Pseudocode and Running Time

3 MAXIMIZING LOOT

4 MAXIMIZING LOOT

5 MAXIMIZING LOOT Fractional knapsack Input:
Weights w1, , wn and values v1, , vn of n items; capacity W. Output: The maximum total value of fractions of items that fit into a knapsack of capacity W.

6 Example $30 $28 $24 5 4 3 9 knapsack

7 Example $30 $28 $24 5 4 3 $30 $28 5 4 total: $58 knapsack

8 Example $30 $28 $24 5 4 3 $30 $24 $7 5 3 1 total: $61 knapsack

9 Example $30 $28 $24 5 4 3 $24 $28 $12 3 4 2 total: $64 knapsack

10 SAFE CHOICE Lemma There exists an optimal solution that uses as much as possible of an item with the maximal value per unit of weight.

11 Proof $30 $28 $24 5 4 3 $6/unit $7/unit $8/unit

12 Proof $30 $28 $24 5 4 3 $6/unit $7/unit $8/unit $30 $28 5 4 total: $58

13 5 4 3 3 2 4 Proof $30 $28 $24 $6/unit $7/unit $8/unit $6*3 $6*2 $28
total: $58

14 5 4 3 3 2 4 3 2 4 Proof $30 $28 $24 $6/unit $7/unit $8/unit $6*3 $6*2
total: $58 $8*3 $6*2 $28 3 2 4 total: $64

15 GREEDY ALGORITHM While knapsack is not full

16 GREEDY ALGORITHM While knapsack is not full
Choose item i with maximum vi/wi

17 GREEDY ALGORITHM While knapsack is not full
Choose item i with maximum vi/wi If item fits into knapsack, take all of it

18 GREEDY ALGORITHM While knapsack is not full
Choose item i with maximum vi/wi If item fits into knapsack, take all of it Otherwise take so much as to fill the knapsack

19 GREEDY ALGORITHM While knapsack is not full
Choose item i with maximum vi/wi If item fits into knapsack, take all of it Otherwise take so much as to fill the knapsack Return total value and amounts taken

20 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

21 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

22 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

23 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

24 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

25 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

26 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

27 BestItem(w1, v1, , wn, vn) maxValuePerWeight ← 0 bestItem ← 0 for i from 1 to n: if wi > 0: if vi/wi > maxValuePerWeight: maxValuePerWeight ← vi/wi bestItem ← i return bestItem

28 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

29 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

30 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

31 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

32 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

33 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

34 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

35 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

36 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

37 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

38 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

39 Knapsack(W, w1, v1, , wn, vn) amounts ← [0, 0, ... , 0] totalValue ← 0 repeat n times: if W = 0: return (totalValue, amounts) i ← BestItem(w1,v1, ..., wn, vn) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

40 Lemma The running time of Knapsack is O(n2).

41 Lemma The running time of Knapsack is O(n2). Proof BestItem uses one loop with n iterations, so it is O(n)

42 Lemma The running time of Knapsack is O(n2). Proof BestItem uses one loop with n iterations, so it is O(n) Main loop is executed n times, and BestItem is called once per iteration

43 Lemma The running time of Knapsack is O(n2). Proof BestItem uses one loop with n iterations, so it is O(n) Main loop is executed n times, and BestItem is called once per iteration Overall, O(n2)

44 OPTIMIZATION It is possible to improve asymptotics!

45 OPTIMIZATION It is possible to improve asymptotics!
First, sort items by decreasing v/w

46 Assume v1/w1 ≥ v2/w2 ≥ · · · ≥ vn/wn
KnapsackFast(W, w1, v1, , wn, vn) amounts ← [0, 0, ..., 0] totalValue ← 0 for i from 1 to n: if W = 0: return (totalValue, amounts) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

47 Assume v1/w1 ≥ v2/w2 ≥ · · · ≥ vn/wn
KnapsackFast(W, w1, v1, , wn, vn) amounts ← [0, 0, ..., 0] totalValue ← 0 for i from 1 to n: if W = 0: return (totalValue, amounts) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

48 Assume v1/w1 ≥ v2/w2 ≥ · · · ≥ vn/wn
KnapsackFast(W, w1, v1, , wn, vn) amounts ← [0, 0, ..., 0] totalValue ← 0 for i from 1 to n: if W = 0: return (totalValue, amounts) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

49 Assume v1/w1 ≥ v2/w2 ≥ · · · ≥ vn/wn
KnapsackFast(W, w1, v1, , wn, vn) amounts ← [0, 0, ..., 0] totalValue ← 0 for i from 1 to n: if W = 0: return (totalValue, amounts) a ← min(wi ,W) totalValue ← totalValue + a*vi/wi wi ← wi − a amounts[i] ← amounts[i] + a W ← W − a

50 ASYMPTOTICS Now each iteration is O(1) Knapsack after sorting is O(n)
Sort + Knapsack is O(n log n)


Download ppt "Greedy Algorithms: Maximizing Loot"

Similar presentations


Ads by Google