| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
- Pre-processing
- baekjoon
- C++
- 오블완
- function
- vscode
- predictive analysis
- string
- Object Oriented Programming
- Python
- pointer
- 배열
- pass by reference
- const
- Deep Learning
- programming
- 파이썬
- 함수
- Data Science
- 티스토리챌린지
- raw data
- 백준
- 문자열
- 포인터
- OOP
- array
- 알고리즘
- 반복문
- assignment operator
- Class
- Today
- Total
Channi Studies
[Python] Dijkstra's Shortest Path | 다익스트라 최단거리 본문
[Python] Dijkstra's Shortest Path | 다익스트라 최단거리
Chan Lee 2025. 5. 27. 08:33Finding the shortest path between vertices in a graph has many applications.
Dijkstra's shortest path algorithm, created by Edsger Dijkstra, determines the shortest path from a start vertex to each vertex in a graph. For each vertex, Dijkstra's algorithm determines the vertex's distance and predecessor pointer.
A vertex's distance is the shortest path distance from the start vertex. A vertex's predecessor pointer points to the previous vertex along the shortest path from the start vertex.
Dijkstra's algorithm initializes all vertices' distances to infinity (∞), initializes all vertices' predecessors to null, and enqueues all vertices into a queue of unvisited vertices. The algorithm then assigns the start vertex's distance with 0. While the queue is not empty, the algorithm dequeues the vertex with the shortest distance. For each adjacent vertex, the algorithm computes the distance of the path from the start vertex to the current vertex and continuing on to the adjacent vertex. If that path's distance is shorter than the adjacent vertex's current distance, a shorter path has been found. The adjacent vertex's current distance is updated to the distance of the newly found shorter path's distance, and vertex's predecessor pointer is pointed to the current vertex.
To perfrom Dijkstra's algorithm, the Graph and Vertex classes are used. The Vertex class is extended to include two additional data members:
- distance: The total sum of the edge weights on a path from some start vertex to the vertex.
- pred_vertex: A reference to the vertex that occurs immediately before the vertex, on a path from some start vertex to the vertex.
def dijkstra_shortest_path(g, start_vertex):
# Put all vertices in an unvisited queue.
unvisited_queue = []
for current_vertex in g.adjacency_list:
unvisited_queue.append(current_vertex)
# start_vertex has a distance of 0 from itself
start_vertex.distance = 0
# One vertex is removed with each iteration; repeat until the list is
# empty.
while len(unvisited_queue) > 0:
# Visit vertex with minimum distance from start_vertex
smallest_index = 0
for i in range(1, len(unvisited_queue)):
if unvisited_queue[i].distance < unvisited_queue[smallest_index].distance:
smallest_index = i
current_vertex = unvisited_queue.pop(smallest_index)
# Check potential path lengths from the current vertex to all neighbors.
for adj_vertex in g.adjacency_list[current_vertex]:
edge_weight = g.edge_weights[(current_vertex, adj_vertex)]
alternative_path_distance = current_vertex.distance + edge_weight
# If shorter path from start_vertex to adj_vertex is found,
# update adj_vertex's distance and predecessor
if alternative_path_distance < adj_vertex.distance:
adj_vertex.distance = alternative_path_distance
adj_vertex.pred_vertex = current_vertex
def get_shortest_path(start_vertex, end_vertex):
# Start from end_vertex and build the path backwards.
path = ''
current_vertex = end_vertex
while current_vertex is not start_vertex:
path = ' -> ' + str(current_vertex.label) + path
current_vertex = current_vertex.pred_vertex
path = start_vertex.label + path
return path'Data Science > Data Structure & Algorithm' 카테고리의 다른 글
| [Data Structure] Graph: Topological Sort (0) | 2025.05.28 |
|---|---|
| [Python] Bellman-Ford's Shortest Path (0) | 2025.05.28 |
| [Data Structure] Python: Graphs (0) | 2025.05.27 |
| [Data Structure] Weighted Graphs (0) | 2025.05.26 |
| [Data Structure] Directed Graphs (0) | 2025.05.26 |