Daniel Sutantyo, Department of Computing, Macquarie University
3.2 - Search Space
3.2 - Search Space
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.
11
12
7
5
10
3.2 - Search Space
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.
3.2 - Search Space
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\]
3.2 - Search Space
for (int i = 0; i < b.length; i++){
for (int j = 0; j < b.length; j++){
for (int k = 0; k < b.length; k++){
for (int l = 0; l < b.length; l++){
for (int m = 0; m < b.length; m++){
...
}}}}}
3.2 - Search Space
3.2 - Search Space
3.2 - Search Space
[10,12,5,7,11] L = 25
[12,5,7,11] L = 15
[10,5,7,11] L = 13
[10,12,7,11] L = 20
[10,12,5,11] L = 18
[10,12,5,7] L = 14
3.2 - Search Space
public static boolean solve(int[] b, int i, int L) {
// if L == 0, we are done
if (L == 0)
return true;
// if we don't have any more rods, then we can't make it
if (b.length == 0)
return false;
// else keep on going with the remaining sum
for (???) {
// HALP
}
}
3.2 - Search Space
3.2 - Search Space
[10,12,5,7,11] L = 25
[12,5,7,11] L = 15
[12,5,7,11] L = 25
don't pick 10
pick 10
[5,7,11] L = 3
[5,7,11] L = 15
[5,7,11] L = 15
[5,7,11] L = 25
don't pick 12
pick 12
pick 12
don't pick 12
[7,11] L = -2
[11] L = -9
pick 5
pick 7
...
[7,11] L = 15
[11] L = 8
pick 7
don't pick 5
don't pick 5
...
...
...
...
don't pick 7
...
[11] L = 15
[7,11] L = 15
pick 5
...
...
don't pick 7
10
12
5
7
3.2 - Search Space
[10,12,5,7,11] L = 25
don't pick 10
pick 10
[10,12,5,7,11] L = 25
[12,5,7,11] L = 15
[10,5,7,11] L = 13
[10,12,7,11] L = 20
[10,12,5,11] L = 18
[10,12,5,7] L = 14
[12,5,7,11] L = 15
[12,5,7,11] L = 25
3.2 - Search Space
3.2 - Search Space
3.2 - Search Space
"a"
""
"ab"
"a"
"b"
""
"abc"
"ab"
"ac"
"a"
"bc"
"b"
"c"
a ?
b ?
c ?
""
""
d ?
"abd"
"ab"
"bcd"
"bc"
"d"
""
... ...
... ...
(and so on)
3.2 - Search Space
"a"
"b"
"z"
"c"
""
"d"
... ...
""
"a"
"b"
"z"
"c"
"d"
... ...
"a"
"b"
"z"
"c"
"d"
... ...
3.2 - Search Space
"a"
"b"
"z"
"c"
""
"d"
... ...
""
"a"
"b"
"y"
"c"
"d"
... ...
"b"
"c"
"z"
"d"
... ...
3.2 - Search Space
3.2 - Search Space
"a" "b" "c" ..... "9" "0"
"a" "b" "c" ..... "9" "0"
3.2 - Search Space
[10,12,5,7,11] L = 25
[12,5,7,11] L = 15
[12,5,7,11] L = 25
don't pick 10
pick 10
[5,7,11] L = 3
[5,7,11] L = 15
[5,7,11] L = 15
[5,7,11] L = 25
don't pick 12
pick 12
pick 12
don't pick 12
[7,11] L = -2
[11] L = -9
pick 5
pick 7
...
[7,11] L = 15
[11] L = 8
pick 7
don't pick 5
don't pick 5
...
...
...
...
don't pick 7
...
[11] L = 15
[7,11] L = 15
pick 5
...
...
don't pick 7
10
12
5
7
3.2 - Search Space
public static boolean solve(int[] b, int i, int L) {
// if L == 0, we are done
if (L == 0){
return true;
}
// if we get to the end of array but still can't make the sum,
// return false
if (i >= b.length) {
return false;
}
// else keep on going with the remaining sum
return solve(b,i+1,L-b[i]) || solve(b,i+1,L);
}
3.2 - Search Space
public static boolean solve(int[] b, int i, int L) {
// if L == 0, we are done
if (L == 0){
return true;
}
// if we get to the end of array but still can't make the sum,
// return false
if (i >= b.length) {
return false;
}
// else keep on going with the remaining sum
return solve(b,i+1,L-b[i]) || solve(b,i+1,L);
}
3.2 - Search Space
public static boolean solve(int[] b, int i, int L) {
...
// else keep on going with the remaining sum
return solve(b,i+1,L-b[i]) || solve(b,i+1,L);
}
[10,12,5,7,11] L = 25
[12,5,7,11] L = 15
[12,5,7,11] L = 25
don't pick 10
pick 10
don't pick 12
pick 12
pick 12
don't pick 12
[5,7,11] L = 3
[5,7,11] L = 15
[5,7,11] L = 15
[5,7,11] L = 25
3.2 - Search Space
"a"
""
"ab"
"a"
"b"
""
"abc"
"ab"
"ac"
"a"
"bc"
"b"
"c"
a ?
b ?
c ?
""
""
d ?
"abd"
"ab"
"bcd"
"bc"
"d"
... ...
... ...
... ...
3.2 - Search Space
3.2 - Search Space
"a"
""
"ab"
"a"
"b"
""
"abc"
"ab"
"ac"
"a"
"bc"
"b"
"c"
a ?
b ?
c ?
""
""
d ?
"abd"
"ab"
"bcd"
"bc"
"d"
""
... ...
... ...
backtrack
3.2 - Search Space