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

5 minute read

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

ps) โ€œknapsackโ€์˜ ๋œป์€ โ€œํ•˜์ดํ‚น ๋”ฐ์œ„์˜ ์•„์ฃผ ๊ฐ„๋‹จํ•œ ์Šคํฌ์ธ ์— ์‚ฌ์šฉ๋˜๋Š” ์ž‘์€ ๋ฅ™์ƒ‰โ€์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด๊ณณ์— ๋ฅ™์ƒ‰์— ๋Œ€ํ•ด ์ž˜ ์„ค๋ช…์ด ๋˜์–ด ์žˆ์œผ๋‹ˆ, ๊ถ๊ธˆํ•œ ์‚ฌ๋žŒ์€ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์ž ๐Ÿ˜


๋„๋‘‘์ด ๋ณด์„๊ฐ€๊ฒŒ์— ๋ฐฐ๋‚ญ์„ ๋ฉ”๊ณ  ์นจ์ž…ํ–ˆ๋‹ค. ๋ฐฐ๋‚ญ์˜ ์ตœ๋Œ€ ์šฉ๋Ÿ‰์€ W์ด๋ฉฐ, ์ด๋ฅผ ์ดˆ๊ณผํ•ด์„œ ๋ณด์„์„ ๋‹ด์œผ๋ฉด ๋ฐฐ๋‚ญ์ด ์ฐข์–ด์งˆ ๊ฒƒ์ด๋‹ค. ๊ฐ ๋ณด์„๋“ค์˜ ๋ฌด๊ฒŒ์™€ ๊ฐ€๊ฒฉ์€ ์•Œ๊ณ  ์žˆ๋‹ค. ๋ฐฐ๋‚ญ์ด ์ฐข์–ด์ง€์ง€ ์•Š๋Š” ์„ ์—์„œ ๊ฐ€๊ฒฉ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ๋ณด์„์„ ๋‹ด๋Š” ๋ฐฉ๋ฒ•์€?

์œ„์™€ ๊ฐ™์ด <๋ƒ…์ƒ‰; knapsack> ๋ฌธ์ œ๋Š” ์œ ํ•œํ•œ ์ตœ๋Œ€ ์šฉ๋Ÿ‰, Capacity ์•„๋ž˜์—์„œ ๋‹ด๊ธด ๊ฐ’์˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ์ตœ์ ํ™” ๋ฌธ์ œ๋‹ค. ๋ƒ…์ƒ‰ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ๋ฐฐ๋‚ญ์— ๋„ฃ๋Š” ๋Œ€์ƒ์ด (1) pick w/ replacement (2) pick w/o replace (3) ๋Œ€์ƒ์„ ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š”์ง€(fractional knapsack)์— ๋”ฐ๋ผ์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ผ์ง„๋‹ค. (1), (2)๋ฒˆ ๊ฒฝ์šฐ๋Š” DP๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜๊ณ , (3)๋ฒˆ์˜ fractional knapsack์€ $v_i / w_i$์˜ ratio๋ฅผ ์ด์šฉํ•ด Greedy๋กœ ํ•ด๊ฒฐํ•œ๋‹ค๐Ÿ’ช


Pick w/ replacement

์ฒซ๋ฒˆ์งธ ๊ฒฝ์šฐ๋Š” ์•„์ดํ…œ์˜ ์ˆ˜๊ฐ€ ๋ฌดํ•œํžˆ ๋งŽ์€(unlimited quantities) ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค. ์ด๋•Œ, ๋ƒ…์ƒ‰ ๋ฌธ์ œ๋Š” capacity๋ฅผ 1๋ถ€ํ„ฐ ์˜ฌ๋ ค๊ฐ€๋ฉฐ ์ด์ „์— ๊ณ„์‚ฐํ•œ ๋ƒ…์ƒ‰ DP์˜ ๊ฐ’์— ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ, ๊ฐ€์žฅ ํฐ value๋ฅผ ์œ ๋„ํ•˜๋Š” ์กฐํ•ฉ์„ ์ฐพ๋Š” ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค. ์ผ๋‹จ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž!!

Initialize all $K(j) = 0$.

for $w=1$ to $W$
โ€ƒโ€ƒ$\displaystyle K(w) = \max_{1\le i\le n} \{ K(w - w_i) + v_i : w - w_i \ge 0 \}$

return $K(W)$

์ •๋ง ๊ฐ„๋‹จํ•˜๋‹ค!!!


Pick w/o replacement

