Greedy Algorithms
Chapter 16
1
Greedy Algorithms Chapter 16 1 Optimization Problems A class of - - PowerPoint PPT Presentation
Greedy Algorithms Chapter 16 1 Optimization Problems A class of problems in which we are asked to find a set (or a sequence ) of items that satisfy some constraints and simultaneously optimize (i.e., maximize or minimize ) some
Chapter 16
1
2
10
3
8
4
4
5
1
6
1
5
7
1
5 4
Number of bins = 3
8
(Optimal Solution) Number of bins = 2
9
10
a.k.a. Task Scheduling
11
π, where 0 β€ π‘π < π π < β. An
π). Two activities are said to be
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
We want to prove that, if π΅ β π is an optimal solution to π, then π΅ β ππ is an optimal solution to S` = ππ: ππ β π β§ π‘π > π
π
Proof by contradiction Assume that π΅ β ππ is not optimal Then, there is another solution πΆ to π` such that πΆ > π΅ β ππ β |πΆ| β₯ |π΅| If this is the case, then the subset π΅` = ππ βͺ πΆ is also a solution to π π΅` > πΆ β |π΅`| > π΅ , which means that π΅ is not optimal which is a contradiction
30
31
$4,500 ο15LBs $1,500 ο3LBs $800 ο2LBs $3,000 ο10LBs $4,000 ο20LBs
ο
45LBs
32
$4,500 ο15LBs $1,500 ο3LBs $800 ο2LBs $3,000 ο10LBs $4,000 ο20LBs
ο
45LBs
33
$4,500 ο15LBs $1,500 ο3LBs $800 ο2LBs $3,000 ο10LBs $4,000 ο20LBs
ο
45LBs
$300/LB $500/LB $400/LB $200/LB $300/LB
34
$4,500 ο15LBs $1,500 ο3LBs $800 ο2LBs $3,000 ο10LBs $4,000 ο20LBs
ο
45LBs
$300/LB $500/LB $400/LB $200/LB $300/LB
35
Total value = $9,800
$4,500 ο15LBs $1,500 ο3LBs $800 ο2LBs $3,000 ο10LBs $4,000 ο20LBs
ο
45LBs
$300/LB $500/LB $400/LB $200/LB $300/LB
36
Total value = $11,500
There isnβt a known solution to the 0-1 Knapsack problem using a greedy algorithm
$4,500 ο15 oz $1,500 ο3 oz $800 ο2 oz $3,000 ο10oz $4,000 ο20oz
ο
45 oz
$300/oz $500/oz $400/oz $200/oz $300/oz
37
π=1 π
0 β€ π¦π β€ π₯π Οπ=1
π
π¦π β€ π
38
Compute the value-per-weight π§π =
π€π π₯π for all items
Sort items by π§π Set π = 0 While (π < π)
Select the item π with the highest π§π Set π¦π = πππ π β π, π₯
π
Remove item π Set π = π + π¦π
Set all remaining π¦πβs to 0
39
40
β²β² = π¦π β² β π¨
β²β² = π¦π β² + π¨ = π¦π (π¦π is the greedy choice)
41
β² = Ο π¦π β²β² β (both are valid solutions)
β²π§π, πβ²β² = Ο π¦π β²β²π§π
β²β² + π§ππ¦π β²β² β π§ππ¦π β² + π§ππ¦π β²
β²β² β π¦π β² β π§π π¦π β² β π¦π β²β²
β²β² β π¦π β² = π¦π β² β π¦π β²β² = π¨
β²β² β π¦π > 0
42
π π Ordered by π§ πβ² π π₯
π
π¦π
β²
π¦π
β²
πβ²β² π π¦π
β²β² = π¦π
π π¦π
β²β²
Input Optimal answer that does not have the greedy choice Optimal answer that has the greedy choice
43
Given the problem π with an optimal solution π, we want to prove that the solution π` = π β π¦π is optimal to the problem π` after removing the item π and updating the capacity π` = π β π¦π Proof by contradiction Assume that π` is not optimal to π` There is another solution π`` to π` that has a higher total value π`` > π` This means we can have a better solution to the problem π which is impossible because π is
44
π π π₯
π
Input Ordered by π§ π π Optimal answer for π πβ² Reduced problem πβ² Should be an
πβ²
45
46
47
A: 1000001 B: 1000010
A: ββ¬ B: β¬βββ E: β T: β¬
48
49
Message βJAVAβ a = β0β, j = β11β, v = β10β Encoded message β110100β Decoding β110100β
51
A = 010 B = 11 C = 00 D = 10 R = 011 D B C R 1 1 1 1 A
52
ASCII: 88 bits Our encoding: 29 bits
A = 010 B = 11 C = 00 D = 10 R = 011 D B C R 1 1 1 1 A
53
B R A D 1 1 1 1 C
54
πβπ·
55
56
57
58
59
60
1 1 1 1
61
1 1 1 1
A=0 B=10 C=1100 D=1101 R=111
62
1 1 1 1
A=0 B=10 C=1100 D=1101 R=111
63
1 1 1 1
A=0 R=10 C=1100 D=1101 B=111
64
n=|C| Q=C for i = 1 to n-1
allocate a new node z z.left = x = Extract-Min(Q) z.right = y = Extract-Min(Q) z.freq = x.freq + y.freq Insert(Q, z)
return Extract-Min(Q) // Root of the tree
Ξ log π Ξ log π n Ξ π log π
Note: Can also be done in linear time
65
The greedy choice yields an optimal solution.
The optimal solution for the bigger problem contains the optimal solution of the sub-problem.
66
67
π§ π¦
π π π
π§ π
π¦ π π`
π π
π¦ π§ π``
68