You are on page 1of 8

Prims Algorithm Worked Example

b 4 a 8 c 9 2 1 10 8 9 8 c 2 1 d 9 f 7 5 2 f 8 d 10 7 9 5 2 e 6 g Connected graph

b 4 a

e 6 g

Step 0 S={a} V \ S = {b,c,d,e,f,g} lightest edge = {a,b}

17

Prims Algorithm Prims Example Continued

b 4 a 8 c 9 2 8

10 7 d 9

e 6 5 2 f g

Step 1.1 before S={a} V \ S = {b,c,d,e,f,g} A={} lightest edge = {a,b}

1 10 8 7 d 2 9 1

b 4 a 8 c 9

e 6 5 2 f g

Step 1.1 after S={a,b} V \ S = {c,d,e,f,g} A={{a,b}} lightest edge = {b,d}, {a,c}

18

Prims Algorithm Prims Example Continued

b 4 a 8 c 9 2 8

10 7 d 9

e 6 5 2 f g

Step 1.2 before S={a,b} V \ S = {c,d,e,f,g} A={{a,b}} lightest edge = {b,d}, {a,c}

1 10 8 7 d 2 9 1

b 4 a 8 c 9

e 6 5 2 f g

Step 1.2 after S={a,b,d} V \ S = {c,e,f,g} A={{a,b},{b,d}} lightest edge = {d,c}

19

Prims Algorithm Prims Example Continued


b 4 a 8 c 9 2 1 10 8 9 8 c 2 1 d 9 f 7 5 2 f 8 d 10 7 9 5 2 e 6 g

Step 1.3 before S={a,b,d} V \ S = {c,e,f,g} A={{a,b},{b,d}} lightest edge = {d,c}

b 4 a

e 6 g

Step 1.3 after S={a,b,c,d} V \ S = {e,f,g} A={{a,b},{b,d},{c,d}} lightest edge = {c,f}

20

Prims Algorithm Prims Example Continued

b 4 a 8 c 9 2 8

10 7 d 9

e 6 5 2 f g

1 10 8 7 d 2 9 1

Step 1.4 before S={a,b,c,d} V \ S = {e,f,g} A={{a,b},{b,d},{c,d}} lightest edge = {c,f}

b 4 a 8 c 9

e 6 5 2 f g

Step 1.4 after S={a,b,c,d,f} V \ S = {e,g} A={{a,b},{b,d},{c,d},{c,f}} lightest edge = {f,g}

21

Prims Algorithm Prims Example Continued

b 4 a 8 c 9 2 8

10 7 d 9

e 6 5 2 f g

Step 1.5 before S={a,b,c,d,f} V \ S = {e,g} A={{a,b},{b,d},{c,d},{c,f}} lightest edge = {f,g}

1 10 8 7 d 2 9 1

b 4 a 8 c 9

e 6 5 2 f g

Step 1.5 after S={a,b,c,d,f,g} V \ S = {e} A={{a,b},{b,d},{c,d},{c,f}, {f,g}} lightest edge = {f,e}

22

Prims Algorithm Prims Example Continued

b 4 a 8 c 9 2 8

10 7 d 9

e 6 5 2 f g

1 10 8 7 d 2 9 1

Step 1.6 before S={a,b,c,d,f,g} V \ S = {e} A={{a,b},{b,d},{c,d},{c,f}, {f,g}} lightest edge = {f,e} Step 1.6 after S={a,b,c,d,e,f,g} V \ S = {} A={{a,b},{b,d},{c,d},{c,f}, {f,g},{f,e}} MST completed

b 4 a 8 c 9

e 6 5 2 f g

23

Recall Idea of Prims Algorithm


Step 0: Choose any element and set (Take as the root of our spanning tree.)

and

Step 1: Find a lightest edge such that one endpoint is in and the other is in . Add this edge to and its (other) endpoint to . Step 2: If , then stop and output the minimum span . ning tree
Otherwise go to Step 1.

Questions:

Why does this produce a Minimum Spanning Tree?

How does the algorithm nd the lightest edge and update efciently? How does the algorithm update

efciently?
24

You might also like