ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

CS/Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜] DP(Dynamic Programming)๋ž€?

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 10. 25. 16:31

DP๋ž€?

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๋™์  ๊ณ„ํš๋ฒ•์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์—ฐ์‚ฐ ์†๋„์™€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ตœ๋Œ€ํ•œ์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

 

DP๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋ฉด ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • problem์ด ๋” ์ž‘์€ sub-problem๋“ค๋กœ ์ชผ๊ฐœ์งˆ ์ˆ˜ ์žˆ์„๋•Œ
  • sub-problem๋“ค์˜ ์†”๋ฃจ์…˜์œผ๋กœ ๋” ํฐ ๊ทœ๋ชจ์˜ problem์˜ ์†”๋ฃจ์…˜์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์„ ๋•Œ
  • ์ชผ๊ฐœ์ง„ sub-problem๋“ค์ด ๊ฒน์น  ๋•Œ

์ฆ‰, ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด ํ’€์–ด ์ €์žฅ(Memoization) ํ•จ์œผ๋กœ์จ, ํ•œ ๋ฒˆ ๊ณ„์‚ฐํ•œ ๋ฌธ์ œ๋Š” ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.


Top-Down vs Bottom-up

DP๋กœ ํ’€์ดํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€, Top-Down ๋ฐฉ์‹๊ณผ Bottom-up๋ฐฉ์‹์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

 

Top-Down ๋ฐฉ์‹

์šฐ๋ฆฌ๋ง๋กœ 'ํ•˜ํ–ฅ์‹'์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ ํฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์€ ๋ฌธ์ œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

  • ๋ณดํ†ต ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ†ตํ•ด ๋‹ต์„ ๊ตฌํ•จ
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ํšŸ์ˆ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด *๋ฉ”๋ชจ์ด์ œ์ด์…˜ ๊ธฐ๋ฒ•์ด ์‚ฌ์šฉ๋จ

 

*๋ฉ”๋ชจ์ œ์ด์…˜ ๊ธฐ๋ฒ•

์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ์ด ๋™์ผํ•œ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๋•Œ, ์ด์ „์— ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•จ์œผ๋กœ์จ ๋™์ผํ•œ ๊ณ„์‚ฐ์˜ ๋ฐ˜๋ณต ์ˆ˜ํ–‰์„ ์ œ๊ฑฐํ•˜์—ฌ ํ”„๋ž˜๊ทธ๋žจ ์‹คํ–‰ ์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•˜๋Š” ๊ธฐ์ˆ 

 

์˜ˆ๋ฅผ ๋“ค์–ด dp[n]์— ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ๊ฐ’์„ ์ €์žฅํ•จ์œผ๋กœ์จ ๋ฉ”๋ชจ์ œ์ด์…˜ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const N = 10;
const dp = Array.from({length: N+1}, () => -1);
const fibonacci = (n) => {
    if (n == 0) return 0;
    if (n == 1) return 1;

    if (dp[n] != -1) return dp[n];

    dp[n] = fibonacci(n - 1) + fibonacci(n - 2);
    return dp[n];
}

console.log(fibonacci(10)); // 55

 

Bottom-Up ๋ฐฉ์‹

์šฐ๋ฆฌ๋ง๋กœ '์ƒํ–ฅ์‹'์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ œ๋“ค๋ถ€ํ„ฐ ๋‹ต์„ ์ฐพ์•„ ๊ฐ€๋ฉด์„œ ๋งˆ์ง€๋ง‰์—๋Š” ์ „์ฒด ๋ฌธ์ œ์˜ ๋‹ต์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

  • ๋ณดํ†ต ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ๊ตฌํ˜„
  • ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹œ๊ฐ„๊ณผ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์šฉ๋Ÿ‰์ด ์ƒ๋Œ€์ ์œผ๋กœ ์ ๋‹ค๋Š” ์ด์ ์ด ์žˆ์Œ

์˜ˆ๋ฅผ ๋“ค์–ด ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

const N = 10;
const dp = Array.from({length: N+1}, () => -1);
const fibonacci = (n) => {
    dp[0] = 0, dp[1] = 1;
    for (let i = 2; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
}
fibonacci(10);
console.log(dp);
// [ 0, 1,  1,  2,  3, 5, 8, 13, 21, 34, 55 ]

 

๋‘ ๋ฐฉ์‹์˜ ๊ตฌํ˜„ ๋‚œ์ด๋„๊ฐ€ ๋ฌธ์ œ๋งˆ๋‹ค ๊ต‰์žฅํžˆ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— DP๋ฌธ์ œ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


[์ฐธ๊ณ ]

https://velog.io/@kimdukbae/%EB%8B%A4%EC%9D%B4%EB%82%98%EB%AF%B9-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-Dynamic-Programming

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ (Dynamic Programming)

ํ˜„์‹ค์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค ํ•  ๋•Œ ์ปดํ“จํ„ฐ๋กœ๋„ ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์šด ๋ฌธ์ œ๋“ค์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋ณดํ†ต ์ตœ์ ์˜ ํ•ด๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ๋งค์šฐ ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ฑฐ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ๋งŽ์ด ํ•„์š”

velog.io

https://www.youtube.com/watch?v=eJC2oetXaNk 

https://velog.io/@chnh506/Top-down-%EB%B0%A9%EC%8B%9D%EA%B3%BC-Bottom-up-%EB%B0%A9%EC%8B%9D

 

Top-down ๋ฐฉ์‹๊ณผ Bottom-up ๋ฐฉ์‹

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹, Top-down๊ณผ Bottom-up

velog.io