Daniel Sutantyo, Department of Computing, Macquarie University
5.0 - Greedy Algorithm - Prelude
Original problem
Subproblem A
Subproblem B
Subproblem C
Subproblem D
Subproblem E
Subproblem F
Subproblem G
Subproblem H
Subproblem I
Subproblem J
Subproblem K
Subproblem M
Subproblem N
Subproblem O
Subproblem P
Subproblem L
5.0 - Greedy Algorithm - Prelude
Original problem
Subproblem A
Subproblem B
Subproblem C
Subproblem D
Subproblem E
Subproblem F
Subproblem G
Subproblem H
Subproblem I
Subproblem J
Subproblem K
Subproblem M
Subproblem N
Subproblem O
Subproblem P
Subproblem L
5.0 - Greedy Algorithm - Prelude
10
15
3
A
F
C
D
3
14
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
Original problem
Subproblem A
Subproblem B
Subproblem C
Subproblem D
Subproblem E
Subproblem F
Subproblem G
Subproblem H
Subproblem I
Subproblem J
Subproblem K
Subproblem M
Subproblem N
Subproblem O
Subproblem P
Subproblem L
5.0 - Greedy Algorithm - Prelude
We have a bunch of metallic rods with fixed lengths. Is it possible to produce a rod of certain length if we are allowed to join any two or more rods together, but we cannot cut any rod into two or more pieces.
\[\sum_{j=0}^k b_{i_j} = L\]
5.0 - Greedy Algorithm - Prelude
[ 50 , 2 , 18 , 11 , 9 , 23 , 5 , 10 , 30 , 6 , 17 ] L = 27
[ 2 , 18 , 11 , ... , 17 ] L = -23
[ 2 , 18 , 11 , ... , 17 ] L = 27
[18 , 11 , ... , 17 ] L = -25
pick 2
don't pick 2
...
...
...
...
...
...
[ 18 , 11, ... , 17] L = -23
[18 , 11, ... , 17 ] L = 25
[ 18 , 11 , ... , 17 ] L = 27
...
...
pick 50
pick 2
don't pick 50
don't pick 2
5.0 - Greedy Algorithm - Prelude
[ 50 , 30 , 23 , 18 , 17 , 11 , 10 , 9 , 6 , 5 , 2 ] L = 27
[ 18 , 17 , 11 , ... , 2 ] L = 4
[ 18 , 17 , 11 , ... , 2 ] L = 27
[ ] L = 2
pick 2
don't pick 2
[ ] L = 4
[9 , 6 , ... , 2 ] L = 9
[ 9 , 6 , ... , 2 ] L = 27
pick 23
pick 18
don't pick 23
don't pick 18
pick 9
but greedy algorithm doesn't try all subproblems!
5.0 - Greedy Algorithm - Prelude
We have an infinite number of metallic rods with fixed lengths. Is it possible to produce a rod of certain length if we are allowed to join any two or more rods together, but we cannot cut any rod into two or more pieces.
\[\sum_{i=1}^n c_i b_i = L\]
5.0 - Greedy Algorithm - Prelude
We have an infinite number of metallic rods with fixed lengths. Is it possible to produce a rod of certain length if we are allowed to join any two or more rods together, but we cannot cut any rod into two or more pieces.
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
f(265)
f(132)
f(133)
f(61)
f(61)
f(62)
f(61)
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
f(265)
f(165)
f(65)
f(215)
f(240)
f(264)
f(115)
f(140)
f(164)
f(115)
f(165)
f(140)
f(190)
f(164)
f(214)
f(239)
f(263)
...
...
f(15)
f(15)
f(15)
f(115)
f(15)
pick 100
pick 50
pick 25
pick 1
5.0 - Greedy Algorithm - Prelude
f(265)
f(165)
f(65)
f(215)
f(240)
f(264)
f(115)
f(140)
f(164)
f(115)
f(165)
f(140)
f(190)
f(164)
f(214)
f(239)
f(263)
...
...
f(15)
f(15)
f(15)
f(115)
f(15)
pick 100
pick 50
pick 25
pick 1
pick 100
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
minR(n)
1+ minR(n-100)
1+minR(n-50)
1+minR(n-25)
1+minR(n-1)
pick 50
pick 25
pick 1
pick 100
5.0 - Greedy Algorithm - Prelude
minR(n)
1+ minR(n-100)
1+minR(n-50)
1+minR(n-25)
1+minR(n-1)
pick 50
pick 25
pick 1
pick 100
5.0 - Greedy Algorithm - Prelude
minR(n)
1+ minR(n-100)
1+minR(n-50)
1+minR(n-25)
1+minR(n-1)
pick 50
pick 25
pick 1
pick 100
5.0 - Greedy Algorithm - Prelude
\(\text{minR}(n)\)
\(1+\text{minR$(n-b_1)$}\)
\(1+\text{minR}(n-b_2)\)
\(1+\text{minR}(n-b_k)\)
...
...
...
5.0 - Greedy Algorithm - Prelude
\(\text{minR}(n)\)
\(1+\text{minR$(n-b_1)$}\)
\(1+\text{minR}(n-b_2)\)
\(1+\text{minR}(n-b_k)\)
...
...
...
5.0 - Greedy Algorithm - Prelude
\(\text{minR}(n)\)
\(1+\text{minR$(n-b_1)$}\)
\(1+\text{minR}(n-b_2)\)
\(1+\text{minR}(n-b_k)\)
...
...
...
5.0 - Greedy Algorithm - Prelude
\[\text{minR}(n)\]
\(1+\text{minR$(n-b_1)$}\)
\(1+\text{minR}(n-b_2)\)
\(1+\text{minR}(n-b_k)\)
...
\(\text{minR}(n)\)
\(1+\text{minR$(n-b_1)$}\)
\(1+\text{minR}(n-b_2)\)
\(1+\text{minR}(n-b_k)\)
...
...
...
5.0 - Greedy Algorithm - Prelude
\[\text{minR}(n) = \begin{cases}\displaystyle\min_{i\ :\ b_i\le n}\left(1 +\text{minR}(n-b_i)\right) & \text{if $n > 0$}\\ \quad \quad \quad\quad 0 & \text{otherwise}\end{cases}\]
\(\text{minR}(n)\)
\(1+\text{minR$(n-b_1)$}\)
\(1+\text{minR}(n-b_2)\)
\(1+\text{minR}(n-b_k)\)
...
...
...
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
minR(265)
1+ minR(165)
1+minR(215)
1+minR(240)
1+minR(264)
pick 50
pick 25
pick 1
pick 100
5.0 - Greedy Algorithm - Prelude
5.0 - Greedy Algorithm - Prelude
minR(100)
1+ minR(30)
1+minR(50)
1+minR(90)
1+minR(99)
pick 50
pick 10
pick 1
pick 70
5.0 - Greedy Algorithm - Prelude