๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜

15-4. ์ตœ์†Œ๋น„์šฉ์‹ ์žฅํŠธ๋ฆฌ( Minimum Spanning Tree)

by devshin.kr 2020. 12. 17.
728x90
  • Prim์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜

- ์ž„์˜์˜ ๋…ธ๋“œ๋ฅผ ์ถœ๋ฐœ๋…ธ๋“œ๋กœ ์„ ํƒ

- ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ํŠธ๋ฆฌ๋ฅผ ์ ์  ํ‚ค์›Œ ๊ฐ

(ํฌ๋Ÿฌ์Šค์นผ์€ ์—์ง€๋“ค์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค๋ฉด, ํ”„๋ฆผ์€ ์ถœ๋ฐœ๋…ธ๋“œ๋ถ€ํ„ฐ ์—ฐ๊ฒฐ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์„ ํƒ์ƒ‰ํ•˜์—ฌ ํŠธ๋ฆฌ๋ฅผ ํ‚ค์šด๋‹ค)

- ๋งค ๋‹จ๊ณ„์—์„œ ์ด๋ฏธ ํŠธ๋ฆฌ์— ํฌํ•จ๋œ ๋…ธ๋“œ์™€ ํฌํ•จ๋˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์—์ง€๋“ค ์ค‘ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ์—์ง€๋ฅผ ์„ ํƒ

Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ ์ถœ๋ฐœ ๋…ธ๋“œ๊ฐ€ ์ดˆ๋ก์ƒ‰ ๋…ธ๋“œ๋ผ๊ณ  ํ•˜๋ฉด, ์ถœ๋ฐœ ๋…ธ๋“œ์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ A๋ผ ํ•˜๋ฉด, ๊ทธ ๋ถ€๋ถ„ํŠธ๋ฆฌ์— ํฌํ•จ๋œ ๋…ธ๋“œ๋“ค์˜ ์ง‘ํ•ฉ์€ V of A(๋นจ๊ฐ„ ๋…ธ๋“œ๋“ค)๋ผ ํ•˜์ž.

 

 

Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1
Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2

 

(a) : ์ถœ๋ฐœ๋…ธ๋“œ a ์—์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ b์™€ ๋…ธ๋“œ k ๊ฐ€ ์žˆ๋‹ค. ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ์—์ง€๋Š” 4์ด๋ฏ€๋กœ ๋…ธ๋“œ b๋ฅผ ์„ ํƒํ•œ๋‹ค.

(b) : ์ด์ œ ๋…ธ๋“œ a ์™€ ๋…ธ๋“œ b๋Š” V of A์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์ด ๋˜์—ˆ๋‹ค.

(c) : ๋…ธ๋“œ a์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ k์™€ ๋…ธ๋“œ b์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ c๊ฐ€ ์žˆ๋‹ค. ๊ฐ ์—์ง€์˜ ๊ฐ€์ค‘์น˜๋Š” ๋‘˜ ๋‹ค 8๋กœ ๋™์ผํ•˜๋‹ค.

    ์ด ๊ฒฝ์šฐ ์–ด๋– ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ์„ ํƒํ•ด๋„ ์ƒ๊ด€์ด ์—†๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ๋Š” ๋…ธ๋“œ c๋ฅผ ๋จผ์ € ์„ ํƒํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

    ์ด์ œ V of A์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์€ ๋…ธ๋“œ a, ๋…ธ๋“œ b, ๋…ธ๋“œ c๊ฐ€ ๋˜์—ˆ๋‹ค.

(d) : ๊ฐ ๋…ธ๋“œ์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ํƒ์ƒ‰ํ•˜์—ฌ ๊ทธ ์ค‘ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ์—์ง€๋ฅผ ํ•˜๋‚˜ ์„ ํƒํ•˜์ž. ์ด ๊ฒฝ์šฐ, ๊ฐ€์ค‘์น˜ 2๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์—์ง€๊ฐ€ ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋…ธ๋“œ i๋ฅผ ์„ ํƒํ•œ๋‹ค.

... ๋ฐ˜๋ณต

