YS's develop story

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ ๋ณธ๋ฌธ

Algorithm

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ

Yusang 2021. 8. 14. 09:40

๐Ÿ‘ฉ‍๐Ÿ’ป ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ with Python

๐Ÿฅ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Dijkstra Algorithm)์ด๋ž€? 

๋‘ ๋…ธ๋“œ๋ฅผ ์ž‡๋Š” ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ(๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ)์—์„œ

ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์  ๊ฐ„์˜ ๊ฐ๊ฐ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

 

์ฒซ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •์ ๋“ค์„ ์ถ”๊ฐ€ํ•ด ๊ฐ€๋ฉฐ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1. ์ดˆ๊ธฐํ™”

  • ์ฒซ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜์—ฌ ์ฒซ ์ •์ ์—์„œ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • ์ดˆ๊ธฐ์—๋Š” ์ฒซ ์ •์ ์˜ ๊ฑฐ๋ฆฌ๋Š” 0, ๋‚˜๋จธ์ง€๋Š” ๋ฌดํ•œ๋Œ€๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. (inf๋ผ๊ณ  ํ‘œํ˜„ํ•จ)
    • ์šฐ์„ ์ˆœ์œ„ ํ์— (์ฒซ ์ •์ , ๊ฑฐ๋ฆฌ 0)๋งŒ ๋จผ์ € ๋„ฃ์Šต๋‹ˆ๋‹ค.

 

2. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ์ถ”์ถœํ•œ (A, 0) [๋…ธ๋“œ, ์ฒซ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ]๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

  • ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋ƒ…๋‹ˆ๋‹ค.
    • ์ฒ˜์Œ์—๋Š” ์ฒซ ์ •์ ๋งŒ ์ €์žฅ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ์ฒซ ์ •์ ์ด ๊บผ๋‚ด์ง‘๋‹ˆ๋‹ค.
    • ์ฒซ ์ •์ ์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค ๊ฐ๊ฐ์— ๋Œ€ํ•ด, ์ฒซ ์ •์ ์—์„œ ๊ฐ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ์™€ ํ˜„์žฌ ๋ฐฐ์—ด์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ฒซ ์ •์ ์—์„œ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค
    • ๋ฐฐ์—ด์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค, ์ฒซ ์ •์ ์—์„œ ํ•ด๋‹น ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ์งง์„ ๊ฒฝ์šฐ, ๋ฐฐ์—ด์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค
    • ๋ฐฐ์—ด์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์—…๋ฐ์ดํŠธ๋œ ๊ฒฝ์šฐ, ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
      • ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ, ์ฒซ ์ •์ ์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค
      • ๋งŒ์•ฝ ๋ฐฐ์—ด์— ๊ธฐ๋ก๋œ ํ˜„์žฌ๊นŒ์ง€ ๋ฐœ๊ฒฌ๋œ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ณด๋‹ค, ๋” ๊ธด ๊ฑฐ๋ฆฌ(๋ฃจํŠธ)๋ฅผ ๊ฐ€์ง„ (๋…ธ๋“œ, ๊ฑฐ๋ฆฌ)์˜ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

3. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ (C, 1) [๋…ธ๋“œ, ์ฒซ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ]๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

  • ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ MinHeap(์ตœ์†Œ ํž™) ๋ฐฉ์‹์ด๋ฏ€๋กœ, ์œ„ ํ‘œ์—์„œ ๋„ฃ์–ด์ง„ (C, 1), (D, 2), (B, 8) ์ค‘ (C, 1) ์ด ๋จผ์ € ์ถ”์ถœ๋ฉ๋‹ˆ๋‹ค.
  • ์œ„ ํ‘œ์—์„œ ๋ณด๋“ฏ์ด 1๋‹จ๊ณ„๊นŒ์ง€์˜ A - B ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” 8 ์ธ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.
    • A - C๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” 1, C์— ์ธ์ ‘ํ•œ B, D์—์„œ C - B๋Š” 5, ์ฆ‰ A - C - B๋Š” 1 + 5 = 6 ์ด๋ฏ€๋กœ, A - B ์ตœ๋‹จ ๊ฑฐ๋ฆฌ 8๋ณด๋‹ค ๋” ์ž‘์€ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐœ๊ฒฌ, ์ด๋ฅผ ๋ฐฐ์—ด์— ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
      • ๋ฐฐ์—ด์— ์—…๋ฐ์ดํŠธํ–ˆ์œผ๋ฏ€๋กœ B, 6 (์ฆ‰ A์—์„œ B๊นŒ์ง€์˜ ํ˜„์žฌ๊นŒ์ง€ ๋ฐœ๊ฒฌํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ) ๊ฐ’์ด ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์–ด์ง‘๋‹ˆ๋‹ค.
    • C - D์˜ ๊ฑฐ๋ฆฌ๋Š” 2, ์ฆ‰ A - C - D๋Š” 1 + 2 = 3 ์ด๋ฏ€๋กœ, A - D์˜ ํ˜„์žฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์ธ 2 ๋ณด๋‹ค ๊ธด ๊ฑฐ๋ฆฌ, ๊ทธ๋ž˜์„œ D์˜ ๊ฑฐ๋ฆฌ๋Š” ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

4. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ (D, 2) [๋…ธ๋“œ, ์ฒซ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ]๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

  • ์ง€๊ธˆ๊นŒ์ง€ ์ ‘๊ทผํ•˜์ง€ ๋ชปํ–ˆ๋˜ E์™€ F ๊ฑฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค.
    • A - D๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์ธ 2์— D - E ๊ฐ€ 3 ์ด๋ฏ€๋กœ ์ด๋ฅผ ๋”ํ•ด์„œ E, 5
    • A - D๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์ธ 2์— D - F ๊ฐ€ 5 ์ด๋ฏ€๋กœ ์ด๋ฅผ ๋”ํ•ด์„œ F, 7

 

5. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ (E, 5) [๋…ธ๋“œ, ์ฒซ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ]๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

  • A - E ๊ฑฐ๋ฆฌ๊ฐ€ 5์ธ ์ƒํƒœ์—์„œ, E์— ์ธ์ ‘ํ•œ F๋ฅผ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๋Š” 1, ์ฆ‰ A - E - F๋Š” 5 + 1 = 6, ํ˜„์žฌ ๋ฐฐ์—ด์— A - F ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ 7๋กœ ๊ธฐ๋ก๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, F, 6์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
    • ์šฐ์„ ์ˆœ์œ„ ํ์— F, 6 ์ถ”๊ฐ€

 

6. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ (B, 6), (F, 6)๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถ”์ถœํ•ด ๊ฐ ๋…ธ๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

  • ์˜ˆ์ œ์˜ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ B ๋…ธ๋“œ๋Š” ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋ฃจํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • F ๋…ธ๋“œ๋Š” A ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋ฃจํŠธ๊ฐ€ ์žˆ์œผ๋‚˜, ํ˜„์žฌ A - A ๊ฐ€ 0 ์ธ ๋ฐ˜๋ฉด์— A - F - A๋Š” 6 + 5 = 11, ์ฆ‰ ๋” ๊ธด ๊ฑฐ๋ฆฌ์ด๋ฏ€๋กœ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

6. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ (F, 7), (B, 8)๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถ”์ถœํ•ด ๊ฐ ๋…ธ๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

  • A - F๋กœ ๊ฐ€๋Š” ํ•˜๋‚˜์˜ ๋ฃจํŠธ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ 7 ์ธ ์ƒํ™ฉ์ด๋‚˜, ๋ฐฐ์—ด์—์„œ ์ด๋ฏธ A - F๋กœ ๊ฐ€๋Š” ํ˜„์žฌ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ 6์ธ ๋ฃจํŠธ์˜ ๊ฐ’์ด ์žˆ๋Š” ์ƒํ™ฉ์ด๋ฏ€๋กœ, ๋” ๊ธด ๊ฑฐ๋ฆฌ์ธ F, 7 ๋ฃจํŠธ ๊ธฐ๋ฐ˜ ์ธ์ ‘ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์Œ, ๊ทธ๋ž˜์„œ ๊ณ„์‚ฐ ์—†์ด ์Šคํ‚ตํ•ฉ๋‹ˆ๋‹ค.
    • ๊ณ„์‚ฐํ•˜๋”๋ผ๋„ A - F ๊ฑฐ๋ฆฌ๊ฐ€ 6์ธ ๋ฃจํŠธ๋ณด๋‹ค ๋ฌด์กฐ๊ฑด ๋” ๊ธด ๊ฑฐ๋ฆฌ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค.
  • B, 8 ๋„ ํ˜„์žฌ A - B ๊ฑฐ๋ฆฌ๊ฐ€ 6์ด๋ฏ€๋กœ, ์ธ์ ‘ ๋…ธ๋“œ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ‹ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Dijkstra Algorithm) ์ฝ”๋“œ

mygraph = {
    'A': {'B': 8, 'C': 1, 'D': 2},
    'B': {},
    'C': {'B': 5, 'D': 2},
    'D': {'E': 3, 'F': 5},
    'E': {'F': 1},
    'F': {'A': 5}
}

import heapq


def Dijkstra(graph, start):
    distances = {node: float('inf') for node in graph}
    distances[start] = 0
    queue = []

    heapq.heappush(queue, [distances[start], start])

    while queue:
        currentDistance, currentNode = heapq.heappop(queue)

        if currentDistance > distances[currentNode]:
            continue

        for adjacent, weight in graph[currentNode].items():
            distance = currentDistance + weight

            if distance < distances[adjacent]:
                distances[adjacent] = distance
                heapq.heappush(queue, [distance, adjacent])

    return distances


print(Dijkstra(mygraph, 'A'))

 

๐Ÿ‰ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Dijkstra Algorithm) ์‹œ๊ฐ„ ๋ณต์žก๋„

  • ๊ฐ ์ •์ ๋งˆ๋‹ค ์ธ์ ‘ํ•œ Edge๋“ค์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ณผ์ •
    ๊ฐ ๋…ธ๋“œ๋Š” ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๊ฐ„์„ ์€ ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๊ณผ์ •์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” 
  • ์šฐ์„ ์ˆœ์œ„ ํ์— [๊ฑฐ๋ฆฌ, ์ •์ ] ์ •๋ณด๋ฅผ ๋„ฃ๊ณ  ๋นผ๋Š” ๊ณผ์ •
    ์ตœ์•…์˜ ๊ฒฝ์šฐ๋Š” ๋ชจ๋“  ๊ฐ„์„ ์„ ๊ฒ€์‚ฌํ•  ๋•Œ๋งˆ๋‹ค ๊ฑฐ๋ฆฌ ๊ฐ’ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ฐฑ์‹ ๋˜๊ณ , ์šฐ์„ ์ˆœ์œ„ ํ์— ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. E๊ฐœ์˜ Edge๋ฅผ ๊ฒ€์‚ฌํ•  ๋•Œ๋งˆ๋‹ค ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ด ๊ณผ์ •์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” 

์œ„ ๋‘ ๊ณผ์ •์„ ๊ฑฐ์น  ๊ฒฝ์šฐ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š”  ๋ฉ๋‹ˆ๋‹ค.

Comments