minimum spanning trees - university of texas at …sxb027100/cs6363/mst.pdfminimum spanning trees...

24
Minimum Spanning Trees Sergey Bereg 2018

Upload: others

Post on 10-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Minimum Spanning Trees

Sergey Bereg

2018

Page 2: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Minimum Spanning Trees

MST problem. Given a connected undirected graph G = (V,E) and aweight w(u, v) for every edge (u, v) ∈ E, find a minimum spanning treeT ⊆ E that connects all the vertices and whose total weight

w(T ) =∑

(u,v)∈T

w(u, v)

is minimized.

2018 1

Page 3: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Example

a

b

h

i

c

g f

e

d4

8

117

1 2

10

9

144

6

2

8 7

graph G = (V,E)

a

b

h

i

c

g f

e

d4

1 2

9

4

2

8 7

a

b

h

i

c

g f

e

d

8

117

1 2

9

144

spanning tree (not minimum)w(T ) = 8+ 11+ 7+ 1+ 2+ 4+ 14+ 9 = 56

minimum spanning tree Tw(T ) = 4+8+2+4+2+1+7+9 = 37

2018 2

Page 4: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Generic Algorithm

Grow a set of edges A by adding one edge at a time so that eventually Abecomes a minimum spanning tree.

Maintain the invariant: A is a subset of some minimum spanning tree.

An edge (u, v) is safe if the invariant holds when (u, v) is added to A.

Generic-MST(G,w)1 A = ∅2 while A does not form a spanning tree3 do find an edge (u, v) that is safe for A4 A = A ∪ {(u, v)}5 return A

2018 3

Page 5: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Cut

A cut (S, V − S) of G is a partition of V into S and V − S.An edge (u, v) crosses the cut (S, V −S) if one of its endpoints is in V andthe other is in V − S.A cut respects a set A if no edge of A crosses the cut.A cross edge of minimum weight is a light edge.

Theorem. If A is a subset of some MST of G and a cut (S, V − S)respects A, then any light edge crossing (S, V − S) is safe for A.

2018 4

Page 6: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Proof

(x, y) /∈ A

w(u, v) ≤ w(x, y)

S V − S

7

8

9

7

u v

x

y

S V − S

7

8

9

7

u v

x

y

10 10

2018 5

Page 7: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Example

a

b

h

i

c

g f

e

d

4

8

11

7

1 2

10

9

144

6

2

8 7

The edges of A are bold. The cut is ({a, b, d, e}, {h, c, i, g, f}).

What edges across this cut are safe?

2018 6

Page 8: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Example

a

b

h

i

c

g f

e

d

4

8

11

7

1 2

10

9

144

6

2

8 7

The edges of A are bold. The cut is ({a, b, d, e}, {h, c, i, g, f}).

The edge (c, d) is safe.

2018 7

Page 9: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 8

Page 10: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 9

Page 11: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 10

Page 12: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 11

Page 13: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 12

Page 14: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 13

Page 15: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 14

Page 16: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 15

Page 17: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Prim’s algorithm

PrimMST (G,w, s)// Input: Graph G = (V,E), weight function w and a start vertex s ∈ V .// Output: MST with edges (v, v.pred), v ∈ V − {s}.1 make an empty Q and an empty T2 insert all v ∈ V − {s} into Q with v.key =∞3 for each neighbor v of s4 DecreaseKey (v, w(v, s))5 while Q 6= ∅ // main loop6 v =RemoveMin ()7 add (v, v.pred) to T8 for each neighbor u of v9 if u ∈ Q and w(u, v) < u.key10 u.pred = v11 DecreaseKey (u,w(u, v))

2018 16

Page 18: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Correctness

Prim’s algorithm is correct since prior to each iteration

1. A = {(v, v.pred) : v ∈ V − {s} −Q}.2. The current tree connects vertices in V −Q.

3. For v ∈ Q with v.pred 6=nil, v.key is the minimum weight of an edgeconnecting v and a vertex in V −Q.

2018 17

Page 19: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Running Time

min-heap

Fibonacci heap

demo

2018 18

Page 20: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Kruskal’s Algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 19

Page 21: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Kruskal’s Algorithm

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

a

b

h

i

c

g f

e

d4

8

11

7

1 2

10

9

144

6

2

8 7

2018 20

Page 22: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Kruskal’s Algorithm

MST-Kruskal (G,w)1 sort the edges of E into non-decreasing order by weight w2 F = ∅3 for each vertex v ∈ V4 makeSet (v) // make a set for every vertex5 for each edge (u, v) ∈ E (use the sorted order)6 if findSet (u) 6= findSet (v) // u and v are in different sets7 union (u, v) and add (u, v) to F8 return F

demo

Running time

2018 21

Page 23: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Bor̊uvka’s Algorithm

MST-Bor̊uvka (G,w)1 A = ∅2 while |A| < n− 13 for each component C4 find the min-weight edge (u, v) with u ∈ C, v /∈ C5 add (u, v) to A (unless it is already there)6 find new components7 return A

2018 22

Page 24: Minimum Spanning Trees - University of Texas at …sxb027100/cs6363/mst.pdfMinimum Spanning Trees MST problem. Given a connected undirected graph G = (V;E) and a weight w(u;v) for

Bor̊uvka’s Algorithm

a

b

h

i

c

g f

e

d1

8

9

8

3 4

11

5

95

7

2

8 3

a

b

h

i

c

g f

e

d1

88

3

11

5

9

6

2

8

a

b

h

i

g f

e

d1

88

3

11

5

9

7

2

8 c

4

5

3

4

5

3

9

9

2018 23