์•Œ๊ณ ๋ฆฌ์ฆ˜) Graph - Network Flow, Bipartite Matching

Network/Maximum Flow ์œ ๋Ÿ‰๊ทธ๋ž˜ํ”„ : ๊ฐ„์„ ์˜ weight๊ฐ€ ์ •์ ์—์„œ ์ •์ ์œผ๋กœ ๋ณด๋‚ผ์ˆ˜์žˆ๋Š” ์ตœ๋Œ€์œ ๋Ÿ‰์„ ์˜๋ฏธํ•˜๋Š” ๊ทธ๋ž˜ํ”„ ์ตœ๋Œ€์œ ๋Ÿ‰ : ํ•ด๋‹น ๊ฐ„์„ ์„ ํ†ตํ•ด ๋™์‹œ์— ํ˜๋ ค๋ณด๋‚ผ์ˆ˜์žˆ๋Š” ๋ฌผ์˜ ์ตœ๋Œ€์น˜ ์ด๋Ÿฌํ•œ ์œ ๋Ÿ‰๊ทธ๋ž˜ํ”„์—์„œ, Source์ •์ ์—์„œ Sink์ •์ ๊นŒ์ง€ ์ตœ๋Œ€ ์–ผ๋งˆ๋งŒํผ์˜ ์œ ๋Ÿ‰์ด ์ง€๋‚˜๊ฐˆ์ˆ˜์žˆ๋Š”์ง€์— ๊ด€ํ•œ ๋ฌธ์ œ๋ฅผ Network/Maximum Flow๋ผ๊ณ  ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด, ์ˆ˜๋กœ(๊ฐ„์„ )์— 1์ดˆ๋งˆ๋‹ค ์ง€๋‚ ์ˆ˜์žˆ๋Š” ๋ฌผ์˜ ์ตœ๋Œ€์น˜๊ฐ€ ์ •์˜๋˜์–ด์žˆ๋‹ค๋ฉด, ์ˆ˜๋กœ๋Š” ์ž์‹ ์˜ ์šฉ๋Ÿ‰์ด์ƒ์˜ ๋ฌผ์€ ํ†ต๊ณผ ์‹œํ‚ฌ์ˆ˜์—†๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ†ต๊ณผํ• ์ˆ˜์žˆ๋Š” ๋ฌผ์˜ ์ตœ๋Œ€๋Ÿ‰์€ ์™ผ์ชฝ์€ 2, ์˜ค๋ฅธ์ชฝ์€ 4์ด๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ๊ทธ๋ž˜ํ”„๋ฅผ Maximum Flow(์ตœ๋Œ€์œ ๋Ÿ‰)์œผ๋กœ ๋ณด๋‚ด๋ฉด ๊ทธ ๊ฐ’์€ 7์ด๋‹ค. ์ขŒ์ธก๊ฐ’์€ ์‹ค์ œ๋กœ ํ•ด๋‹น ๊ฐ„์„ ์— ํ๋ฅธ ์œ ๋Ÿ‰(flow), ์šฐ์ธก๊ฐ’์€ ํ•ด๋‹น ๊ฐ„์„ ์˜ ..

Algorithm 2019. 9. 13. 14:48
์•Œ๊ณ ๋ฆฌ์ฆ˜) ๊ธฐํ•˜ - Plane Sweeping, CCW, Convex Hull

