httpitiwtf Algorithms and Data Structures Artem A Golubnichiy artemgolubnichijru Department of Software of Computer Facilities and Automated Systems Katanov Khakass State University ID: 760449
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.
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
Slide2STRUCTURE OF THE CLASS
2
Celebration Party Problem
Greedy Algorithm
Implementation and Analysis
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.
Slide4NAIVE ALGORITHM
4
Try all possible distributions of children into one or more groups
Slide5NAIVE 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
Slide6NAIVE 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
Slide7RUNNING TIME
7
Lemma
The running time of the naive algorithm is at least 2
n
, where n is the number of children.
Slide8RUNNING 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.
Slide9ASYMPTOTICS
9
Naive algorithm works in time
Ω(2
n
)
Slide10ASYMPTOTICS
10
Naive algorithm works in time
Ω(2
n
)
For
n
= 50 it is at least
2
50
= 1125899906842624
operations!
Slide11ASYMPTOTICS
11
Naive algorithm works in time
Ω(2
n
)
For
n
= 50 it is at least
2
50
= 1125899906842624
operations!
We will improve this significantly
Slide12COVERING 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.
Slide1313
Example
14
Example
15
Example
CONNECTION WITH GROUPING CHILDREN
16
Points x
1
, ...,
x
n
correspond to children’ ages
Slide17CONNECTION WITH GROUPING CHILDREN
17
Points x
1
, ...,
x
n
correspond to children’ ages
Segments correspond to groups
Slide18CONNECTION 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
Slide19CONNECTION 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
Slide20CONNECTION WITH GROUPING CHILDREN
20
Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.
Slide21CONNECTION WITH GROUPING CHILDREN
21
Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.
Slide22CONNECTION WITH GROUPING CHILDREN
22
Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.
Slide23CONNECTION WITH GROUPING CHILDREN
23
Safe choice: cover the leftmost point with a segment of length 2 which starts in this point.
Slide24GREEDY ALGORITHM
24
Cover the leftmost point with a segment of length 2
Slide25GREEDY ALGORITHM
25
Cover the leftmost point with a segment of length 2
Remove all the points within this segment
Slide26GREEDY 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
Slide2727
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
Slide2828
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
Slide2929
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
Slide3030
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
Slide3131
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
Slide3232
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
Slide3333
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
Slide3434
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
Slide3535
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
Slide3636
Lemma
The running time of
PointsCoverSorted
is O(n).
Slide3737
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)
Slide3838
TOTAL RUNNING TIME
PointsCoverSorted
works in O(n
) time
Slide3939
TOTAL RUNNING TIME
PointsCoverSorted
works in O(n
) time
Sort {x
1
, x
2
, . . . ,
x
n
}, then
call
PointsCoverSorted
Slide4040
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
)
Slide4141
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)
Slide4242
ASYMPTOTICS
Straightforward solution is
Ω(2
n
)
Slide4343
ASYMPTOTICS
Straightforward solution is
Ω(2
n
)
Very long for n =
50
Slide4444
ASYMPTOTICS
Straightforward solution is
Ω(2
n
)
Very long for n = 50
Sort + greedy is O(n log n
)
Slide4545
ASYMPTOTICS
Straightforward solution is
Ω(2
n
)
Very long for n = 50
Sort + greedy is O(n log n)
Fast for n = 10 000
000
Slide4646
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!
Slide4747
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)