๐ŸŽฒ[์•Œ๊ณ ๋ฆฌ์ฆ˜] Prim vs Dijkstra

์ด๊ฐ•์šฑยท2022๋…„ 10์›” 12์ผ
2

๐ŸŽฒ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
2/5
post-thumbnail

MST (Minimum Spanning Tree)

Given an undirected weighted graph, a minimum spanning tree (MST) is a subgraph that connects all the vertices with the lowest possible sum of its edge weights.

๋ฐฉํ–ฅ์„ฑ์ด ์—†๊ณ  ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„์—์„œ, ๊ฐ€์žฅ ์ตœ์†Œํ•œ์˜ ๊ฐ€์ค‘์น˜๋กœ ๋ชจ๋“  ์ •์ ์„ ์—ฐ๊ฒฐํ• ์ˆ˜ ์žˆ๋Š” subgraph๋ฅผ ์˜๋ฏธ.

Shortest Path

Given a weighted graph, the shortest path (or geodesic path) between two vertices is a path with the lowest possible sum of edge weights.

๋ฐฉํ–ฅ์„ฑ์€ ๋ฌด๊ด€. ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„์—์„œ, ๋‘ ์ •์  ์‚ฌ์ด๋ฅผ ์ž‡๋Š” ๊ฐ€์žฅ ์ ์€ ๊ฐ€์ค‘์น˜์˜ ๊ฒฝ๋กœ๋ฅผ ์˜๋ฏธ.

์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด, Shortest path๋Š” ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ์„ ํฌํ•จํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.

Prim vs Dijkstra

์„ธ ๊ฐ€์ง€ ์ •๋„์˜ ์ฐจ์ด์ ์œผ๋กœ ์š”์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. Dijkstraโ€™s algorithm finds the shortest path, but Primโ€™s algorithm finds the MST.
    Dijkstra๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ, Prim์€ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ์ฐพ์•„์ค€๋‹ค.
    ์–ด๋Š ํ•œ ์ชฝ์ด ๋‹ค๋ฅธ ํ•œ ์ชฝ์„ ๋ณด์žฅํ•ด ์ค„ ์ˆ˜ ์—†๋‹ค.

  2. Dijkstraโ€™s algorithm can work on both directed and undirected graphs, but Primโ€™s algorithm only works on undirected graphs.
    Prim์€ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ๋งŒ ๋™์ž‘ํ•œ๋‹ค. Dijkstra๋Š” ์ƒ๊ด€์—†๋‹ค.

  3. Primโ€™s algorithm can handle negative edge weights, but Dijkstraโ€™s algorithm may fail to accurately compute distances if at least one negative edge weight exists.
    Prim์€ ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์—์„œ๋„ ์ž˜ ๋™์ž‘ํ•œ๋‹ค. Dijkstra๋Š” ์ด์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

๊ตฌํ˜„์—์„œ

๋‹ค์Œ ๊ทธ๋ฆผ์„ ๋ณด์ž. Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ค๋ช…ํ•˜๋Š” ๊ทธ๋ฆผ์ด๋‹ค.

์ง€๊ธˆ Y(์™„๋ฃŒ๋œ ์ •์  ์ง‘ํ•ฉ)์— ์ •์  E ๊นŒ์ง€ ์ถ”๊ฐ€๋œ ์ƒํ™ฉ. ๊ทธ ์ดํ›„์˜ B ์ •์ ์— ๋Œ€ํ•œ distance์˜ ๊ฐฑ์‹ ์„ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

  • Prim -> ๊ธฐ์กด distance = 7 ๊ณผ, ์ƒˆ๋กœ์šด distance = 2 (E์™€ ์ธ์ ‘ํ•˜๊ฒŒ ๋จ์œผ๋กœ์จ ์–ป์–ด์ง€๋Š”) ์™€์˜ ๋น„๊ต.
  • Dijkstra -> ๊ธฐ์กด distance = 7 ๊ณผ, ์ƒˆ๋กœ์šด distance = 5 (E๋ฅผ ๊ฑฐ์ณ์„œ ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋ถ€ํ„ฐ ์–ป์–ด์ง€๋Š”) ์™€์˜ ๋น„๊ต.

์ด ์ฐจ์ด์˜ ๊ทผ๋ณธ์  ์›์ธ์€ prim์€ ๊ทธ์ € ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ์ตœ์†Œํ•œ์˜ ๊ฑฐ๋ฆฌ ๋งŒํผ์”ฉ๋งŒ ํ™•์žฅํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๊ณ ,
dijkstra๋Š” ์–ด๋– ํ•œ ์ถœ๋ฐœ์ (source)์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ ์ •์ ์— ๋Œ€ํ•œ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ฐธ๊ณ ๋กœ ์•Œ์•„๋‘˜ ๊ฒƒ์€, Prim ์ด๋“  Dijkstra๋“  heap์„ ํ†ตํ•ด ์ตœ์†Œ๊ฐ’์„ ์–ป๊ณ ์ž ํ•  ๋•Œ๋Š”
heap์— ์ด์ „ distance์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์ด ์ฃฝ์ง€ ์•Š๊ณ  ๋‚จ์•„์žˆ๋‹ค๋Š” ์ ์„ ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ.

