๐ค Dynamic Programming
๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ์ด๋ ํฐ ๋ฌธ์ ๋ฅผ ์์๋ฌธ์ ๋ก ๋๋์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ์ ๋งํฉ๋๋ค.
๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ๋ ๋ค์ํ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
์ด๋ฒ ํฌ์คํ ์์๋ LIS๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด Dynamic Programming์ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ๋ ๊ณผ์ ์ ์ ๋ฆฌํ์ต๋๋ค.
๐ LIS(Longest Increasing Subsequence)
โ๏ธSubsequence
๊ธธ์ด n ์ธ sequence (ordered list) $S = a_1, a_2, …, a_n$ ์ด ์ฃผ์ด์ก์ ๋, sequence $S’ = a_{i1}, a_{i2}, .., a_{ik} ๊ฐ 1≤ i1 < i2 < … < ik ≤n$ ๋ฅผ ๋ง์กฑํ ๊ฒฝ์ฐ $S’$ ์ $S$์ subsequence ๋ผ๊ณ ํฉ๋๋ค.
๋ํ $a_1 < a_2 < …< a_n$ ์ผ ๋, sequence S ๋ increasing sequence๋ผ๊ณ ํ ์ ์์ต๋๋ค.
โ๏ธ LIS
LIS๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด DP๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ด์ ํฌ์คํธ์์ ๋งํ ํด๊ฒฐ ๊ณผ์ ์ ํตํด LIS๋ฅผ ๊ตฌํด๋ณด๊ฒ ์ต๋๋ค.
1. ๋ฌธ์ ๋ฅผ ์ ํํ๊ฒ ์ ์ํ๋ค.
S๋ฅผ $a_1, a_2, ..., a_n$๋ก ๊ตฌ์ฑ๋ Sequence๋ผ๊ณ ํ๊ฒ ์ต๋๋ค.
Problem : S์์ ๊ฐ์ฅ ๊ธด increasing subsequence์ ๊ธธ์ด ๊ตฌํ๊ธฐ.
2. ์ ์๋ ๋ฌธ์ ์ ๋ํ subproblem ์ ์ ์ํ ๋ค subproblem ์ ์ด์ฉํ recurrence relation ์ ์ธ์ด๋ค.
L(i) = S์ ์ฒซ i๊ฐ์ element $a_1, a_2, … , a_i$ ๋ก ์ด๋ฃจ์ด์ง sequence ์์ LIS ์ ๊ธธ์ด๋ผ๊ณ ๋์.
L(i)๋ฅผ L(1), L(2), …, L(i-1) ๋ก ๋ํ๋ผ ์ ์์ ์ง๋ฅผ ํ์ธํฉ๋๋ค.
L(i)๋ฅผ ์ฒซ i๊ฐ์ element $a_1, a_2, … , a_i$ ๋ก ์ด๋ฃจ์ด์ง sequence ์์ $a_i$ ๋ก ๋๋๋increasing subsequence ๋ค ์ค ๊ฐ์ฅ ๊ธด subsequence์ ๊ธธ์ด๋ผ ํ์
L(i)๋ฅผ ์์ ๊ฐ์ด ์ ์ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ recurrence relation์ด ๋ง์กฑํ๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
$$L(i) = 1 + max_{j < i, a_j < a_i}L(j)$$
๋ํ ์ด ๊ฒฝ์ฐ S์ LIS์ ๊ธธ์ด๋ $max^n_{i=1}L(i)$๊ฐ ๋ฉ๋๋ค.
์์ recurrence relation์์ ์ค์ํ ํฌ์ธํธ๋ "$a_i$๋ก ๋๋๋"์ ๋๋ค.
์ด๋ฅผ ํตํด memoization์ ์คํ ํ ์ ์์ต๋๋ค.
๐ LIS ์๋์ฝ๋

