๐Ÿ‘ฉ๐Ÿป‍๐ŸŒพ

Chap 5. Backtracking ๋ณธ๋ฌธ

Study/Algorithm

Chap 5. Backtracking

์ฅฌ์Šค์ด 2023. 6. 4. 20:46
728x90
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;
    }
}
728x90

'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
Comments