design and analysis of algorithms - ucf computer sciencesharma/cop3503lectures/lecture15.pdf ·...

40
1 Design and Analysis of Algorithms Instructor: Sharma Thankachan Lecture 15: Minimum Spanning Tree Slides modified from Dr. Hon, with permission

Upload: others

Post on 14-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

1

Design and Analysis of Algorithms

Instructor: Sharma Thankachan

Lecture 15: Minimum Spanning Tree

Slides modified from Dr. Hon, with permission

Page 2: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

2

About this lecture • What is a Minimum Spanning Tree?• Some History

• The Greedy Choice Lemma• Kruskal’s Algorithm• Prim’s Algorithm• Borůvka's Algorithm

Page 3: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

3

• Let G = (V,E) be an undirected, connected graph

• A spanning tree of G is a tree, using only edges in E, that connects all vertices of G

Minimum Spanning Tree

Page 4: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

4

• Sometimes, the edges in G have weights• weight ó cost of using the edge

• A minimum spanning tree (MST) of a weighted G is a spanning tree such that the sum of edge weights is minimized

Minimum Spanning Tree

4

8

11

8 79

10

144

21

2

67

Total cost = 4 + 8 + 7 + 9 + 2 + 4 + 1 + 2 = 37

Page 5: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

5

• MST of a graph may not be unique

Minimum Spanning Tree

4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

Page 6: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

6

Some History• Borůvka [1926] : First algorithm

• for electrical coverage of Moravia

• Kruskal [1956] : Kruskal’s algorithm• Jarník [1930], Prim [1957] : Prim’s algorithm• Fredman-Tarjan [1987] : O(E log*(V)) time• Gabow et al [1986]: O(E log log*(V) time• Chazelle [1999]: O(E a(E,V)) time

Remark: log* = iterated log, a(m,n) = inverse Ackermann

Page 7: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

7

Greedy Choice Lemma• Suppose all edge weights are distinct

• If not, we give an arbitrary orderingamong equal-weight edges

• E.g., 4

8

11

8 79

10

144

21

2

67

Give an arbitrary ordering among these two edges, so that one costs “fewer” than the other

Page 8: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

8

Greedy Choice Lemma• Let ev to be the cheapest edge adjacent

to v, for each vertex v

4

8

11

8 79

10

144

21

2

67a

c

b

ea

eb , ec

Theorem: The minimum spanning tree of Gcontains every ev

Page 9: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

9

Proof• Recall that all edge weights are distinct• Suppose on the contrary that MST of G

does not contain some edge ev = (u,v)• Let T = optimal MST of G• By adding ev = (u,v) to T, we obtain a cycle

u, v, w, …, u [why??]

u

vw

Page 10: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

10

Proof• By our choice of ev ,

we must have weight of (u,v) cheaper than weight of (v,w) to T

u

vw

• If we delete (v,w) and include ev, we obtain a spanning tree cheaper than Tè contradiction !!

u

vw

Page 11: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

11

Optimal SubstructureLet E’ = a set of edges which are known to

be in an MST of G = (V,E)Let G* = the graph obtained by contracting

each component of G’ = (V,E’) into a single vertex

Let T* be (the edges of) an MST of G*

Theorem: T* [ E’ is an MST of GProof: By contradiction

Page 12: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

12

Example

4

8

11

8 79

10

144

21

2

67a

c

b

ea

eb , ecG

8

11

8 79

10

144

21

67

G* edges in T*

Page 13: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

13

Kruskal’s Algorithm

Kruskal-MST(G)• Find the cheapest (non-self-loop) edge (u,v)

in G• Contract (u,v) to obtain G*• Kruskal-MST(G*)

Page 14: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

14

Example4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

Page 15: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

15

Example

4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

Page 16: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

16

Example4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

Page 17: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

17

Example4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

Page 18: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

18

Example

4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

Page 19: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

19

Performance• Kruskal’s algorithm can be implemented

efficiently using Union-Find :• First, sort edges according to the weights• At each step, pick the cheapest edge

• If end-points are from different component, we perform Union (and include this edge to the MST)

è Time for Union-Find = O(Ea(E))

Total Time: O(E log E + E a(E)) = O(E log V)

Page 20: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

20

Prim’s Algorithm

Prim-MST(G, u)• Set u as the source vertex • Find the cheapest (non-self-loop) edge

from u, say, (u,v) • Merge v into u to obtain G*• Prim-MST(G*, u)

Page 21: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

21

Example4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

u

u

Page 22: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

22

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

Page 23: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

23

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

Page 24: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

24

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

Page 25: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

25

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

Page 26: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

26

Performance• Prim’s algorithm can be implemented

efficiently using Binary Heap H:• First, insert all edges adjacent to u into H• At each step, extract the cheapest edge

• If an end-point, say v, is not in MST, include this edge and v to MST• Insert all edges adjacent to v into H

• At most O(E) Insert/Extract-Minè Total Time: O(E log E) = O(E log V)

Page 27: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

27

Performance (speed-up)• In fact, Prim’s algorithm can be sped up

using a Fibonacci Heap F

• Instead of keeping edges in the heap, we keep distinct vertices

• This avoids Q(E) Extract-Min in the worst case

• At the beginning, each vertex (except source) is inserted into the heap, with key = ¥• key represents distance between u and the vertex

Page 28: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

28

Performance (speed-up)• Next, we scan all adjacent edges in u and

update the distance of the corresponding vertices (using Decrease-Key)

è the vertex with the smallest key must be joined to u with the cheapest edge

(since key = distance from u)

• So, we extract the minimum vertex, scan all its adjacent edges, and update corresponding vertices …

Page 29: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

29

Performance (speed-up)• The process is repeated until all vertices

in the heap are goneè MST obtained !

• Running Time: • O(V) Insert/Extract-Min• At most O(E) Decrease-Keyè Total Time: O(E + V log V)

Page 30: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

30

4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

u

u

1

1

11

1

10

4

8

1

1

1

1

10

4

8

Example

8

Page 31: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

31

Example

4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

1

1

0

4

8

8

10

4

7

8

7

7

2

2

4

46

Page 32: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

32

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

0

4

1

8 7

2

42

0

4

7

8 7

2

42

10

10

Page 33: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

33

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

0

4

1

8 7

2

42

10

0

4

1

8 7

2

42

9

Page 34: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

34

Example4

8

11

8 79

10

144

21

2

67

u

4

8

11

8 79

10

144

21

2

67

u

0

4

1

8 7

2

42

9

Page 35: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

35

Borůvka's AlgorithmBorůvka-MST(G)

• Find cheapest adjacent edge ev for each vertex v

• Contract all ev to obtain G*• Borůvka-MST(G*)

Page 36: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

36

Example4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

u

u

After 1 iterations

Page 37: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

37

Example4

8

11

8 79

10

144

21

2

67

4

8

11

8 79

10

144

21

2

67

u

After 2 iterations

Page 38: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

38

Performance• In Step 1 of Borůvka’s algorithm, each

vertex v needs to find ev

• can be done in O(E) time, without sorting of edges

• In Step 2, when all ev are contracted, we need to re-label the end-points of the edges so that they refer to the new vertices in G*• can be done in O(E) time, using DFS to

find connected components

Page 39: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

39

Performance• After Step 2, each new vertex of G*

represents at least two vertices of G• #vertices in G* £ V/2

è In general, if Borůvka-MST() is called for k iterations,

#vertices in G* £ V/2k

è At most O(log V) iterations

Total time: O(E log V) In practice, #iterations can be much smaller than O(log V)

Page 40: Design and Analysis of Algorithms - UCF Computer Sciencesharma/COP3503lectures/lecture15.pdf · Prim’s Algorithm Prim-MST(G,u) •Set uas the source vertex •Find the cheapest

40

Modifying Borůvka• Now, suppose we run Borůvka-MST() for

only k = log log V iterations

#vertices in G* £ V/2log log V = V/log V#edges in G* £ E

• Then, we switch back to Prim• Running Time:

O(E log log V) + O( E + (V/log V) log V)

= O(E log log V) ß could be better than both !!

Borůvka Prim