You are on page 1of 34

Minimum Spanning Tree

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Spanning Trees
Consider a graph that represents airline connection
If economic situation forces this airline to shut down as many connection as possible, which of them should be retained to make sure that it is still possible to reach any city from other cities The point is to find a solution that allows us to remove as many edges as possible such that any node can be reached from any other node You may be able to find several solutions to this problem; however, if there is cost involved in going from node A to node B , then we prefer to minimize the number of connections with the lowest cost possible

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

This type of problem is called finding Minimum Spanning Tree (MST) which is a Spanning Tree of a graph in which the scan of the weights of its edges is minimum There are several algorithms to find MST. Some well-known algorithms are discussed in this course Boruvkas algorithm Kruskals Algorithm Jannik Primes Algorithm Dijkstras Algorithm
a b a b a b

c e g
A.R. Hadaegh Dr. Ahmad R. Hadaegh

d f

c e g

c e g
Page

d f

Solution 1

Solution 2

California State University San Marcos (CSUSM)

Boruvkas algorithm: Finding MST


Proposed in 1926 In this method start with |V| on-vertex trees For each vertex v, we look for an edge (vw) of minimum weight among all edges outgoing from v and create small trees by including these edges Look for edges of minimal weight that can connect the resulting trees of large trees The process is finished when one tree is created

The pseudocode is:


BoruvkaAlgorithm (weighted connected undirected graph) make each vertex the root of one-node tree while there is more than one tree for each tree t e = minimum weight edge (vu) where v is included in t and u is not create a tree by combining t and the tree that includes u if such a tree does not exist yet
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 4

First Iteration: (step 1)


We first start with |V| vertices that are 7 isolated trees For each tree we look for an edge with minimum weight uv where u and v belong to two different trees For a we pick ac For b we pick ba For c we pick ca (same as ac) For d we pick df For e we pick eg For f we pick fg For g we pick gf (same as fg) The result is shown in here

a 5 c 12

6 9 13

b 16 15

Original graph

d 7 f 3

e
8 g

a 5 c

d e 8 3 g
Page 5

7 f

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Second Iteration: (step 2)


We are not done yet because we still have more than one tree: t1 and t2 For each tree we look for an edge with minimum weight uv where u and v belong to two different trees For t1, our options are cf, be, and cd but we pick cf because cf has the minimum weight Similarly, for t2, our options are fc, eb, and dc but we pick fc because fc has the minimum weight The result is shown in here This is the final result because we now have only one tree What is the runing time of this algorithm?
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c

6 13

b 16

t1 t2
d 7

12 e 8 3 g

a 5 c

d 12 e 8 3 g
Page 6

7 f

California State University San Marcos (CSUSM)

Kruskals algorithm: Finding MST


In this method Edges are ordered by weight in ascending order Each edge in this ordered sequence is checked to see if it can be considered part of the tree construction The edge is added if no cycle is created after its inclusion
The pseudocode is:
KruskalAlgorithm (weighted connected undirected graph) tree = null; edges = sequence of all edges of graph sorted by weight; for each edge uv in the sorted edge list if adding uv does not cause a cycle with other edges in the tree then add uv to tree
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 7

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, df, eg, bc, cf, be, de, cd a

6 9 13

b 16

We find the first edge with minimum weight

5 c 12

d 7 f 3

15 e 8 g

This edge is gf with weight 3


So we add gf

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, df, eg, bc, cf, be, de, cd a

6 9 13

b 16

We find the next edge with minimum weight


This edge is ac with weight 5

5 c 12

d 7 f 3

15 e 8 g

We add ac because it does not cause a cycle

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, df, eg, bc, cf, be, de, cd a

6 9 13

b 16

We find the next edge with minimum weight This edge is ab with weight 6 We add ab because it does not cause a cycle

5 c 12

d 7 f 3

15 e 8 g

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 10

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, fd, eg, bc, cf, be, de, cd a

6 9 13

b 16

We find the next edge with minimum weight

5 c 12

d 7 f 3

This edge is fd with weight 7


We add fd because it does not cause a cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

15 e 8 g

California State University San Marcos (CSUSM)

Page 11

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, df, eg, bc, cf, be, de, cd a

6 9 13

b 16

We find the next edge with minimum weight

5 c 12

d 7 f 3

This edge is eg with weight 8


We add eg because it does not cause a cycle

15 e 8 g

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 12

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, df, eg, cb, cf, be, de, cd a

6 9 13

b 16

We find the next edge with minimum weight This edge is cb with weight 9 We cannot add cb because cb causes cycle in the tree The next one is cf, with weight 12 We add cf because cf does not cause cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

5 c 12

d 7 f 3

15 e 8 g

California State University San Marcos (CSUSM)

Page 13

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

gf, ac, ab, df, eg, cb, cf, be, de, cd a

Now we only have one connected tree and adding any more edge (be, de, and cd) causes cycle
Thus, we are done Can you find the running time of Kruskal Algorithm?
A.R. Hadaegh Dr. Ahmad R. Hadaegh

5 c 12 e 8 g 3 f 7 d

California State University San Marcos (CSUSM)

Page 14

Jarnik Prims Algorithm : Finding MST


