-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
020fc08
commit 99d20df
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
def union(x, y, parents): | ||
root1 = find(x, parents) | ||
root2 = find(y, parents) | ||
parent = max(root1, root2) | ||
child = min(root1, root2) | ||
parents[child] = parent #더 큰 수를 부모노드를 설정함. | ||
|
||
#루트노드 찾는 메서드 | ||
def find(x, parents): | ||
if parents[x] == x: #루트노드인 경우 | ||
return x | ||
return find(parents[x], parents) | ||
|
||
def solution(n, costs): | ||
edges = 0 | ||
answer = 0 | ||
parents = [i for i in range(n)] | ||
costs.sort(key = lambda x:x[2]) #비용기준 오름차순 정렬 | ||
|
||
for i in range(len(costs)): | ||
if edges == n-1: #간선이 n-1개 연결되면 모든 노드들이 연결된 것임. | ||
break | ||
|
||
if find(costs[i][0], parents) != find(costs[i][1], parents): #두 노드의 루트 노드가 같지 않다면(=사이클이 형성되지 않음) | ||
edges += 1 | ||
answer += costs[i][2] | ||
union(costs[i][0], costs[i][1], parents) | ||
return answer | ||
|
||
n = 4 | ||
costs = [[0,1,1],[0,2,2],[1,2,5],[1,3,1],[2,3,8]] | ||
print(solution(n, costs)) |