Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function dijkstra!(graph,start)
- # We start by initializing all nodes.
- initialize_single_source!(graph, start)
- # Then, we put all nodes in a priority queue, based on the risk of the node.
- queue = PriorityQueue(u=>u.risk for u in graph)
- # We look at each node in the queue.
- while !isempty(queue)
- u = dequeue!(queue)
- # For each neighbour of a node,
- # we try to update the risk of that neighbour.
- for (v,cost) in u.neighbours
- relax!(u,v, cost)
- # if the neighbour exists in the queue,
- # we update the priority of that node with it's (possibly) new risk.
- # NOTE: If we do not incorporate the if-statement,
- # we create an infinite loop if there is a cycle in the graph.
- if haskey(queue,v) queue[v] = v.risk end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement