pptx

CSE 2331/5331
Topic 11:
Minimum Spanning
Tree
CSE 2331 / 5331
๏ฎ
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.
1
3
a
1
d
2
5
b
c
2 1 1
e
4
f
CSE 2331 / 5331
A Tree
๏ฎ
An undirected graph G = (V, E) is a tree if:
๏ฎ
๏ฎ
Equivalently, an undirected graph G = (V, E) is a
tree if:
๏ฎ
๏ฎ
G is connected, and there is no cycle
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.
If G is not connected, then no spanning
tree exists.
CSE 2331 / 5331
Examples
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
CSE 2331 / 5331
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:
๏ฎ
๏ฎ
Output:
๏ฎ
๏ฎ
๏ฎ
An undirected graph G = (V, E) with weight ๐‘ค: ๐ธ โ†’ ๐‘…
A minimum spanning tree MST(G) of G.
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
CSE 2331 / 5331
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
๏ฎ
Input:
๏ฎ
๏ฎ
Output:
๏ฎ
๏ฎ
An undirected graph G = (V, E) with weight ๐‘ค: ๐ธ โ†’ ๐‘…
A minimum spanning tree MST(G) of G.
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 ๐‘†โ€ฒ = ๐‘† โˆช
๐‘ข
โ€ฒ is still a sub-tree of some MST of G
๏ฎ Such that ๐‘‡ ๐‘†
CSE 2331 / 5331
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
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.
๏ฎ
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 subtree
of some minimum spanning tree of input graph G.
๏ฎ Termination:
when all nodes are connected, we obtain a MST of G.
๏ฎ
CSE 2331 / 5331
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 weightedge 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 +
๏ฎ
= ๐‘‚ ๐‘‰2 + ๐ธ
๐‘ฃ๐‘— โˆˆ๐‘‰ deg
๐‘ฃ๐‘—
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 !
Use a min-priority queue!
CSE 2331 / 5331
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?
๏ฎ
๏ฎ
# iterations of each call of the inner for-loop?
๏ฎ
๏ฎ
V
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.DecreaseKey
๏ฎ
๏ฎ
๏ฎ
Total #: 2E
Total cost: ๐‘‚(๐ธ lg ๐‘‰)
Q.Contains and Q.Key
๏ฎ
๏ฎ
Total #: 2E
Total cost: ๐‘‚(๐ธ)
Q.DeleteMin
๏ฎ
๏ฎ
Total #: V
Total cost: ๐‘‚(๐‘‰ lg ๐‘‰)
Total time complexity:
๐‘‚ ๐‘‰ + ๐ธ lg ๐‘‰ = ๐‘‚ ๐ธ lg ๐‘‰
CSE 2331 / 5331
Remarks
๏ฎ
Primโ€™s algorithm is a greedy algorithm
๏ฎ
๏ฎ
One can use even more efficient Priority-Queue
implementation, based on the Fibonacci Heap
๏ฎ
๏ฎ
The choice of the minimum-weight edge at each
iteration
๐‘‚(๐‘‰ lg ๐‘‰ + ๐ธ)
There are many possible MST algorithms
๏ฎ
Another popular one is Kruskal Algorithm, which also
runs in ๐‘‚ ๐ธ lg ๐‘‰ time
CSE 2331 / 5331