2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

6 minute read

2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

MST๋Š” weighted undirected graph $G=(V, E, W)$๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ฌ ๋•Œ, ์•„๋ž˜์˜ ์‹์„ ๋งŒ์กฑํ•˜๋Š” tree $T=(V, Eโ€™)$๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

\[\underset{E'}{\text{argmin}} \sum_{e\in E'} w_e\]

Kruskalโ€™s Algorithm

<Kruskalโ€™s Algorithm>์€ empty graph์—์„œ ์‹œ์ž‘ํ•ด ์•„๋ž˜์˜ ์ˆœ์„œ๋Œ€๋กœ Greedy ๋ฐฉ์‹์œผ๋กœ edge๋ฅผ ์„ ํƒํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

๋‚จ์€ edge ์ค‘์— ๊ฐ€์žฅ lightํ•œ edge๋ฅผ ๊ทธ๋ž˜ํ”„์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‹จ, edge๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ, ๊ทธ๋ž˜ํ”„์—์„œ cycle์ด ์ƒ๊ธฐ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ correctness๋Š” <cut property>์— ์˜ํ•ด ๋ณด์žฅ๋œ๋‹ค.

Theorem. Cut Property

Supp. edges in $X \subset E$ are part of a MST $T$ of $G = (V, E)$.

Pick any subset of nodes $S$ for which no edge in $X$ crosses btw $S$ and $V\setminus S$,

and let $e$ be the lighstest edge across this partition.

Then $X \cup \{ e \}$ is part of some MST.

ํ•ด์„ค์„ ์ข€ ํ•˜์ž๋ฉด, $X$๊ฐ€ MST $T$์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•˜์ž. ์ด๋•Œ, ์ด $X$์— ๋‹จ ํ•˜๋‚˜์˜ edge๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ์—๋„ ์—ฌ์ „ํžˆ MST $T$์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์ด ๋˜๊ฒŒ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด, $X$์˜ ๋ชจ๋“  edge๊ฐ€ cross ํ•˜์ง€ ์•Š๋„๋ก ์ง‘ํ•ฉ $S$, $V \setminus S$๋ฅผ ์žก์•˜์„ ๋•Œ, ๋‘ ์ง‘ํ•ฉ์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” edge ์ค‘ ๊ฐ€์žฅ lightํ•œ edge $e$๋ฅผ ์„ ํƒํ•ด $X$์— ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด, $X \cup \{e\}$๊ฐ€ ์—ฌ์ „ํžˆ MST์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์ด ๋œ๋‹ค๋Š” ์ •๋ฆฌ์ด๋‹ค.

Proof.

(๊ท€๋ฅ˜๋ฒ•) Assume $e = (u, v) \notin T$.
[1] ์ด๋•Œ์˜ $e$๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ $S$, $V\setminus S$๋ฅผ ์ž‡๋Š” ๊ฐ€์žฅ lightํ•œ edge๋‹ค.
[2] ์ด๋•Œ์˜ $T$๋Š” true MST $T$๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

We can construct a different MST $Tโ€™$ containing $X \cup \{ e \}$ by altering $T$ slightly.

$u$ and $v$ are connected by a path in $T$ which contains an edge $eโ€™$ crossing $S$ and $V\setminus S$.

Construct a new tree $Tโ€™$ from $T$ by removing $eโ€™$ and adding $e$.

Then, $Tโ€™$ is a spanning tree with $\texttt{cost}(Tโ€™) \le \texttt{cost}(T)$ (์™œ๋ƒํ•˜๋ฉด, $w_e \le w_{eโ€™}$์ด๊ธฐ ๋•Œ๋ฌธ.) ์ด๊ฒƒ์€ ST $T$๊ฐ€ MST๋ผ๋Š” ์‚ฌ์‹ค์— ๋ชจ์ˆœ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์ฒ˜์Œ์— ๊ฐ€์ •ํ•œ $e = (u, v) \notin T$๋Š” ๊ฑฐ์ง“์ด๋‹ค.

๋”ฐ๋ผ์„œ, $S$, $V\setminus S$๋ฅผ ์—ฐ๊ฒฐํ•  ๋•Œ, the lightest edge $e$๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์‹ค์ œ๋กœ MST๊ฐ€ ๋จ์ด ๋ณด์žฅ๋œ๋‹ค.


<Kruskal Algorithm>์€ <set> ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•ด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค! ๐Ÿคฉ

Algorithm: Kruskal($G$, $w$)
($G = (V, E)$ is a connected undirected graph with edge weights $w_e$.)


// initialization
for all $u \in V$ do
โ€ƒโ€ƒ $\texttt{makeset}(u)$
end for

// construct empty MST
$X = \{ \}$

Sort the edges $E$ by weight.

// greedy process!
for all edges $\{ u, v\} \in E$, in increasing order of weight
โ€ƒโ€ƒif $\texttt{find}(u) \ne \texttt{find}(v)$ // cycle check!
โ€ƒโ€ƒโ€ƒโ€ƒadd edge $\{u, v\}$ to $X$
โ€ƒโ€ƒโ€ƒโ€ƒ$\texttt{union}(u, v)$ // merge two sets!
โ€ƒโ€ƒend if
end for

