Kruskalโs Algorithm & Primโs Algorithm
2020-1ํ๊ธฐ, ๋ํ์์ โ์๊ณ ๋ฆฌ์ฆโ ์์ ์ ๋ฃ๊ณ ๊ณต๋ถํ ๋ฐ๋ฅผ ์ ๋ฆฌํ ๊ธ์ ๋๋ค. ์ง์ ์ ์ธ์ ๋ ํ์์ ๋๋ค :) ์ ์ฒด ํฌ์คํธ๋ Algorithm ํฌ์คํธ์์ ํ์ธํ์ค ์ ์์ต๋๋ค.
MST๋ weighted undirected graph
Kruskalโs AlgorithmPermalink
<Kruskalโs Algorithm>์ empty graph์์ ์์ํด ์๋์ ์์๋๋ก Greedy ๋ฐฉ์์ผ๋ก edge๋ฅผ ์ ํํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
๋จ์ edge ์ค์ ๊ฐ์ฅ lightํ edge๋ฅผ ๊ทธ๋ํ์ ์ถ๊ฐํ๋ค. ๋จ, edge๋ฅผ ์ถ๊ฐํ์ ๋, ๊ทธ๋ํ์์ cycle์ด ์๊ธฐ์ง ์์์ผ ํ๋ค.

ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ correctness๋ <cut property>์ ์ํด ๋ณด์ฅ๋๋ค.
Theorem. Cut Property

Supp. edges in
Pick any subset of nodes
and let
Then
ํด์ค์ ์ข ํ์๋ฉด,
Proof.

(๊ท๋ฅ๋ฒ) Assume
[1] ์ด๋์
[2] ์ด๋์
We can construct a different MST
Construct a new tree
Then,
๋ฐ๋ผ์,
<Kruskal Algorithm>์ <set> ์๋ฃํ์ ์ฌ์ฉํด ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค! ๐คฉ
Algorithm: Kruskal(
(
// initialization
for all
โโ
end for
// construct empty MST
Sort the edges
// greedy process!
for all edges
โโif
โโโโadd edge
โโโโ
โโend if
end for
์๊ฐ๋ณต์ก๋๋ฅผ ์ดํด๋ณด๋ฉด,
- Weight์ ๋ฐ๋ผ ์ ๋ ฌ:
: ๋ฒ : ๋ฒ : ๋ฒ
Primโs AlgorithmPermalink
<Primโs Algorithm>์ผ๋ก๋ MST๋ฅผ ์ป์ ์ ์๋ค. ์ด ์๊ณ ๋ฆฌ์ฆ์ ์์ด๋์ด๋ ์์ฃผ ๊ฐ๋จํ๋ค!
<Primโs Algorithm>์ ๊ทธ๋ํ
Algorithm: Prime(
(
// initialization
for all
โโ
Pick any initial node
// greedy process!
while
โโ
โโfor each
โโโโif
โโโโโโ
โโโโโโ
ํฌ๋ฃจ์ค์ปฌ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๋ฌ๋ฆฌ ์ ๋ ฌ์ด ์๊ธฐ ๋๋ฌธ์, ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐ๋ณต์ก๋๋ Priority Queue๋ฅผ ์ฐ๋๋ฐ ๊ฑธ๋ฆฌ๋
ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ํํ๋ฅผ ์ ์ดํด๋ณด๋ฉด, ์์์ ๋ดค๋ Dijkstraโs Algorithm๊ณผ ์๋นํ ๋น์ทํ๋ค! ๋์ ์ฐจ์ด์ ์
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์์ ์ถ๋ฐ ๋
ธ๋
ํฌ๋ฃจ์ค์ปฌ๊ณผ ํ๋ฆผ์ ๋น๊ตํ๋ค๋ฉด, ๊ฐ์ธ์ ์ผ๋ก ํ๋ฆผ์ด ๋ ์ฌ์ด ์๊ณ ๋ฆฌ์ฆ์ด๋ผ๊ณ ์๊ฐํ๋ค. ์๋ํ๋ฉด, ํ๋ฆผ์์๋ cycle์ ํ๋จํ ํ์๋ ์๊ณ , ์ ๋ง greedy์ ์ถฉ์คํ๊ฒ ๋ ธ๋๋ฅผ ์ ํํด๋๊ฐ๋ฉด ๋๊ธฐ ๋๋ฌธ์ด๋ค!
์ด์ด์ง๋ ํฌ์คํธ์์๋ <Kruskalโs Algorithm>์์ ์ธ๊ธ๋์๋ <Disjoint Set>์ ๋ํด ์ดํด๋ณธ๋ค. ์ด ๋ถ๋ถ์ Greedy Algorithm๊ณผ ์ง์ ์ ์ผ๋ก ์ฐ๊ด๋ ๋ถ๋ถ์ ์๋๋ฉฐ, <Disjoint Set>์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด๋ป๊ฒ ๊ตฌํํ ์ ์๊ณ , ๊ทธ๋ฆฌ๊ณ ๊ทธ๋์ ์ฌ์ฉ๋๋ ํ ํฌ๋์ ๋ํด ๋ค๋ฃฌ๋ค.
๐ Disjoint Set