-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst.py
More file actions
51 lines (42 loc) · 1.28 KB
/
mst.py
File metadata and controls
51 lines (42 loc) · 1.28 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import heapq
# DSU 클래스 필요
class DSU:
def __init__(self, n):
self.parent = list(range(n + 1))
def find(self, x):
if self.parent[x] != x: self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, a, b):
root_a, root_b = self.find(a), self.find(b)
if root_a == root_b: return False
self.parent[root_b] = root_a
return True
# Kruskal 알고리즘 edges = [(u, v, w), ...]
def kruskal(n, edges):
edges.sort(key=lambda x: x[2]) # 가중치 기준 정렬
dsu = DSU(n)
total_weight = 0
count = 0
for u, v, w in edges:
if dsu.unite(u, v):
total_weight += w
count += 1
if count < n - 1: return -1 # 연결 안됨
return total_weight
# Prim 알고리즘
def prim(n, start, adj):
visited = [False] * (n + 1)
pq = [(0, start)]
total_weight = 0
count = 0
while pq:
w, cur = heapq.heappop(pq)
if visited[cur]: continue
visited[cur] = True
total_weight += w
count += 1
for nxt, weight in adj[cur]:
if not visited[nxt]:
heapq.heappush(pq, (weight, nxt))
if count < n: return -1
return total_weight