[ํŒŒ์ด์ฌ/python] ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Dijkstra Algorithm)

2021. 9. 2. 16:05

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ทธ๋ž˜ํ”„์—์„œ ์ •์  ๊ฐ„์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

๊ทธ๋ž˜ํ”„์—์„œ ์ฃผ์–ด์ง„ ์‹œ์ž‘ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฅธ ์ •์  ๊ฐ„์˜ ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋‹ค.

์ฆ‰, u์—์„œ v๋กœ ๊ฐˆ ๋•Œ ๊ฒฝ๋กœ ์ค‘ ๊ฐ„์„ ๋“ค์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์–ด๋–ค ํ•œ ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ๋•Œ

๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๊ฐ€ ๊ฒฐ์ •๋˜๋ฉด ๋ฉˆ์ถ”๋Š” ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

์œ„์™€ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„์ด๊ณ  ์‹œ์ž‘๋…ธ๋“œ๊ฐ€ 1์ผ ๋•Œ, 

์‹œ์ž‘๋…ธ๋“œ : [(๋„์ฐฉ๋…ธ๋“œ, ๊ฐ€์ค‘์น˜)] ํ˜•์‹์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค

n = 1
graph = { 1: [(2,2), (3,3)],
          2: [(3,4), (4,5)],
          3: [(4,6)],
          5: [(1,1)]
        }

 

๊ฐ ์ •์ ์— ๋Œ€ํ•œ ์‹œ์ž‘๋…ธ๋“œ๋ถ€ํ„ฐ ํ•ด๋‹น ์ •์ ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์„ ์ €์žฅํ•  point ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.

ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์˜ ์ดˆ๊ธฐ๊ฐ’์€ ๋ฌดํ•œํ•œ ๊ฐ’์œผ๋กœ 1e9๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

INF = int(1e9)
point = [INF]*(5+1)	# ๋ชจ๋“  ์ •์ ๊ณผ ๊ฐ™์€ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋„๋ก ์ •์ ๊ฐœ์ˆ˜ + 1 ๋งŒํผ ๋งŒ๋“ฆ

 

point๋ฅผ ํ‘œ๋กœ ๊ทธ๋ ค๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํ•œ์นธ ๋” ํฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์ธ๋ฑ์Šค 0์€ ์‚ฌ์šฉํ•˜์ง€์•Š๋Š”๋‹ค.

์ •์  (์ธ๋ฑ์Šค) 0 1 2 3 4 5
point INF INF INF INF INF INF

 

dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ heapq๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

import heapq

def dijkstra(n):
    queue = []
    point[n] = 0
    heapq.heappush(queue, (0, n))

    while queue:
        c, now = heapq.heappop(queue) 
        
        if point[now] < c:  
            continue

        for node in graph[now]:
            cost = c + node[1]

            if cost < point[node[0]]:
                point[node[0]] = cost
                heapq.heappush(queue, (cost, node[0]))

 

queue๋Š” ํƒ์ƒ‰ํ•  ๋…ธ๋“œ๋“ค์˜ (๊ฐ€์ค‘์น˜ ํ•ฉ, ์ •์ ) ์Œ์ด ๋“ค์–ด๊ฐˆ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.

n์€ ์‹œ์ž‘๋…ธ๋“œ๋กœ์จ ๊ฐ€์ค‘์น˜ ํ•ฉ์ธ point[n]์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

heapq๋ฅผ ์ด์šฉํ•˜์—ฌ queue์— ์ฒซ๋ฒˆ์จฐ๋กœ ํƒ์ƒ‰ํ•  ๋…ธ๋“œ์— n์— ๋Œ€ํ•œ (๊ฐ€์ค‘์น˜ ํ•ฉ, ์ •์ )์ธ (0,n)์„ ๋„ฃ๋Š”๋‹ค.

 

queue์— ํƒ์ƒ‰ํ•  ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”๋™์•ˆ ๋ฐ˜๋ณต๋œ๋‹ค.

heapq๋ฅผ ์ด์šฉํ•˜์—ฌ queue์— ์žˆ๋Š” ๊ฐ’์„ ๊บผ๋‚ด c, now์— ๋„ฃ๋Š”๋‹ค.

c์—๋Š” ์ด์ „ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด, now์—๋Š” ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.

 

