You are on page 1of 18

Minimum spanning trees

Minimum Connector Algorithms


Kruskals algorithm Prims algorithm

1. 2. 3.

Select the shortest edge in a network Select the next shortest edge which does not create a cycle

1. 2. 3.

Select any vertex Select the shortest edge connected to that vertex Select the shortest edge connected to any vertex already connected Repeat step 3 until all vertices have been connected

Repeat step 2 until all vertices have been connected 4.

Example
A cable company want to connect five villages to their network which currently extends to the market town of Avonford. What is the minimum length of cable needed? Brinleigh

Cornwell

3 8
Avonford

6 8
Fingley

4
Donster

7 4

5 2
Edan

We model the situation as a network, then the problem is to find the minimum connector for the network B

3 8
A

6 8
F

4
D

7 4
E

5 2

Kruskals Algorithm
List the edges in order of size:

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

ED AB AE CD BC EF CF AF BF DF

2 3 4 4 5 5 6 7 8 8

Kruskals Algorithm Select the shortest edge in the network C 6 8 7 4 E 5 2 F D 4


ED 2

B 3 8 A

Kruskals Algorithm
Select the next shortest edge which does not create a cycle ED 2 AB 3

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

Kruskals Algorithm
Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4)

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

Kruskals Algorithm
Select the next shortest edge which does not create a cycle ED AB CD AE 2 3 4 4

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

Kruskals Algorithm
Select the next shortest edge which does not create a cycle ED AB CD AE BC EF 2 3 4 4 5 forms a cycle 5

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

Kruskals Algorithm
All vertices have been connected.

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

The solution is ED AB CD AE EF 2 3 4 4 5

Total weight of tree: 18

Prims Algorithm
Select any vertex

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

A Select the shortest edge connected to that vertex AB 3

Prims Algorithm
Select the shortest edge connected to any vertex already connected.

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

AE 4

Prims Algorithm
Select the shortest edge connected to any vertex already connected.

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

ED 2

Prims Algorithm
Select the shortest edge connected to any vertex already connected.

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

DC 4

Prims Algorithm
Select the shortest edge connected to any vertex already connected.

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

CB 5 forms a cycle EF 5

Prims Algorithm
All vertices have been connected.

B 3 8 A 4 E 7

C 6 8 F 5 2 D 4

The solution is ED AB CD AE EF 2 3 4 4 5

Total weight of tree: 18

Some points to note Both algorithms will always give solutions with the same length. They will usually select edges in a different order you must show this in your workings. Occasionally they will use different edges this may happen when you have to choose between edges with the same length. In this case there is more than one minimum connector for the network.