Alogrithm[7]:⭐⭐ - Dijkstra’s algorithm(Shortest Path)

Dijkstra’s algorithm

Dijkstra’s algorithm

an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks.

Pseudocode 분석

Pseudocode의 출처 -> https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue

1  function Dijkstra(Graph, source):
2      dist[source] ← 0                           // Initialization
3
4      create vertex priority queue Q
5
6      for each vertex v in Graph:          
7          if v ≠ source
8              dist[v] ← INFINITY                 // Unknown distance from source to v
9              prev[v] ← UNDEFINED                // Predecessor of v
10
11         Q.add_with_priority(v, dist[v])
12
13
14     while Q is not empty:                      // The main loop
15         u ← Q.extract_min()                    // Remove and return best vertex
16         for each neighbor v of u:              // only v that are still in Q
17             alt ← dist[u] + length(u, v)
18             if alt < dist[v]
19                 dist[v] ← alt
20                 prev[v] ← u
21                 Q.decrease_priority(v, alt)
22
23     return dist, prev