๋งŒ์•ฝ ํ˜„์žฌ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ point[now]๊ฐ€ ์ด์ „ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ c ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด

c์—์„œ now๋กœ ๊ฐ€๋Š” ๊ฐ€์ค‘์น˜ ํ•ฉ์„ ๋”ํ•ด๋ณผ ํ•„์š”์—†์ด ์–ด์ฐจํ”ผ point[now]๊ฐ€ ์ž‘์œผ๋ฏ€๋กœ ์•„๋ž˜ ๋‚ด์šฉ์„ ์‹คํ–‰ํ•˜์ง€์•Š๋Š”๋‹ค.

 

๋งŒ์•ฝ point[now]๊ฐ€ c๋ณด๋‹ค ํฌ๋‹ค๋ฉด now์—์„œ ์—ฐ๊ฒฐ๋˜๋Š” ์ •์ ๋“ค์„ graph[now]๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณธ๋‹ค.

node[0]์—๋Š” ์—ฐ๊ฒฐ๋˜๋Š” ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€, now[1]์—๋Š” ํ•ด๋‹น ์ •์ ์œผ๋กœ ๊ฐ€๋Š”๋ฐ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋“ค์–ด์žˆ์„ ๊ฒƒ์ด๋‹ค.

cost์— ์ด์ „ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ + node[0]์œผ๋กœ ๊ฐ€๋Š”๋ฐ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋”ํ•ด ์ €์žฅํ•œ๋‹ค.

 

๋งŒ์•ฝ cost๊ฐ€ ์›๋ž˜ ๋“ค์–ด์žˆ๋˜ cost[0]์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜ ํ•ฉ์ธ point[node[0]]๋ณด๋‹ค ์ž‘๋‹ค๋ฉด

point[node[0]]๋ฅผ cost๋กœ ๊ฐฑ์‹ ํ•ด์ฃผ๊ณ  

๋‹ค์Œ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด heapq๋ฅผ ์ด์šฉํ•˜์—ฌ queue์— cost์™€ node[0]์„ ์ €์žฅํ•ด์ค€๋‹ค.

 


์œ„์˜ ๊ธ€๋กœ ๋œ ์„ค๋ช…์ด ์ดํ•ด๊ฐ€ ์•ˆ๋œ๋‹ค๋ฉด ์•„๋ž˜์˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณธ๋‹ค.

 

์šฐ์„  ์‹œ์ž‘ ๋…ธ๋“œ 1์— ๋Œ€ํ•œ point ๊ฐ’์„ 0์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์‹œ์ž‘ํ•œ๋‹ค.

queue์—๋Š” 1๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ 0, ์ •์  1์„ ์ €์žฅํ•œ๋‹ค.

์ •์  (์ธ๋ฑ์Šค) 0 1 2 3 4 5
point INF 0 INF INF INF INF

 


queue์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’ (0,1)์„ ํ™•์ธํ•œ๋‹ค.

point[1]์ด 0์œผ๋กœ ์ฝ”๋“œ์ƒ c์™€ ๊ฐ™์œผ๋ฏ€๋กœ for๋ฌธ์„ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

 

graph[now]๋ฅผ ํ†ตํ•ด 1๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์  2=(2,2)์™€ 3=(3,3)์„ ํƒ์ƒ‰ํ•œ๋‹ค.

 

(2,2)์˜ ๊ฒฝ์šฐ,

์ด์ „ ๋…ธ๋“œ 1๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ 0์ด๋ฏ€๋กœ 2๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ cost๋Š” ๊ทธ๋Œ€๋กœ 2๊ฐ€ ๋œ๋‹ค.

cost=2๋Š” ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ point[2]์˜ ๊ฐ’ INF๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ point๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

์ •์  (์ธ๋ฑ์Šค) 0 1 2 3 4 5
point INF 0 2 INF INF INF

๊ทธ๋ฆฌ๊ณ  queue์— 2๊นŒ์ง€์˜ cost 2์™€ ์ •์  2๋ฅผ ์ €์žฅํ•œ๋‹ค. = (2,2)

 

(3,3)์˜ ๊ฒฝ์šฐ,

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด์ „ ๋…ธ๋“œ 1๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ 0์ด๋ฏ€๋กœ 3๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ cost๋Š” 3์ด ๋œ๋‹ค.

cost=3์€ ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ point[3]์˜ ๊ฐ’ INF๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ point๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

์ •์  (์ธ๋ฑ์Šค) 0 1 2 3 4 5
point INF 0 2 3 INF INF

๊ทธ๋ฆฌ๊ณ  queue์— 3๊นŒ์ง€์˜ cost 3๊ณผ ์ •์  3์„ ์ €์žฅํ•œ๋‹ค. = (3,3)


queue์— ๋“ค์–ด์žˆ๋Š” ๋‹ค์Œ ๊ฐ’ (2,2)๋ฅผ ํ™•์ธํ•œ๋‹ค.

point[2]๊ฐ€ 2๋กœ ์ฝ”๋“œ์ƒ c์ธ 2์™€ ๊ฐ™์œผ๋ฏ€๋กœ for๋ฌธ์„ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

 

graph[now]๋ฅผ ํ†ตํ•ด 2์™€ ์—ฐ๊ฒฐ๋œ ์ •์  3=(3,4)์™€ 4=(4,5)๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.

 

(3,4)์˜ ๊ฒฝ์šฐ,

์ด์ „ ๋…ธ๋“œ 2๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ 2์ด๋ฏ€๋กœ 3๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ cost๋Š” 6์ด ๋œ๋‹ค.

cost=6์€ ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ point[3]์˜ ๊ฐ’ 3๋ณด๋‹ค ํฌ๋ฏ€๋กœ point๋ฅผ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

(4,5)์˜ ๊ฒฝ์šฐ

์ด์ „ ๋…ธ๋“œ 2๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ 2์ด๋ฏ€๋กœ 4๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ cost๋Š” 7์ด ๋œ๋‹ค.

cost=7์€ ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ point[4]์˜ ๊ฐ’ INF๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ point๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

์ •์  (์ธ๋ฑ์Šค) 0 1 2 3 4 5
point INF 0 2 3 7 INF

๊ทธ๋ฆฌ๊ณ  queue์— 4๊นŒ์ง€์˜ cost 7๊ณผ ์ •์  4๋ฅผ ์ €์žฅํ•œ๋‹ค. = (7,4)


queue์— ๋“ค์–ด์žˆ๋Š” ๋‹ค์Œ ๊ฐ’ (3,3)์„ ํ™•์ธํ•œ๋‹ค.

point[3]์ด 3์œผ๋กœ ์ฝ”๋“œ์ƒ c์ธ 3๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ for๋ฌธ์„ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

 

graph[now]๋ฅผ ํ†ตํ•ด 3๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์  4=(4,6)์„ ํƒ์ƒ‰ํ•œ๋‹ค.

 

(4,6)์˜ ๊ฒฝ์šฐ,

์ด์ „ ๋…ธ๋“œ 3๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ 3์ด๋ฏ€๋กœ 4๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ cost๋Š” 9๊ฐ€ ๋œ๋‹ค.

cost=9๋Š” ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ point[4]์˜ ๊ฐ’ 7๋ณด๋‹ค ํฌ๋ฏ€๋กœ point๋ฅผ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๋Š”๋‹ค.


queue์— ๋“ค์–ด์žˆ๋Š” ๋‹ค์Œ ๊ฐ’ (7,4)๋ฅผ ํ™•์ธํ•œ๋‹ค.

point[4]๊ฐ€ 7์œผ๋กœ ์ฝ”๋“œ์ƒ c์ธ 7๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ for๋ฌธ์„ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

 

graph[now]๋ฅผ ํ†ตํ•ด 4์™€ ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ํ™•์ธํ•ด์•ผํ•˜์ง€๋งŒ ์—ฐ๊ฒฐ๋œ ์ •์ ์ด ์—†์œผ๋ฏ€๋กœ for๋ฌธ์€ ์‹คํ–‰๋˜์ง€์•Š๋Š”๋‹ค.


์ด๋กœ์จ queue์—๋Š” ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.

์ตœ์ข… ์ •์  ๊ฐ„์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ point๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ •์  (์ธ๋ฑ์Šค) 0 1 2 3 4 5
point INF 0 2 3 7 INF
728x90

BELATED ARTICLES

more