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 8 Notes: Minimum Spanning Trees / Prim's #9

Open
varmichelle opened this issue Oct 29, 2016 · 0 comments
Open

Week 8 Notes: Minimum Spanning Trees / Prim's #9

varmichelle opened this issue Oct 29, 2016 · 0 comments

Comments

@varmichelle
Copy link
Owner

varmichelle commented Oct 29, 2016

Prim's Algorithm (Finding the Minimum Spanning Tree) O(N^2)

Algorithm: Pick a starting node, at each step pick the cheapest edge (smallest weight) and add the node to the current tree. Repeat for n-1 steps.

  1. Initialize distances to infinity
    for x in 1 to N: dist[x] = INF

  2. Set distance to root node to 0
    dist[start] = 0 // start can be any node

  3. Pick the minimum edge
    loop through all x from 1 to N and find minimum dist that's not visited

  4. Mark visited
    visited[node] = true

  5. Update neighbors
    loop through neighbors of the node: dist[j] = min(dist[j], mat[x][j])

@varmichelle varmichelle changed the title Minimum Spanning Trees / Prim's - Week 8 Notes Week 8 Notes: Minimum Spanning Trees / Prim's Nov 14, 2016
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