prim์ kruskal๋ฅผ ๊ฐ๋ ๊ณผ ์ค์ต ๋ฌธ์ ๋ฅผ ๋ณต์ตํ๊ณ ๋ฐฑ์ค ๋ฌธ์ ๋ฅผ ํตํด ์ค์ ๋ฌธ์ ์ ์ ์ฉํด๋ด์ผ๋ก์จ
๋์ ์ฐจ์ด์ ์๊ณ ๋ฆฌ์ฆ ๋์๋ฐฉ์์ ๋ํด ํ์คํ๊ฒ ์๋ ๊ณ๊ธฐ๊ฐ ๋์๋ค.
- ๋ชฉํ -
1. prim ์๊ณ ๋ฆฌ์ฆ
import heapq
def prim(edges, vertices, n):
queue = []
heapq.heappush(queue, [0, vertices[0], vertices[1]]) # ๊ฐ์ค์น, ์์, ๋์ฐฉ
visit = {}
for i in vertices:
visit[i] = False
result = []
sum = 0
while queue:
h = heapq.heappop(queue)
if visit[h[2]] == True: # ๋์ฐฉ์ง์ ๋ฐฉ๋ฌธ์ฌ๋ถ ํ์ธ
continue
else:
visit[h[2]] = True
sum += h[0] # ๊ฐ์ค์น ํฉ์ฐ
result.append([h[1], h[2], h[0]])
for e in edges[h[2]]: # ๋ค์ ๋์ฐฉ์ง์
if (visit[e[0]]) : continue # ๋ค์ ๋์ฐฉ์ง์ ๋ฐฉ๋ฌธ์ฌ๋ถ ํ์ธ
heapq.heappush(queue, [e[1], h[2], e[0]]) # ๊ฐ์ค์น, ์์, ๋์ฐฉ
for e in visit.values(): # ๋ฐฉ๋ฌธํ์ง ์์ ๋
ธ๋๊ฐ ์์ ๊ฒฝ์ฐ(๊ณ ๋ฆฝ๋ ๋
ธ๋ ์กด์ฌ)
if e == False:
sum = 0
break
return sum
def main():
n, m = map(int, input().split())
vertices = input().split()
edges = {}
for i in vertices:
edges[i] = []
for _ in range(m):
u, v, c = input().split() #์์, ๋์ฐฉ, ๊ฐ์ค์น
c = int(c)
edges[u].append([v, c]) #์๋ฐฉํฅ ๊ทธ๋ํ
edges[v].append([u, c])
print(prim(edges, vertices, n))
if __name__ == "__main__":
main()
2. kruskal ์๊ณ ๋ฆฌ์ฆ
parent = {}
rank = {}
def make_set(v):
parent[v] = v
rank[v] = 0
def find(v):
if parent[v] != v:
parent[v] = find(parent[v])
return parent[v]
def union(u, v):
root1 = find(u)
root2 = find(v)
if root1 != root2:
if rank[root1] > rank[root2]:
parent[root2] = root1
else:
parent[root1] = root2
if rank[root1] == rank[root2]:
rank[root2] += 1
def kruskal(vertices, edges, n):
for u in vertices:
make_set(u)
edges.sort()
sum = 0
for e in edges:
cost, u, v = e # ๊ฐ์ค์น, ์์, ๋์ฐฉ
if find(u) != find(v): # ์์ ์ ์ ๊ณผ ๋์ฐฉ ์ ์ ์ ๋ถ๋ชจ๊ฐ ๋ค๋ฅผ ๊ฒฝ์ฐ
union(u,v)
sum += cost
return sum
def main():
n, m = map(int, input().split())
vertices = []
edges = []
vertices = input().split()
for _ in range(m):
u, v, c = input().split()
c = int(c)
edges.append((c, u, v)) # ๊ฐ์ค์น, ์์, ๋์ฐฉ
print(kruskal(vertices, edges, n))
if __name__ == "__main__":
main()
3. ๊ฐ์ฅ ์ค์ํ ๊ธธ
parent = {}
rank = {}
def make_set(v):
parent[v] = v
rank[v] = 0
def find(v):
if parent[v] != v:
parent[v] = find(parent[v])
return parent[v]
def union(u, v):
root1 = find(u)
root2 = find(v)
if root1 != root2:
if rank[root1] > rank[root2]:
parent[root2] = root1
else:
parent[root1] = root2
if rank[root1] == rank[root2]:
rank[root2] += 1
def kruskal(vertices, edges, n):
for u in vertices:
make_set(u)
edges.sort()
sum = 0
for e in edges:
cost, u, v = e # ๊ฐ์ค์น, ์์, ๋์ฐฉ
if find(u) != find(v): # ์์ ์ ์ ๊ณผ ๋์ฐฉ ์ ์ ์ ๋ถ๋ชจ๊ฐ ๋ค๋ฅผ ๊ฒฝ์ฐ
union(u,v)
sum += cost
return sum
def main():
n, m = map(int, input().split())
vertices = []
edges = []
vertices = input().split()
for _ in range(m):
u, v, c = input().split()
c = int(c)
edges.append((c, u, v)) # ๊ฐ์ค์น, ์์, ๋์ฐฉ
standard = kruskal(vertices, edges, n) # ๊ธฐ์ค์ด ๋๋ MST
new_edges = []
result = [] # ์ค์ํ ๊ธธ์ ์ ์ฅํ ๋ฆฌ์คํธ
for i in edges:
new_edges = edges.copy()
new_edges.remove(i)
if kruskal(vertices, new_edges, n) > standard: # MST ๊ฐ์ด ์ปค์ง๋ฉด ์ ๊ฑฐํ ์ฃ์ง๊ฐ ์ค์ํ ๊ธธ
result.append([i[1], i[2]])
print(len(result))
if __name__ == "__main__":
main()
4. 1197๋ฒ ์ต์ ์คํจ๋ ํธ๋ฆฌ
import heapq
def prim(edges, vertices, n):
queue = []
heapq.heappush(queue, [0, vertices[0], vertices[1]]) # ๊ฐ์ค์น, ์์, ๋์ฐฉ
visit = {}
for i in vertices:
visit[i] = False
result = []
sum = 0
while queue:
h = heapq.heappop(queue)
if visit[h[2]] == True: # ๋์ฐฉ์ง์ ๋ฐฉ๋ฌธ์ฌ๋ถ ํ์ธ
continue
else:
visit[h[2]] = True
sum += h[0] # ๊ฐ์ค์น ํฉ์ฐ
result.append([h[1], h[2], h[0]])
for e in edges[h[2]]: # ๋ค์ ๋์ฐฉ์ง์
if (visit[e[0]]) : continue # ๋ค์ ๋์ฐฉ์ง์ ๋ฐฉ๋ฌธ์ฌ๋ถ ํ์ธ
heapq.heappush(queue, [e[1], h[2], e[0]]) # ๊ฐ์ค์น, ์์, ๋์ฐฉ
for e in visit.values(): # ๋ฐฉ๋ฌธํ์ง ์์ ๋
ธ๋๊ฐ ์์ ๊ฒฝ์ฐ(๊ณ ๋ฆฝ๋ ๋
ธ๋ ์กด์ฌ)
if e == False:
sum = 0
break
return sum
def main():
n, m = map(int, input().split())
vertices = []
for i in range(n):
vertices.append(i+1)
edges = {}
for i in vertices:
edges[i] = []
for _ in range(m):
u, v, c = map(int, input().split()) #์์, ๋์ฐฉ, ๊ฐ์ค์น
edges[u].append([v, c]) #์๋ฐฉํฅ ๊ทธ๋ํ
edges[v].append([u, c])
print(prim(edges, vertices, n))
if __name__ == "__main__":
main()
- ๊นํ๋ธ -
github.com/k906506/2020_Winter_Assemble-And-selfcode
'โน๏ธ ๋ผ์ดํ > 2020 ๊ฒจ์ธ๋ฐฉํ ๋ชจ๊ฐ์ฝ(๊ฐ์ธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2020.01.05(ํ) - 3์ฃผ์ฐจ ๊ฐ์ธ ๊ฒฐ๊ณผ (0) | 2021.01.05 |
---|---|
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.05(ํ) - 3์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.05 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2020.12.30(์) - 2์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2020.12.30 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2020.12.23(์) - 1์ฃผ์ฐจ ๊ฐ์ธ ๊ฒฐ๊ณผ (0) | 2020.12.23 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2020.12.23(์) - 1์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2020.12.23 |