Plane Sweeping ์ง๊ฐ๋„ํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ• ๋•Œ ์“ฐ์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณ‚์นœ ๋ถ€๋ถ„์ด ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๋‹จ์ˆœํ•œ ๊ณต์‹์„ ๋„“์ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š๋‹ค. ๊ฐ ์ง์‚ฌ๊ฐํ˜•์€ 4๊ฐœ์˜ ๊ผญ์ง“์ ์„ ๊ฐ€์ง„๋‹ค. ๋”ฐ๋ผ์„œ n๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•์ด๋ผ๋ฉด, ์ตœ๋Œ€ 2n๊ฐœ์˜ x์ขŒํ‘œ์™€ 2n๊ฐœ์˜ y์ขŒํ‘œ๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ ๊ผญ์ง“์ ์œผ๋กœ ๋‚˜๋ˆ„๋ฉด, ๊ฐ ์˜์—ญ์˜ ๋„“์ด๋ฅผ ๊ตฌํ•œ๋’ค ๊ทธ ํ•ฉ์œผ๋กœ ์ •๋‹ต์„ ๊ตฌํ• ์ˆ˜์žˆ๋‹ค. ๋จผ์ € ์‚ฌ์šฉ๋˜๋Š” x,y์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ•œ๋’ค ์ •๋ ฌ์‹œํ‚จ๋‹ค. (์›์†Œ๋“ค์ด ๊ผญ ์œ ์ผํ•  ํ•„์š”๋Š” ์—†๋‹ค) ์ดํ›„ x,y์ขŒํ‘œ๋กœ ์ž˜๋ ค์ง„ ์˜์—ญ๋“ค์„ ์ˆœํšŒํ•˜๋ฉฐ, ์‚ฌ๊ฐํ˜•์ด ํฌํ•จ๋˜์—ˆ์œผ๋ฉด ๋„“์ด๋ฅผ ๋”ํ•ด์ค€๋‹ค. ์ด๋•Œ, ์›์†Œ๋“ค์ด ์œ ์ผํ•˜์ง€์•Š๋‹ค๋ฉด ์ •๋ ฌ๋ ๋•Œ ๊ฐ™์€๊ฐ’์ด ๋ถ™์–ด์žˆ์œผ๋ฏ€๋กœ ๊ทธ ์˜์—ญ์˜ ๋„“์ด๊ฐ€ 0์ด ๋˜์–ด ๋ฌด์‹œํ•ด๋„ ๋˜๊ฒŒ ๋œ๋‹ค. ์ด ๊ณผ์ •์—์„œ Segment Tree๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌ๊ฐ„๊ฐฑ์‹ ์†๋„๋ฅผ ์ค„์—ฌ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ผ์ˆ˜์žˆ๋‹ค. ..

Algorithm 2019. 9. 13. 14:38
์•Œ๊ณ ๋ฆฌ์ฆ˜) KMP (Knuth–Morris–Pratt, ๋ฌธ์ž์—ด ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

KMP (Knuth–Morris–Pratt, ๋ฌธ์ž์—ด ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜) ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ž์—ด ์™„์ „ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋น„ํ•ด, ์•ฝ๊ฐ„์˜ ๋ฉ”๋ชจ๋ฆฌ๊ณต๊ฐ„์„ ์ด์šฉํ•ด ๋ถˆํ•„์š”ํ•œ ๋น„๊ตํšŸ์ˆ˜๋ฅผ ์ค„์—ฌ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, samsamsung ๋ฌธ์ž์—ด์—์„œ samsamsong ๋ฌธ์ž์—ด์„ ์ฐพ๋Š”๋‹ค๋ฉด ์›๋ž˜๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค s๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ญ‰ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๋‹ค๊ฐ€ u๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ์‹คํŒจํ•˜๊ณ  ์ดํ›„ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค a๋ถ€ํ„ฐ ๋‹ค์‹œ ๋น„๊ตํ•˜๊ธฐ ์‹œ์ž‘ํ•ด, ์ด๋• ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค๋ฅด๋ฏ€๋กœ ๋ฐ”๋กœ ์‹คํŒจ, ์ดํ›„ 2๋ฒˆ์งธ ์ธ๋ฑ์Šค m๋ถ€ํ„ฐ ~... ๋ฐฉ์‹์œผ๋กœ ๋น„๊ตํ•œ๋‹ค. => ์ด๋•Œ KMP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋งจ์ฒ˜์Œ u๊ฐ€ ๋‹ฌ๋ผ์„œ ์‹คํŒจํ•œ ์ƒํƒœ์—์„œ samsamsung์˜ sams์™€ samsamsong์˜ sams๊ฐ€ ๊ฐ™๋‹ค๋Š” ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋‹ค. (๋ฌผ๋ก  ๊ฒฐ๊ณผ์ ์œผ๋ก  ๋น„๊ต๊ฐ€ ์‹คํŒจํ•˜๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ) ์œ„์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด..

Algorithm 2019. 9. 13. 14:32
์•Œ๊ณ ๋ฆฌ์ฆ˜) 5 DP Examples - Knapsack, LCS, LIS, Edit Distance, Matrix Chain Multiplication

