minimum spanning tree dr. bernard chen ph.d. university of central arkansas fall 2008

33
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008

Upload: hortense-morris

Post on 27-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Minimum Spanning Tree

Dr. Bernard Chen Ph.D.University of Central Arkansas

Fall 2008

Minimum Spanning Tree

Example of MST

Problem: Laying Telephone Wire

Central office

Wiring: Naïve Approach

Central office

Expensive!

Wiring: Better Approach

Central office

Minimize the total length of wire connecting the customers

Growing an MST: general idea

GENERIC-MST(G,w)

1. A{}2. while A does not form a spanning tree3. do find an edge (u,v) that is safe

for A4. A A U {(u,v)}5. return A

Tricky part

How do you find a safe edge? This safe edge is part of the minimum

spanning tree

Algorithms for MST

Prim’s Grow a MST by adding a single edge at a

time

Kruskal’s Choose a smallest edge and add it to the

forest If an edge is formed a cycle, it is

rejected

Prim’s greedy algorithm Start from some (any) vertex.

Build up spanning tree T, one vertex at a time.

At each step, add to T the lowest-weight edge in G that does not create a cycle.

Stop when all vertices in G are touched

Prim’s MST algorithm

Example

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min EdgePick a root

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

= in heap

Min Edge = 1

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 2

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 2

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 3

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 4

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 3

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 4

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 6

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Example II

Kruskal’s Algorithm

Choose the smallest edge and add it to a forest

Keep connecting components until all vertices connected

If an edge would form a cycle, it is rejected.

Example

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 1

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 2

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 2

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 3

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Now have 2 disjoint components:

ABFG and CH

Min Edge = 3

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 4

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Two components now merged into one.

Min Edge = 4

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7

Min Edge = 5

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7Rejected due to a cycle BFGB

Min Edge = 6

A

B

D

G

C

F

IE

H

2 3

4

57

8

4

3

1

6

9

2

7