Skip to content

Instantly share code, notes, and snippets.

@robwhess
Created November 24, 2021 21:27
Show Gist options
  • Save robwhess/f87a89e879a95b6b88ff697156189826 to your computer and use it in GitHub Desktop.
Save robwhess/f87a89e879a95b6b88ff697156189826 to your computer and use it in GitHub Desktop.
Corrected Dijkstra's algorithm pseudocode for CS 261 assignment 4

Here's complete corrected pseudocode for Dijkstra's algorithm:

for every node N:
  cost[N] = infinity
  prev[N] = undefined

Q = new priority queue
insert N_start into Q with priority 0

while Q is not empty:
  c = Q.first_priority()  // c is the total cost of the path to N
  {N, N_prev} = Q.remove_first()  // N_prev is the previous node on the path to N
  if c < cost[N]:
    cost[N] = c
    prev[N] = N_prev
    for each neighbor N_i of N:  // A neighbor is a node connected to N by an edge
      c_i = cost of edge from N to N_i
      insert {N_i, N} into Q with priority c + c_i  // N is the previous node on the path to N_i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment