๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ฒ˜๋Ÿผ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‚ฎ์€ ๊ฐ„์„ ๋ถ€ํ„ฐ ์„ ํƒํ•ด์„œ MST๋ฅผ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€์ง€๋งŒ, ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์—ฌ๋Ÿฌ ํŠธ๋ฆฌ๋“ค์„ ๋งŒ๋“ค๊ณ  ์ ์  ํ•ฉ์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ๋ฅผ ์œ ์ง€ํ•ด๊ฐ€๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ฐ„์„ ์„ ์ถ”๊ฐ€ํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.

Algorithm Concept

ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค.

  1. ์ž„์˜์˜ ๋ฃจํŠธ๋…ธ๋“œ r์„ ํฌํ•จํ•˜๋Š” ํŠธ๋ฆฌ A๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. ํŠธ๋ฆฌ A์— ์†ํ•˜๋Š” ์ •์ ๋“ค๊ณผ ์†ํ•˜์ง€ ์•Š๋Š” ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๋“ค์„ cross ํ•˜๋Š” ๊ฐ„์„ ๋“ค ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ light edge ๋ฅผ ์ฐพ๋Š”๋‹ค.
  3. ํ•ด๋‹น light edge๋ฅผ ํŠธ๋ฆฌ A๋กœ ํŽธ์ž…์‹œํ‚จ๋‹ค.
  4. ๋น ๋ฅธ light edge ํƒ์ƒ‰์„ ์œ„ํ•ด min heap ์„ ์ด์šฉํ•œ priority queue ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Pseudo Code

MST-Prim(G, w, r)
    priority queue Q <- arbitrary vertex from graph G
    for each vertex u in Q
        key[u] = INFINITY
        parent[u] = NIL

    key[root] = 0
    parent[root] = NULL

    while(Q is not empty)
        u = Extract Min from Q
        for each v in adjacency list adj[u]
            if v is in Q and w(u, v) is less than key[v]
                parent[v] = u
                key[v] = w(u, w);

๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•ด๋ณด๋ฉด, ์šฐ์„ ์ˆœ์œ„ ํ์— ๋“ค์–ด์žˆ๋Š” ๋…ธ๋“œ๋“ค ์ค‘ key ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์ž‘์€ key๋ฅผ ๊ฐ€์ง„ ์ •์ ์„ ๋ฝ‘๊ณ , ํ•ด๋‹น ์ ์ •๊ณผ ์ด์–ด์ ธ ์žˆ๋Š” ๋‹ค๋ฅธ ์ •์ ๋“ค์„ ๋‘ ์ •์  ์‚ฌ์ด์— ๊ฐ„์„ ์ด ๊ฐ€์ง„ ๊ฐ€์ค‘์น˜์™€ ์ •์ ์ด ๊ฐ€์ง„ key ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฐ’์€ key๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

Example


ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์ผ๋˜ ๋™์ผํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์–ด๋ณด์ž

Phase 1

์ผ๋‹จ ๊ฐ ์ •์ ์˜ key๋ฅผ INFINITE์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค. ์ด key ๊ฐ’๋“ค์€ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋“ค์–ด์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž„์˜์˜ ์ •์ ํ•˜๋‚˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์ด ์ •์ ์ด ์‹œ์ž‘์ ์ด ๋  ๊ฒƒ์ด๋‹ค. ์–ด์ฐจํ”ผ ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ๋ฅผ ๊ณ„์† ํ™•์žฅํ•ด๋‚˜๊ฐ€๋Š” ๊ณผ์ •์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์ž‘์ ์€ ํฌ๊ฒŒ ์ƒ๊ด€์—†๋‹ค.

Phase 3

์„ ํƒํ•œ ์ดˆ๊ธฐ์˜ ์ •์ ์˜ key๋ฅผ 0์œผ๋กœ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ dequeue ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด 0์ด ๋‚˜์˜ค๊ฒŒ๋œ๋‹ค. ์ด์ œ ์ด key๊ฐ’์„ ๊ฐ€์ง„ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ ๊ฒ€์‚ฌํ•œ๋‹ค.

Phase 4

