ma/cs 6a2014-15/1term/ma006a/class10.pdfย ยท 2014-12-09ย ยท 10/20/2014 3 back to desiging a network...

16
10/20/2014 1 Ma/CS 6a Class 10: Spanning Trees By Adam Sheffer Problem: Designing a Network Problem. We wish to rebuild Caltechโ€™s communication network. โ—ฆ We have a list off all the routers, and the cost of connecting every pair of routers (some connections might be impossible). โ—ฆ We wish to obtain a connected network, while minimizing the total cost. 2

Upload: others

Post on 10-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

10/20/2014

1

Ma/CS 6a Class 10: Spanning Trees

By Adam Sheffer

Problem: Designing a Network

Problem. We wish to rebuild Caltechโ€™s communication network.

โ—ฆ We have a list off all the routers, and the cost of connecting every pair of routers (some connections might be impossible).

โ—ฆ We wish to obtain a connected network, while minimizing the total cost.

2

10/20/2014

2

A Solution?

We wish to build a graph ๐บ = (๐‘‰, ๐ธ). What are the vertices of ๐‘‰?

โ—ฆ A vertex for every router.

What are the edges of ๐ธ?

โ—ฆ An edge between every two routers that can be connected.

Is the graph directed?

โ—ฆ No.

Where are the connection prices presented in the graph?

Weighted Graphs

Given a graph ๐บ = (๐‘‰, ๐ธ), we can define a weight function over the edges

๐‘ค: ๐ธ โ†’ โ„.

โ—ฆ That is, for every edge ๐‘’ โˆˆ ๐ธ, the weight of ๐‘’ is denote as ๐‘ค(๐‘’).

1.3 2

0

-1 -3

5 2

10/20/2014

3

Back to Desiging a Network

We have an undirected graph ๐บ = ๐‘‰, ๐ธ whose vertices represent the routers and edges represent possible connections.

How are the costs related to the graph?

โ—ฆ The weight of every edge should be the corresponding cost.

5

5

9 1

1

2

Example: Network Design

Whatโ€™s the cheapest solution in this case?

5

5

9 1

1

2

5

1

1

Network cost:9 2

10/20/2014

4

Spanning Trees

A spanning tree is a tree that contains all of the vertices of the graph.

A graph can contain many distinct spanning trees.

The Weight of a Spanning Tree

The weight ๐‘ค(๐‘‡) of a spanning tree ๐‘‡ is the sum of its edge weights.

1

1 1

2

2 2

2

2

5

8

1

1 1

2

2 2

2

2

5

8

1

1 1

2

2 2

2

2

5

8

๐‘ค ๐‘‡ = 13 ๐‘ค ๐‘‡ = 19 ๐‘ค ๐‘‡ = 16

10/20/2014

5

Minimum Spanning Tree

Given a connected undirected graph ๐บ = ๐‘‰, ๐ธ and a weight function ๐‘ค: ๐ธ โ†’ โ„, a minimum spanning tree (or, MST) is a spanning tree of ๐บ of a minimum weight.

1

1 1

2

2 2

2

2

5

8

1

1 1

2

2 2

2

2

5

8

1

1 1

2

2 2

2

2

5

8

Routers and MSTs

In the network design problem we are looking for an MST of the graph.

5

5

9 1

1

2

10/20/2014

6

MSTs Are Also Useful Forโ€ฆ

Genetic research

Galaxies research

Cacti species in the desert

The Size of a Spanning Tree

Given a graph ๐บ = ๐‘‰, ๐ธ , how many edges are in each of its spanning trees? โ—ฆ Exactly ๐‘‰ โˆ’ 1.

10/20/2014

7

Graphs with ๐‘‰ โˆ’ 1 Edges

Claim. Consider a graph ๐บ = ๐‘‰, ๐ธ containing no cycles and with ๐ธ = ๐‘‰ โˆ’ 1. Then ๐บ is a spanning tree.

Proof. By induction on |๐‘‰|.

โ—ฆ Induction basis. Obvious when ๐‘‰ = 1.

โ—ฆ Induction step. Since there are no cycles in ๐บ, there must be a vertex ๐‘ฃ โˆˆ ๐‘‰ of degree 1.

โ—ฆ Remove ๐‘ฃ and the edge ๐‘’ adjacent to it. By the induction hypothesis, the resulting graph is a spanning tree. After reconnecting ๐‘ฃ and ๐‘’, we still have a spanning tree.

Spanning Trees: Unique Paths

Claim. In any spanning tree ๐‘‡ there is exactly one path between any two vertices.

