Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8...

10
Minimum Spanning Tree 3/25/14 15:52 1 © 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 1 Minimum Spanning Trees Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 © 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 2 Minimum Spanning Trees Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning tree Spanning subgraph that is itself a (free) tree Minimum spanning tree (MST) Spanning tree of a weighted graph with minimum total edge weight Applications Communications networks Transportation networks ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3 2 5 7 4

Transcript of Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8...

Page 1: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

1

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 1

Minimum Spanning Trees

Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 2

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 2: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

2

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 3

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

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 4

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 3: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

3

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 5

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

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 6

Prim-Jarnik Pseudo-code

Page 4: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

4

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 7

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

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 8

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 5: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

5

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 9

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

© 2014 Goodrich, Tamassia, Goldwasser

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 n  Key: weight n  Element: edge

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

Minimum Spanning Trees 10

Page 6: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

6

© 2014 Goodrich, Tamassia, Goldwasser

Kruskal’s Algorithm

Minimum Spanning Trees 11

© 2014 Goodrich, Tamassia, Goldwasser Campus Tour 12

Example

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

Page 7: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

7

© 2014 Goodrich, Tamassia, Goldwasser Campus Tour 13

Example (contd.)

four steps

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

B G

C

A

F

D

4

1 3 5

10

2

8

7

6 E

H 11

9

© 2014 Goodrich, Tamassia, Goldwasser

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 14

Page 8: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

8

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 15

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

© 2014 Goodrich, Tamassia, Goldwasser

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 16

Page 9: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

9

© 2014 Goodrich, Tamassia, Goldwasser

Java Implementation

Minimum Spanning Trees 17

© 2014 Goodrich, Tamassia, Goldwasser

Java Implementation, 2

Minimum Spanning Trees 18

Page 10: Minimum Spanning Trees - Michigan Technological University...Minimum Spanning Tree 3/25/14 15:52 8 © 2014 Goodrich, Tamassia , Goldwasser Minimum Spanning Trees 15 List-based Partition

Minimum Spanning Tree 3/25/14 15:52

10

© 2014 Goodrich, Tamassia, Goldwasser Minimum Spanning Trees 19

Baruvka’s Algorithm (Exercise) 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

© 2014 Goodrich, Tamassia, Goldwasser

Example of Baruvka’s Algorithm (animated)

Minimum Spanning Trees 20

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