์šฐ์„  ๋ฐ”๋กœ ์œ„์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋˜ ์ •์ ์— ์ฐพ์•„๊ฐ„๋‹ค. ํ•ด๋‹น ์ •์ ์˜ key๊ฐ’์€ ์ฒ˜์Œ์— ์ดˆ๊ธฐํ™”ํ–ˆ๋˜๋Œ€๋กœ ๋ฌดํ•œ๋Œ€๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ธฐ์ค€์ด ๋˜๋Š” ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์ด ์ •์ ์œผ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 14์ด๋‹ค. ์ด ์ค‘ ๋” ์ž‘์€ ๊ฐ’์„ ์ •์ ์˜ key ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ์ž. ์ด์ œ ๋ฃจํŠธ๋…ธ๋“œ ์ƒ๋‹จ์— ์žˆ๋˜ ๋…ธ๋“œ์˜ key ๊ฐ’์€ ๋ฌดํ•œ๋Œ€๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜์˜€๋˜ 14๊ฐ€ ๋œ๋‹ค.

๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฃจํŠธ๋…ธ๋“œ์™€ ์ด์–ด์ง„ ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ์ด ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์•„๋ž˜์— ๋ถ™์–ด์žˆ๋˜ ์ •์ ์˜ key๊ฐ’์€ 3์œผ๋กœ ์„ค์ •๋œ๋‹ค.

Phase 5

๋ฃจํŠธ ๋…ธ๋“œ์˜ ์ฒ˜๋ฆฌ๋Š” ๋๋‚ฌ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์•„์ง ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— dequeue ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฃจํŠธ๋…ธ๋“œ๋Š” ์ด๋ฏธ pop ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ์œผ๋กœ key ๊ฐ’์ด ์ž‘์€ 3์„ ๊ฐ€์ง„ ์ •์ ์ด ๋‚˜์˜จ๋‹ค. ์ด ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์ด ์ด์ „๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ key๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ฒŒ๋œ๋‹ค.

  1. 14 ๋ฅผ key๋กœ ๊ฐ€์ง€๋˜ ์ •์ ์€ ์ƒˆ๋กœ์šด ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜์ธ 10์„ ๋งŒ๋‚˜๊ฒŒ ๋˜๊ณ , ๋” ์ž‘์€ ๊ฐ’์ธ 10์„ key์˜ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.
  2. ๊ฐ„์„  ๊ฐ€์ค‘์น˜ 8๋กœ ์—ฐ๊ฒฐ๋œ ์ •์ ์€ ๋ฌดํ•œ๋Œ€์˜ key ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— key์˜ ๊ฐ’์ด ๋” ์ž‘์€ ๊ฐ’์ธ 8๋กœ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.

Phase 6

๋‹ค์Œ์— dequeue ๋˜๋Š” ์ •์ ์€ ๋ฐฉ๊ธˆ ์ „์— key ๊ฐ€ 8๋กœ ์—…๋ฐ์ดํŠธ ๋œ ์ •์ ์ด๋‹ค. ์ด ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ •์ ์˜ key๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด key๊ฐ€ ์„ค์ •๋œ๋‹ค.

Phase 7

ํ•œ๋ฒˆ ๋” ์ง„ํ–‰ํ•˜๋ฉด ๊ฐ ์ •์ ์˜ key๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.

More Steps...

ํ ์•ˆ์— ์•„์ง ์ •์ ๋“ค์ด ๋‚จ์•„์žˆ์ง€๋งŒ, ๊ฐ ์ •์ ๋“ค์„ ๊บผ๋‚ด ํ™•์ธํ•ด๋ณด๋ฉด, ๋” ์ด์ƒ key๋ฅผ ์—…๋ฐ์•„ํŠธ ํ•˜๋Š” ํ๊ฐ€ ์—†์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ช‡์ฐจ๋ก€ ๊ฐ™์€ ๊ณผ์ •์„ ๊ณ„์† ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๊ธด ํ•˜์ง€๋งŒ ์ตœ์ข… MST๋Š” Phase 7๊ณผ ๋™์ผํ•œ ํŠธ๋ฆฌ๊ฐ€ ์™„์„ฑ๋œ๋‹ค.