โ—ฆ Assume, for contradiction, that there are two paths ๐‘ƒ, ๐‘„ in ๐‘‡ between vertices ๐‘  and ๐‘ก.

โ—ฆ ๐‘ข โ€“ the last common vertex before the paths ๐‘ƒ, ๐‘„ split (when traveling from ๐‘  to ๐‘ก).

โ—ฆ ๐‘ฃ โ€“ the first vertex common to both paths after ๐‘ข.

โ—ฆ The portions of ๐‘ƒ and ๐‘„ between ๐‘ข and ๐‘ฃ form a cycle. Contradiction!

๐‘  ๐‘ข ๐‘ฃ ๐‘ก

10/20/2014

8

Spanning Trees: Removing an Edge

Claim. Removing any edge of a spanning tree splits it into a forest of two trees.

Proof. ๐‘’ = (๐‘ข, ๐‘ฃ) โ€“ the removed edge.

โ—ฆ The edge ๐‘’ was the unique path between ๐‘ข and ๐‘ฃ. After ๐‘’โ€™s removal there is no path between ๐‘ข and ๐‘ฃ. Thus, at least two trees.

โ—ฆ Every vertex whose unique path to ๐‘ข uses ๐‘ข, ๐‘ฃ remains connected to ๐‘ฃ. Every other vertex remains connected to ๐‘ข. Thus, exactly two trees.

Spanning Trees: Adding an Edge

Claim. Adding an edge to a spanning tree yields exactly one cycle.

Proof. Homework exercise.

10/20/2014

9

How Many MSTs Are There?

What is the maximum number of MSTs that a graph can contain?

1

1 1

2

2 2

2

2

5

8

1

1 1

2

2 2

2

2

5

8

1

1 1

2

2 2

2

2

5

8

A Lot of MSTs

1 1 1 1

1

1

1

1

1 1 1 1

1

1

1

1

1

๐‘‰ MSTs ๐‘‰ ๐‘‰ โˆ’2 MSTs (try to prove: at least ๐‘‰ โˆ’ 1 !)

10/20/2014

10

(Not so) Comic Relief

What is the origin of the word algorithm?

It is named after a person! Who?

Al Gore? Al-Khwฤrizmฤซ!

? 19

(Not so) Comic Relief

What other major mathematical concept has a similar origin?

Al Khwarizmiโ€™s book is called

Al-Kitฤb al-mukhtaแนฃar fฤซ hฤซsฤb al-ฤŸabr waโ€™l-muqฤbala

20

10/20/2014

11

Greedy Algorithms

A greedy algorithm makes the locally optimal choice at each stage, without having a long-term strategy.

โ—ฆ Might not yield the optimal result.

โ—ฆ Example. The coloring algorithm for graphs of maximum degree ๐‘˜.

Primโ€™s MST Algorithm

Given a connected graph ๐บ = ๐‘‰, ๐ธ , we find an MST by using a greedy approach. We gradually grow a tree ๐‘‡ until it becomes an MST.

Choose an arbitrary vertex ๐‘Ÿ โˆˆ ๐‘‰ to be the root and set ๐‘‡ โ† ๐‘Ÿ.

As long as ๐‘‡ is not a spanning tree:

โ—ฆ Find the lightest edge ๐‘’ that connects a vertex of ๐‘‡ to a vertex ๐‘ฃ not in ๐‘‡. Add ๐‘ฃ and ๐‘’ to ๐‘‡.

10/20/2014

12

Example: Primโ€™s Algorithm

23

4

2

2

5

3

1 4

2

2

5

3

1 4

2

2

5

3

1 4

2

2

5

3

1

Data Structure: Priority Queue

A priority queue stores โ€œobjectsโ€ (in our case โ€“ vertices). Each object has a priority.

Supports the operations:

โ—ฆ Enqueue โ€“ insert an object to the the queue.

โ—ฆ Dequeue โ€“ remove the object with the highest priority from the queue.

10/20/2014

13

Primโ€™s Algorithm in More Detail

25

๐‘„- priority queue according to the values of ๐‘˜๐‘’๐‘ฆ[๐‘ฃ].

As in BFS, ๐œ‹[๐‘ฃ] is the parent of ๐‘ฃ in the tree.

๐‘˜๐‘’๐‘ฆ ๐‘ฃ is the best known weight for connecting ๐‘ฃ to the tree.

Another Prim Run

r

b a

c e

d

3 2

2

4 3 5

8 5

r

b a

c e

d

3 2

2

4 3 5

8 5

r

b a

c e

d

