30
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

1

Chapter 4 GreedyAlgorithms

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

Page 2: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

MST : Union-Find or Disjoint Sets Data Structure

A Disjoint set data structure implements three functions:

- MakeSet(x) : creates a set with one element x in it. MakeSet(V), creates n sets each with one element of V.

- Find(x) : returns a set-canonical member for set-of(x). - Union(x,y) : merges the set containing x and the set

containing y into one new set with one canonical element.

Let V={1,2,3,4,5} MakeSet(V) = { {1}, {2}, {3}, {4}, {5} } Find(4) = 4 Union(1,2) changes the sets to { {1,2}, {3}, {4}, {5} } Find(2) = 1. Union(2,5) changes the sets to { {1,2,5}, {3}, {4} } Find(5) = 1

Page 3: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

0 01 12 23 34 45 26 67 2… …

- A first try using simple array organization

0 01 12 23 34 45 56 67 7… …

MakeSet(V)

Union(2,5)

0 01 12 23 34 45 26 67 7… …

Union(5,7)

Find(2) = Find(7) = 2 2 = Find(5) ≠ Find(6) = 6

Union-Find DS : Union-Find or Disjoint Sets 1

Page 4: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

0 01 12 23 34 45 26 67 2… …

- A first try using simple array organization

0 01 12 23 34 45 26 67 7… …

Union(5,7)- Find is O(1)

- Makeset is O(1) or O(n)

- But Union is Ω(n)

- We have to change all elements that point to 2 when an element that returns 2 is unionized with another.

Union-Find DS : Union-Find or Disjoint Sets 2

Page 5: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- A second try using array and linked list

MakeSet(V)01234567… …

0 nil1 nil2 nil3 nil4 nil5 nil6 nil7 nil

Union(2,5)

01234567… …

0 nil1 nil23 nil4 nil

5 nil

6 nil7 nil

Union-Find DS : Union-Find or Disjoint Sets 3

Page 6: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- A second try using array and linked list

01234567… …

0 nil1 nil23 nil4 nil

5 nil

6 nil7 nil

Union(5,7)

Union-Find DS : Union-Find or Disjoint Sets 4

Page 7: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- A second try using array and linked list

01234567… …

0 nil1 nil23 nil4 nil

5 nil

6 nil

Union(5,7)

7

Union-Find DS : Union-Find or Disjoint Sets 4

Page 8: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

3

- A second try using array and linked list

01234567… …

0 1 nil

2 5 nil

4 6

7

- How long does it take to do Union(5,1) ?

- You need to update all the pointers in the main array to the new combined list.

- Hence, in the worst case, Union requires Ω(n) time - Find and MakeSet still require constant time.

Union-Find DS : Union-Find or Disjoint Sets 5

Page 9: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- A second try using array and linked list

01234567… …

0 1 nil

2 5 nil

4 6

3 7

- There are sequences of Union that require Ω(n2) amortized time. Just take Union(0,i+1) for all 1≤i≤n. If the second list always gets moved into the first one, then after n Union, we have modified Ω(n2) values in the main array.

- But there is a heuristic that we can use to counter this bad side effect. It is called Union-by-rank.

Union-Find DS : Union-Find or Disjoint Sets 6

Page 10: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

0 11 12 23 14 15 16 17 1… …

- A second try using array and linked list

0 nil1 nil23 nil4 nil

5 nil

6 nil

Union(5,1)

7 nil

Union(6,7)

7

Union-Find DS : Union-by-rank

Page 11: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

0 11 12 33 14 15 16 17 2… …

- A second try using array and linked list

0 nil

23 nil4 nil

5 nil

6 nil

Union(5,1)

1

7 nil

Union(6,7)

7

Union-Find DS : Union-by-rank

Page 12: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- A second try using array and linked list

0 11 12 33 14 15 16 17 2… …

0 nil

23 nil4 nil

1 5 nil

7 6 nil

Union(5,6)

Union-Find DS : Union-by-rank 2

Page 13: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

7 6

- A second try using array and linked list

0 11 12 53 14 15 16 17 2… …

0 nil

23 nil4 nil

1 5 nil

Union(5,6)

Union-Find DS : Union-by-rank 2

Page 14: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- Each time a list is added at the back of another list, its size doubles (the smallest).

- How many time can the size of list double if the maximum size it can achieve is n ?

- log(n) (no element can be updated more than) - Yes, in the final step we work Ω(n), but we can

work for only log(n) times. - Hence the total work done to do n-1 Union and m

Find is O(m+n log(n)) - A definite improvement over O(n2).

0 nil

23 nil4 nil

1 5 nil

0 11 12 53 14 15 16 17 2… …

7 6

Union-Find DS : Union-by-rank 3

Page 15: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

10 11 12 53 14 15 16 17 2… …

5 nil

6

- Find(x) takes O(1) - n Union(x,y) takes in the worst case O(n log n) - MakeSet(V) takes O(n) - Hence m Find operations and n-1 Union operations

require in the worst case O(m+n log n) - But we can still do better : Almost linear in the worst

case for m Find operations and n-1 Unions

0 nil

2 7

Union-Find DS : Union-by-rank 4

Page 16: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

1

7

2

6

5

4

3

Makeset{V}

Union(2,5)

1

7

2

6

5

4

3Union(6,7)

1

7

2

6

5

4

3

Union-Find DS : Path compression