(i) ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณตํ•˜๋‹ค ๋ณด๋ฉด ๋ชจ๋“  ๋…ธ๋“œ๋“ค์ด ๊นŒ๋งฃ๊ฒŒ ์น ํ•ด์ง„๋‹ค. ํƒ์ƒ‰์ด ๋๋‚ฌ๋‹ค.

 

 

 

  • ์™œ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด MST(Minimum Spanning Tree)๊ฐ€ ์ฐพ์•„์ง€๋Š”๊ฐ€?

- Prim ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ž„์˜์˜ ํ•œ ๋‹จ๊ณ„๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž.

- A๋ฅผ ํ˜„์žฌ๊นŒ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์„ ํƒํ•œ ์—์ง€์˜ ์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•˜๊ณ , A๋ฅผ ํฌํ•จํ•˜๋Š” MST๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

 

 

 

  • ๊ฐ€์ค‘์น˜๊ฐ€ ์ตœ์†Œ์ธ ์—์ง€ ์ฐพ๊ธฐ

- V of A : ์ด๋ฏธ ํŠธ๋ฆฌ์— ํฌํ•จ๋œ ๋…ธ๋“œ๋“ค

- V of A์— ์•„์ง ์†ํ•˜์ง€ ์•Š์€ ๊ฐ ๋…ธ๋“œ v์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ’์„ ์œ ์ง€

    - key(v) : ์ด๋ฏธ V of A์— ์†ํ•œ ๋…ธ๋“œ์™€ ์ž์‹ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์—์ง€๋“ค ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ์ตœ์†Œ์ธ ์—์ง€ (u, v)์˜ ๊ฐ€์ค‘์น˜

    - π(v) : ๊ทธ ์—์ง€ (u, v)์˜ ๋์  u

 

MST-Prim (G, w, r)
	for each u∈V do
    	key[u] <-∞
        π[u] <- NIL
    end.
    V of A <- {r}
    key[r] <- 0
    while |V of A| < n do	// n-1๋ฒˆ ๋ฐ˜๋ณต
    	find u ∉ V of A with the minimum key value;	// ์ตœ์†Œ๊ฐ’ ์ฐพ๊ธฐ O(n)
        V of A <- V of A {u}
        for each V ∉ V of A adjacent to u do	// degree(u) = O(n)
        	if key[v] > w(u, v) then
            	key[v] <- w(u, v)
                π[v] <- u
            end.
        end.
    end.

 

* minimum key

  • Key ๊ฐ’์ด ์ตœ์†Œ์ธ ๋…ธ๋“œ ์ฐพ๊ธฐ

- ์ตœ์†Œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉ

    - V-V of A์— ์†ํ•œ ๋…ธ๋“œ๋“ค์„ ์ €์žฅ

    - Extract-Min : key ๊ฐ’์ด ์ตœ์†Œ์ธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋ฐ˜ํ™˜

 

 

MST-Prim (G, w, r)
	for each u∈V do
    	key[u] <-∞
        π[u] <- NIL
    key[r] <- 0
    Q <- V[G]
    while Q != ๊ณต์ง‘ํ•ฉ
    	do u <- EXTRACT-MIN(Q)	// ์ตœ์†Œ๊ฐ’ ์ฐพ๊ธฐ O(logn)
        	for each v ∈ Adj[u]
            	do if v ∈ Q and w(u, v) < key[v]
                	then [v] <- u
                    	key[v] <- w(u, v)	// ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ key๊ฐ’ decrease : O(logn)

 

 

 

Prim์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์‹œ๊ฐ„๋ณต์žก๋„

- ์ด์ง„ ํž™(binary heap)์„ ์‚ฌ์šฉํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ

- while loop :

    - n๋ฒˆ์˜ Extract-Min ์—ฐ์‚ฐ : O(nlogn)

    - m๋ฒˆ์˜ Decrease-Key ์—ฐ์‚ฐ : O(mlogn)

- ๋”ฐ๋ผ์„œ ์‹œ๊ฐ„๋ณต์žก๋„ : O(nlogn + mlogn) = O(mlogn)

- ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ : O(n^2)

- Fibonacci ํž™์„ ์‚ฌ์šฉํ•˜์—ฌ O(m+nlogn)์— ๊ตฌํ˜„ ๊ฐ€๋Šฅ[Fredman-Tarjan 1984]

๋Œ“๊ธ€