prim’s algorithm

17
© 2001 by Charles E. Leiserson © 2001 by Charles E. Leiserson Prim’s algorithm IDEA: 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. V y[v] for all v V y[s] 0 for some arbitrary s V hile Q do u EXTRACT-MIN(Q) for each v Adj[u] do if v Q and w(u, v) < key[v] then key[v] w(u, v) DECREASE-KE [v] u the end, {(v, [v])} forms the MST.

Upload: december

Post on 07-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

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 ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Prim’s algorithmIDEA: 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 Vkey[v] for all v V key[s] 0 for some arbitrary s Vwhile Q

do u EXTRACT-MIN(Q)for each v Adj[u]

do if v Q and w(u, v) < key[v]then key[v] w(u, v) ⊳ DECREASE-KEY

[v] u

At the end, {(v, [v])} forms the MST.

Page 2: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 3: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 4: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 5: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 6: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 7: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 8: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 9: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 10: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 11: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 12: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 13: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 14: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

Page 15: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Handshaking Lemma (E) implicit DECREASE-KEY’s.

Q Vkey[v] for all v V key[s] 0 for some arbitrary s Vwhile Q

do u EXTRACT-MIN(Q)for each v Adj[u]do if v Q and w(u, v) < key[v]

then key[v] w(u, v)[v] u

Analysis of Prim

degree(u)times

|V | times

(V) total

Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY

Page 16: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

Analysis of Prim (continued)

Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY

Q TEXTRACT-MIN TDECREASE-KEYTotal

array O(V) O(1) O(V2)

binary heap O(lg V) O(lg V) O(E lg V)

Fibonacci heap

O(lg V)amortized

O(1)amortized

O(E + V lg V)worst case

Page 17: Prim’s algorithm

© 2001 by Charles E. Leiserson© 2001 by Charles E. Leiserson

MST algorithms

Kruskal’s algorithm (see CLRS):• Uses the disjoint-set data structure (Lecture 20).• Running time = O(E lg V).

Best to date:• Karger, Klein, and Tarjan [1993].• Randomized algorithm.• O(V + E) expected time.