Advertisement
AquaBlitz11

BFS vs Dijkstra

May 14th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. bfs:
  2.  
  3. // initialize
  4. visited all = false
  5. create empty queue
  6. create empty dist
  7.  
  8. // starting point
  9. visited[1] = true
  10. queue push 1
  11. dist[1] = 0
  12.  
  13. // go
  14. while q not empty:
  15. u = q top pop
  16. for each v
  17. set visited[v] = true
  18. dist[v] = dist[u]+1
  19. push v into queue
  20.  
  21. done
  22.  
  23. ------------
  24.  
  25. dijkstra:
  26.  
  27. // initialize
  28. visited all = false
  29. empty priority queue
  30. dist = 1e9 or 2e18 for everything
  31.  
  32. // starting point
  33. dist[1] = 0
  34. push starting point 1 into queue
  35. NO VISIT YET
  36.  
  37. // go
  38. while q not empty:
  39. u = q top pop
  40. visited[u] = true (confirm u)
  41. for each v
  42. IF not visited [v] and dist[u]+w < dist[v]:
  43. dist[v] = dist[u]+w
  44. push v to queue (DONT VISIT YET)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement