3.5 Graph - 3.5.4 Dijkstra’s Algorithm

2024. 1. 30. 03:12ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.5 Graph - 3.5.4 Dijkstra's Algorithm

 

Dijkstra’s Algorithm

๐Ÿ’กDijkstra’s algorithm is a graph traversal algorithm that finds the shortest path between two nodes in a graph.

It is a weighted graph algorithm, meaning that each edge in the graph has a weight associated with it.

The algorithm works by finding the shortest path from the starting node to all other nodes in the graph.

It does this by keeping track of the distance from the starting node to each node, and then choosing the node with the shortest distance from the starting node to visit next.

It then updates the distance of each node from the starting node, and repeats the process until all nodes have been visited.

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

๊ฐ€์ค‘ ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ๊ทธ๋ž˜ํ”„์˜ ๊ฐ ์—์ง€์—๋Š” ๊ฐ€์ค‘์น˜๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. 

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ๊ทธ๋ž˜ํ”„์˜ ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๊นŒ์ง€ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. 

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ”์ ํ•œ ๋‹ค์Œ, ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ๋‹ค์Œ ๋ฐฉ๋ฌธ ๋…ธ๋“œ๋กœ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿฐ ๋‹ค์Œ ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ๊ฐ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•  ๋•Œ๊นŒ์ง€ ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜( Dijkstra's Algorithm)์€

1.2.4 Graph representation, 1.3 Heap ์—์„œ ์–ธ๊ธ‰์„ ํ•œ๋ฒˆ ํ–ˆ์—ˆ๋‹ค.

(https://parkpakrsu.tistory.com/331 - ํž™๊ณผ ๊ด€๋ จ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

 

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

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 'ํƒ์š• ์•Œ๊ณ ๋ฆฌ์ฆ˜(Greedy Algorithms)'์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋Š” ๋งค ๋‹จ๊ณ„์—์„œ ๊ฐ€์žฅ ๋น„์šฉ์ด ์ ์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋™์ž‘์€ ์ด๋Ÿฐ ์ˆœ์„œ์ด๋‹ค.

1. ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

2. ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ‘œ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. (์‹œ์ž‘ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋Š” 0, ๋‚˜๋จธ์ง€ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋Š” ๋ฌดํ•œ๋Œ€)

3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘์—์„œ ๊ฐ€์žฅ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•œ๋‹ค.

4. ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์„ ๊ณ„์‚ฐ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ‘œ๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

5. ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

In Python

 

import heapq

def dijkstra(graph, start):
    # ๊ฐ ๋…ธ๋“œ์˜ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    # ์ดˆ๊ธฐ์—๋Š” ๋ชจ๋“  ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •
    distances = {node: float('inf') for node in graph}
    distances[start] = 0  # ์‹œ์ž‘ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋Š” 0์œผ๋กœ ์„ค์ •
    
    # ์šฐ์„ ์ˆœ์œ„ ํ ์ƒ์„ฑ
    queue = []
    # ์‹œ์ž‘ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ(0)์™€ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์Œ
    heapq.heappush(queue, [distances[start], start])

    while queue:  # ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋™์•ˆ
        # ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๋…ธ๋“œ์— ๋Œ€ํ•œ ๊ฑฐ๋ฆฌ์™€ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊บผ๋ƒ„
        current_distance, current_node = heapq.heappop(queue)

        # ๊ธฐ์กด์— ๊ณ„์‚ฐํ•œ ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ์งง๋‹ค๋ฉด, ๋ฌด์‹œ
        if distances[current_node] < current_distance:
            continue

        # ์ธ์ ‘ ๋…ธ๋“œ, ๊ฐ€์ค‘์น˜๋ฅผ ์ˆœํšŒ
        for adjacent, weight in graph[current_node].items():
            # ์„ ํƒ๋œ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ์ธ์ ‘ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐ
            distance = current_distance + weight

            # ๋งŒ์•ฝ ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ์ธ์ ‘ ๋…ธ๋“œ๋กœ ๋ฐ”๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค ํ˜„์žฌ ๊ณ„์‚ฐํ•œ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ์งง๋‹ค๋ฉด
            if distance < distances[adjacent]:
                # ์ธ์ ‘ ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธ
                distances[adjacent] = distance
                # ๋‹ค์Œ ์ธ์ ‘ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ์™€ ๋‹ค์Œ ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ํ์— ๋„ฃ์Œ
                heapq.heappush(queue, [distance, adjacent])

    # ๋ชจ๋“  ๋…ธ๋“œ๋กœ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜
    return distances

 

์‹œ์ž‘ ๋…ธ๋“œ์—์„œ  ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ๋กœ ์ด๋Ÿฐ ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด A ๋…ธ๋“œ์—์„œ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

A-A ์ž๊ธฐ ์ž์‹ ์ด๋‹ˆ 0,

A-B 8, A-C-B ๊ฐ€ 1+5 ๋กœ 6. ๋”ฐ๋ผ์„œ ์ตœ์†Œ๊ฐ’ 6

์ด๋Ÿฐ์‹์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ž‘๋™์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

ํ™œ์šฉ

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋„คํŠธ์›Œํฌ ๋ผ์šฐํŒ…, GPS, ์ „๋ ฅ ๊ทธ๋ฆฌ๋“œ, SNS ์นœ๊ตฌ์ถ”์ฒœ ๋“ฑ 

์–ด๋–ค ๋…ธ๋“œ์—์„œ - ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

728x90

'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.6 Greedy Algorithms  (0) 2024.02.02
3.5 Graph - 3.5.5 A* Algorithm  (0) 2024.01.31
3.5 Graph - 3.5.3 Bellman Fordโ€™s Algorithm  (1) 2024.01.29
3.5 Graph - 3.5.2 Depth First Search(DFS)  (1) 2024.01.29
3.5 Graph - 3.5.1 Breadth First Search(BFS)  (0) 2024.01.28