Generate and Test Framework. Nattee Niparnan. Optimization Example: Finding Max Value in an Array. There are N possible answers The first element The second element 3 rd , 4 th … Try all of them Remember the best one. Generate and Test Framework. Define the set of admissible solution
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Generate and Test Framework
Nattee Niparnan
Backtracking
Undone the second step, switch to another value
Initial Step
1. Get a step that is not complete
Engine
Storage
Step that have been done
2. Try any possible next step
3. Store each newly generated next step
If length(curr) == 4
For all symbol i
New = curr+I
Storage.push(new)
void backtracking(intstep,int*sol)
{
if (step < num_step) {
for (inti= 0; i < num_symbol; i++) {
sol[step] = i;
backtracking(step + 1,sol);
}
} else {
check(sol);
}
}
0
1
2
3
…
01
02
03
04
… … … …
… … … …
… … … …
Mark_on_board is a binary string indicate that whether the position is selected
void e_queen(intstep,int *mark_on_board)
{
if (step < 64) {
mark_on_board[step] = 0;
e_queen(step + 1,mark_on_board);
mark_on_board[step] = 1;
e_queen(step + 1,mark_on_board);
} else {
check(mark_on_board);
}
}
Also has to check whether we mark exactly 8 spots
Number of possible 0
void e_queen(intstep,int *mark_on_board,int chosen)
{
if (step < 64) {
if ((64 – 8) – (step – chosen) > 0) {
mark_on_board[step] = 0;
e_queen(step + 1,mark_on_board,chosen);
}
if (8 - chosen > 0) {
mark_on_board[step] = 1;
e_queen(step + 1,mark_on_board,chosen+1);
}
} else {
check(mark_on_board);
}
}
Number of possible 1
void e_queen(intstep,int *queen_pos)
{
if (step < 8) {
for (inti = 0; i < 8; i++) {
queen_pos[step] = i;
e_queen(step + 1, queen_pos);
}
} else {
check(queen_pos);
}
}
void backtracking(intstep,int *sol)
{
if (step < num_step) {
for (inti = 0; i < num_symbol; i++) {
if not_used(sol,i,step) {
sol[step] = i;
backtracking(step,sol);
}
}
} else {
check(sol);
}
}
Boolnot_used(int *sol,intvalue,int step)
{
for (inti = 0;i < step; i++) {
if (sol[i] == value) return false;
}
return true;
}
Technique to reduce enumeration
0
1
2
00
01
02
10
11
12
20
21
22
000
001
002
010
011
012
020
021
022
100
101
102
110
111
112
120
121
122
200
201
202
210
211
212
220
221
222
0
1
2
00
01
02
10
11
12
20
21
22
000
001
002
010
011
012
020
021
022
100
101
102
110
111
112
120
121
122
200
201
202
210
211
212
220
221
222
void backtracking(int step,int *sol)
{
if (step < num_step) {
for (int i = 0; i < num_symbol; i++) {
sol[step] = i;
backtracking(step,sol);
}
} else {
value = evaluate(sol);
if (value > max) remember(value,sol);
}
}
Let value at this point be 10
If we know that this path never bet higher than 13 (which make 10 + 13 < 35)
We can neglect it
23
35
2
Implement 0-1 Knapsack