Page 17: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

1

7

2

6

5

4

3 Union(1,2)

1

7

2

6

5

4

3 Union(5,6)

1

7

26

5

4

3

2 = Find(5) = Find(6) 2 = Find(7) ≠ Find(4) = 4

Union-Find DS : Path compression 2

Page 18: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

- How long does it take on average to do a Find(x) operation ?

- Union(x,y) operation ? - Makeset(V) ? 1

234567… …

- We can use Union-by-rank. - Just keep a pointer in the

array or in each cell.

1

7

26

5

4

3

Union-Find DS : Path compression 3

Page 19: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

Find(x)::while x ≠ prev(x)x=prev(x)return x

Here rank means depth of the tree

rooted at rx

Union(x,y)::rx = Find(x)ry = Find(y)if rx==ry returnif rank(rx)>rank(ry)prev(ry)=rxelseprev(rx)=ryif rank(rx)==rank(ry)rank(ry)=rank(ry)+1

1

7

26

5

4

3

Union-Find DS : Path compression 4

Page 20: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

1

7

2

6

5

4

3

8

Union(6,7)

Union(8,2)

1

7

2

6

5

4

38

Union(7,5)

Union(4,5)Union(3,7)

71

2

6

54

3

8

Union-Find DS : Path compression 5

Page 21: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

A few observations: - rank(x)<rank(prev(x)) - Any sub-tree of rank k has at least 2k nodes.Pf: At the beginning all nodes have rank 0. 20=1.

When does the rank change ? When we unionize equal rank trees. Hence if both trees had at least 2k nodes, then the new tree has 2k+2k=2k+1 nodes and rank k+1.

Finally, over n nodes, only n/2k nodes can have rank k. From this we conclude that the maximum depth of a tree is log(n) since n/2log n = 1.

71

2

6

54

3

8

Union-Find DS : Path compression 6

Page 22: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

Since the maximum dept of a tree is at most log n, we can conclude that: Find(x) takes at most O(log n) time, n Union(x,y) takes at most O(n log n) time, Makeset(V) is still linear.

But Find(x) for our linked list implementation used O(1) and we are not quicker for Union and Makeset. What happened ? Well, we are not done. This data structure needs another heuristic in order to be competitive.

71

2

6

54

3

8

Union-Find DS : Path compression 7

Page 23: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

Find(x)::if x ≠ prev(x) then prev(x) = Find(prev(x))return prev(x)

Find(x)::r = xwhile r ≠ prev(r) r = prev(r)while x ≠ prev(x) x’ = prev(x) prev(x) = r x = x’return x

71

2

6

54

3

8

Union-Find DS : Path compression 8

Page 24: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

Find(x)::if x ≠ prev(x) then prev(x) = Find(prev(x))return prev(x)

Union(x,y)::rx = Find(x)ry = Find(y)if rx==ry returnif rank(rx)>rank(ry) prev(ry)=rxelse prev(rx)=ry if rank(rx)=rank(ry) rank(ry)=rank(ry)+1

71

2

6

54

3

8

Union-Find DS : Path compression 8

Page 25: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

1

7

2

6

5

4

3

Makeset{V}

Union(2,5)

1

7

2

6

5

4

3Union(6,7)

1

7

2

6

5

4

3

Union-Find DS : Path compression 9

Page 26: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

1

7

2

6

5

4

3 Union(1,2)

1

7

2

6

5

4

3

2 = Find(5) = Find(6) 2 = Find(7) ≠ Find(4) = 3

Union(5,6)

Union(3,4)

1

7

26

5

43

Union-Find DS : Path compression 10

Page 27: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

Union(4,1)1

7

26

5

43

1

7

26

5

43

Find(7)

2

1

76

5

43

Union-Find DS : Path compression 11

Page 28: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

2

1

76

5

43

- Intuitively, the tree cannot get very deep as we constantly make it flatter.

- Makeset(V) takes O(n). - In fact, any sequence of m operations

(Makeset, Union and Find) n of which are Makeset(x) operations require Θ( m• α(m,n) ) operations.

- The function α(m,n) is the extremely slowly growing inverse of Ackermann ’ s function.For all practical (and un-practical) problem instances, it is at most 4.

- So in Practice, it requires Θ(4m) time for m operations, n of which are Union.

Union-Find DS : Path compression 12

In fact, α(n) is less than 5 for any practical input size n, since A(4, 4) is on the order of 222216

.

Page 29: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

Input: G=(V,E,cost(E)) output:T

for all u in V Makeset(u)

T={} sort the edges E by cost(E) for each edges (u,v) in (sorted) E

if Find(u)≠Find(v) T=T+(u,v) Union(u,v) if(size(T)=|V|-1) done

n Makeset

m log n O(1)

n-1 Union

2m Find

m O(1)

n Makeset + 2m Find + n-1 Union 2n+2m operations, n of which are Makeset.

Hence, our data structure operations will require Θ( (m+n)•α((m+n),n) ) or roughly Θ( m+n ).

Union-Find DS : Kruskal’s algorithm

Page 30: 4.MST2 - copie 2crypto.cs.mcgill.ca/~crepeau/COMP610/04MST2.pdf · instances, it is at most 4. - So in Practice, it requires Θ(4m) time for m operations, n of which are Union. Union-Find

30

Chapter 4 GreedyAlgorithms

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.