Advertisement
Guest User

Untitled

a guest
Nov 5th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.72 KB | None | 0 0
  1. function dijkstra!(graph,start)
  2. # We start by initializing all nodes.
  3. initialize_single_source!(graph, start)
  4. # Then, we put all nodes in a priority queue, based on the risk of the node.
  5. queue = PriorityQueue(u=>u.risk for u in graph)
  6. # We look at each node in the queue.
  7. while !isempty(queue)
  8. u = dequeue!(queue)
  9. # For each neighbour of a node,
  10. # we try to update the risk of that neighbour.
  11. for (v,cost) in u.neighbours
  12. relax!(u,v, cost)
  13. # if the neighbour exists in the queue,
  14. # we update the priority of that node with it's (possibly) new risk.
  15. # NOTE: If we do not incorporate the if-statement,
  16. # we create an infinite loop if there is a cycle in the graph.
  17. if haskey(queue,v) queue[v] = v.risk end
  18. end
  19. end
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement