1 / 17

Prim’s algorithm

Prim’s algorithm. I DEA : Maintain V – A as a priority queue Q . Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in A. Q  V key [ v ]  ¥ for all v Î V key [ s ]  0 for some arbitrary s Î V while Q ¹  do u  E XTRACT -M IN ( Q )

december
Download Presentation

Prim’s algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prim’s algorithm IDEA: Maintain V – Aas a priority queue Q. Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in A. • Q V • key[v]  ¥ for all vÎV • key[s]  0 for some arbitrary sÎV • whileQ¹  • dou  EXTRACT-MIN(Q) • for each vÎAdj[u] • do ifvÎQ and w(u, v) < key[v] • thenkey[v]  w(u, v) ⊳DECREASE-KEY • p[v]  u At the end, {(v, p[v])} forms the MST.

  2. ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ ¥ ¥ 7 14 15 8 ¥ 0 ¥ 10 3 ¥

  3. ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ ¥ ¥ 7 14 15 8 ¥ 0 ¥ 10 3 ¥

  4. ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ 7 ¥ 7 14 15 8 ¥ 0 15 10 3 10

  5. ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ 7 ¥ 7 14 15 8 ¥ 0 15 10 3 10

  6. ÎA ÎV – A Example of Prim’s algorithm 12 6 12 9 5 5 7 9 7 14 15 8 ¥ 0 15 10 3 10

  7. ÎA ÎV – A Example of Prim’s algorithm 12 6 12 9 5 5 7 9 7 14 15 8 ¥ 0 15 10 3 10

  8. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 14 0 15 10 3 8

  9. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 14 0 15 10 3 8

  10. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 14 0 15 10 3 8

  11. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 3 0 15 10 3 8

  12. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 3 0 15 10 3 8

  13. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 3 0 15 10 3 8

  14. ÎA ÎV – A Example of Prim’s algorithm 6 6 12 9 5 5 7 9 7 14 15 8 3 0 15 10 3 8

  15. Q(V) total |V| times degree(u) times Handshaking Lemma  Q(E)implicit DECREASE-KEY’s. Analysis of Prim • Q V • key[v]  ¥ for all vÎV • key[s]  0 for some arbitrary sÎV • whileQ¹  • dou  EXTRACT-MIN(Q) • for each vÎAdj[u] • do ifvÎQ and w(u, v) < key[v] • thenkey[v]  w(u, v) • p[v]  u Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY

  16. array O(V) O(1) O(V2) binary heap O(lgV) O(lgV) O(ElgV) Fibonacci heap O(lgV) amortized O(1) amortized O(E + VlgV) worst case Analysis of Prim (continued) Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY Q TEXTRACT-MIN TDECREASE-KEY Total

  17. MST algorithms • Kruskal’s algorithm (see CLRS): • Uses the disjoint-set data structure (Lecture 20). • Running time = O(ElgV). • Best to date: • Karger, Klein, and Tarjan [1993]. • Randomized algorithm. • O(V + E) expected time.

More Related