Proposed in 1936 In this method Edges are ordered by weight in ascending order Add an edge to the spanning tree with the minimum cost if The inclusion makes no cycle and The edge is incident to a vertex that is already in the spanning tree (i. e. the spanning tree grows as one connected tree) The pseudocode is:
JarnikPrimeAlgorithm (weighted connected undirected graph) tree = null; edges = sequence of all edges of graph sorted by weight; for i = 1 to |V| -1 for j= 1 to |edges| if adding uv does not cause a cycle with other edge in the spanning tree and uv is incident to a vertex that is in the spanning tree then add uv to the spanning tree
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 15

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

6 9 13

b 16

We find the first edge with minimum weight


This edge is fg with weight 3

5 c 12

d 7 f 3

15 e 8 g

So we add fg first

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 16

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

Next we find the edges incident to gf. They are, ge, fc and fd We pick fd because it has minimum weight and causes no cycle

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 g

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 17

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

Next we find the edges incident to vertices of the current spanning tree They are ge, fc, de, and dc. We pick ge because it has minimum weight and causes no cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 g

California State University San Marcos (CSUSM)

Page 18

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

Again we find the edges incident to vertices of the current spanning tree They are ed, eb, fc, and dc We pick fc because it has minimum weight and causes no cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 g

California State University San Marcos (CSUSM)

Page 19

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

Again we find the edges incident to vertices of the current spanning tree They are ca, cb, cd, eb, and ed We pick ca because it has minimum weight and causes no cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 g

California State University San Marcos (CSUSM)

Page 20

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

Again we find the edges incident to vertices of the current spanning tree They are ab, cb, cd, eb, and ed We pick ab because it has minimum weight and causes no cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 g

California State University San Marcos (CSUSM)

Page 21

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

Other edges are cb, cd, eb, and ed. Addition of any of these edges causes cycle Therefore, we are done What is the running time?
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

d e 8 g 3 7 f

California State University San Marcos (CSUSM)

Page 22

Dijkstras Algorithm : Finding MST


In this method no sorting is required Pick up an edge from unsorted list and add it to the tree If cycle occurs, remove the edge with maximum weight to break the cycle

The pseudocode is:


DijkstraAlgorithm (weighted connected undirected graph) tree = null; edges = unsorted list of all edges of graph; for each uv in edges add uv to spanning tree if there is a cycle in the spanning tree then remove an edge with maximum weight to break the cycle

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 23

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

Assuming that the edges are ordered alphabetically and not by weight We pick the first edge which is ab We add ab first
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

6 9 13

b 16

d 7 f 3
Page 24

15
e 8 g

California State University San Marcos (CSUSM)

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

Next, we pick ac We add ac and check for cycle No cycle is detected

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 g

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 25

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8 ab, ac, bc, be, cd, cf, de, df, eg, fg

Next, we pick bc We add bc and check for cycle Since we have cycle we remove the edge with maximum weight which is bc to break the cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c 12

6 9 13

b 16 15

a 5 d 7 f 3 c 12

6 9 13

b 16

d 7 f 3

15
e 8 g

e
8 g

California State University San Marcos (CSUSM)

Page 26

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

Next, we pick be We add be and check for cycle No cycle is detected

6 9 13

b 16

5
c 12

d 7
f 3

15 e 8 g

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 27

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg a

6 9 13

b 16

Next, we pick dc We add dc and check for cycle No cycle is detected

5
c 12

d 7
f 3

15 e 8 g

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 28

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

a 5

b 13 16 d 7 f 8 g 3 15

Next, we pick cf We add cf and check for cycle No cycle is detected

c
12

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 29

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

Next, we pick de We add de and check for cycle

a 5 c

6 9

a 5 16 d 7 c

6 9 13

b 16

13
15

Since we have cycle we remove the edge with maximum weight which is dc to break the cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

d 7

12
e

12
e

15

f 8
g 3

f 8
g 3
Page 30

California State University San Marcos (CSUSM)

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

Next, we pick df
We add df and check for cycle Since we have cycle we remove the edge with maximum weight which is de to break the cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 5 c

6 9

a 5 16 d 7 c

6 9 13

b 16

13
15

d 7

12
e

12
e

15

f 8
g 3

f 8
g 3
Page 31

California State University San Marcos (CSUSM)

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

a 5

6 9
13

b 16

Next, we pick eg We add eg and check for cycle No cycle is detected

c
12 e 8

d 7 f 3

15

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 32

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

ab, ac, bc, be, cd, cf, de, df, eg, fg

Next, we pick fg We add fg and check for cycle

a 5 c 12

6 9 13

b 16

a 5 d 7 f 3 c 12

6 9 13

b 16

d 7 f 3

Since we have cycle we remove the edge with maximum weight which is be to break the cycle
A.R. Hadaegh Dr. Ahmad R. Hadaegh

15
e 8 g

15
e 8 g

California State University San Marcos (CSUSM)

Page 33

a 5 c 12

6 9 13

b 16 15

d 7 f 3

e 8

There is no more edge to add. All vertices are connected to each other We are done

a 5 c 12

d e 8 g 3 7 f

What is the running time?


A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM)

Page 34

You might also like