minimum spanning trees › ~goodrich › teach › graph › notes › mst.pdf · minimum spanning...

11
Minimum Spanning Tree 4/12/17 15:19 1 © 2015 Goodrich and Tamassia Minimum Spanning Trees 1 Minimum Spanning Trees Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 © 2015 Goodrich and Tamassia Application: Connecting a Network q Suppose the remote mountain country of Vectoria has been given a major grant to install a large Wi-Fi the center of each of its mountain villages. q Communication cables can run from the main Internet access point to a village tower and cables can also run between pairs of towers. q The challenge is to interconnect all the towers and the Internet access point as cheaply as possible. Minimum Spanning Trees 2

Upload: others

Post on 07-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

1

© 2015 Goodrich and Tamassia Minimum Spanning Trees 1

Minimum Spanning Trees

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

© 2015 Goodrich and Tamassia

Application: Connecting a Network q  Suppose the remote mountain country of Vectoria has been given

a major grant to install a large Wi-Fi the center of each of its mountain villages.

q  Communication cables can run from the main Internet access point to a village tower and cables can also run between pairs of towers.

q  The challenge is to interconnect all the towers and the Internet access point as cheaply as possible.

Minimum Spanning Trees 2

Page 2: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

2

© 2015 Goodrich and Tamassia

Application: Connecting a Network q  We can model this problem using a graph, G, where

each vertex in G is the location of a Wi-Fi the Internet access point, and an edge in G is a possible cable we could run between two such vertices.

q  Each edge in G could then be given a weight that is equal to the cost of running the cable that that edge represents.

q  Thus, we are interested in finding a connected acyclic subgraph of G that includes all the vertices of G and has minimum total cost.

q  Using the language of graph theory, we are interested in finding a minimum spanning tree (MST) of G.

Minimum Spanning Trees 3

© 2015 Goodrich and Tamassia Minimum Spanning Trees 4

Minimum Spanning Trees Spanning subgraph

n  Subgraph of a graph G containing all the vertices of G

Spanning tree n  Spanning subgraph that is

itself a (free) tree Minimum spanning tree (MST)

n  Spanning tree of a weighted graph with minimum total edge weight

q  Applications n  Communications networks n  Transportation networks

ORD

PIT

ATL

STL

DEN

DFW

DCA

10 1

9

8

6

3

2 5

7

4

Page 3: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

3

© 2015 Goodrich and Tamassia Minimum Spanning Trees 5

Cycle Property Cycle Property:

n  Let T be a minimum spanning tree of a weighted graph G

n  Let e be an edge of G that is not in T and C let be the cycle formed by e with T

n  For every edge f of C, weight(f) ≤ weight(e)

Proof: n  By contradiction n  If weight(f) > weight(e) we

can get a spanning tree of smaller weight by replacing e with f

8 4

2 3 6

7

7

9

8 e

C

f

8 4

2 3 6

7

7

9

8

C

e

f

Replacing f with e yields a better spanning tree

© 2015 Goodrich and Tamassia Minimum Spanning Trees 6

U V

Partition Property Partition Property:

n  Consider a partition of the vertices of G into subsets U and V

n  Let e be an edge of minimum weight across the partition

n  There is a minimum spanning tree of G containing edge e

Proof: n  Let T be an MST of G n  If T does not contain e, consider the

cycle C formed by e with T and let f be an edge of C across the partition

n  By the cycle property, weight(f) ≤ weight(e)

n  Thus, weight(f) = weight(e) n  We obtain another MST by replacing

f with e

7 4

2 8 5

7

3

9

8 e

f

7 4

2 8 5

7

3

9

8 e

f

Replacing f with e yields another MST

U V

Page 4: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

4

© 2015 Goodrich and Tamassia Minimum Spanning Trees 7

Prim-Jarnik’s Algorithm q  Similar to Dijkstra’s algorithm q  We pick an arbitrary vertex s and we grow the MST as

a cloud of vertices, starting from s q  We store with each vertex v label d(v) representing

the smallest weight of an edge connecting v to a vertex in the cloud

q  At each step: n  We add to the cloud the vertex u outside the cloud with the

smallest distance label n  We update the labels of the vertices adjacent to u

© 2015 Goodrich and Tamassia Minimum Spanning Trees 8

Prim-Jarnik Pseudo-code

Page 5: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

5

© 2015 Goodrich and Tamassia Minimum Spanning Trees 9

Example

B D

C

A

F

E

7 4

2 8

5

7

3

9

8

0 7

2

8 ∞

B D

C

A

F

E

7 4

2 8

5

7

3

9

8

0 7

2

5 ∞

7

B D

C

