Anda di halaman 1dari 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

8
Avonford

Donster

Fingley

5
4

2
Edan

We model the situation as a network, then the problem


is to find the minimum connector for the network

8
A

5
4

2
E

Kruskals Algorithm
List the edges in
order of size:

8
A

7
5
4

2
E

ED
AB
AE
CD
BC
EF
CF
AF
BF
CF

2
3
4
4
5
5
6
7
8
8

Kruskals Algorithm

Select the shortest


edge in the network

C
ED 2

8
A

7
5
4

2
E

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

ED 2
AB 3

8
A

7
5
4

2
E

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

ED 2
AB 3
CD 4 (or AE 4)

8
A

7
5
4

2
E

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

ED
AB
CD
AE

8
A

7
5
4

2
E

2
3
4
4

Kruskals Algorithm

Select the next shortest


edge which does not
create a cycle

8
A

7
5
4

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

8
A

7
5
4

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

8
A

7
5
4

2
E

AB 3

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

AE 4

4
8

7
5
4

2
E

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

ED 2

4
8

7
5
4

2
E

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

DC 4

4
8

7
5
4

2
E

Prims Algorithm

Select the shortest


edge connected to
any vertex already
connected.

3
6

EF 5

4
8

7
5
4

2
E

Prims Algorithm
All vertices have been
connected.

The solution is

8
A

7
5
4

2
E

AB 3
AE 4
ED 2
DC 4
EF 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.

Anda mungkin juga menyukai