powerpoint presentation - topology based methods in shape...

23
CSE 2331 / 5331 CSE 2331/5331 Topic 10: Minimum Spanning Tree

Upload: others

Post on 20-May-2020

5 views

Category:

Documents


0 download

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