๋ฐฐ์—ด๋กœ distance ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฐฑ์‹ ํ•œ๋‹ค๋ฉด ์ด์ „ distance๋“ค์ด ์ƒˆ๋กœ์šด distance๋กœ ๋Œ€์ฒด๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ด์•„๋‚จ์ง€ ๋ชปํ•˜์ง€๋งŒ, ํž™์€ ์ตœ์†Œ๊ฐ’๋งŒ ๋ฝ‘์•„์˜ค๋Š” ์›๋ฆฌ๋ผ์„œ ์ด์ „ ์ •๋ณด๊ฐ€ ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” B๋ฅผ Y์— ๋„ฃ๊ณ  ๋‚˜์„œ(Prim)/์ตœ์†Œ๊ฑฐ๋ฆฌ๊ฐ€ ํ™•์ •๋˜๊ณ  ๋‚˜์„œ(Dijk.),
A์™€ B๋ฅผ ์—ฐ๊ฒฐํ•œ ๊ฐ€์ค‘์น˜ 7์˜ ๊ฐ„์„ ์ด heap์— ๊ณ„์† ๋‚จ์•„์žˆ๊ฒŒ ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด heappop์„ ํ•ด์ค„ ๋•Œ ์กฐ๊ฑด์„ ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Dijkstra

# Dijkstra
#
# distance ๋ฐฐ์—ด์€ ํ•ญ์ƒ distance์— ๋Œ€ํ•œ ์ตœ์‹  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
# heap์„ ๋”ฐ๋กœ ์“ฐ๋Š” ์ด์œ ๋Š” distance ๋ฐฐ์—ด์„ ์„ ํ˜•ํƒ์ƒ‰ํ•ด ์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” ๋ฒˆ๊ฑฐ๋Ÿฌ์›€์„ ๋œ๊ธฐ ์œ„ํ•ด.
# ๊ทธ๋ž˜์„œ distance_heap์€ distance ๋ฐฐ์—ด์˜ ์ตœ์‹  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋ฉด์„œ๋„, ์ด์ „ ๋‹จ๊ณ„์˜ distance์ •๋ณด๋„ ๋‚จ์•„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.
# ๊ทธ๋ž˜์„œ a์—์„œ ์ตœ์‹ ์˜ ์ •๋ณด์ธ์ง€ ํ™•์ธ์„ ํ•ด์ค€๋‹ค. distance๋Š” ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ค„์–ด๋“ค๋ฉด ์ค„์–ด๋“ค์—ˆ์ง€ ๋Š˜์ง„ ์•Š๋Š”๋‹ค. (ํ•ญ์ƒ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜๋ ค๊ณ  ํ•จ.)
# ๋”ฐ๋ผ์„œ distance_heap์—์„œ ๊บผ๋‚ธ v์— ๋Œ€ํ•œ dist์ •๋ณด๊ฐ€, distance๋ฐฐ์—ด์˜ ํ•ด๋‹น ๊ฐ’๋ณด๋‹ค 'ํฌ๋‹ค๋ฉด' ์ด๋ฏธ ์ด์ „ ๋‹จ๊ณ„์˜ ์ •๋ณด์ธ (visited๋œ) ๊ฒƒ์ด๋‹ค.
# ๊ทธ๋ž˜์„œ continue๋ฅผ ํ†ตํ•ด heap์˜ ๋‹ค์Œ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
while distance_heap:
    dist, v = heappop(distance_heap)
    if distance[v] < dist:        # (a) visited check.
        continue
    # visited check๋ฅผ ํ†ต๊ณผํ•œ ์ •๋ณด์˜ v๋Š” ์ตœ์‹ ์˜ distance์ •๋ณด(Y ์—์„œ ๋‹ค๋ฅธ ์ •์ ์˜ ๊ฑฐ๋ฆฌ)์—์„œ ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ง„ ์ •์ ์ด๋‹ค.
    # ์ด์ œ v๋Š” Y์— ๋“ค์–ด์˜จ ๊ฒƒ์ด๋‹ค.
    ...

Prim

# Prim
while distance:
        if (e := heappop(distance))[1] not in Y:    # ์ด์ „ distance์ •๋ณด๋“ค์ด heap์— ๊ณ„์† ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์—
                                                    # Y์ง‘ํ•ฉ์„ ํ–ฅํ•˜์ง€ ์•Š๋Š” ์—ฃ์ง€๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€ pop ํ•œ๋‹ค.
                                                    # ๋ฐฐ์—ด์„ ์“ด๋‹ค๋ฉด ๋ฐฐ์—ด์€ ๋ฐฐ์—ด ๊ฐ’ ์ž์ฒด๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „ distance์ •๋ณด๊ฐ€ ๋‚จ์„ ์ˆ˜ ์—†์„ ๊ฒƒ.
            Y.add(e[1])  #  vertex
            cost += e[0] #  weight
            next = e
            break
       ...



์ฐธ๊ณ ์‚ฌ์ดํŠธ

https://www.baeldung.com/cs/prim-dijkstra-difference

profile
I think I think too much.

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 10์›” 12์ผ

์™€์šฐ

1๊ฐœ์˜ ๋‹ต๊ธ€