โ€ป ๊ท€์ฐฎ์•„๋„ ์ง์ ‘ ์†์œผ๋กœ ์“ฐ๊ณ  ๊ทธ๋ฆฌ๋ฉฐ ์ƒ๊ฐํ•ด์•ผ ์ดํ•ด๊ฐ€ ๋น ๋ฅด๋‹ค Example 1 : Knapsack (๋ฐฐ๋‚ญ ๋ฌธ์ œ) Dynamic Programming ๋ฌด๊ฒŒ์™€ ๊ฐ€์น˜๊ฐ€ ์ •ํ•ด์ง„ N๊ฐœ์˜ ๋ฌผ๊ฑด์ด ์žˆ์„๋•Œ, ๊ฐ€์น˜์˜ ์ดํ•ฉ์ด max๊ฐ€ ๋˜๋„๋ก ๋ฐฐ๋‚ญ์„ ์‹ธ๋Š” ๋ฌธ์ œ. ์ด๋•Œ, ๋ฐฐ๋‚ญ์˜ ๋ฌด๊ฒŒ ์ œํ•œ์€ K์ด๋‹ค. (๊ฐ ๋ฌผ๊ฑด์€ 1๊ฐœ์”ฉ๋งŒ ์กด์žฌํ•˜๋ฉฐ, ๋ฐฐ๋‚ญ์— ๋„ฃ๊ฑฐ๋‚˜/์•ˆ๋„ฃ๊ฑฐ๋‚˜) ๋™์ „๊ตํ™˜๋ฌธ์ œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Greedyํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ๋˜์ง€์•Š์Œ. ex) V[N] = {13,8,6,12} W[N] = {6,4,3,5} D[i][j] = i๋ฒˆ์งธ ๋ฌผ๊ฑด๊นŒ์ง€ ๋„ฃ์„์ง€/์•ˆ๋„ฃ์„์ง€ ์ •ํ–ˆ์„๋•Œ, ๋ฌด๊ฒŒ j์ดํ•˜๋ฅผ ์ฑ„์šฐ๋Š” ๊ฐ€์น˜์˜ ํ•ฉ์ค‘ ์ตœ๋Œ€๊ฐ’ => ์ ํ™”์‹ : D[i][j] = Max(D[i-1][j], D[i-1][j-W[i]]+V[i]) Max์—์„œ ๋น„๊ตํ•˜๋Š” ๋‘ ๊ฐ’์ค‘, ์ „์ž๋Š” ์ด์ „..

Algorithm 2019. 9. 13. 14:22
์•Œ๊ณ ๋ฆฌ์ฆ˜) DP (Dynamic Programming, ๋™์ ๊ณ„ํš๋ฒ•), ex: Coin Change Problem

Dynamic Programming (๋™์ ๊ณ„ํš๋ฒ•) ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž‘์€๋ถ€๋ถ„๋ฌธ์ œ(sub-problem)์œผ๋กœ ๋‚˜๋ˆ„์–ด ํ•ด๊ฒฐํ•˜๋Š” ๊ธฐ๋ฒ•. ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๋ถ€๋ถ„ ๋ฌธ์ œ๊ฐ€ ๋‹ค์‹œ ๋ฐœ์ƒํ•˜๋ฉด, ์ƒˆ๋กญ๊ฒŒ ๊ณ„์‚ฐํ•˜์ง€์•Š๊ณ  ์ด์ „์˜ ๊ณ„์‚ฐ๊ฐ’์„ ์ฐธ์กฐํ•ด ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. DP๋ฅผ ํ†ตํ•ด ๋ถ€๋ถ„๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ํ•ด๊ฒฐํ•˜๋Š” ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด์ „ ๊ณ„์‚ฐ๊ฐ’์„ ์ €์žฅํ•  ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋ฏ€๋กœ Time - Space๊ฐ„์˜ trade off๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ex. ํ”ผ๋ณด๋‚˜์น˜ ๋‹จ์ˆœํƒ์ƒ‰์œผ๋กœ ๊ตฌํ˜„์‹œ, ์œ„์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ๋ฒˆ ๊ณ„์‚ฐ๋œ๋‹ค. => Memorization : ํ•œ๋ฒˆ ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ๊ธฐ๋กํ•ด๋‘๊ณ  ์ดํ›„ ์ค‘๋ณตํ˜ธ์ถœ ๋˜์—ˆ์„๋•Œ ์ƒˆ๋กญ๊ฒŒ ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ์•„๋‹Œ ์ €์žฅํ•ด๋‘”๊ฐ’์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•œ๋‹ค. fibonacci_topdown Top-Down / Bottom-Up Top-down ๋ฐฉ์‹์€ ํฐ ๋ฌธ์ œ์—์„œ..

