각 간선의 가중치 합이 최소가 되는 두 정점(또는 노드) 사이의 경로를 찾는 문제
function Dijkstra(Graph, source):
create vertex set Q
# 초기화
for each vertex v in Graph:
# 소스에서 v까지의 아직 모르는 길이는 무한으로 설정. 실제 무한은 아니고, 아직 방문하지 않았음을 의미.
dist[v] = INFINITY
# 소스에서 최적 경로의 이전 꼭짓점
prev[v] = UNDEFINED
# 모든 노드는 초기에 Q에 속해있다 (미방문 집합)
add v to Q
# 소스에서 소스까지의 길이
dist[source] = 0
while Q is not empty:
# 최소 거리를 갖는 꼭짓점을 가장 먼저 선택하고 제거(우선순위 큐 사용)
u = vertex in Q with min dist[u]
remove u from Q
# v는 Q에 남아 있는 정점으로, 최소 거리 갖는 현재 노드의 인접 노드들
for each neighbor v of u:
# v 까지의 더 짧은 경로를 찾았을 때
alt = dist[u] + length(u, v)
if alt < dist[v]:
# 다음 인점 노드 v까지의 거리
dist[v] = alt
# v 이전 노드 업데이트
prev[v] = u
return dist[], prev[]
OPEN //the set of nodes to be evaluated
CLOSED //the set of nodes already evaluated
add the start node to OPEN
loop
current = node in OPEN with the lowest f_cost
remove current from OPEN
add current to CLOSED
if current is the target node //path has been found
return
foreach neighbour of the current node
if neighbour is not traversable or neighbour is in CLOSED
skip to the next neighbour
if new path to neighbour is shorter OR neighbour is not in OPEN
set f_cost of neighbour
set parent of neighbour to current
if neighbour is not in OPEN
add neighbour to OPEN
OPEN
:
CLOSED
:
current
:
OPEN
에서 f_cost
가 가장 작은 노드OPEN
에서 제거하고, CLOSED
에 추가current
가 target
이면 종료