0% found this document useful (0 votes)
578 views

D1, L5 1 Kruskal's and Prim's Algorithms

This document describes two algorithms for finding minimum spanning trees: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm involves selecting the shortest edge in the network that does not create a cycle, repeating until all vertices are connected. Prim's algorithm involves selecting any starting vertex and then the shortest edge connected to vertices already in the tree, repeating until all vertices are connected. Both algorithms are demonstrated on a network connecting five villages to a market town, with the minimum spanning tree found having a total weight of 18 using either algorithm.

Uploaded by

mokhtarppg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
578 views

D1, L5 1 Kruskal's and Prim's Algorithms

This document describes two algorithms for finding minimum spanning trees: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm involves selecting the shortest edge in the network that does not create a cycle, repeating until all vertices are connected. Prim's algorithm involves selecting any starting vertex and then the shortest edge connected to vertices already in the tree, repeating until all vertices are connected. Both algorithms are demonstrated on a network connecting five villages to a market town, with the minimum spanning tree found having a total weight of 18 using either algorithm.

Uploaded by

mokhtarppg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Minimum spanning trees

Minimum Connector Algorithms


Kruskals algorithm

1.
2.
3.

Select the shortest edge in a


network

Prims algorithm

1.

Select any vertex

2.

Select the shortest edge


connected to that vertex

3.

Select the shortest edge


connected to any vertex
already connected

4.

Repeat step 3 until all


vertices have been
connected

Select the next shortest edge


which does not create a cycle
Repeat step 2 until all vertices
have been connected

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

Avonford

7
4

Fingley

5
2
Edan

Donster

We model the situation as a network, then the problem


is to find the minimum connector for the network

2
E

Kruskals Algorithm

List the edges in


order of size:

7
5

2
E

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

ED 2

7
5

2
E

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

ED 2
AB 3

7
5

2
E

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

ED 2
AB 3
CD 4 (or AE 4)

7
5

2
E

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

ED
AB
CD
AE

7
5

2
E

2
3
4
4

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

7
5

2
E

ED
AB
CD
AE
BC
EF

2
3
4
4
5 forms a cycle
5

Kruskals Algorithm

All vertices have been


connected.

The solution is

7
5

2
E

ED
AB
CD
AE
EF

2
3
4
4
5

Total weight of tree: 18

Prims Algorithm
Select any vertex

Select the shortest


edge connected to
that vertex

7
5

2
E

AB 3

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

AE 4

4
8

7
5

2
E

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

ED 2

4
8

7
5

2
E

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

DC 4

4
8

7
5

2
E

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

CB 5 forms a cycle

EF 5

7
5

2
E

Prims Algorithm

All vertices have been


connected.

The solution is

7
5

2
E

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.

You might also like