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

2024. 2. 7. 23:02ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.6 Greedy Algorithms - 3.6.4 Prim's algorithm

 

Prim’s Algorithm

๐Ÿ’ก Prim’s algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. A minimum spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. A minimum spanning tree for a weighted undirected graph is also called a minimum weight spanning tree or minimum cost spanning tree.

ํ”„๋ฆผ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ฑ ์—†๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ์ฐพ๋Š” ํƒ์š•์Šค๋Ÿฌ์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋Š” ๋ชจ๋“  ์ •์ ์„ ์ˆœํ™˜ ์—†์ด ๊ฐ€๋Šฅํ•œ ์ตœ์†Œํ•œ์˜ ์ด ์—์ง€ ๊ฐ€์ค‘์น˜๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ์—ฐ๊ฒฐ๋œ ์—์ง€ ๊ฐ€์ค‘ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์˜ ์—์ง€ ํ•˜์œ„ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๊ฐ€ ์ ์šฉ๋œ ๋ฐฉํ–ฅ์„ฑ ์—†๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋Š” ์ตœ์†Œ ๊ฐ€์ค‘์น˜ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ๋˜๋Š” ์ตœ์†Œ ๋น„์šฉ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 


Prim's Algorithm์€ ๊ทธ๋ž˜ํ”„ ์ด๋ก ์—์„œ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ๋“ค์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ํŠธ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‹œ์ž‘ ์ •์ ์—์„œ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ •์ ์„ ์„ ํƒํ•˜์—ฌ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ํ™•์žฅํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

Prim's Algorithm์˜ ์ž‘๋™ ๊ณผ์ •์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค.
1. ์‹œ์ž‘ํ•  ์ •์ ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
2. ์„ ํƒํ•œ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ๋“ค ์ค‘์—์„œ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๊ฐ„์„ ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
3.์„ ํƒํ•œ ๊ฐ„์„ ์— ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์˜ ์ผ๋ถ€๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
4. ๋ชจ๋“  ์ •์ ์ด ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋  ๋•Œ๊นŒ์ง€ 2-3๋‹จ๊ณ„๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    

In Python
import heapq

def prim(graph, start_node):
    mst = list()
    visited = [False] * len(graph)
    queue = [(0, start_node)]
    
    while queue:
        weight, node = heapq.heappop(queue)
        if not visited[node]:
            visited[node] = True
            mst.append((weight, node))
            for edge in graph[node]:
                heapq.heappush(queue, edge)
    return mst

 

 

 

 

Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ํ™•์žฅํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์‹œ์ž‘ ๋…ธ๋“œ 0์—์„œ ์–ด๋–ค์‹์œผ๋กœ ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ง„ํ–‰๋˜๋Š”์ง€ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


1. ์‹œ์ž‘ ๋…ธ๋“œ๋Š” 0์ด๋ฉฐ, ์•„์ง ์–ด๋–ค ๋…ธ๋“œ๋„ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹œ์ž‘ ๋…ธ๋“œ 0์„ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ์ƒํƒœ๋ฅผ (0, 0)์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


2. ๋…ธ๋“œ 0๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ 0๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋Š” 1, 2์ด๋ฉฐ, ์ด ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ 1์„ ์„ ํƒํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ์ƒํƒœ๋ฅผ (2, 1)๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


3. ์ด์ œ ๋…ธ๋“œ 0๊ณผ 1์ด ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ 1๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ 3์„ ์„ ํƒํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ์ƒํƒœ๋ฅผ (1, 3)๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


4. ๋…ธ๋“œ 0, 1, 3์ด ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ธ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ 3๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ 2์„ ์„ ํƒํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ์ƒํƒœ๋ฅผ (1, 2)๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


5. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋…ธ๋“œ 0, 1, 2, 3์ด ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋„ค ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ 2์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ 4์„ ์„ ํƒํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ์ƒํƒœ๋ฅผ (5, 4)๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์—ˆ์œผ๋ฏ€๋กœ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ ๋‹จ๊ณ„์—์„œ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

 

ํ™œ์šฉ

 

์—ฌ๋Ÿฌ ์œ„์น˜๋ฅผ ๊ฐ€์žฅ ํšจ์œจ์ ์œผ๋กœ ์—ฐ๊ฒฐํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ• ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

"์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ"๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ ์ถ”์ƒํ™” ํ• ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

1. ํ†ต์‹  ๋„คํŠธ์›Œํฌ

2. ์ „๋ ฅ ๊ทธ๋ฆฌ๋“œ

๋“ฑ๋“ฑ..

728x90