Algorithm 2019. 9. 13. 14:15
์•Œ๊ณ ๋ฆฌ์ฆ˜) Disjoint-set Algorithm (์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

Disjoint-set Algorithm (์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜) ์ง‘ํ•ฉ, ๊ทธ๋ฃน์„ ๊ด€๋ฆฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ ๊ทธ๋ฃน์„ ํŠธ๋ฆฌ๊ตฌ์กฐ๋กœ ๊ด€๋ฆฌ ํฌ๊ฒŒ 2๊ฐ€์ง€์˜ ์—ฐ์‚ฐ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. find(x) : x๋ฒˆ ๋…ธ๋“œ์˜ root๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜ link(x, y) : x ๋…ธ๋“œ๊ฐ€ ์†ํ•œ ๊ทธ๋ฃน๊ณผ y ๋…ธ๋“œ๊ฐ€ ์†ํ•œ ๊ทธ๋ฃน์„ ํ•ฉ์น˜๋Š”(์—ฐ๊ฒฐํ•˜๋Š”) ํ•จ์ˆ˜ ๋˜ํ•œ ์œ„ ์—ฐ์‚ฐ์„ ์œ„ํ•ด, parent[i] : i๋ฒˆ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋กœ ์ •์˜๋˜๋Š” ๋ฐฐ์—ด์ด ์‚ฌ์šฉ๋œ๋‹ค. ์•„๋ž˜์˜ ๋‚ด์šฉ์€ codeground ์‚ฌ์ดํŠธ์—์„œ ๊ฐ€์ ธ์˜จ ๋‚ด์šฉ Disjoint-Set ์„œ๋กœ์†Œ ์ง‘ํ•ฉ(Disjoint-Set)์€ ์ง‘ํ•ฉ, ํ˜น์€ ๊ทธ๋ฃน์„ ๊ด€๋ฆฌํ•˜๋Š” ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๊ทธ๋ฃน์„ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€์˜ ์—ฐ์‚ฐ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. find(x): x๋ฒˆ ๋…ธ๋“œ์˜ ์ตœ๊ณ  ์กฐ์ƒ(๋ฃจํŠธ)์„ ์ฐพ๋Š” ํ•จ์ˆ˜ ..

Algorithm 2019. 9. 13. 14:11
์•Œ๊ณ ๋ฆฌ์ฆ˜) Graph - Dijkstra/Floyd-Warshall/Bellman-Ford 's Alg (distance)

Dijkstra Algorithm ๊ทธ๋ž˜ํ”„์˜ ํ•œ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (๋งŒ์•ฝ ๋ชจ๋“  ๋…ธ๋“œ ์Œ๊ฐ„์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ• ๋•, n๊ฐœ์˜ ๊ฐ ์ •์ ์— ๋Œ€ํ•ด Dijkstra Alg๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋ฏ€๋กœ, ์ด๋•Œ๋Š” Floyd-Warshall Alg์ด ๋” ์œ ๋ฆฌํ•˜๋‹ค) ๋™์ž‘๊ณผ์ •์€ BFS์™€ ๋น„์Šทํ•˜์ง€๋งŒ, Queue๊ฐ€ ์•„๋‹Œ Priority Queue๋ฅผ ์ด์šฉํ•œ๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„=O(ElogE) ํ•ด๋‹น๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•  distance[] ๋ฐฐ์—ด ์ค€๋น„ํ›„, ๋ฏธ๋ฐฉ๋ฌธ์„ ์˜๋ฏธํ•˜๋Š” -1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๋…ธ๋“œ1์—์„œ ์‹œ์ž‘ํ•œ๋‹ค๋ฉด, ์ด๋•Œ๊นŒ์ง€ ์ž๊ธฐ์ž์‹  1๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” 0์ด๋ฏ€๋กœ distance[1]์— 0์„, pq์— (1,0)์„ ๋„ฃ๋Š”๋‹ค. 2์˜10๋ณด๋‹ค ๊ฐ€๊น๋‹ค. distance[2]=8๋กœ ๊ฐฑ์‹ , pq์—๋„ (2,8) ๋„ฃ๋Š”๋‹ค. pq์—” (..

Algorithm 2019. 9. 13. 14:00
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