๐ฉ๐ป๐พ
Chap 5. Backtracking ๋ณธ๋ฌธ
Backtracking
ํด๋ฅผ ์ฐพ๋ ๋์ค ๋งํ๋ฉด ์ด์ ๋จ๊ณ๋ก ๋์๊ฐ์ ํด๋ฅผ ์ฐพ์๊ฐ๋ ๊ธฐ๋ฒ
Backtracking์ ์ฌ์ฉํ๋ 3๊ฐ์ง ์์๋ฅผ ๋ณด์.
1. N-queens problem
- N x N ํฌ๊ธฐ์ ์ฒด์ค๋ณด๋ํ์ N๊ฐ์ ํธ๋ค์ ์๋ก ์ํํ์ง ์๊ฒ ๋ฐฐ์นํ๋ ๋ฌธ์
4 queens problem์ผ ๋
16๊ฐ์ ์๋ฆฌ ์ค 4์๋ฆฌ๋ฅผ ๋ฝ๋ ๊ฒฝ์ฐ : C(16, 4) = 1820
๋ ํธ์ด ๊ฐ์ ํ, ์ด ๋๋ ๋๊ฐ์ ์ ์์นํ ์ ์์ผ๋ฏ๋ก ๋ฐฐ์ ํ ์ ์๋ ๊ฒฝ์ฐ : 4 x 4 x 4 x 4 = 256
<i,j> : iํ j์ด์ ํธ์ด ์์น
- ์ฒ์ ์์ํ ๋์, <1, 2>์๋ง ํธ์ด ์์นํ ๋ -> ํ์ ๋ ธ๋์์ ๋ต์ ๋ฐ๊ฒฌํ ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฏ๋ก promising
- <1, 1> , <2, 1> , <3, 4>์ ํธ๋ค์ด ์์นํ ๋, ๊ฐ์ ์ด์ ์์นํ๋ฏ๋ก ๋ต์ด ๋ ์ ์์ผ๋ฏ๋ก nonpromising
- <1, 1> , <2, 2>์ ํธ๋ค์ด ์์นํ ๋ ๋ํ ๋๊ฐ์ ์ ์์นํ๋ฏ๋ก nonpromising
๊ฐ ๋ ธ๋๋ค์ด promisingํ์ง ํ์ธํ๋ฉฐ DFS ํ์ํ๋ค. ๋ง์ฝ nonpromising์ด๋ผ๋ฉด, ๋ถ๋ชจ๋ ธ๋๋ก ๋๋์๊ฐ์ ๋ค๋ฅธ ์์๋ ธ๋ ํ์ ์๋ํ๋ค.
โก๏ธ Backtracking์ ์ฌ๊ท์ ์ธ DFS ๋ฐฉ์์ผ๋ก ๊ตฌํ๋ ์ ์๋ค.
public static 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(u);
}
public static void queens(index i){
index j;
if(promising(i))
if(i == n)
system.out.print(col[1]...col[n])
else
for(j = 1; j <= n; j++){
col[i + 1] = j;
queens(i+1);
}
}
public static boolean promising(index i){
index k; boolean switch;
k = 1;
switch = true;
while(k < i && switch){
if(col[i] == col[k] || abs(col[i] - col[k] == i - k)
switch = false;
k++;
}
return swtich;
}
2. The m-coloring problem
- ์ต๋ m๊ฐ์ ์๋ก ๋ค๋ฅธ ์์ ์ฌ์ฉํ์ฌ ๋ฐฉํฅ์ด ์๋ ๊ทธ๋ํ๋ฅผ ์ธ์ ํ ๋ ์ ์ ์ด ๊ฐ์ ์์ด ๋์ง ์๊ฒ ์์น ํ๋ ๋ชจ๋ ๋ฐฉ๋ฒ์ ์ฐพ๋ ๋ฌธ์
3-coloring problem์ผ ๋
public static void m_coloring(index i){
int color;
if(promising(i))
if(i == n)
system.out.print(vcolor[1] .. vcolor[n]
else
for(color = 1; color <= m; color++){
vcolor[i + 1]= color;
m_coloring(i + 1);
}
}
public static bolean promising(index i){
index j; bool switch;
switch = true;
j = 1;
while(j < i && switch){
if(W[i][j] && vcolor[i] == vcolor[j]
switch = false;
j++;
}
}
3. The 0-1 Knapsack Problem
knapsack์ ์ํ
- current profit
- current weight
- the upper bound on the maximum profit -> item์ ๋ถ๋ถ ์กฐ๊ฐ์ ํ์ฉํ์ฌ ์ต๋ ์ด์ต์ ๊ตฌํ ์ ์๋ค.
์ ์ฌ์ง์ผ๋ก knapsack problem์ state space tree๋ฅผ ์ดํดํด๋ณด์.
ํ ๋ ธ๋์ 3๊ฐ์ง์ ๊ฐ์ด ๋ค์ด์๋๋ฐ ์์์๋ถํฐ ์์๋๋ก 'ํ์ฌ ์ด์ต, ํ์ฌ ๋ฌด๊ฒ, ๋ ธ๋๋ฅผ ํ์ฅํ์ ๋ ๊ฐ๋ฅํ ์ต๋์ด์ต'์ด๋ค.
- ์์ดํ ์ ์๋ฌด๊ฒ๋ ๋ฃ๊ธฐ ์ ์๋ ์ด์ต๊ณผ ๋ฌด๊ฒ๋ ๋ชจ๋ 0์ด๊ณ , ์ต๋ ์ด์ต์ item1์ ๊ฐ, item2์ ๊ฐ, item3์ ๊ฐ * 9/10๋ฅผ ๋ํด์ค ๊ฐ์ด๋ค. (์ด๋, ์ต๋ ์ด์ต์ ์์ดํ ์ ๋ถ๋ถ์กฐ๊ฐ์ ํ์ฉํ๊ธฐ ๋๋ฌธ์ item3์ ์ชผ๊ฐ์ ๋ด์ ์ ์๋ ๊ฒ์ด๋ค.)
- ์ด์ item 1์ ๋ด์์ ๋์ ๋ด์ง ์์์ ๋์ ๋
ธ๋๋ฅผ ๋ณด์.
- item1 in : item1์ ๊ฐ๊ฒฉ๊ณผ ๋ฌด๊ฒ๋ฅผ ๋ํด์ฃผ๊ณ ์ต๋์ด์ต์ ์ด์ ๊ณผ ๋์ผํ๋ค
- item 1 out : ๋ถ๋ชจ๋ ธ๋์ ๋ฌ๋ผ์ง๋ ๊ฒ์ ์ต๋์ด์ต์ด๋ค. item1์ ์ ์ธํ ๋๋จธ์ง item๋ค๋ก ๊ตฌํ ์ ์๋ ์ต๋์ด์ต์ผ๋ก ๊ฐ์ ๋ฐ๊ฟ์ค์ผํ๋ค. ์ด๋, ๋ฐ๋ ์ต๋์ด์ต ๊ฐ์ item2์ ๊ฐ, item3์ ๊ฐ, item4์ ๊ฐ * 1/5์ ํฉ์ธ $82์ด๋ค.
- ์ด๋ฐ ์์ผ๋ก 3๊ฐ์ง ๊ฐ๋ค์ ์ ๋ฐ์ดํธ ์ํฌ ๋, ์ด ๋ฌด๊ฒ๊ฐ 16๋ณด๋ค ํฌ๋ฉด backtracking์ ํ๋ค.
- node1๊น์ง state space tree๋ฅผ ํ์ฅ์ํค๊ณ node2์์ item4๋ฅผ ๋ด์ ๊ฒฝ์ฐ์ ๋ด์ง ์์ ๊ฒฝ์ฐ๋ฅผ ํ์ฅ์์ผ์ผ ํ๋๋ฐ node2์ ์ต๋์ด์ต์ด $50์ด๊ณ , current best solution์ด $90์ด๋ค. node2๋ฅผ ๋ ํ์ฅํด๋ ์ต๋ ์ด์ต์ด $90์ ๋ฏธ์น์ง ๋ชปํ๋ฏ๋ก ํ์ฅํ์ง ์๋๋ค.
public static 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;
}
}
'Study > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Chap 9. Introduction to the Theory of NP (0) | 2023.06.11 |
---|---|
Chap 7. Introduction to Computational Complexity: The Sorting Problem (0) | 2023.06.08 |
Chap 6. Branch and Bound (0) | 2023.06.06 |
Chap 4. The Greedy Approach (0) | 2023.06.01 |
Chap 3. Dynamic Programming (0) | 2023.04.20 |