When to use
get MST of undirected & weighted Graph
Idea
maintain two sets of vertices (1: MST, 2: not visited)
find cut vertex minimum weight in each loop
until all vertices be included in MST
๐ cut vertex: connect vertex which connects two disjoint subsets
Algorithm
(1) Create key-value table (vertex - weight) , empty MST Set
(2) initialize first value of key as '0' to visit first, remain key as 'MAX'
(3) while MST Set includes all vertices , iterates next
a. pick the minimum cut vertex
b. include it to MST
c. update key-value table (adjacent vertex of (b))
value of b is less than old value? -> update
equal or more? -> nothing to do
Prim vs Kruskal
Prim | Kruskal |
can start any vertex | only can start minimum edge |
run faster in dense graphs (a lot of edges) โต In loop, this cacluates on weight of each vertex |
run faster in sparse graphs (a few edges) โต at first, sort all edges |
it works only on connected graph โต in loop, find cut vertex which connects disjoint set |
it works on disconnected components too |
Greedy , generate MST |
Problem Solving Example
Reference
'Algorithm > Algorithm (์ด๋ก )' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm] Heap Sort (0) | 2021.02.11 |
---|---|
[Data Structure] Priority Queue (0) | 2021.02.10 |
[Algorithm] Kruskal Algorithm (0) | 2021.02.07 |
Backtracking (0) | 2021.01.26 |
[Data Structure] Hash (0) | 2020.10.17 |