function Dijkstra(Graph, source):
for each vertex v in Graph: // Initializations
dist[v] := infinity ; // Unknown distance function from source to v
previous[v] := undefined ; // Previous node in optimal path from source
end for ;
dist[source] := 0 ; // Distance from source to source
Q := the set of all nodes in Graph ;
// All nodes in the graph are unoptimized - thus are in Q
while Q is not empty: // The main loop
u := vertex in Q with smallest dist[] ;
if dist[u] = infinity:
break ; // all remaining vertices are inaccessible from source
fi ;
remove u from Q ;
for each neighbor v of u: // where v has not yet been removed from Q.
alt := dist[u] + dist_between(u, v) ;
if alt < dist[v]: // Relax (u,v,a)
dist[v] := alt ;
previous[v] := u ;
fi ;
end for ;
end while ;
return dist[] ;
end Dijkstra.
위 알고리즘은 Dijkstra 최단 경로에 대해 위키피디아에서 언급되었습니다. 여기서 내가 이해할 수없는 것은 모든 정점에 대한 거리를 무한대 [행 번호 3]로 설정하는 동안 9 행에서 u := vertex in Q with smallest dist[]
을 할당하지만 모든 거리가 무한대이므로 (행 번호 3에 설정된대로)) 어떻게 가장 작은 거리가있을 수 있습니까?위키 백과에서 Dijkstra의 알고리즘 구현에 관한 질문
, dist_between (u, v)도 dist []에 근거한 거리를 계산하고 있다고 생각했습니다. –