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!