an illustration of prim's algorithm prepared spring 2006 by sean szumlanski computer science...

13
An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

Upload: leon-hill

Post on 11-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

An Illustration of Prim's Algorithm

Prepared Spring 2006 by Sean Szumlanski

Computer Science II, University of Central Florida

Page 2: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

3 4

2

Note that there may be more than one valid MST for a graph.

A deterministic algorithm will always produce the same MST for a given graph, but precisely which MST it produces will depend on your

tie-breaking mechanisms. We will see an illustration of this here.

Goal: find a minimum spanning tree (MST) for the given graph.

Page 3: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

3 4

2

Start by initializing the value at each vertex to infinity(denoted here by “inf”), except for the start vertex,

which has a value of zero.

0 inf

inf inf

inf

start vertex

(This vertex is included in the MST right off the bat, as denoted here by its color.)

Page 4: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

Update the values at vertices adjacent to A. (If the edge weight from A to some vertex is less than the value already listed at that vertex, we update that

value accordingly.)

A

B C

D

E7

9

4

1 5

3 4

2

0 4, A

7, A inf

1, A

Page 5: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value

associated with it and add it to the MST via the edge that got us there with the cost indicated.

0 4, A

7, A inf

1, A

3 4

The value associated with vertex E (1) was less than the values at the other white vertices (5, 7, infinity), so we added this vertex to the MST. Here, the A next to the 1 indicates that the edge we use to connect E is the one going to vertex A.

Vertices A and E are now in the MST, as is the edge connecting them.

Page 6: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Update the values at vertices adjacent to the new vertex, E. (If the edge weight from E to some vertex is

less than the value already listed at that vertex, we update that value accordingly.)

0 4, A

3, E 4, E

1, A

3 4

Here, the edge from E to D has a cost greater than the one already listed at this node, so we do not change this value.

We don't even consider updating the values at vertices that are already in the MST.

Page 7: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value

associated with it and add it to the MST via the edge that got us there with the cost indicated.

0 4, A

3, E 4, E

1, A

3 4

Page 8: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Update the values at vertices adjacent to the new vertex, B. (If the edge weight from B to some vertex is

less than the value already listed at that vertex, we update that value accordingly.)

0 4, A

3, E 4, E

1, A

3 4

Recall that we don't even consider updating the values at vertices that are already in the MST, like vertices A and B.

We do consider updating the value at C, but since 9 is greater than 4, we decide not to make a change.

Page 9: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value

associated with it and add it to the MST via the edge that got us there with the cost indicated.

0 4, A

3, E 4, E

1, A

3 4

I'm choosing to include C because it comes before D in the alphabet, but we could just as easily have chosen vertex D here.

Any time we perform a tie breaker like this, it means there is more than one MST for the graph (all of which are equally valid)!

Page 10: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Update the values at vertices adjacent to the new vertex, C. (If the edge weight from C to some vertex is

less than the value already listed at that vertex, we update that value accordingly.)

0 2, E

3, E 4, E

1, A

3 4

Recall that we don't even consider updating the values at vertices that are already in the MST, like vertices B and E.

We update this one, since 2 is less than the previous value of 4.

Page 11: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E7

9

4

1 5

2

Of all the vertices not already in the MST (the white vertices), we pick the one with the smallest value

associated with it and add it to the MST via the edge that got us there with the cost indicated.

0 2, E

3, E 4, E

1, A

3 4

This was the only vertex not in the MST already, so of course it's the one we pick.

Page 12: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E

1

2

Now that all the vertices are included, the algorithm terminates and we have our MST.

3 4

Resulting MST

Page 13: An Illustration of Prim's Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

A

B C

D

E

4

1

2

Note that this is also a valid MST for the original graph, and we would have come up with it if we had decided to

include vertex D instead of vertex C back on slide 9.

3

Another Perfectly Valid MST