250 likes | 740 Views
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 )
E N D
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.
ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ ¥ ¥ 7 14 15 8 ¥ 0 ¥ 10 3 ¥
ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ ¥ ¥ 7 14 15 8 ¥ 0 ¥ 10 3 ¥
ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ 7 ¥ 7 14 15 8 ¥ 0 15 10 3 10
ÎA ÎV – A Example of Prim’s algorithm ¥ 6 12 9 5 ¥ 7 ¥ 7 14 15 8 ¥ 0 15 10 3 10
Î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
Î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
Î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
Î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
Î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
Î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
Î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
Î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
Î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
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
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
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.