Top Banner
Minimum spanning trees 1
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ch 3

Minimum spanning treesMinimum spanning trees

1

Page 2: Ch 3

MSTA minimum spanning tree connects all nodes in a given graphA MST must be a connected and undirected graphA MST can have weighted edgesMultiple MSTs can exist within a given undirected graphMultiple MSTs can be generated depending on which algorithm is usedIf you wish to have an MST start at a specific nodeHowever, if there are weighted edges and all weighted edges are unique, only one MST will exist

2

Page 3: Ch 3

Borůvka’s Algorithm

• The first MST Algorithm was created by Otakar Borůvka in 1926

• The algorithm was used to create efficient connections between the electricity network in the Czech Republic

• No longer used since Prim’s and Kruskal’s algorithms were discovered

3

Page 4: Ch 3

Prim’s Algorithm

• Initially discovered in 1930 by Vojtěch Jarník, then rediscovered in 1957 by Robert C. Prim

• Similar to Dijkstra’s Algorithm regarding a connected graph

• Starts off by picking any node within the graph and growing from there

4

Page 5: Ch 3

Kruskal’s Algorithm• Created in 1957 by Joseph Kruskal• Finds the MST by taking the smallest weight in the graph

and connecting the two nodes and repeating until all nodes are connected to just one tree

• This is done by creating a priority queue using the weights as keys

• Each node starts off as it’s own tree• While the queue is not empty, if the edge retrieved

connects two trees, connect them, if not, discard it• Once the queue is empty, you are left with the minimum

spanning tree

5

Page 6: Ch 3

Minimum Connector Algorithms

Kruskal’s algorithm

1. Select the shortest edge in a network

2. Select the next shortest edge which does not create a cycle

3. Repeat step 2 until all vertices have been connected

Prim’s 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

Prim’s 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

6

Page 7: Ch 3

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

Addis Hawassa

Adama Awash

Dilla

Wolita

2

7

45

8 6 4

5

3

8

Example

7

Page 8: Ch 3

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

A F

B C

D

E

2

7

45

8 6 4

5

3

8

8

Page 9: Ch 3

A F

B C

D

E

2

7

45

8 6 4

5

3

8

List the edges in order of size:

ED 2AB 3AE 4CD 4BC 5EF 5CF 6AF 7BF 8CF 8

Kruskal’s Algorithm

9

Page 10: Ch 3

Select the shortestedge in the network

ED 2

Kruskal’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

10

Page 11: Ch 3

Select the next shortestedge which does notcreate a cycle

ED 2AB 3

Kruskal’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

11

Page 12: Ch 3

Select the next shortestedge which does notcreate a cycle

ED 2AB 3CD 4 (or AE 4)

Kruskal’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

12

Page 13: Ch 3

Select the next shortestedge which does notcreate a cycle

ED 2AB 3CD 4 AE 4

Kruskal’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

13

Page 14: Ch 3

Select the next shortestedge which does notcreate a cycle

ED 2AB 3CD 4 AE 4BC 5 – forms a cycleEF 5

Kruskal’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

14

Page 15: Ch 3

All vertices have beenconnected.

The solution is

ED 2AB 3CD 4 AE 4EF 5

Total weight of tree: 18

Kruskal’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

15

Page 16: Ch 3

A F

B C

D

E

2

7

45

8 6 4

5

3

8

Select any vertex

A

Select the shortest edge connected to that vertex

AB 3

Prim’s Algorithm

16

Page 17: Ch 3

A F

B C

D

E

2

7

45

8 6 4

5

3

8

Select the shortestedge connected to any vertex already connected.

AE 4

Prim’s Algorithm

17

Page 18: Ch 3

Select the shortestedge connected to any vertex already connected.

ED 2

Prim’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

18

Page 19: Ch 3

Select the shortestedge connected to any vertex already connected.

DC 4

Prim’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

19

Page 20: Ch 3

Select the shortestedge connected to any vertex already connected.

EF 5

Prim’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

20

Page 21: Ch 3

Prim’s Algorithm

A F

B C

D

E

2

7

45

8 6 4

5

3

8

All vertices have beenconnected.

The solution is

AB 3AE 4ED 2DC 4EF 5

Total weight of tree: 18

21

Page 22: Ch 3

•Both algorithms will always give solutions with the same length.

•They will usually select edges in a different order•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.

Some points to note

22

Page 23: Ch 3

Questions?

23