Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 11 Notes: Priority Queue Optimization to Prim's / Dijkstra's #12

Open
varmichelle opened this issue Dec 4, 2016 · 0 comments
Open

Comments

@varmichelle
Copy link
Owner

  • Priority queue sorts in increasing or decreasing order
  • To flip you can either write a comparison function or negate the distances
  • Priority queue speeds up prim's and dijkstra's when picking the minimum edge / vertex

Example implementation:
graph[N][N]
dist[N] = INF
dist[root] = 0
while q ! empty
k = q.top().index
w = q.top().distance
visited[k] = true
for j from 1 to graph[k].size()
v = graph[k][j].vertex
if (dist[v] > graph[k][k].distance && !visited[v])
dist[v] = graph[k][j].distance
q.push(v,dist[v] or -dist[v] depending on inc/dec sort order)

for dijkstra's:
if (dist[v] > graph[k][j] + dist[j]) then update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant