powerpoint presentation - topology based methods in shape...
TRANSCRIPT
CSE 2331 / 5331
CSE 2331/5331
Topic 10:Minimum Spanning
Tree
Edge-weighted un-directed graph G = (V, E) and edge weight function ๐ค๐ค:๐ธ๐ธ โ ๐ ๐ E.g, road network, where each node is a city, and each
edge is a road, and weight is the length of this road.
CSE 2331 / 5331
3 5ba
f
c
d e
1 1
42
11 2
A Tree
An undirected graph G = (V, E) is a tree if: G is connected, and there is no cycle
Equivalently, an undirected graph G = (V, E) is a tree if: G is connected, and ๐ธ๐ธ = ๐๐ โ 1
One key property: Given a tree T on nodes V, if we add any edge ๐๐ to T, it
will create a cycle. That is, ๐๐ โช ๐๐ contains a cycle for any ๐๐ โ ๐๐.
CSE 2331 / 5331
A Spanning Tree
A spanning tree T of an undirected graph G = (V, E) is a tree that includes all vertices V of G ๐๐ โ ๐ธ๐ธ
Weight of a spanning tree T: ๐ค๐ค ๐๐ = โ ๐ข๐ข,๐ฃ๐ฃ โ๐๐ ๐ค๐ค ๐ข๐ข, ๐ฃ๐ฃ where ๐ค๐ค:๐ธ๐ธ โ ๐ ๐ is the weight function on edges of G
A minimum spanning tree of G is a spanning tree with smallest weight.
CSE 2331 / 5331
If G is not connected, then no spanningtree exists.
Examples
CSE 2331 / 5331
Is minimum spanning tree unique?
CSE 2331 / 5331
MST
MST for given G may not be unique
Since MST is a spanning tree: # edges : |V| - 1
If the graph is unweighted: All spanning trees have same weight
Key Property of MST
Given a MST ๐๐ of ๐บ๐บ = (๐๐,๐ธ๐ธ), let ๐๐ โ ๐ธ๐ธ be any edge in ๐ธ๐ธ but not in ๐๐. The following then holds: There is a unique cycle C containing e in ๐๐ โช ๐๐ . ๐๐ is an edge with largest weight in C.
Sketch of proof: Why unique? If ๐๐ does not have largest weight, let ๐๐โฒ โ ๐ถ๐ถ be an edge
with largest weight in C. ๐๐โฒ = ๐๐ โ ๐๐๐ + ๐๐ is a spanning tree of G ๐ค๐ค๐๐๐ค๐ค๐ค๐ค๐ค๐ค๐ค ๐๐โฒ โค ๐ค๐ค๐๐๐ค๐ค๐ค๐ค๐ค๐ค๐ค(๐๐) โ ๐๐ cannot be MST. Contradiction โ e must have largest weight in C.
CSE 2331 / 5331
Construct MST
Input: An undirected graph G = (V, E) with weight ๐ค๐ค:๐ธ๐ธ โ ๐ ๐
Output: A minimum spanning tree MST(G) of G.
CSE 2331 / 5331
A MST T is V-1 number of edges that connect all nodes, with no cycle.
Intuitively, we will choose โsafeโ edges to incrementally build T
Example
How to choose the first edge that is โsafeโ, namely, it must be in some MST? Note, it is some, not all !
CSE 2331 / 5331
Intuition
Intuitively, Incrementally grow a sub-tree ๐๐ ๐๐ โ ๐ธ๐ธ connecting a subset of
nodes ๐๐ โ ๐๐ At the beginning of each iteration, ๐๐ ๐๐ is a sub-tree of some MST
of G At each iteration, grow ๐๐ (๐๐๐) to include one more vertex ๐๐๐ = ๐๐ โช
๐ข๐ข Such that ๐๐ ๐๐โฒ is still a sub-tree of some MST of G
CSE 2331 / 5331
Input: An undirected graph G = (V, E) with weight ๐ค๐ค:๐ธ๐ธ โ ๐ ๐
Output: A minimum spanning tree MST(G) of G.
Primโs MST Algorithm: High-level
๐๐ : unconnected vertices ๐๐ = ๐๐ โ ๐๐: vertices connected by current partial tree
CSE 2331 / 5331
Example
Greedy algorithm Break ties arbitrarily
CSE 2331 / 5331
MST Theorem
CSE 2331 / 5331
Key to the correctness of PrimMST algorithm. Invariance:
each time PrimMST() algorithm grows the partial tree (i.e, adds another edge to it), the invariance is that the new tree is still a subtreeof some minimum spanning tree of input graph G. Termination:
when all nodes are connected, we obtain a MST of G.
MST Theorem:Let T be a sub-tree of a minimum spanning tree.
If e is a minimum weight edge connecting T to some vertex not in T, then ๐๐ โช ๐๐ is a subtree of a minimum spanning tree.
Proof of MST Theorem
By the theoremโs hypothesis, T is a subtree of some MST A of G.
If e is not an edge of A, then ๐ด๐ด โช ๐๐ contains a cycle. Let ๐ถ๐ถ be this cycle. There must exists some edge ๐๐โฒ โ๐ถ๐ถ from T to a vertex not in T.
Since e is a minimum weight edge from T to vertices not in T, ๐ค๐ค๐๐๐ค๐ค๐ค๐ค๐ค๐ค๐ค ๐๐ โค ๐ค๐ค๐๐๐ค๐ค๐ค๐ค๐ค๐ค๐ค ๐๐โฒ .
Replacing ๐๐โฒ โ ๐ด๐ด by e gives a new tree ๐ต๐ต = ๐ด๐ด โ ๐๐๐ +๐๐ such that ๐ค๐ค๐๐๐ค๐ค๐ค๐ค๐ค๐ค๐ค ๐ต๐ต โค ๐ค๐ค๐๐๐ค๐ค๐ค๐ค๐ค๐ค๐ค ๐ด๐ด .
๐๐ โช ๐๐ โ ๐ต๐ต. So ๐๐ โช ๐๐ is also a subtree of some MST. Done.
CSE 2331 / 5331
Naive Implementation of Primโs MST
Naรฏve implementation: linear scan all edges to identify min-weight edge ๐ฃ๐ฃ๐๐ ,๐ฃ๐ฃ๐๐ at each iteration
Total time complexity: ๐๐ ๐๐๐ธ๐ธCSE 2331 / 5331
First Improvement of Implementation
Storing costs at vertices Each unvisted vertex in ๐๐ maintain the smallest weight-
edge to visited vertices
CSE 2331 / 5331
First Improvement: Cost at Vertices
CSE 2331 / 5331
Analysis
Time to update cost at each vertex at each iteration (in the While-loop): Line 6: ๐๐ ๐๐ Lines 8โ13: ๐๐(deg ๐ฃ๐ฃ๐๐ )
Total time complexity: ๐๐ ๐๐2 + โ๐ฃ๐ฃ๐๐โ๐๐ deg ๐ฃ๐ฃ๐๐ = ๐๐ ๐๐2 + ๐ธ๐ธ
CSE 2331 / 5331
Second Improvement: Efficient Update
Consider Line 6 of previous implementation We spend linear time to identify the vertex ๐ฃ๐ฃ๐๐ with
smallest cost from ๐๐ Can we do better? What operations do we need to support?
extracting the smallest value from a set, updating the cost of some vertices
The cost can only decrease !
CSE 2331 / 5331
Use a min-priority queue!
Second Improvement: Priority Queue
CSE 2331 / 5331
Analysis:
We use min-heap to implement the priority queue The maximum size of Q is V # iterations of While-loop?
V # iterations of each call of the inner for-loop?
deg ๐ฃ๐ฃ๐๐ Total #times lines 7โ10 are executed:
โ๐ฃ๐ฃ๐๐โ๐๐ deg ๐ฃ๐ฃ๐๐
CSE 2331 / 5331
Analysis cont.
Q.insert: Total #: V Total cost: ๐๐(๐๐ lg๐๐)
Q.IsNotEmpty and Q.MinKey: Total #: V Total cost: O(V)
Q.DeleteMin Total #: V Total cost: ๐๐(๐๐ lg๐๐)
CSE 2331 / 5331
Q.DecreaseKey Total #: 2E Total cost: ๐๐(๐ธ๐ธ lg ๐๐)
Q.Contains and Q.Key Total #: 2E Total cost: ๐๐(๐ธ๐ธ)
Total time complexity:๐๐ ๐๐ + ๐ธ๐ธ lg๐๐ = ๐๐ ๐ธ๐ธ lg๐๐
Remarks
Primโs algorithm is a greedy algorithm The choice of the minimum-weight edge at each
iteration One can use even more efficient Priority-Queue
implementation, based on the Fibonacci Heap ๐๐(๐๐ lg๐๐ + ๐ธ๐ธ)
There are many possible MST algorithms Another popular one is Kruskal Algorithm, which also
runs in ๐๐ ๐ธ๐ธ lg๐๐ time
CSE 2331 / 5331