Transcript
Page 1: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

CSE 2331 / 5331

CSE 2331/5331

Topic 10:Minimum Spanning

Tree

Page 2: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 3: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 4: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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.

Page 5: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

Examples

CSE 2331 / 5331

Is minimum spanning tree unique?

Page 6: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 7: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 8: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 9: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 10: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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.

Page 11: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

Prim’s MST Algorithm: High-level

𝑈𝑈 : unconnected vertices 𝑆𝑆 = 𝑉𝑉 – 𝑈𝑈: vertices connected by current partial tree

CSE 2331 / 5331

Page 12: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

Example

Greedy algorithm Break ties arbitrarily

CSE 2331 / 5331

Page 13: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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.

Page 14: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 15: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 16: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

First Improvement of Implementation

Storing costs at vertices Each unvisted vertex in 𝑈𝑈 maintain the smallest weight-

edge to visited vertices

CSE 2331 / 5331

Page 17: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

First Improvement: Cost at Vertices

CSE 2331 / 5331

Page 18: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 19: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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!

Page 20: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

Second Improvement: Priority Queue

CSE 2331 / 5331

Page 21: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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

Page 22: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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𝑉𝑉

Page 23: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec10.pdf · Incrementally grow a sub -tree 𝑇𝑇𝑆𝑆⊆𝐸𝐸connecting

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


Top Related