Daniel Sutantyo, Department of Computing, Macquarie University
4.3 - Developing a DP Algorithm - Recursive Relation
4.3 - Developing a DP Algorithm - Recursive Relation
\(i\)
\(p_i\)
1 2 3 4 5 6 7 8 9 10
1 5 8 9 10 17 17 20 24 30
\(i\)
optimal
1 2 3 4 5 6 7 8 9 10
1 5 8 10 13 17 ?? ?? ?? ??
4.3 - Developing a DP Algorithm - Recursive Relation
\(i\)
\(p_i\)
1 2 3 4 5 6 7 8 9 10
1 5 8 9 10 17 17 20 24 30
\(i\)
optimal
1 2 3 4 5 6 7 8 9 10
1 5 8 10 13 17 ?? ?? ?? ??
$10
$9 + $1
$8 + $5
$5 + $8
$1 + $10
4.3 - Developing a DP Algorithm - Recursive Relation
4.3 - Developing a DP Algorithm - Recursive Relation
4.3 - Developing a DP Algorithm - Recursive Relation
\[r_n = \max_{1\le i \le n} (p_i + r_{n-i}) \]
4.3 - Developing a DP Algorithm - Recursive Relation
\[ \text{LCSS}(X,Y) = \begin{cases} 0 & \text{if \(X\) or \(Y\) is empty}\\1 + \text{LCSS}(X_2,Y_2) & \text{if $x_1 = y_1$}\\\max\left(\text{LCSS}(X_2,Y_1),\text{LCSS}(X_1,Y_2)\right) & \text{if $x_1 \ne y_2$}\end{cases} \]
4.3 - Developing a DP Algorithm - Recursive Relation
4.3 - Developing a DP Algorithm - Recursive Relation
\[r_n = \max_{1\le i \le n} (p_i + r_{n-i}) \]
// assume p[i] gives price of rod of length i
public static int cut(int n) {
if (n == 0)
return 0;
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
answer = Math.max(answer, p[i] + cut(n-i));
}
return answer;
}
4.3 - Developing a DP Algorithm - Recursive Relation
static int[] p = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30,
33, 36, 40, 42, 45, 50, 52, 58, 58, 60,
62, 65, 66, 72, 80, 82, 83, 85, 87, 88};
public static int cut(int n) {
if (n == 0)
return 0;
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
answer = Math.max(answer, p[i] + cut(n-i));
}
return answer;
}
n | optimal answer | no. of recursive calls |
---|---|---|
10 | 30 | 1,024 |
15 | 45 | 33,792 |
20 | 63 | 1,082,368 |
25 | 80 | 34,636,800 |
30 | 94 | 1,108,378,624 |
4.3 - Developing a DP Algorithm - Recursive Relation
static int[] p = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30,
33, 36, 40, 42, 45, 50, 52, 58, 58, 60,
62, 65, 66, 72, 80, 82, 83, 85, 87, 88};
public static int cut(int n) {
if (n == 0)
return 0;
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
answer = Math.max(answer, p[i] + cut(n-i));
}
return answer;
}
public static int cut(int n) {
if (r[n] != 0) return r[n]; // memoise
if (n == 0)
return 0;
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
answer = Math.max(answer, p[i] + cut(n-i));
}
return r[n] = answer; // store the result
}
4.3 - Developing a DP Algorithm - Recursive Relation
n | optimal answer | no. of recursive calls |
---|---|---|
10 | 30 | 56 |
15 | 45 | 122 |
20 | 63 | 213 |
25 | 80 | 329 |
30 | 94 | 470 |
public static int cut(int n) {
if (r[n] != 0) return r[n]; // memoise
if (n == 0)
return 0;
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
answer = Math.max(answer, p[i] + cut(n-i));
}
return r[n] = answer; // store the result
}
4.3 - Developing a DP Algorithm - Recursive Relation
recursion
top-down
DP
bottom-up
DP
???
4.3 - Developing a DP Algorithm - Recursive Relation
public static int cut_bottom_up(int n) {
r[0] = 0;
for (int j = 1; j <= n; j++) {
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= j; i++) {
answer = Math.max(answer, p[i] + r[j-i]);
}
r[j] = answer;
}
return r[n];
}
4.3 - Developing a DP Algorithm - Recursive Relation
public static int cut_bottom_up(int n) {
r[0] = 0;
for (int j = 1; j <= n; j++) {
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= j; i++) {
answer = Math.max(answer, p[i] + r[j-i]);
}
r[j] = answer;
}
return r[n];
}
4.3 - Developing a DP Algorithm - Recursive Relation
4.3 - Developing a DP Algorithm - Recursive Relation
public static int cut_bottom_up(int n) {
r[0] = 0;
for (int j = 1; j <= n; j++) {
int answer = Integer.MIN_VALUE;
for (int i = 1; i <= j; i++) {
if (answer < p[i] + r[j-i]) {
answer = p[i] + r[j-i];
s[j] = i;
}
}
r[j] = answer;
}
return r[n];
}
4.3 - Developing a DP Algorithm - Recursive Relation
4.3 - Developing a DP Algorithm - Recursive Relation
\(i\)
\(p_i\)
1 2 3 4 5 6 7 8 9 10
1 5 8 9 10 17 17 20 24 30
\(s_i\)
1 2 3 2 2 6 1 2 3 10
4.3 - Developing a DP Algorithm - Recursive Relation
\(i\)
\(p_i\)
1 2 3 4 5 6 7 8 9 10
1 5 8 9 10 17 17 20 24 30
\(s_i\)
1 2 3 2 2 6 1 2 3 10
public static String construct_solution(int n) {
String ans = "";
while (n > 0) {
ans = ans + s[n] + " ";
n = n - s[n];
}
return ans;
}
4.3 - Developing a DP Algorithm - Recursive Relation
static int[] p = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30,
33, 36, 40, 42, 45, 50, 52, 58, 58, 60,
62, 65, 66, 72, 80, 82, 83, 85, 87, 88};
n | optimal answer | optimal cuts |
---|---|---|
10 | 30 | 10 |
15 | 45 | 2 and 13 |
20 | 63 | 2 and 18 |
25 | 80 | 25 |
30 | 94 | 12 and 18 |
4.3 - Developing a DP Algorithm - Recursive Relation