void dfs(node v){
node u;
visit v;
for(each child u of v)
dfs(u)
}
Promising
Non-Promising
void checknode(node v){
node u;
if(promising(v))
if(there is a solution at v)
write the solution;
else
for(each child u of v)
checknode(v);
}
//Better Altorithm
void expand(node v){
node u;
for(each child u of v)
if(promising(u))
if(solution at u)
write the solution;
else
expand(u);
}
차이점
bool promising(index i){
index k;
bool switch;
k = 1;
switch=true;
while(k<i && switch){
// 같은 row에 있거나 대각선에 있는지 확인
if (col[i]==col[k] || abs(col[i]-col[k]) == i-k)
switch = false;
k++;
}
return swtich;
}
void queens(index i){
index j;
if(promissing(i))
if(i==n)
cout<<col[1] through col[n];
else
for(j=1; j<=n; j++){
col[i+1] = j;
queen(i+1);
}
}
합이 W가 되는 부분 집합 구하기
non-promising
bool promising(index i){
return (weight+total >=W) && (weight==W || weight+w[i+1] <= W);
}
bool promising(index i){
index j;
bool switch;
switch = true;
j=1;
while(j<i && switch){
if(W[i][j] && vcolor[i] == vcolor[j]) //i j 가 연결되어 있고 가은 색일 때
switch = false;
j++;
}
return switch
}
weight : level i 까지 weights의 합
profit : 그 node 까지 profits의 합
totweight =
bound (upper, 가능한 최대 profit) =
void knapsack(index i, int profit, int weight){
if(weight <=W && profit > maxprofit){
maxprofit = profit;
numbest = i;
bestset = include;
}
if(promising(i)){
include[i+1] = "yes";
knapsack(i+1, profit+p[i+1], weight + w[i+1]);
include[i+1] "no";
knapsack(i+1, profit, weight);
}
non-promising
bool promising(index i){
index j,k; int totweight; float bound;
if(weight >= W)
return false;
else {
j= i+1; bound = profit; totweight= weight;
while(j<=n && totweight+w[j] <= W){
totweight = totweight + w[j];
bound = bound + p[j];
j++;
}
k = j;
if(k<=n)
bound = bound + (W-totweight)* p[k]/w[k];
return bound>maxprofit;
}
}