ma/cs 6a2014-15/1term/ma006a/class10.pdfย ยท 2014-12-09ย ยท 10/20/2014 3 back to desiging a network...
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!