๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Computer Science/Algorithms

[Algorithms] ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ (Minimum Spanning Tree)

by The Future Engineer, Lucy 2024. 11. 29.
728x90
๋ฐ˜์‘ํ˜•

https://www.geeksforgeeks.org/what-is-minimum-spanning-tree-mst/

 

What is Minimum Spanning Tree (MST) - 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


์‹ ์žฅ ํŠธ๋ฆฌ(Spanning Tree)๋ž€?

์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ํŠธ๋ฆฌ์˜ ์ผ๋ถ€์ธ ๋น„์ˆœํ™˜ ํŠธ๋ฆฌ๋ฅผ ํ˜•์„ฑํ•˜๋Š” ๊ทธ๋ž˜ํ”„์˜ ๊ฐ„์„ ์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์ด๋‹ค.

์‹ ์žฅ ํŠธ๋ฆฌ ํŠน์„ฑ

  • ๊ทธ๋ž˜ํ”„์™€ ์‹ก์•„ ํŠธ๋ฆฌ์˜ ์ •์ ์˜ ์ˆ˜๋Š” ๊ฐ™๋‹ค.
  • ์‹ ์žฅ ํŠธ๋ฆฌ์—๋Š” ์ •์ ์˜ ์ดˆ ๊ฐœ์ˆ˜๋ณด๋‹ค ํ•˜๋‚˜ ์ ์€ ๊ณ ์ •๋œ ๊ฐœ์ˆ˜์˜ ๊ฐ„์„ ์ด ์žˆ๋‹ค.
  • ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๋ถ„๋ฆฌ๋˜์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.
  • ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ์‚ฌ์ดํด์ด ์—†์–ด์•ผ ํ•œ๋‹ค.
  • ์‹ ์žฅ ํŠธ๋ฆฌ์˜ ์ด ๋น„์šฉ์€ ์‹ ์žฅ ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์ด๋‹ค.
  • ๊ทธ๋ž˜ํ”„์—๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹ ์žฅ ํŠธ๋ฆฌ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(Minimum Spanning Tree, MST)๋ž€?

์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์‹ ์žฅ ํŠธ๋ฆฌ ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค. ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์‹ ์žฅ ํŠธ๋ฆฌ ์ค‘ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ์ œ์•ฝ์ด ์žˆ์œผ๋ฉฐ, ๋ชจ๋“  ์‹ ์žฅ ํŠธ๋ฆฌ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง„๋‹ค.

์‹ ์žฅ ํŠธ๋ฆฌ์™€ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•œ ๊ฐ€๋Šฅํ•œ MST๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Kruskal's Algorithm)

์—ฐ๊ฒฐ๋œ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ์ฐพ๋Š” ๋ฐ ๋„๋ฆฌ ์“ฐ์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ, ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜(Greedy algorithm)์ด๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜ํ–‰ํ•œ๋‹ค.

  1. ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด ๊ทธ๋ž˜ํ”„์˜ ๊ฐ„์„ ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  2. ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ์ฐพ์œผ๋ฉฐ ๋ฐ˜๋ณตํ•œ๋‹ค.
  3. ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜์˜ ๊ฐ„์„ ์„ ์ถ”๊ฐ€ํ•ด์„œ ์ˆœํ™˜์„ ํ˜•์„ฑํ•˜์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๊ฐ„์„ ์„ ์„ ํƒํ•œ๋‹ค.

๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ๋œ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ถ”์ ํ•˜๊ธฐ ์œ„ํ•ด Disjoint-SET ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋„คํŠธ์›Œํฌ ์„ค๊ณ„, ํด๋Ÿฌ์Šคํ„ฐ๋ง, ๋ฐ์ดํ„ฐ ๋ถ„์„๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์‹ค์šฉ์ ์ธ ์‘์šฉ ๋ถ„์•ผ์— ์‚ฌ์šฉ๋œ๋‹ค.

ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Prim's Algorithm)

ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ฐ๊ฒฐ๋œ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜ํ–‰ํ•œ๋‹ค.

  1. ์ž„์˜์˜ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ ์‹œ์ž‘ํ•˜๊ณ  MST์— ์ด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
  2. MST์˜ ํ•˜๋‚˜์˜ ์ •์ ๊ณผ ์•„์ง MST์— ์—†๋Š” ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” ๊ฐ„์„ ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ™•์ธํ•œ๋‹ค.
  3. MST์— ๋ชจ๋“  ์ •์ ์ด ํฌํ•จ๋  ๋•Œ๊นŒ์ง€ ์œ„ ๊ณผ์ •์„ ๊ณ„์†ํ•œ๋‹ค.

๊ฐ ๋ฐ˜๋ณต์— ๋Œ€ํ•œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” ๊ฐ„์„ ์„ ํšจ์œจ์ ์œผ๋กœ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋˜ํ•œ ๋™์‹œ์— ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ณ ๋ คํ•  ๋•Œ ์ ํ•ฉํ•œ ๋‹ค๋ฅธ ์ž๋ฃŒ ๊ตฌ์กฐ๋‚˜ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ MST๋ฅผ ์ถ”์ ํ•œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•