Docsity

Algorithms for Finding Minimum Spanning Trees: Prim's and Kruskal's, Lecture notes of Computer Science

Partial preview of the text

Download Algorithms for Finding Minimum Spanning Trees: Prim's and Kruskal's and more Lecture notes Computer Science in PDF only on Docsity!

Minimum Spanning Trees,

Prim’s, Kruskal’s

Minimum-Weight Spanning Tree

Prim(G,w) O((E+V) log V)

  1. Input: graph G with edge weights w
  2. Output: MST defined by prev(v)
  3. cost(v in V)=inf, prev(v in V)= nil, cost(v 0
  1. H = makePQ(V) # with cost(v) as priority
  2. while (H not empty):
  3. v = deletemin(H)
  4. for all edges (v,z) in E:
  5. if (cost(z) > w(v,z):
  6. cost(z) = w(v,z)
  7. prev(z) = v
  8. decreasekey(H, z)

Prim(G,w) (fib) O(E+V log V)

  1. Input: graph G with edge weights w
  2. Output: MST defined by prev(v)
  3. cost(v in V)=inf, prev(v in V)= nil, cost(v 0
  1. H = makePQ(V) # with cost(v) as priority
  2. while (H not empty):
  3. v = deletemin(H)
  4. for all edges (v,z) in E:
  5. if (cost(z) > w(v,z):
  6. cost(z) = w(v,z)
  7. prev(z) = v
  8. decreasekey(H, z)

Kruskal(G, w) (Union-Find DS)

 Input: graph G, weights w

 Output: MST defined by edges X

 (add lightest non-cycle edge)*

 For all u in V:

 makeset(u) # disjoint sets of size 1

 X = {}

 for all edges (u,v) sorted by weight:

 if (find(u) ≠ find(v)): # not in same set

 add edge (u,v) to X

 union(u, v) # combine sets