+ All Categories
Home > Documents > 11 Graphs MST

11 Graphs MST

Date post: 29-May-2017
Category:
Upload: frankjamison
View: 219 times
Download: 2 times
Share this document with a friend
34
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Minimum Spanning Tree
Transcript

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

1

Minimum Spanning

Tree

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

2

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

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

3

• 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

• Boruvka’s algorithm• Kruskal’s Algorithm• Jannik Prime’s Algorithm• Dijkstra’s Algorithm

b

c

a

e

d

g

f

b

c

a

e

d

g

f

b

c

a

e

d

g

f

Solution 1 Solution 2

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

4

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

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

5

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• 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

b

c

a

e

d

g

f

5

6

8

7

3

First Iteration: (step 1) Original graph

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

6

• 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?

b

c

a

e

d

g

f

5

6

8

7

3

Second Iteration: (step 2) t1

t2

12

b

c

a

e

d

g

f

5

6

8

7

3

12

1316

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

7

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

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

8

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• We find the first edge with minimum weight

• This edge is gf with weight 3

• So we add gf

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

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

9

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• We find the next edge with minimum weight

• This edge is ac with weight 5

• We add ac because it does not cause a cycle

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

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

10

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• 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

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

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

11

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• We find the next edge with minimum weight

• This edge is fd with weight 7

• We add fd because it does not cause a cycle

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

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

12

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• We find the next edge with minimum weight

• This edge is eg with weight 8

• We add eg because it does not cause a cycle

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

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

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• 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

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

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

14

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5

6

8

12 7

3

• 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?

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

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

15

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

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

16

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• We find the first edge with

minimum weight

• This edge is fg with weight 3

• So we add fg first

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

17

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

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

18

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

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

19

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

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

20

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

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

21

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

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

22

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5

6

8

12 7

3

• 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?

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

23

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

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

24

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Assuming that the edges are ordered alphabetically and not by weight

• We pick the first edge which is ab

• We add ab first

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

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

25

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13• Next, we pick ac

• We add ac and check for cycle

• No cycle is detected

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

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

26

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

27

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Next, we pick be

• We add be and check for cycle

• No cycle is detected

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

28

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Next, we pick dc

• We add dc and check for cycle

• No cycle is detected

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

29

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Next, we pick cf

• We add cf and check for cycle

• No cycle is detected

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

30

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Next, we pick de

• We add de and check for cycle

• Since we have cycle we remove the edge with maximum weight which is dc to break the cycle

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

31

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• 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

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

32

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Next, we pick eg

• We add eg and check for cycle

• No cycle is detected

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

33

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• Next, we pick fg

• We add fg and check for cycle

• Since we have cycle we remove the edge with maximum weight which is be to break the cycle

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

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

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

34

b

c

a

e

d

g

f

5 916

6

15

8

12 7

3

13

• There is no more edge to add.

• All vertices are connected to each other

• We are done

• What is the running time?

b

c

a

e

d

g

f

5

6

8

12 7

3


Recommended