Kruskal's and Prim's algorithms can both be used to find the minimum spanning tree (MST) and minimum total edge weight needed to connect all vertices in a network. Kruskal's algorithm works by sorting all the edges by weight and building the MST by adding the shortest edges that do not form cycles. Prim's algorithm starts from one vertex and builds out the MST by always adding the shortest edge connected to any already included vertex. Both algorithms will produce the same total edge weight for the MST, but may select edges in a different order or use different edges in some cases of ties.
2. Minimum Connector Algorithms
Kruskal’s algorithm Prim’s algorithm
1. Select the shortest edge in a 1. Select any vertex
network
2. Select the shortest edge
2. Select the next shortest edge connected to that vertex
which does not create a cycle
3. Select the shortest edge
3. Repeat step 2 until all vertices connected to any vertex
have been connected already connected
4. Repeat step 3 until all
vertices have been
connected
3. 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 5 Cornwell
3
4
8 6
8
Avonford Fingley Donster
7
5
4 2
Edan
4. We model the situation as a network, then the
problem is to find the minimum connector for the
network
B 5 C
3
4
8 6
8
A F D
7
5
4 2
E
5. Kruskal’s Algorithm
List the edges in
order of size:
B 5 C ED 2
AB 3
3 AE 4
4
8 6 CD 4
BC 5
8 EF 5
A D CF 6
7 F
AF 7
5 BF 8
4 CF 8
2
E
6. Kruskal’s Algorithm
Select the shortest
edge in the network
B 5 C
ED 2
3
4
8 6
8
A D
7 F
5
4 2
E
7. Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
ED 2
3 AB 3
4
8 6
8
A D
7 F
5
4 2
E
8. Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
ED 2
3 AB 3
4
8 6 CD 4 (or AE 4)
8
A D
7 F
5
4 2
E
9. Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
ED 2
3 AB 3
4
8 6 CD 4
AE 4
8
A D
7 F
5
4 2
E
10. Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
ED 2
3 AB 3
4
8 6 CD 4
AE 4
8 BC 5 – forms a cycle
A D EF 5
7 F
5
4 2
E
11. Kruskal’s Algorithm
All vertices have been
connected.
B 5 C The solution is
3 ED 2
4
8 6 AB 3
CD 4
8 AE 4
A D EF 5
7 F
5
4 Total weight of tree: 18
2
E
12. Prim’s Algorithm
Select any vertex
B 5 A
C
Select the shortest
3 edge connected to
4
8 6 that vertex
8 AB 3
A D
7 F
5
4 2
E
13. Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 AE 4
4
8 6
8
A D
7 F
5
4 2
E
14. Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 ED 2
4
8 6
8
A D
7 F
5
4 2
E
15. Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 DC 4
4
8 6
8
A D
7 F
5
4 2
E
16. Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 CB 5 – forms a cycle
4
8 6
EF 5
8
A D
7 F
5
4 2
E
17. Prim’s Algorithm
All vertices have been
connected.
B 5 C The solution is
3 ED 2
4
8 6 AB 3
CD 4
8 AE 4
A D EF 5
7 F
5
4 Total weight of tree: 18
2
E
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.