๋‘๋ฒˆ์งธ ๊ฒฝ์šฐ๋Š”, ๋ฐฐ๋‚ญ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์•„์ดํ…œ์˜ ์ˆ˜๊ฐ€ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์žˆ๋Š”(only one of each item available) ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„  ์•„์ดํ…œ์„ ๊ณ ๋ฅผ์ง€ ๋ง์ง€๋กœ sub-problem์„ ์ƒ์„ฑํ•˜๋Š” binary choice์˜ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.1 ๊ทธ๋ž˜์„œ ์ „์ฒด $1$๋ถ€ํ„ฐ $n$๊ฐœ์˜ ์•„์ดํ…œ ์ค‘์—์„œ $j$-th ์•„์ดํ…œ๊นŒ์ง€ ์‚ฌ์šฉํ•ด ๋ƒ…์ƒ‰ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ $(j+1)$-th ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•œ ๊ฒฝ์šฐ๋ฅผ ํ’€๋•Œ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

์ด๋•Œ, $j$-th ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•  ๋•Œ, ๋ช‡๊ฐ€์ง€ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋Š”๋ฐ,

1. ๋งŒ์•ฝ ํ˜„์žฌ ์‚ดํŽด๋ณด๋Š” capacity $w$๊ฐ€ $j$-th ์•„์ดํ…œ์˜ ๋ฌด๊ฒŒ๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— $j$-th ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ง์ „์˜ ๋ƒ…์ƒ‰ solution์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ,

\[\text{if} \quad w_j > w \; \text{,} \quad \text{then} \quad K(w, j) = K(w, j-1)\]

2. ๋งŒ์•ฝ ํ˜„์žฌ $j$-th ์•„์ดํ…œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ($w - w_j \ge 0$)๋ผ๋ฉด, (1) $j$-th ์•„์ดํ…œ์„ ๋‹ด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ (2) $j$-th ์•„์ดํ…œ์„ ๋‹ด๋Š” ๊ฒฝ์šฐ์—์„œ์˜ ๊ฐ€์น˜๋ฅผ ๋น„๊ตํ•ด ๋” ํฐ ๊ฒƒ์„ ์ทจํ•œ๋‹ค.

\[\text{if} \quad w \ge w_j \; \text{,} \quad \text{then} \quad K(w, j) = \max \{ K(w, j-1), K(w-w_j, j-1) \}\]

์œ„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฝ”๋“œ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ 2์ฐจ์› DP ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค! ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž!

Initialize all $K(0, j) = 0$ and all $K(w, 0) = 0$.

for $j=1$ to $n$
โ€ƒโ€ƒfor $w=1$ to $W$
โ€ƒโ€ƒโ€ƒโ€ƒif $w_j > w$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$K(w, j) = K(w, j-1)$
โ€ƒโ€ƒโ€ƒโ€ƒelse
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$\displaystyle K(w, j) = \max \{ K(w, j-1), K(w-w_j, j-1) + v_j\}$

return $K(W, n)$

2์ฐจ์› DP์˜ ๋ชจ์Šต์„ ๊ทธ๋ ค๋ณด๋ฉด ์š”๋Ÿฐ ๋Š๋‚Œ์œผ๋กœ DP ๋ฐฐ์—ด์ด ์ฑ„์›Œ์ง„๋‹ค


๋ณธ์ธ์˜ PS ๊ฒฝํ—˜์ƒ, ๋ƒ…์ƒ‰ ๋ฌธ์ œ์ž„์„ ๋ช…ํ™•ํžˆ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฑด ์–ด๋ ต์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ, ๋ƒ…์ƒ‰ ๋ฌธ์ œ์ธ์ง€ ์ „ํ˜€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ, ์‚ฌ์‹ค์€ ๋ƒ…์ƒ‰์œผ๋กœ ํ’€์–ด์•ผ ๋˜๋Š” ๋ฌธ์ œ๋“ค์ด ์ข…์ข… ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด 2019 ICPC ํ•œ๊ตญ ์˜ˆ์„  1๋ฒˆ ๋ฌธ์ œ์ธ 17528๋ฒˆ: Two Machines๊ฐ€ ๋ƒ…์ƒ‰์ธ์ง€ ํŒ๋‹จํ•˜๋Š”๊ฒŒ ์‰ฝ์ง€ ์•Š์€ ๊ทธ๋Ÿฐ ๋ฌธ์ œ์˜€๋‹ค.


์ถ”์ฒœ ๋ฌธ์ œ

์ฐธ๊ณ ์ž๋ฃŒ


  1. ๋˜๋‹ค๋ฅธ DP ๋ฌธ์ œ์ธ Weighted Interval Scheduling์—์„œ๋„ binary choice ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค!ย 

Categories:

Updated: