SlideShare a Scribd company logo
1 of 10
Download to read offline
By POSTECH Computer Algorithm Team
최소 신장 트리
이건규
Minimum Spanning Tree
POSTECH Computer Algorithm Team
- 그래프 G(v,e,w) 가 주어질 때, 최소의 가중치 합을 지니는 트리를 말함
- 위 그래프에서는 A-C, B-C, C-D, D-E, D-F 5개의 간선을 선택, 13의 가중치로 트리 구현
최소 신장 트리란? Graph
POSTECH Computer Algorithm Team
- 가중치 포함 그래프 G(V,E,W)가 주어질 때, 가중치의 합이 최소가 되는 tree를 반환
- 최소 신장 트리 MST(minimum spanning tree)
- 크루스칼은 edge기반, 프림은 node기반으로 작동
최소 신장 트리(MST) 알고리즘 MST
POSTECH Computer Algorithm Team
- N개의 원소를 몇 개의 영역으로 나눌 때 절대적인 기준이 없는 경우 사용
- 2개의 원소를 합치는 union, 한 원소와 같은 그룹의 원소를 반환하는 find을 지원
- 처음 n개의 원소가 n개의 그룹으로 나누어져 있음 각각의 그룹은 tree로 구성
- Union(a,b) : a.tree.root.parent = b.tree.root
- Find(a) : return a.tree
- 최악의 상황?
- N개의 원소가 일렬로 연결될 경우 O(N^2)의 시간이 소요
상호 배타적 집합(union-find) 데이터 구조4
POSTECH Computer Algorithm Team
- Find(a,b) : a와 b의 depth를 비교하여 같거나 낮은 tree.root의 parent를 다른 tree.root로 한다
- Depth는 처음에 1, 같은 depth가 합쳐진 경우 +1한다
- 최대 depth = log(N)
- 최대 시간 = O(Nlog(N))
상호 배타적 집합(union-find) 데이터 구조5
POSTECH Computer Algorithm Team
- N개의 node를 가지는 tree는 반드시 N-1개의 edge를 가짐
- 초기에는 N개의 node가 N개의 tree로 나누어져 있다.
- 모든 edge E를 가중치 순서대로 정렬
- 가중치가 작은 edge부터 살펴보며 2개의 다른 tree를 연결하면 포함, 아니면 포함하지 않는다.(union-find 사용)
- 1개의 edge가 추가될 때마다 tree의 개수가 1개씩 줄어든다 -> N-1번 포함하면 MST 완성
- 정렬에 Elog(E), 탐색에 E, union-find에 Nlog(N)으로 총 O(Elog(E)) = O(Elog(N))
크루스칼 알고리즘 Kruskal algorithm
POSTECH Computer Algorithm Team
크루스칼 Kruskal algorithm
POSTECH Computer Algorithm Team
- Node를 1개씩 추가하는 방식
- 1개의 시작점을 정하고, N개의 node까지의 가중치를 edge과 같이 저장, edge가 없는 경우 무한대
- N개의 node를 돌아보며 tree에 포함되지 않은 node중 가중치가 최소인 node를 추가
- 추가한 node의 edge를 돌아보며 아직 포함되지 않은 node중 가중치가 더 적어지는 edge가 있으면 갱신
- N개의 node를 추가할 때까지 반복
- N개의 node를 N번에 걸쳐서 추가, E개의 edge를 확인 총 O(N^2)
프림 알고리즘 Prim algorithm
POSTECH Computer Algorithm Team
프림 Prim algorithm
POSTECH Computer Algorithm Team
https://www.acmicpc.net/problem/1197 - 최소 스패닝 트리
https://algospot.com/judge/problem/read/LAN# - 근거리 네트워크
https://algospot.com/judge/problem/read/TPATH - 여행 결로 정하기
예시 문제 problem

More Related Content

What's hot

Graph search
Graph searchGraph search
Graph searchGNGLB
 
Data Structure 2
Data Structure 2Data Structure 2
Data Structure 2yonsei
 
Binary Search
Binary SearchBinary Search
Binary Searchskku_npc
 
4. dynamic programming(1)
4. dynamic programming(1)4. dynamic programming(1)
4. dynamic programming(1)Hongjun Jang
 
2021 여름방학 정기 세미나 3주차
2021 여름방학 정기 세미나 3주차2021 여름방학 정기 세미나 3주차
2021 여름방학 정기 세미나 3주차Moonki Choi
 
Sqrt decomposition
Sqrt decompositionSqrt decomposition
Sqrt decompositionHongjun Jang
 

What's hot (7)

BOJ10547
BOJ10547BOJ10547
BOJ10547
 
Graph search
Graph searchGraph search
Graph search
 
Data Structure 2
Data Structure 2Data Structure 2
Data Structure 2
 
Binary Search
Binary SearchBinary Search
Binary Search
 
4. dynamic programming(1)
4. dynamic programming(1)4. dynamic programming(1)
4. dynamic programming(1)
 
2021 여름방학 정기 세미나 3주차
2021 여름방학 정기 세미나 3주차2021 여름방학 정기 세미나 3주차
2021 여름방학 정기 세미나 3주차
 
Sqrt decomposition
Sqrt decompositionSqrt decomposition
Sqrt decomposition
 

Graph mst

  • 1. By POSTECH Computer Algorithm Team 최소 신장 트리 이건규 Minimum Spanning Tree
  • 2. POSTECH Computer Algorithm Team - 그래프 G(v,e,w) 가 주어질 때, 최소의 가중치 합을 지니는 트리를 말함 - 위 그래프에서는 A-C, B-C, C-D, D-E, D-F 5개의 간선을 선택, 13의 가중치로 트리 구현 최소 신장 트리란? Graph
  • 3. POSTECH Computer Algorithm Team - 가중치 포함 그래프 G(V,E,W)가 주어질 때, 가중치의 합이 최소가 되는 tree를 반환 - 최소 신장 트리 MST(minimum spanning tree) - 크루스칼은 edge기반, 프림은 node기반으로 작동 최소 신장 트리(MST) 알고리즘 MST
  • 4. POSTECH Computer Algorithm Team - N개의 원소를 몇 개의 영역으로 나눌 때 절대적인 기준이 없는 경우 사용 - 2개의 원소를 합치는 union, 한 원소와 같은 그룹의 원소를 반환하는 find을 지원 - 처음 n개의 원소가 n개의 그룹으로 나누어져 있음 각각의 그룹은 tree로 구성 - Union(a,b) : a.tree.root.parent = b.tree.root - Find(a) : return a.tree - 최악의 상황? - N개의 원소가 일렬로 연결될 경우 O(N^2)의 시간이 소요 상호 배타적 집합(union-find) 데이터 구조4
  • 5. POSTECH Computer Algorithm Team - Find(a,b) : a와 b의 depth를 비교하여 같거나 낮은 tree.root의 parent를 다른 tree.root로 한다 - Depth는 처음에 1, 같은 depth가 합쳐진 경우 +1한다 - 최대 depth = log(N) - 최대 시간 = O(Nlog(N)) 상호 배타적 집합(union-find) 데이터 구조5
  • 6. POSTECH Computer Algorithm Team - N개의 node를 가지는 tree는 반드시 N-1개의 edge를 가짐 - 초기에는 N개의 node가 N개의 tree로 나누어져 있다. - 모든 edge E를 가중치 순서대로 정렬 - 가중치가 작은 edge부터 살펴보며 2개의 다른 tree를 연결하면 포함, 아니면 포함하지 않는다.(union-find 사용) - 1개의 edge가 추가될 때마다 tree의 개수가 1개씩 줄어든다 -> N-1번 포함하면 MST 완성 - 정렬에 Elog(E), 탐색에 E, union-find에 Nlog(N)으로 총 O(Elog(E)) = O(Elog(N)) 크루스칼 알고리즘 Kruskal algorithm
  • 7. POSTECH Computer Algorithm Team 크루스칼 Kruskal algorithm
  • 8. POSTECH Computer Algorithm Team - Node를 1개씩 추가하는 방식 - 1개의 시작점을 정하고, N개의 node까지의 가중치를 edge과 같이 저장, edge가 없는 경우 무한대 - N개의 node를 돌아보며 tree에 포함되지 않은 node중 가중치가 최소인 node를 추가 - 추가한 node의 edge를 돌아보며 아직 포함되지 않은 node중 가중치가 더 적어지는 edge가 있으면 갱신 - N개의 node를 추가할 때까지 반복 - N개의 node를 N번에 걸쳐서 추가, E개의 edge를 확인 총 O(N^2) 프림 알고리즘 Prim algorithm
  • 9. POSTECH Computer Algorithm Team 프림 Prim algorithm
  • 10. POSTECH Computer Algorithm Team https://www.acmicpc.net/problem/1197 - 최소 스패닝 트리 https://algospot.com/judge/problem/read/LAN# - 근거리 네트워크 https://algospot.com/judge/problem/read/TPATH - 여행 결로 정하기 예시 문제 problem