Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bfs:
- // initialize
- visited all = false
- create empty queue
- create empty dist
- // starting point
- visited[1] = true
- queue push 1
- dist[1] = 0
- // go
- while q not empty:
- u = q top pop
- for each v
- set visited[v] = true
- dist[v] = dist[u]+1
- push v into queue
- done
- ------------
- dijkstra:
- // initialize
- visited all = false
- empty priority queue
- dist = 1e9 or 2e18 for everything
- // starting point
- dist[1] = 0
- push starting point 1 into queue
- NO VISIT YET
- // go
- while q not empty:
- u = q top pop
- visited[u] = true (confirm u)
- for each v
- IF not visited [v] and dist[u]+w < dist[v]:
- dist[v] = dist[u]+w
- push v to queue (DONT VISIT YET)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement