2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

3 minute read

2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)


DFS ํŠธ๋ฆฌ ํƒ์ƒ‰์„ ์ƒ๊ฐํ•ด๋ณด์ž. ๋งŒ์•ฝ ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์—์„œ ์•ž์œผ๋กœ ๋” ํƒ์ƒ‰ํ•ด๋„ ์›ํ•˜๋Š” ์ตœ์ ํ•ด๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค๋ฉด, ๋” ๊นŠ์ด ๋ฐฉ๋ฌธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค! ๊ทธ๋ž˜์„œ ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ๋” ํ™•์žฅํ•˜์ง€ ์•Š๊ณ , ๋‹ค์Œ ๋…ธ๋“œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ํƒ์ƒ‰ ๊ธฐ๋ฒ•์ด ๋ฐ”๋กœ <Backtracking>์ด๋‹ค.

์–ด๋–ค ๋…ธ๋“œ์—์„œ ๋ฉˆ์ถ”๋Š” ํ–‰์œ„๋ฅผ <pruning; ๊ฐ€์ง€์น˜๊ธฐ>๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰ ํ•  Search Space๋ฅผ ์ค„์—ฌ ํƒ์ƒ‰์— ๋“œ๋Š” ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค!

๋ณดํ†ต <N-queen> ๋ฌธ์ œ๊ฐ€ <Backtracking> ๊ธฐ๋ฒ•์„ ์“ฐ๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ˆ˜์—…์—์„œ ์•ˆ ๋‹ค๋ค˜์œผ๋‹ˆ ๋„˜์–ด๊ฐ€๊ณ , ๋Œ€์‹  <SAT> ๋ฌธ์ œ๋ฅผ <Backtracking>์œผ๋กœ ํ’€์–ด๋ณด์ž.


SAT with Backtracking

6๊ฐœ์˜ clause์™€ 4๊ฐœ์˜ variable์ด ์žˆ๋Š” ์•„๋ž˜์˜ SAT ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž.

\[\phi(w, x, y, z) = (w \lor x \lor y \lor z) (w \lor \bar{x}) (x \lor \bar{y}) (y \lor \bar{z}) (z \lor \bar{w}) (\bar{w} \lor \bar{z})\]

์œ„ formula์˜ $(w \lor \bar{x})$ clause๋ฅผ ํ†ตํ•ด $w = 0, x = 1$์ด ์กด์žฌํ•˜๋Š” assignment๋Š” ๋ชจ๋‘ pruning ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, not-satisfying partial assignment๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋Š” Search Space๋ฅผ ์ค„์—ฌ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋‹ค๋ฅธ partial assignment์ธ $w = 0, x = 0$๋กœ ํƒ์ƒ‰์„ ๊ณ„์†ํ•ด๋„ satisfying assignment๋Š” ์ฐพ์„ ์ˆ˜ ์—†๋‹ค.

<SAT> ๋ฌธ์ œ๋ฅผ Backtracking์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ ค ํ•  ๋•Œ ์ƒ๊ฐํ•  ๋ถ€๋ถ„์ด ํ•˜๋‚˜ ๋” ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‹ค์Œ ์Šคํ…์—์„œ ์–ด๋–ค variable์„ ์ฒดํฌํ•ด partial assn์„ ํ™•์žฅํ•  ๊ฒƒ์ธ๊ฐ€? ์‰ฝ์ง€ ์•Š์€ ๋ฌธ์ œ์ด์ง€๋งŒ, Greedy ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

โ€œChoose the subproblem that contains the smallest clause, and then branch on a variable in that clause.โ€

SAT ๋ฌธ์ œ์—์„œ ๊ฐ€์žฅ ์ž‘์€ clause๋ผ ํ•˜๋ฉด $(z)$์™€ ๊ฐ™์€ singleton clause์ด๋‹ค. singleton clause์˜ ๊ฒฝ์šฐ, assn T/F ์ค‘ ํ•˜๋‚˜๋Š” ๋ฌด์กฐ๊ฑด non-satisfying์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด๋Ÿฐ <Backtracking> ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Start with some problem $P_0$

Let $S = \{ P_0 \}$, the set of active subproblems.

while $S$ is non-empty:
ย ย choose a subproblem $P \in S$ and remove it from $S$.
ย ย expand it into smaller subproblems $P_1, P_2, \dots, P_k$
ย ย for each $P_i$:
ย ย ย ย if $\text{test}(P_i)$ succeed: halt and report it as a solution.
ย ย ย ย if $\text{test}(P_i)$ succeed: halt and report it as a solution.
ย ย ย ย otherwise: add $P_i$ to $S$.

Announce that there is no solution.


<Backtracking>์˜ ํ•ต์‹ฌ์€ ๊ฐ€๋ง์ด ์—†๋Š” subproblem์„ ์ง€์›Œ๋ฒ„๋ฆฌ๋Š” ๊ฒ€์ƒ‰ ๋ฐฉ์‹์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด Search Space๋ฅผ ์ค„์—ฌ Exponential Time์ด ๊ฑธ๋ฆด ํƒ์ƒ‰์„ ํš๊ธฐ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ์˜ ์–ด๋–ค instance๊ฐ€ ๋‚˜์˜ค๋Š๋ƒ์— ๋”ฐ๋ผ, ๋˜๋Š” partial solution์„ ์–ด๋–ป๊ฒŒ ํ™•์žฅํ•  ๊ฑด์ง€์— ๋”ฐ๋ผ ๊ณ„์‚ฐ ์†๋„๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— input $N$์ด ๋„ˆ๋ฌด ํฌ๋‹ค๋ฉด, ์‚ฌ์šฉํ•˜๊ธฐ ์ ์ ˆํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ ํฌ์ŠคํŠธ์—์„  <Backtracking> ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํŒŒ์ƒ๊ฒฉ์ธ <Branch-and-Bound> ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ดํŽด๋ณธ๋‹ค ๐Ÿ™

๐Ÿ‘‰ Branch and Bound

Categories:

Updated: