๋ฐ˜์‘ํ˜•
[ Algorithm / ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ] + ๋ฐฑ์ค€ 1865 ์›œํ™€ ๋ฌธ์ œํ’€์ด
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2024. 8. 22. 15:26[ Algorithm / ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ] + ๋ฐฑ์ค€ 1865 ์›œํ™€ ๋ฌธ์ œํ’€์ด

"๋ฒจ๋งŒ ํฌ๋“œ(bellman-ford) ์•Œ๊ณ ๋ฆฌ์ฆ˜ " ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„์ด ๋ง˜์— ์•ˆ๋“ ๋‹ค. =3= ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‚˜ ์ˆ˜ํ•™์  ๋ฒ•์น™์„ ๋ณด๋ฉด ๋ฐœ๊ฒฌํ•œ ์‚ฌ๋žŒ๋“ค์˜ ์ด๋ฆ„์œผ๋กœ ์ด๋ฆ„์„ ๋งŒ๋“ ๋‹ค. ์ง๊ด€์ ์ด์ง€ ์•Š์•„์„œ ๊ณ„์† ๊นŒ๋จน๊ฒŒ ๋œ๋‹ค. ์ง๊ด€์ ์œผ๋กœ ์ด๋ฆ„์„ ์ง€์–ด์คฌ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "์Œ์ˆ˜์‚ฌ์ดํด ๋‹ค์ต์ŠคํŠธ๋ผ" ์ด๋Ÿฐ์‹์œผ๋กœ ~  1. ์ตœ๋‹จ ๊ฒฝ๋กœ ํŠธ๋ฆฌ (shortest-path tree), ์ตœ์ ํ•ด ๊ตฌ์กฐ(optimal substructure ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋‹ต์€ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์กด์žฌ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋‹ค.  2. ์Œ์ˆ˜ ์‚ฌ์ดํด์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์ต์ŠคํŠธ๋ผ๋ž‘ ๋‹ค๋ฅธ ์ !!์œผ๋กœ ์Œ์ˆ˜ ์‚ฌ์ดํด์˜ ๋ฐœ์ƒ์œผ๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๋ฌดํ•œ์œผ๋กœ ๋น ์ง€๋Š” ์Œ์ˆ˜์ฐจ์ดํด์„ ์ฐพ๋Š”๋‹ค.  3. ๋ชจ๋“  ์Œ์ˆ˜ ๊ฐ„์„ ์ด ๋ฌธ์ œ๋‹ค ? ์•„๋‹ˆ๋‹ค. (-) ๋งˆ์ด๋„ˆ์Šค ๊ฐ’์ด ์žˆ๋Š”..

[ Algorithm/ KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜ ]
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2024. 7. 23. 16:39[ Algorithm/ KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜ ]

" Algorithm/ KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜ " ๐Ÿ”ธ KMP( Knuth, Morris, Prett ) Algorithm? ๋Œ€ํ‘œ์ ์ธ ์ƒ์ˆ˜์‹œ๊ฐ„ ๋‚ด์— ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋ช…์นญ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์“ฐ์ž„์ƒˆ์™€ ์ƒ๊ด€์—†์ด ๋งŒ๋“  ์‚ฌ๋žŒ์ด๋ฆ„์ด๋‹ค. ๋ฌธ์ž์—ด ๋งค์นญ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๊ฒŒ(?) ๋” ์ข‹์„๊ฑฐ ๊ฐ™๋‹ค.  ๐Ÿ”ธ ํ•ด๋‹ตhttps://www.acmicpc.net/problem/1786"baekjoon 1786๋ฒˆ - ์ฐพ๊ธฐ"์™€ ํ•จ๊ป˜ kmp ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐฐ์šฐ๊ณ  ํ’€์–ด๋ณด์ž. P: ํŒจํ„ด(๊ธธ์ด: m) T: ํ…์ŠคํŠธ(๊ธธ์ด: n) ๋งŒ์•ฝ ๋ชจ๋“  ๋งค์นญ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ํ™•์ธํ•œ๋‹ค๋ฉด? (n - m + 1) x m  = ์‹œ๊ฐ„ ๋ณต์žก๋„ O(nm)๋ชฉํ‘œ: ์‹œ๊ฐ„ ๋ชฉ์žก๋„ O(n) ๋งŒ๋“ค๊ธฐ.  ๐Ÿ”ฅ step 1.  ์ฐพ์œผ๋ ค๋Š” ๋ฌธ์ž์—ด ์ „์ฒ˜๋ฆฌ ๊ณผ์ • ๊ฑฐ์น˜๊ธฐ ์ ‘๋‘์‚ฌ(์ด์„ ์ ‘ : ๆŽฅ..

binary search & lower bound & upper bound [c++ ๊ตฌํ˜„]
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2024. 7. 5. 16:46binary search & lower bound & upper bound [c++ ๊ตฌํ˜„]

' binary search & lower bound & upper bound ' ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด ์ด๋ถ„ ํƒ์ƒ‰์„ ์ž์ฃผํ•˜๊ฒŒ ๋œ๋‹ค. ๋Œ€๋ถ€๋ถ„ ๋‚ด์žฅํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€๋งŒ,  ๋ฌธ์ œ ์ข…๋ฅ˜์— ๋”ฐ๋ผ์„œ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ์กฐ๊ฑด์„ ๋”ํ•ด์•ผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. c++ ๊ธฐ์ค€์œผ๋กœ ์ด์ง„ ํƒ์ƒ‰ ํ•จ์ˆ˜๋Š” ์ด 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ด ์„ธ๊ฐ€์ง€ ๋ชจ๋‘ ์ด์ง„ ํƒ์ƒ‰์ด ๊ธฐ๋ฐ˜์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. 1. [ #include ]  std::binary_searchval๊ฐ’์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ [ return bool ] std::lower_bound๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜. 2. [ #include ] std::lower_bound val๊ฐ’์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ 3. [ #include..

[Algorithm/ ๋น„ํŠธ๋งˆ์Šคํฌ]
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2023. 2. 17. 18:43[Algorithm/ ๋น„ํŠธ๋งˆ์Šคํฌ]

๊ณต๋ถ€ ์ „ Tmi.๋งค์ผ ์ผ ์ง‘ ์ผ ์ง‘ ํ•˜๋‹ˆ ๊พธ๋ฏธ๋Š” ๋‚ ๋„ ์—†๊ณ  ๋‚˜๋ฅผ ๋„ˆ๋ฌด ๋Œ€์ถฉ๋Œ€์ถฉ ์‚ด๊ฒŒ ๋‘๋Š” ๊ฑฐ ๊ฐ™์•„์„œ ํ˜„ํƒ€๊ฐ€ ์˜ต๋‹ˆ๋‹ค. ์˜ท๋„ ๊ฐ€๋ฐฉ๋„ ํ™”์žฅํ’ˆ๋„ ์•ˆ ์‚ฐ ์ง€ ์˜ค๋ž˜ ๋œ ๊ฑฐ ๊ฐ™์•„์š”. ๋‚˜๋ฅผ ์œ„ํ•œ ์†Œ๋น„๋„ ์—†์ด ๊ทธ๋ƒฅ ๋จน๊ณ  ์ž๊ณ  ์ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ด๋ฉด ์ข‹์„๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ์ข…์ข… ๋“ญ๋‹ˆ๋‹ค. ์ž˜ ์‚ด๊ณ  ์žˆ๋Š”๊ฑด ์•„๋‹Œ๊ฑฐ ๊ฐ™์ฃ  ? ใ… ใ…  ๊ณต๋ถ€๋„ ์กฐ๊ธˆ ์‰ฌ์—ˆ์–ด์š”. ๋‚˜์˜ ์†๋„๋กœ ๊ณต๋ถ€ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋‚จ๋“ค์˜ ์†๋„๋ฅผ ๋ณด๊ณ  ๋ถˆ์•ˆํ•จ์„ ๋Š๋ผ๋ฉด์„œ ์šฐ์šธ๊ฐ์— ์‰ฌ๋Š” ๊ฒƒ…? ์ฐธ ์•„์ด๋Ÿฌ๋‹ˆํ•ฉ๋‹ˆ๋‹ค. (ํ•œ์‹ฌ..) ๋ฐฑ์ค€ ๋“œ๋””์–ด 200๊ฐœ ๋ŒํŒŒํ–ˆ์Šต๋‹ˆ๋‹ค. ์•„์ง ์ ์€ ์ˆ˜์ง€๋งŒ ์ด๋ฒˆ ์—ฐ๋„์— 500๊ฐœ๋Š” ๋„˜๊ณ  ์‹ถ๋„ค์š”. ์ง€๊ธˆ์€ ์ •๋ง ํ‘ธ๋Š” ๋ฐ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ์ด์ „์— ํ’€์—ˆ๋˜ ๊ฑฐ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์—ฌ๋„ ํ—ค๋งค๋Š”๋ฐ ๊ทธ๋Ÿฌ์ง€ ์•Š์„ ๋‚ ์ด ์˜ฌ๊นŒ์š”? ํ˜„์žฌ ๊ณจ๋“œ 2 ์ž…๋‹ˆ๋‹ค. ๋ฐฑ์ค€์ด ํ”Œ๋ ˆ๋‹ˆ๋„˜์ด ๋˜๋Š” ๋‚  ํ—Œ ๋ฒˆ ๋” Tm..

[Algorithm/ Sparse Table (ํฌ์†Œํ…Œ์ด๋ธ”)] ๋ฐฑ์ค€ 17435๋ฒˆ๊ณผ ํ•จ๊ป˜
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2023. 2. 1. 11:22[Algorithm/ Sparse Table (ํฌ์†Œํ…Œ์ด๋ธ”)] ๋ฐฑ์ค€ 17435๋ฒˆ๊ณผ ํ•จ๊ป˜

Sparse Table (์ŠคํŒŒ์Šค ํ…Œ์ด๋ธ”) ํŠน์ง• ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ ์ž…๋‹ˆ๋‹ค.๋ชจ๋“  ์ ์ด ๋ชฉ์ ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.๊ทธ ์ ์„ ํƒ€๊ณ  ์ƒˆ๋กœ์šด ์ ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.2์˜ ์ œ๊ณฑ๊ทผ์œผ๋กœ ๋„์ฐฉํ•œ ์ ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. (1๋ฒˆ, 2๋ฒˆ, 4๋ฒˆ, 8๋ฒˆ ์ด๋™์— ๋Œ€ํ•œ ๋ฐฐ์—ด์„ ๋ชจ๋‘ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.) ์˜ˆ์ œ๋ฅผ ํ’€๋ฉด์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. https://www.acmicpc.net/problem/17435 17435๋ฒˆ: ํ•ฉ์„ฑํ•จ์ˆ˜์™€ ์ฟผ๋ฆฌํ•จ์ˆ˜ f : {1, 2, ..., m}→{1, 2, ..., m}์ด ์žˆ๋‹ค. ์ด๋•Œ fn : {1, 2, ..., m}→{1, 2, ..., m}์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜์ž. f1(x) = f(x) fn+1(x) = f(fn(x)) ์˜ˆ๋ฅผ ๋“ค์–ด f4(1) = f(f(f(f(1))))์ด๋‹ค. n๊ณผ x๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ fn(x)๋ฅผ ๊ณ„์‚ฐํ•˜๋Š”www.acmicpc.n..

[Algorithm/Shortest Path] A* ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๊ตฌํ˜„C++
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2022. 6. 20. 16:33[Algorithm/Shortest Path] A* ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๊ตฌํ˜„C++

์Šคํ„ฐ๋””์—์„œ A* ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•œ ๋ฒˆ ์ž‘์„ฑํ•ด๋ณด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ˜์†”๋กœ ๋„์–ด๋ณด๊ณ .. ์œ ๋‹ˆํ‹ฐ๋กœ ์ ์šฉํ•ด๋ณด๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ์œ ๋‹ˆํ‹ฐ๋กœ ์˜ฎ๊ธฐ๋Š” ์ž‘์—… ๋ฒŒ์จ๋ถ€ํ„ฐ ๊ฑฑ์ •์ด ๋œ๋‹ค. ์ผ๋‹จ A*์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ญ”์ง€ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์ž ..A* ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™•์žฅํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ฃผ์–ด์ง„ ์ถœ๋ฐœ ๊ผญ์ง“์ ์—์„œ๋ถ€ํ„ฐ ๋ชฉํ‘œ ๊ผญ์ง“์ ๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ฃผ๋กœ ๊ฒŒ์ž„์—์„œ ๋ชฌ์Šคํ„ฐ๊ฐ€ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๋ชฉํ‘œ์ง€์ ์œผ๋กœ ์ด๋™ํ•˜๊ฑฐ๋‚˜  ์ž๋™ ์‚ฌ๋ƒฅ ๊ฒŒ์ž„์—์„œ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ํƒ€๊ฒŸ(๋ชฌ์Šคํ„ฐ๋‚˜ ๋‹ค๋ฅธ PC)์„ ํ–ฅํ•ด ์ด๋™์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ A* ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ชฉํ‘œ์ ์‹œ์ž‘์  -> ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์ •์ ๋“ค๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์‹œ์ž‘์  -> ๋ชฉํ‘œ์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ฐจํ›„ ๊ฒฝ๋กœ ๋„์ถœ์„ ์œ„ํ•œ ํ•จ์ˆ˜f(n)ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ..

[Algorithm/MST] ํ”„๋ฆผ(Prim) ์•Œ๊ณ ๋ฆฌ์ฆ˜
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2022. 3. 7. 17:02[Algorithm/MST] ํ”„๋ฆผ(Prim) ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ”„๋ฆผ(Prim) ์•Œ๊ณ ๋ฆฌ์ฆ˜1. ๊ทธ๋ž˜ํ”„์—์„œ ์‹œ์ž‘์ ์„ ์ •ํ•œ๋‹ค - (์–ด๋–ค ๋…ธ๋“œ์ด์–ด๋„ ์ƒ๊ด€์—†์Œ)2. ์„ ํƒํ•œ ์ •์ ๊ณผ ์ธ์ ‘ํ•˜๋Š” ์ •์ ๋“ค ์ค‘ ์ตœ์†Œ ๋น„์šฉ์ธ ๊ฐ„์„ ์— ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ์„ ํƒํ•œ๋‹ค.3. ๋ชจ๋“  ์ •์ ์ด ์„ ํƒ๋  ๋•Œ๊นŒ์ง€ 1,2๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.โ€ป ํ”„๋ฆผ์€ ์‹œ์ž‘์ ์„ ์ •ํ•˜๊ณ , ์‹œ์ž‘์ ์—์„œ ๊ฐ€๊นŒ์šด ์ •์ ์„ ์„ ํƒํ•˜๋ฉด์„œ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ ๊ทธ ๊ณผ์ •์—์„œ ์‚ฌ์ดํด์„ ์ด๋ฃจ์ง€ ์•Š์ง€๋งŒ ํฌ๋ฃจ์Šค์นผ์€ ์‹œ์ž‘์ ์„ ๋”ฐ๋กœ ์ •ํ•˜์ง€ ์•Š๊ณ  ์ •๋ ฌ ํ›„ ๊ฐ€์žฅ ์ตœ์†Œ ๋น„์šฉ์œผ๋กœ ๊ฐ„์„ ์„ ์„ ํƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ํŒ๋‹จํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

[Algorithm/MST] ํฌ๋ฃจ์Šค์นผ(Kruscal) ์•Œ๊ณ ๋ฆฌ์ฆ˜
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2022. 3. 3. 17:56[Algorithm/MST] ํฌ๋ฃจ์Šค์นผ(Kruscal) ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํฌ๋ฃจ์Šค์นผ(Kruscal) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅํŠธ๋ฆฌ(Minimum Spanning Tree) ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜๋กœ ๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ๋ชจ๋“  ์ •์ ๋“ค์„ ๊ฐ€์žฅ ์ ์€ ๋น„์šฉ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. [Algorithm] ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ (MST, Minimum SpanningTree) (tistory.com) [Algorithm] ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ (MST, Minimum SpanningTree)์‹ ์žฅํŠธ๋ฆฌ(SpanningTree) ๊ทธ๋ž˜ํ”„ ์ค‘ ๋ชจ๋“  ์ •์ ์ด ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ , ๊ฐ„์„ ๋“ค ์‚ฌ์ด์— ์‚ฌ์ดํด์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ํŠน์ง•์œผ๋กœ๋Š” N๊ฐœ์˜ ์ •์ ์„ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ตœ์†Œ ๊ฐ„์„ (edge)์˜ ์ˆ˜๋Š” (N-1)๊ฐœcclient.tistory.com์ด์ „์— ํฌ์ŠคํŒ…ํ•œ ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ์˜ ๋‹ค์„ฏ๊นŒ์ง€ ํŠน์ง•..

[Algorithm] ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ (MST, Minimum SpanningTree)
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2022. 2. 17. 16:40[Algorithm] ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ (MST, Minimum SpanningTree)

์‹ ์žฅํŠธ๋ฆฌ(SpanningTree)๊ทธ๋ž˜ํ”„ ์ค‘ ๋ชจ๋“  ์ •์ ์ด ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ , ๊ฐ„์„ ๋“ค ์‚ฌ์ด์— ์‚ฌ์ดํด์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ํŠน์ง•์œผ๋กœ๋Š” N๊ฐœ์˜ ์ •์ ์„ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ตœ์†Œ ๊ฐ„์„ (edge)์˜ ์ˆ˜๋Š” (N-1)๊ฐœ์ด๋‹ค. ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ (MST, Minimum SpanningTree)์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๊ทธ๋ž˜ํ”„์—์„œ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•œ ์ตœ์†Œํ•œ์˜ ์—ฐ๊ฒฐ๋งŒ์„ ๋‚จ๊ธด ๊ทธ๋ž˜ํ”„์ด๋‹ค. ํ•œ ๊ณณ์œผ๋กœ ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, ์ฆ‰ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ตœ์†Œํ•œ์˜ ์—ฐ๊ฒฐ์ด๋ผ ๋งํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“  ์œ„์น˜ ํ•˜๋‚˜์—์„œ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹จ ํ•œ ๊ฐ€์ง€๋กœ ๊ฒฐ์ •๋˜๋„๋ก ํ•ญ์ƒ ํŠธ๋ฆฌ์˜ ํ˜•ํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์‹ ์žฅ ํŠธ๋ฆฌ๋“ค ์ค‘ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ๊ฐ€์žฅ ์ž‘์€ ํŠธ๋ฆฌ์ด๋‹ค. ์ตœ์†Œ ๋น„์šฉ ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๊ทธ๋ฆฌ๋”” ..

[Algorithm] Union-Find
๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป programming/โ—ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜2022. 2. 14. 18:26[Algorithm] Union-Find

Union-Find(์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ)๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜๋กœ ์„œ๋กœ์†Œ(disjoint sets) ์ง‘ํ•ฉ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ์„œ๋กœ์†Œ ์ง‘ํ•ฉ์ด๋ž€ ๊ณตํ†ต ์›์†Œ๊ฐ€ ์—†๋Š” ๋‘ ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š”๋ฐ, ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์ •๋ณด(ํ•ฉ์ง‘ํ•ฉ ์—ฐ์‚ฐ)๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ด์„œ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š” ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ๊ณ„์‚ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.1. union(ํ•ฉ์ง‘ํ•ฉ)์—ฐ์‚ฐ์„ ํ™•์ธํ•˜์—ฌ, ์„œ๋กœ ์—ฐ๊ฒฐ๋œ ๋‘ ๋…ธ๋“œ A, B๋ฅผ ํ™•์ธํ•œ๋‹ค.       - A์™€ B์˜ ๋ฃจํŠธ ๋…ธํŠธ A', B'๋ฅผ ๊ฐ๊ฐ ์ฐพ๋Š”๋‹ค.       - A'๋ฅผ B'์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•œ๋‹ค.(B'๊ฐ€ A'๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•œ๋‹ค.)2. ๋ชจ๋“  union(ํ•ฉ์ง‘ํ•ฉ) ์—ฐ์‚ฐ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ๊นŒ์ง€ 1๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.17. Union-Find(..

๋ฐ˜์‘ํ˜•
image