์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์‚ดํŽด๋ณด๋ฉด,

  • Weight์— ๋”ฐ๋ผ ์ •๋ ฌ: $O(E \log E)$
  • $\texttt{makeset}$: $V$ ๋ฒˆ
  • $\texttt{find}$: $E$ ๋ฒˆ
  • $\texttt{union}$: $V-1$ ๋ฒˆ

$\texttt{makeset}$, $\texttt{find}$, $\texttt{union}$ ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ถ”ํ›„์— Disjoint Set ํฌ์ŠคํŠธ์—์„œ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.



Primโ€™s Algorithm

<Primโ€™s Algorithm>์œผ๋กœ๋„ MST๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์•„์ด๋””์–ด๋„ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๋‹ค!

<Primโ€™s Algorithm>์€ ๊ทธ๋ž˜ํ”„ $G$์—์„œ ๊ฐ€์žฅ lightํ•œ edge๋ฅผ ์„ ํƒํ•˜๋ฉฐ, intermediate MST $X$๋ฅผ grow ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ด๋•Œ, ํฌ๋ฃจ์Šค์ปฌ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ ํƒํ•œ edge๋กœ ์ธํ•ด cycle์ด ํ˜•์„ฑ๋˜์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.

Algorithm: Prime($G$, $w$)
($G = (V, E)$ is a connected undirected graph with edge weights $w_e$.)


// initialization
for all $u \in V$
โ€ƒโ€ƒ$\texttt{cost}(u) = \infty$, and $\texttt{prev}(u)=\texttt{nil}$

Pick any initial node $u_0$ and set $\texttt{cost}(u_0) = 0$ // ๋‹น์—ฐํ•˜๊ฒŒ๋„ ์–ด๋–ค ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘์œผ๋กœ ์‚ผ๋“  ์ „ํ˜€ ์ƒ๊ด€์ด ์—†๋‹ค!

$H=\texttt{makequeue}(V)$ // priority queue, using (cost, value) pair

// greedy process!
while $H$ is not empty
โ€ƒโ€ƒ$v=\texttt{deletemin}(H)$
โ€ƒโ€ƒfor each $\{v, z\} \in E$
โ€ƒโ€ƒโ€ƒโ€ƒif $\texttt{cost}(z) > w(v, z)$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$\texttt{cost}(z) = w(v, z)$, and $\texttt{prev}(z) = v$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$\texttt{decreasekey}(H, z)$

ํฌ๋ฃจ์Šค์ปฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋‹ฌ๋ฆฌ ์ •๋ ฌ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—, ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” Priority Queue๋ฅผ ์“ฐ๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” $E \times O(\log V) = O(E \log V)$์ด๋‹ค.

ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ˜•ํƒœ๋ฅผ ์ž˜ ์‚ดํŽด๋ณด๋ฉด, ์•ž์—์„œ ๋ดค๋˜ Dijkstraโ€™s Algorithm๊ณผ ์ƒ๋‹นํžˆ ๋น„์Šทํ•˜๋‹ค! ๋‘˜์˜ ์ฐจ์ด์ ์€ $\texttt{cost}(\cdot)$ ์—…๋ฐ์ดํŠธ rule์—์„œ ์žˆ๋Š”๋ฐ,

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„  ์ถœ๋ฐœ ๋…ธ๋“œ $s$์—์„œ ๋„๋‹ฌํ•˜๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ ๋น„์šฉ์„ $\texttt{cost}(\cdot)$์— ๊ธฐ๋กํ•˜๊ณ , ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ๋…ธ๋“œ $v$๋ฅผ MST์— ํฌํ•จ์‹œํ‚ฌ ๋•Œ์˜ ๋น„์šฉ์„ $\texttt{cost}(\cdot)$์— ๊ธฐ๋กํ•œ๋‹ค.


ํฌ๋ฃจ์Šค์ปฌ๊ณผ ํ”„๋ฆผ์„ ๋น„๊ตํ•œ๋‹ค๋ฉด, ๊ฐœ์ธ์ ์œผ๋ก  ํ”„๋ฆผ์ด ๋” ์‰ฌ์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ํ”„๋ฆผ์—์„œ๋Š” cycle์„ ํŒ๋‹จํ•  ํ•„์š”๋„ ์—†๊ณ , ์ •๋ง greedy์— ์ถฉ์‹คํ•˜๊ฒŒ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ด๋‚˜๊ฐ€๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!


์ด์–ด์ง€๋Š” ํฌ์ŠคํŠธ์—์„œ๋Š” <Kruskalโ€™s Algorithm>์—์„œ ์–ธ๊ธ‰๋˜์—ˆ๋˜ <Disjoint Set>์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค. ์ด ๋ถ€๋ถ„์€ Greedy Algorithm๊ณผ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ด€๋œ ๋ถ€๋ถ„์€ ์•„๋‹ˆ๋ฉฐ, <Disjoint Set>์ด๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋•Œ์— ์‚ฌ์šฉ๋˜๋Š” ํ…Œํฌ๋‹‰์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค.

๐Ÿ‘‰ Disjoint Set


์ถ”์ฒœ ๋ฌธ์ œ

Categories:

Updated: