

function Dijkstra (Graph, src, dest):
for each node n in Graph: # Initialisation
dist[n] := infinity # currently unknown how to get to n
previous[n] := undefined
dist[source] := 0 # cost of, src -> src = 0
addToListOfChoices(source) # initially the only choice
while list of choices is not empty:
u := get_best_choice() # remove best node from choice list
if u == dest
return True # found route
for each neighbor v of u:# where n has not yet been visited
addToListOfChoices(v)
alt = dist[u] + length(u, v) # add absolute(u) +
# relative(u,v)
if alt < dist[v] # have we found shorter route?
dist[v] := alt # yes, so update absolute v
previous[v] := u # where did we come from?
return False

print "the choices list:", theGraph.getChoices()
print "the choices list:", theGraph.getVisited()
print u, "has neighbours", theGraph.getNode(u).getNeighbours()
print "the distance is", theGraph.getNode(v).getDistanceFrom(source)
# # simple route used by Wikipedia # # Node neighbours[cost] a b:2 c:4 b a:2 c:1 c b:1 a:4 find a -> c # answer is a -> b -> c total 3
This document was produced using groff-1.19.