3 2

2

4 3 5

8 5

b a

c e

3 2

2

4 3 5

8 5

r r

b a

c e

3 2

2

4 3 5

8 5 d

b a

c e

3 2

2

4 3 5

8 5 d d

r

๐‘„

10/20/2014

14

Correctness of Prim

Let ๐‘‡ be a spanning tree that Prim returns. Assume, for contradiction, that there exists an MST ๐‘€ such that ๐‘ค ๐‘‡ > ๐‘ค ๐‘€ .

Let ๐‘’1, โ€ฆ , ๐‘’๐‘› be the edges of ๐‘‡ in their insertion order.

Let ๐‘’๐‘˜ be the first edge of ๐‘‡ that is not in ๐‘€.

๐‘‡๐‘˜ โ€“ the tree consisting of ๐‘’1, ๐‘’2, โ€ฆ , ๐‘’๐‘˜โˆ’1.

We claim that there exists an edge ๐‘’โ€ฒ โˆˆ ๐‘€ such that ๐‘ค ๐‘’๐‘˜ โ‰ค ๐‘ค ๐‘’โ€ฒ and that ๐‘’โ€ฒ connects a vertex of ๐‘‡๐‘˜ with a vertex not in ๐‘‡๐‘˜.

๐‘’๐‘˜

๐‘’โ€ฒ ๐‘‡๐‘˜

Proving the Claim

๐‘‡๐‘˜ โ€“ a tree consisting of ๐‘’1, ๐‘’2, โ€ฆ , ๐‘’๐‘˜โˆ’1.

Claim. There exists an edge ๐‘’โ€ฒ โˆˆ ๐‘€ such that ๐‘ค ๐‘’๐‘˜ โ‰ค ๐‘ค ๐‘’โ€ฒ and ๐‘’โ€ฒ connects a vertex of ๐‘‡๐‘˜ with a vertex not in ๐‘‡๐‘˜.

โ—ฆ Write ๐‘’๐‘˜ = ๐‘ข, ๐‘ฃ . Let ๐‘ƒ be the path in ๐‘€ between ๐‘ข and ๐‘ฃ.

โ—ฆ Let ๐‘’โ€ฒ be an edge of ๐‘ƒ that connects a vertex of ๐‘‡๐‘˜ with a vertex not in ๐‘‡๐‘˜.

โ—ฆ We have ๐‘ค ๐‘’๐‘˜ โ‰ค ๐‘ค ๐‘’โ€ฒ , since otherwise Prim would have chosen ๐‘’โ€ฒ before ๐‘’๐‘˜.

๐‘’๐‘˜

๐‘’โ€ฒ ๐‘‡๐‘˜

10/20/2014

15

Correctness of Prim (cont.)

Let ๐‘‡ be a tree that Prim returns.

Assume, for contradiction, that there exists an MST ๐‘€ such that ๐‘ค ๐‘‡ > ๐‘ค ๐‘€ .

Let ๐‘’๐‘˜ be the first edge of ๐‘‡ that is not in ๐‘€.

There exists ๐‘’โ€ฒ โˆˆ ๐‘€ that connects a vertex of ๐‘‡๐‘˜ with a vertex not in ๐‘‡๐‘˜ and ๐‘ค ๐‘’๐‘˜ โ‰ค ๐‘ค ๐‘’โ€ฒ

Adding ๐‘’๐‘˜ to ๐‘€ creates a cycle that also contains ๐‘’โ€ฒ. Removing ๐‘’โ€ฒ results in a spanning tree ๐‘€โ€ฒ of a smaller or equal weight.

Thus, ๐‘€โ€ฒ is another MST. ๐‘’๐‘˜

๐‘’โ€ฒ ๐‘‡๐‘˜

Correctness of Prim (end)

Let ๐‘‡ be the tree that Prim returns. Let ๐‘’1, โ€ฆ , ๐‘’๐‘› be the edges of ๐‘‡ in their insertion order.

We started with an MST that contains the edges ๐‘’1, โ€ฆ , ๐‘’๐‘˜โˆ’1 but not ๐‘’๐‘˜ and found an MST that contains the edges ๐‘’1, โ€ฆ , ๐‘’๐‘˜โˆ’1, ๐‘’๐‘˜.

Repeating this process, we eventually obtain an MST that contains ๐‘’1, โ€ฆ , ๐‘’ ๐‘‰ โˆ’1.

That is, ๐‘‡ is an MST!

10/20/2014

16

The End: A common Syndrome of Spanning Trees

Can anyone make me a variant of this with spanning trees?