Vous êtes sur la page 1sur 20

Wiltshire

Decision Maths
Networks Prims Algorithm
Click to edit Master subtitle style

Prim`s Algorithm

Wiltshire

n In Lesson 1 we learnt about Kruskal`s algorithm,

n n n n n

which was used to solve minimum connector problems. Another method that can be used is Prim`s algorithm. Step 1 Select any node Step 2 Connect it to the nearest node Step 3 Connect one node already selected to the nearest unconnected node. Step 4 Repeat 3 until all nodes are connected.

Prim`s Algorithm

Wiltshire

n Consider the example we looked at last lesson. n Select any node you like. n Lets select F.

Prim`s Algorithm

Wiltshire

n Connect it to the nearest node. n C and D are both 3 away so we can choose either. n Lets select C.

Prim`s Algorithm
3 away from F. n So connect D to F.

Wiltshire

n The nearest node to either of F or C is D, which is only

Prim`s Algorithm

Wiltshire

n The nearest to D, F or C is E which is 2 from D. n So connect E to D.

Prim`s Algorithm
away from F. n Connect A to F.

Wiltshire

n The nearest to any of these four nodes is A which is 5

Prim`s Algorithm
n We now need to connect the last node, B. n The shortest arc is AB, which is 2. n Connect B to A.

Wiltshire

Prim`s Algorithm
n All the nodes are now connected so this is the

Wiltshire

minimum connector or minimal spanning tree.

Distance Table
between the two nodes.

Wiltshire

n The Network can also be represented as a table. n The infinity symbol () means there is no edge

Prim`s on a Distance Table

Wiltshire

n We are going to apply Prim`s algorithm to

the distance table. n This demonstrates how a computer could apply the algorithm. n Prim`s is more suitable than Kruskal`s as computers have a problem recognising loops. n As you go through the algorithm, see if you can relate the procedure to the last example.

Prim`s on a Distance Table


n Step 1 Select any arbitrary node. n Step 2 Delete the row and loop the column n n n n n

Wiltshire

that correspond to the node selected. Step 3 Choose the smallest number in the loop. Step 4 Delete the row that this smallest number is in. Step 5 Loop the column that corresponds to the row just deleted. Step 6 Choose the smallest number in any loop. Step 7 Repeat steps 4, 5 and 6 until all rows have been deleted and columns looped.

Prim`s on a Distance Table


n Here I have chosennumber in the loop. Select the smallest F. n Delete the row. n Loop the column.

Wiltshire

Prim`s on a Distance Table

Wiltshire

n Delete the smallest number in any loop that is not Select row C. n crossed out. C. Loop column

Prim`s on a Distance Table

Wiltshire

n Delete the smallest number in any loop that is not Select row D. n crossed out. D. Loop column

Prim`s on a Distance Table

Wiltshire

n Delete the smallest number in any loop that is not Select row E. n crossed out. E. Loop column

Prim`s on a Distance Table

Wiltshire

n Delete the smallest number in any loop that is not Select row A. n crossed out. A. Loop column

Prim`s on a Distance Table


n Delete row B. n Loop column B.

Wiltshire

Prim`s on a Distance Table

Wiltshire

n The algorithm is complete when all the columns have

been looped and the rows crossed out. n The circles show the edges in the minimum connector.

Prim`s on a Distance Table


as applying Prim`s algorithm?

Wiltshire

n In this case they are AB, DE, AF, CF, DF n Can you explain why this procedure is exactly the same

Vous aimerez peut-être aussi