A

F

E

7 4

2 8

5

7

3

9

8

0 7

2

5 ∞

7

B D

C

A

F

E

7 4

2 8

5

7

3

9

8

0 7

2

5 4

7

© 2015 Goodrich and Tamassia Minimum Spanning Trees 10

Example (contd.)

B D

C

A

F

E

7 4

2 8

5

7

3

9

8

0 3

2

5 4

7

B D

C

A

F

E

7 4

2 8

5

7

3

9

8

0 3

2

5 4

7

Page 6: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

6

© 2015 Goodrich and Tamassia Minimum Spanning Trees 11

Analysis q  Graph operations

n  We cycle through the incident edges once for each vertex q  Label operations

n  We set/get the distance, parent and locator labels of vertex z O(deg(z)) times

n  Setting/getting a label takes O(1) time q  Priority queue operations

n  Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time

n  The key of a vertex w in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time

q  Prim-Jarnik’s algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure

n  Recall that Σv deg(v) = 2m q  The running time is O(m log n) since the graph is connected

© 2015 Goodrich and Tamassia

Kruskal’s Approach q  Maintain a partition of the vertices into clusters

n  Initially, single-vertex clusters n  Keep an MST for each cluster n  Merge “closest” clusters and their MSTs

q  A priority queue stores the edges outside clusters (or you could even sort the edges) n  Key: weight n  Element: edge

q  At the end of the algorithm n  One cluster and one MST

Minimum Spanning Trees 12

Page 7: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

7

© 2015 Goodrich and Tamassia

Kruskal’s Algorithm

Minimum Spanning Trees 13

© 2015 Goodrich and Tamassia Campus Tour 14

Example of Kruskal’s Algorithm

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

12

12 12

12

D H

D H

D H

D H

Page 8: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

8

© 2015 Goodrich and Tamassia Campus Tour 15

Example (contd.)

five steps

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

B G

C

A

F

4

1 3 5

10

2

8

7

6 E

11

9

12

12 12

12 D H

D H

D H

D H

© 2015 Goodrich and Tamassia

Data Structure for Kruskal’s Algorithm q  The algorithm maintains a forest of trees q  A priority queue extracts the edges by increasing

weight q  An edge is accepted it if connects distinct trees q  We need a data structure that maintains a

partition, i.e., a collection of disjoint sets, with operations: n  makeSet(u): create a set consisting of u n  find(u): return the set storing u n  union(A, B): replace sets A and B with their union

Minimum Spanning Trees 16

Page 9: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

9

© 2015 Goodrich and Tamassia Minimum Spanning Trees 17

List-based Partition q  Each set is stored in a sequence q  Each element has a reference back to the set

n  operation find(u) takes O(1) time, and returns the set of which u is a member.

n  in operation union(A,B), we move the elements of the smaller set to the sequence of the larger set and update their references

n  the time for operation union(A,B) is min(|A|, |B|)

q  Whenever an element is processed, it goes into a set of size at least double, hence each element is processed at most log n times

© 2015 Goodrich and Tamassia

Partition-Based Implementation q  Partition-based version of Kruskal’s

Algorithm n  Cluster merges as unions n  Cluster locations as finds

q  Running time O((n + m) log n) n  Priority Queue operations: O(m log n) n  Union-Find operations: O(n log n)

Minimum Spanning Trees 18

Page 10: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

10

© 2015 Goodrich and Tamassia

Alternative Implementation

Minimum Spanning Trees 19

© 2015 Goodrich and Tamassia Minimum Spanning Trees 20

Baruvka’s Algorithm q  Like Kruskal’s Algorithm, Baruvka’s algorithm grows many

clusters at once and maintains a forest T q  Each iteration of the while loop halves the number of

connected components in forest T q  The running time is O(m log n)

Algorithm BaruvkaMST(G) T ← V {just the vertices of G} while T has fewer than n - 1 edges do for each connected component C in T do Let edge e be the smallest-weight edge from C to another component in T if e is not already in T then Add edge e to T return T

Page 11: Minimum Spanning Trees › ~goodrich › teach › graph › notes › MST.pdf · Minimum Spanning Tree 4/12/17 15:19 8 © 2015 Goodrich and Tamassia Campus Tour 15 Example (contd.)

Minimum Spanning Tree 4/12/17 15:19

11

© 2015 Goodrich and Tamassia

Example of Baruvka’s Algorithm (animated)

Minimum Spanning Trees 21

1

5 4

3

2

3

4

4 9

6

8 7

6

5 4

9

6

8

Slide by Matt Stallmann included with permission.

1

5 4

3

2

3

4

4 9

6

8 7

6

5