Skip to content

Instantly share code, notes, and snippets.

@nlowe
Created April 23, 2016 19:55
Show Gist options
  • Save nlowe/5b8a40a660d8acf541f31719abfd6f3a to your computer and use it in GitHub Desktop.
Save nlowe/5b8a40a660d8acf541f31719abfd6f3a to your computer and use it in GitHub Desktop.
Minimum Spanning Trees
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
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