Daniel Sutantyo, Department of Computing, Macquarie University
4.1 - Optimal Substructure
4.1 - Optimal Substructure
public static int fib(int n) {
if (n <= 2)
return 1;
else return fib(n-1) + fib(n-2);
}
4.1 - Optimal Substructure
fib(n)
fib(n-1)
fib(n-2)
find_largest(0,n)
find_largest(0,0)
find_largest(1,n)
binary_search(0,n)
binary_search(0,n/2)
binary_search(n/2+1,n)
4.1 - Optimal Substructure
binary_search(0,n)
binary_search(0,n/2)
binary_search(n/2+1,n)
(p.s. I have seen discard and conquer)
4.1 - Optimal Substructure
find_largest(0,n)
find_largest(0,0)
find_largest(1,n)
4.1 - Optimal Substructure
public static int find_largest(int[] a, int i, int j, int max) {
if(i > j)
return -1;
if(a[i] > max)
max = a[i];
return Math.max(max, find_largest(a,i+1,j,max));
}
public static int find_largest(int[] a, int i, int j, int max) {
if(i > j)
return -1;
int m = (i+j)/2;
if(a[m] > max)
max = a[m];
return Math.max(max,Math.max(find_largest(a,i,m-1,max), find_largest(a,i+1,j,max)));
}
4.1 - Optimal Substructure
public static int find_largest(int[] a, int i, int j, int max) {
if(i > j)
return -1;
int m = (i+j)/2;
if(a[m] > max)
max = a[m];
return Math.max(max,Math.max(find_largest(a,i,m-1,max), find_largest(a,i+1,j,max)));
}
find_largest(0,n)
find_largest(0,n/2)
find_largest(n/2+1,n)
4.1 - Optimal Substructure
fib(n)
fib(n-1)
fib(n-2)
binary_search(0,n)
binary_search(0,n/2)
binary_search(n/2+1,n)
find_largest(0,n)
find_largest(0,n/2)
find_largest(n/2+1,n)
4.1 - Optimal Substructure
dynamic
programming
divide and
conquer
4.1 - Optimal Substructure
4.1 - Optimal Substructure
A | B | C | D | E | F | G | H | |
---|---|---|---|---|---|---|---|---|
weight | 7 | 5 | 12 | 5 | 6 | 4 | 8 | 11 |
value | $12 | $9 | $18 | $9 | $12 | $5 | $15 | $21 |
[ A ]
L = 33
V = 12
L (Limit) = 40
L = 40
V = 0
[ ]
pick A
[ ]
pick B
pick B
[ A B ]
L = 28
V = 21
[ A ]
L = 33
V = 12
[ B ]
L = 35
V = 9
[ ]
L = 40
V = 0
L = 40
V = 0
pick C
pick C
pick C
don't pick C
pick C
[ A B C ]
L = 16
V = 39
[ A B ]
L = 28
V = 21
[ A C ]
L = 21
V = 30
[ A ]
L = 33
V = 12
[ B C ]
L = 23
V = 27
[ B ]
L = 35
V = 9
[ C ]
L = 28
V = 18
[ ]
L = 40
V = 0
don't pick C
don't pick C
don't pick C
don't pick B
don't pick B
don't pick A
4.1 - Optimal Substructure
4.1 - Optimal Substructure
A problem exhibits optimal substructure if the optimal solution to the problem can be constructed using optimal solutions to the subproblems
4.1 - Optimal Substructure
A problem exhibits optimal substructure if the optimal solution to the problem can be constructed using optimal solutions to the subproblems
fib(n)
fib(n-1)
fib(n-2)
find_largest(0,n)
find_largest(0,n/2)
find_largest(n/2+1,n)
4.1 - Optimal Substructure
A | B | C | D | E | F | G | H | |
---|---|---|---|---|---|---|---|---|
weight | 7 | 5 | 12 | 5 | 6 | 4 | 8 | 11 |
value | $12 | $9 | $18 | $9 | $12 | $5 | $15 | $21 |
[ A ]
L = 33
V = 12
L (Limit) = 40
L = 40
V = 0
[ ]
pick A
[ ]
pick B
pick B
[ A B ]
L = 28
V = 21
[ A ]
L = 33
V = 12
[ B ]
L = 35
V = 9
[ ]
L = 40
V = 0
L = 40
V = 0
pick C
pick C
pick C
don't pick C
pick C
[ A B C ]
L = 16
V = 39
[ A B ]
L = 28
V = 21
[ A C ]
L = 21
V = 30
[ A ]
L = 33
V = 12
[ B C ]
L = 23
V = 27
[ B ]
L = 35
V = 9
[ C ]
L = 28
V = 18
[ ]
L = 40
V = 0
don't pick C
don't pick C
don't pick C
don't pick B
don't pick B
don't pick A
4.1 - Optimal Substructure
A | B | C | D | E | F | G | H | |
---|---|---|---|---|---|---|---|---|
weight | 7 | 5 | 12 | 5 | 6 | 4 | 8 | 11 |
value | $12 | $9 | $18 | $9 | $12 | $5 | $15 | $21 |
[ A ]
L = 33
V = 12
L (Limit) = 40
L = 40
V = 0
[ ]
pick A
[ ]
pick B
pick B
L = 40
V = 0
pick C
pick C
pick C
don't pick C
pick C
[ A B C ]
L = 16
V = 39
[ A B ]
L = 28
V = 21
[ A C ]
L = 21
V = 30
[ A ]
L = 33
V = 12
[ B C ]
L = 23
V = 27
[ B ]
L = 35
V = 9
[ C ]
L = 28
V = 18
[ ]
L = 40
V = 0
don't pick C
don't pick C
don't pick C
don't pick B
don't pick B
don't pick A
4.1 - Optimal Substructure
A | B | C | D | E | F | G | H | |
---|---|---|---|---|---|---|---|---|
weight | 7 | 5 | 12 | 5 | 6 | 4 | 8 | 11 |
value | $12 | $9 | $18 | $9 | $12 | $5 | $15 | $21 |
L (Limit) = 40
L = 40
V = 0
[ ]
pick A
pick B
pick B
pick C
pick C
pick C
don't pick C
pick C
[ A B C ]
L = 16
V = 39
[ A B ]
L = 28
V = 21
[ A C ]
L = 21
V = 30
[ A ]
L = 33
V = 12
[ B C ]
L = 23
V = 27
[ B ]
L = 35
V = 9
[ C ]
L = 28
V = 18
[ ]
L = 40
V = 0
don't pick C
don't pick C
don't pick C
don't pick B
don't pick B
don't pick A
4.1 - Optimal Substructure
A | B | C | D | E | F | G | H | |
---|---|---|---|---|---|---|---|---|
weight | 7 | 5 | 12 | 5 | 6 | 4 | 8 | 11 |
value | $12 | $9 | $18 | $9 | $12 | $5 | $15 | $21 |
L (Limit) = 40
pick A
pick B
pick B
pick C
pick C
pick C
don't pick C
pick C
[ A B C ]
L = 16
V = 39
[ A B ]
L = 28
V = 21
[ A C ]
L = 21
V = 30
[ A ]
L = 33
V = 12
[ B C ]
L = 23
V = 27
[ B ]
L = 35
V = 9
[ C ]
L = 28
V = 18
[ ]
L = 40
V = 0
don't pick C
don't pick C
don't pick C
don't pick B
don't pick B
don't pick A
4.1 - Optimal Substructure
What is the optimal answer to the problem
171
Subproblem B
Subproblem C
Subproblem D
43
Subproblem A
89
4.1 - Optimal Substructure
What is the optimal answer to the problem
171
43
89
152
193
23
4.1 - Optimal Substructure
What is the optimal answer to the problem
171
43
89
152
193
23
4.1 - Optimal Substructure
4.1 - Optimal Substructure
Z
B
A
C
X
Y
???
4.1 - Optimal Substructure
4.1 - Optimal Substructure
4.1 - Optimal Substructure
4.1 - Optimal Substructure
4.1 - Optimal Substructure
A
C
B
E
D
4.1 - Optimal Substructure
A
C
B
E
D
A
B
A
B
A
B
A
C
B
E
D
A
B
A
B
A
B
4.1 - Optimal Substructure
A
C
B
E
D
A
B
A
B
A
B
A
C
B
E
D
A
B
A
B
A
B
longest path from A to B
longest path from B to E
4.1 - Optimal Substructure
A
C
B
E
D
A
B
A
B
A
B
A
C
B
E
D
A
B
A
B
A
B
longest path from A to B
longest path from B to E
4.1 - Optimal Substructure
4.1 - Optimal Substructure
4.1 - Optimal Substructure