Subproblem $L(i)$๋ฅผ ๊ตฌํ๊ธฐ ์ํด์ $L(1), ... , L(i-1)$์ ๊ฐ์ ์์์ผ ํ๋ฏ๋ก $L(1)$๋ถํฐ ์ผ์ชฝ ๋ถํฐ ํด๊ฒฐํด์ผ ํฉ๋๋ค.
3. Memoizaion ์ ์ด์ฉํ์ฌ recurrence relation ๊ณ์ฐํฉ๋๋ค.
- ์ค๊ฐ ๊ฒฐ๊ณผ๋ฌผ๋ค์ ์ ์ฅํ data structure๋ฅผ ๊ณ ๋ฆ ๋๋ค. (๋ณดํต n ์ฐจ์ array ์ฌ์ฉ.)
→ 1์ฐจ์ array๋ฅผ ์ฌ์ฉํ๋ฉด $L(i)$๊ฐ์ ์ ์ฅํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- Subproblem ๋ค ๊ฐ์ dependency๋ฅผ ํ์ธํฉ๋๋ค. (์ : $ ์ ๊ตฌํ๋ ค๋ฉด ์ด๋ค subproblem์ ๋ต๋ค์ ๊ณ์ฐํด ๋์์ผ ํ๋๊ฐ?)
→ $L(i)$๋ $L(j)$์ ๊ฐ์ ์์กดํฉ๋๋ค.
- Dependency ์ ๊ธฐ๋ฐํ์ฌ subproblem ๋ค์ ๋ต์ ๊ตฌํ๋ ์์๋ฅผ ์ ํฉ๋๋ค.
์ด๋ฅผ ์์ ๋ฅผ ํตํด ์ดํดํด๋ณด๊ฒ ์ต๋๋ค.
LIS์ ๊ธธ์ด = L(7) = L(8) = 4
- ์ค์ LIS ๋ $L(i) = L(j)+1$ ๋ก ์ ๋ฐ์ดํธ ํ ๋๋ง๋ค $a_i$ ๋ก ๋๋๋ LIS ์ ์ง์ element ๊ฐ $a_j$๋ผ๋ ์ฌ์ค์ ๋ฐ๋ก array ๋ก ์ ์ฅํฉ๋๋ค.(์ ์์์ pre)
์ด ๊ฒฝ์ฐ S์ LIS ๊ฐ max ์์ ๋๋๋ค๋ฉด $S_max$ ๋ก๋ถํฐ pre ๋ฅผ ๋ฐ๋ผ ์ญ์ผ๋ก ์ถ์ ํ๋ฉด ๋ฉ๋๋ค.
4. Time complexity๋ฅผ ๋ถ์ํ๊ณ ์๊ณ ๋ฆฌ์ฆ์ ์์ฑํฉ๋๋ค.
$L(i)$๋ฅผ ๊ณ์ฐํ ๋ ์กฐ๊ฑด์ ๋ง๋ $L(j)$ ๊ฒ์ → $O(i - 1)$
์ฆ, i๋ฒ์งธ element๋ฅผ ๋ถ์ํ ๋ ๋ง๋ค i-1๋ฒ์ ์ฐ์ฐ์ ์งํํ๋ค๋ ๊ฒ์ด๋ฏ๋ก ์ด ์๊ฐ๋ณต์ก๋ → $O(1 + 2 + ... + n-1) = O(n^2)$
๋ชจ๋ $L(i)$ ๊ณ์ฐ ๋ค, ํด๋น ๊ฐ์ด ๊ฐ์ฅ ํฐ i๋ฅผ ์ฐพ์ต๋๋ค. → $O(n)$
์ด ์๊ฐ๋ณต์ก๋ $O(n^2)$
์ฐธ๊ณ ๋ก LIS๋ฅผ ๊ตฌํ๋ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ณ ๋ฆฌ์ฆ์ $O(nlogn)$ ์ ๋๋ค.
โ๏ธ LIS๋ฅผ ๊ตฌํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ
DAG๋ฅผ ์ด์ฉํ์ฌ LIS๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
์ด๋ ์๋์ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ์๊ธธ ๋ฐ๋๋๋ค.
https://www.geeksforgeeks.org/find-longest-path-directed-acyclic-graph/
Longest Path in a Directed Acyclic Graph - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
๋ค์ ํฌ์คํ ์์๋ Edit Distance ์๊ณ ๋ฆฌ์ฆ์ ๋ํด ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
'Computer Science > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm] Dynamic Programming(4) - ๋ฒจ๋งํฌ๋(Bellman-Ford) (0) | 2022.11.19 |
---|---|
[Algorithm] Dynamic Programming(3) - Edit Distance(ํธ์ง๊ฑฐ๋ฆฌ) (0) | 2022.11.19 |
[Algorithm] Dynamic Programming(1) - Warm Up (0) | 2022.11.15 |
[Algorithm] ๋ถํ ์ ๋ณต(4) - Selection (0) | 2022.10.20 |
[Algorithm] ๋ถํ ์ ๋ณต(3) - Matrix Multiplication (0) | 2022.10.20 |