Professional Documents
Culture Documents
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
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
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 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
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5 c 12
d 7 f 3
15 e 8 g
Page
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5 c 12
d 7 f 3
15 e 8 g
Page
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
Page 10
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5 c 12
d 7 f 3
15 e 8 g
Page 11
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5 c 12
d 7 f 3
15 e 8 g
Page 12
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
Page 13
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
Page 14
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5 c 12
d 7 f 3
15 e 8 g
So we add fg first
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
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
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
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
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
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
Page 22
Page 23
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8 g
Page 25
a 5 c 12
6 9 13
b 16 15
d 7 f 3
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
Page 26
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5
c 12
d 7
f 3
15 e 8 g
Page 27
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
6 9 13
b 16
5
c 12
d 7
f 3
15 e 8 g
Page 28
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
a 5
b 13 16 d 7 f 8 g 3 15
c
12
Page 29
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
a 5
6 9
13
b 16
c
12 e 8
d 7 f 3
15
Page 32
a 5 c 12
6 9 13
b 16 15
d 7 f 3
e 8
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
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
Page 34