Created
April 23, 2016 19:55
-
-
Save nlowe/5b8a40a660d8acf541f31719abfd6f3a to your computer and use it in GitHub Desktop.
Minimum Spanning Trees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MST-Kruskal(G, w) | |
A = ∅ | |
for each vertex v ∈ G.V | |
Make-Set(v) | |
sort the edges of E by increasing w (weight) value | |
for each edge (u, v) ∈ E (taken in weight order) | |
if Find-Set(u) ≠ Find-Set(v) | |
A = A ∪ {(u, v)} | |
Union(u, v) | |
return A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MST-Prim(G, w, r) # r is an arbitrarily chosen vertex | |
for each u ∈ G.V | |
u.key = ∞ | |
u.π = NIL | |
r.key = 0 | |
Q = G.V # Q is a min-priority queue | |
while Q ≠ ∅ | |
u = Extract-min(Q) | |
for each v ∈ Adj[u] | |
if v ∈ Q and w(u, v) < v.key | |
v.π = u | |
v.key = w(u, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment