/
Greedy Algorithms: Celebration Party Greedy Algorithms: Celebration Party

Greedy Algorithms: Celebration Party - PowerPoint Presentation

briana-ranney
briana-ranney . @briana-ranney
Follow
358 views
Uploaded On 2019-06-27

Greedy Algorithms: Celebration Party - PPT Presentation

httpitiwtf Algorithms and Data Structures Artem A Golubnichiy artemgolubnichijru Department of Software of Computer Facilities and Automated Systems Katanov Khakass State University ID: 760449

segments left pointscoversorted children left segments children pointscoversorted time algorithm segment point groups points running length return append sort

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Greedy Algorithms: Celebration Party" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Greedy Algorithms:Celebration Party

http://iti.wtfAlgorithms and Data Structures

Artem A. Golubnichiyartem@golubnichij.ruDepartment of Software of Computer Facilities and Automated Systems Katanov Khakass State University

Slide2

STRUCTURE OF THE CLASS

2

Celebration Party Problem

Greedy Algorithm

Implementation and Analysis

Slide3

3

Many children came to a celebration. Organize them into the minimum possible number of groups such that the age of any two children in the same group differs by at most two years.

Slide4

NAIVE ALGORITHM

4

Try all possible distributions of children into one or more groups

Slide5

NAIVE ALGORITHM

5

Try all possible distributions of children into one or more groups

For each distribution, check whether any two children in any group differ by at most 2 years of age

Slide6

NAIVE ALGORITHM

6

Try all possible distributions of children into one or more groups

For each distribution, check whether any two children in any group differ by at most 2 years of age

Return the minimum number of groups among valid distributions

Slide7

RUNNING TIME

7

Lemma

The running time of the naive algorithm is at least 2

n

, where n is the number of children.

Slide8

RUNNING TIME

8

LemmaThe running time of the naive algorithm is at least 2n, where n is the number of children.

Proof

This algorithm will consider all possible distributions of children into two groups (and many other distributions of children into groups). First of these two groups corresponds to any subset of children, and there are 2

n

different subsets.

Slide9

ASYMPTOTICS

9

Naive algorithm works in time

Ω(2

n

)

Slide10

ASYMPTOTICS

10

Naive algorithm works in time

Ω(2

n

)

For

n

= 50 it is at least

2

50

= 1125899906842624

operations!

Slide11

ASYMPTOTICS

11

Naive algorithm works in time

Ω(2

n

)

For

n

= 50 it is at least

2

50

= 1125899906842624

operations!

We will improve this significantly

Slide12

COVERING POINTS BY SEGMENTS

12

Covering points by segments

Input:

A set of n points x

1

,

...,

x

n

∈ R.

Output:

The minimum number of segments of length at most 2 needed to cover all the points.

Slide13

13

Example

Slide14

14

Example

Slide15

15

Example

Slide16

CONNECTION WITH GROUPING CHILDREN

16

Points x

1

, ...,

x

n

correspond to children’ ages

Slide17

CONNECTION WITH GROUPING CHILDREN

17

Points x

1

, ...,

x

n

correspond to children’ ages

Segments correspond to groups

Slide18

CONNECTION WITH GROUPING CHILDREN

18

Points x

1

, ...,

x

n

correspond to children’ ages

Segments correspond to groups

Any two children within the same segment of length 2 differ by at most 2 years of age

Slide19

CONNECTION WITH GROUPING CHILDREN

19

Points x

1

, ...,

x

n

correspond to children’ ages

Segments correspond to groups

Any two children within the same segment of length 2 differ by at most 2 years of age

Any valid group of children can be put into a segment of length 2

Slide20

CONNECTION WITH GROUPING CHILDREN

20

Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.

Slide21

CONNECTION WITH GROUPING CHILDREN

21

Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.

Slide22

CONNECTION WITH GROUPING CHILDREN

22

Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.

Slide23

CONNECTION WITH GROUPING CHILDREN

23

Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.

Slide24

GREEDY ALGORITHM

24

Cover the leftmost point with a segment of length 2

Slide25

GREEDY ALGORITHM

25

Cover the leftmost point with a segment of length 2

Remove all the points within this segment

Slide26

GREEDY ALGORITHM

26

Cover the leftmost point with a segment of length 2

Remove all the points within this segment

Solve the same problem with the remaining points

Slide27

27

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide28

28

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide29

29

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide30

30

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide31

31

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide32

32

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide33

33

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide34

34

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide35

35

Assume x1 ≤ x2 ≤ ... ≤ xn

PointsCoverSorted

(x

1

, . . . ,

x

n

)

segments ← empty list

left ← 1

while left ≤ n:

(

l,r

)←(

x

left

,x

left

+2)

segments.append

((l, r ))

left ← left + 1

while left ≤ n and

x

left

≤ r:

left ← left + 1

return segments

Slide36

36

Lemma

The running time of

PointsCoverSorted

is O(n).

Slide37

37

LemmaThe running time of PointsCoverSorted is O(n).

Proof

left changes from 1 to n

For each left, append at most 1 new segment to solution

Overall, running time is O(n)

Slide38

38

TOTAL RUNNING TIME

PointsCoverSorted

works in O(n

) time

Slide39

39

TOTAL RUNNING TIME

PointsCoverSorted

works in O(n

) time

Sort {x

1

, x

2

, . . . ,

x

n

}, then

call

PointsCoverSorted

Slide40

40

TOTAL RUNNING TIME

PointsCoverSorted

works in O(n

) time

Sort {x

1

, x

2

, . . . ,

x

n

}, then

call

PointsCoverSorted

Soon you’ll learn to sort in O(n log n

)

Slide41

41

TOTAL RUNNING TIME

PointsCoverSorted

works in O(n

) time

Sort {x

1

, x

2

, . . . ,

x

n

}, then

call

PointsCoverSorted

Soon you’ll learn to sort in O(n log n)

Sort +

PointsCoverSorted

is O(n

log n)

Slide42

42

ASYMPTOTICS

Straightforward solution is

Ω(2

n

)

Slide43

43

ASYMPTOTICS

Straightforward solution is

Ω(2

n

)

Very long for n =

50

Slide44

44

ASYMPTOTICS

Straightforward solution is

Ω(2

n

)

Very long for n = 50

Sort + greedy is O(n log n

)

Slide45

45

ASYMPTOTICS

Straightforward solution is

Ω(2

n

)

Very long for n = 50

Sort + greedy is O(n log n)

Fast for n = 10 000

000

Slide46

46

ASYMPTOTICS

Straightforward solution is

Ω(2

n

)

Very long for n = 50

Sort + greedy is O(n log n)

Fast for n = 10 000 000

Huge improvement!

Slide47

47

CONCLUSION

Straightforward solution is exponential

Important to reformulate the problem

in mathematical

terms

Safe choice is to cover leftmost point

Sort in O(n log n) + greedy in O(n)