0% found this document useful (0 votes)
20 views6 pages

Revisedhomework 3

The document outlines Homework 3 for a CSE course, focusing on greedy algorithms, data structures for disjoint sets, and approximation algorithms. It includes ungraded problems that require students to analyze various greedy strategies for optimal solutions, as well as graded problems that involve defining instances, solutions, and constraints for specific scenarios. The homework is due on February 23, 2024, at 11:59 PM.

Uploaded by

jsliang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Revisedhomework 3

The document outlines Homework 3 for a CSE course, focusing on greedy algorithms, data structures for disjoint sets, and approximation algorithms. It includes ungraded problems that require students to analyze various greedy strategies for optimal solutions, as well as graded problems that involve defining instances, solutions, and constraints for specific scenarios. The homework is due on February 23, 2024, at 11:59 PM.

Uploaded by

jsliang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE 202 Homework 3

Winter, 2024
New due date: Friday, Feb 23 at 11:59 PM
Greedy algorithms
Data structures for disjoint sets
Approximation algorithms

Ungraded problems

For each of the first five problems, two greedy strategies are given. Decide
which greedy strategy gives optimal solutions. Find a counter-example for the
other, prove the correct one is correct, and give as asymptotically efficient an
algorithm as possible for the correct strategy.

Weighted average completion time You are given a list of n jobs j1 , ..jn , each
with a time t(j) to perform the job, and a weight w(j). You wish to order
the jobs as
jσ(1) , ., jσ(n)
in such a way as to minimize:
X X
w(jσ(i) )( t(jσ(k) ))
i 1≤k<i

; the weighted sum of the time each job has to wait before being performed.
Greedy Strategy A: Perform the job with the shortest completion time
first. If there are ties, use the one with the highest weight. Repeat on
remaining jobs.
Greedy Strategy B: Perform the job with the largest ratio r(j) = w(j)/t(j)
first. Repeat on remaining jobs.
Oxen pairing Consider the following problem: We have n oxen, Ox1 , ..Oxn ,
each with a strength rating Si . We need to pair the oxen up into teams
to pull a plow; if Oxi and Oxj are in a team, we must have Si + Sj ≥ P ,
where P is the weight of a plow. Each ox can only be in at most one team.
Each team has exactly two oxen. We want to maximize the number of
teams.
Greedy Strategy A: Take the strongest and weakest oxen. If together they
meet the strength requirement, make them a team. Recursively find the
most teams among the remaining oxen.
Otherwise, delete the weakest ox. Recursively find the most teams among
the remaining oxen.
Greedy Strategy B: Take the two weakest oxen. If together they meet
the strength requirement, make them a team. Recursively find the most
teams among the remaining oxen.

1
Otherwise, delete the weakest ox. Recursively find the most teams among
the remaining oxen.
Biggest Area Segment Matching You are given a set of n horizontal seg-
ments of lengths a1 , . . . , an and a set of vertical segments of length b1 , . . . , bn .
Each horizontal segment ai can be combined with a vertical segment bj to
form a rectangle of area ai · bj . A matching between the lengths assigns
each horizontal length ai a distinct vertical length bji . Give as efficient an
algorithm as possible that on input a1 , . . . , an , b1 , . . . , bn , finds a match-
ing between the horizontal and vertical
P segments that maximizes the total
area of all resulting rectangles, i ai bji .
Greedy strategy A: Pair the largest horizontal segment with the shortest
vertical segment; Repeat on the remaining segments.
Greedy strategy B: Pair the largest horizontal segment with the largest
vertical segment; Repeat on the remaining segments.
Cookies Consider the following problem:
You are baby-sitting n children and have m > n cookies to divide between
them. You must give each child exactly one cookie (of course, you cannot
give the same cookie to two different children). Each child has a greed
factor gi , 1 ≤ i ≤ n which is the minimum size of a cookie that the child
will be content with; and each cookie has a size sj , 1 ≤ j ≤ m. Your goal
is to maximize the number of content children, i.e.., children i assigned a
cookie j with ci ≤ sj .
Candidate Strategy A: Assign the largest cookie to the greediest child.
Repeat.
Candidate Strategy B: Look at the greediest child. If the largest cookie
makes the child content, give the child the largest cookie. Otherwise, give
the child the smallest cookie.
Homework grade maximization In a class, there are n assignments. You
have H hours to spend on all assignments, and you cannot divide an
hour between assignments, but must spend each hour entirely on a single
assignment. The I’th hour you spend on assignment J will improve your
grade on assignment J by B[I, J], where for each J, B[1, J] ≥ B[2, J] ≥
... ≥ B[H, J] ≥ 0. In other words, if you spend h hours on assignment
Pi=h
J, your grade will be i=1 B[i, J] and time spent on each project has
diminishing returns, the next hour being worth less than the previous one.
You want to divide your H hours between the assignments to maximize
your total grade on all the assignments.
Greedy strategy A: Spend each hour on the assignment with the current
smallest total grade.

2
Greedy strategy B: Spend the next hour h on the assignment that has the
largest increase in grade for the additional hour.

Other ungraded problems

Movie selection version A You are subscribed to the VidStream movie stream-
ing service for a test period of N days. You have a list of M movies you
want to watch during this period, but don’t want to watch more than one
movie a day. Unfortunately, some of the movies won’t be available right
away; movie K is available starting day AK (until the end of the N -day
stretch). Each movie only takes a day to watch and you won’t rewatch the
same movie. Not all the movies are equal; your desire to watch movie K
is given by weight wK > 0. In the following, you will design and analyse
a greedy algorithm that maximizes the total weight of movies on your list
you can watch during the trial period.
For example, say N = 5 and there are seven movies, A, B, C, D, E, F, G,
with A, B available starting day 1, C, D starting day 3, and E, F, G start-
ing day 4. The weights are 1, 3, 2, 4, 5, 2, 7 respectively. Then one schedule
is to watch A on day 1, B on day 2, D on day 3, E on day 4, and G on day
5, which would have total weight of watched movies 1 + 3 + 4 + 5 + 7 = 20.

1. (6 points) Fill out the rest of the problem specification:


• Instance: Integer N ; list of M movies each with availability time
AK , 1 ≤ AK ≤ N and weight wK > 0.
• Solution Format: Array W atch[1..N ] giving which movie you
watch, if any, on each day D. We use W atch[D] = 0 to mean we
are not watching a movie on that day.
• Constraints:
(i) You don’t watch the same movie twice, i.e., if D1 6= D2 , and
W atch[D1 ] 6= 0, then W atch[D1 ] 6= W atch[D2 ].
(ii) You don’t watch a movie before it’s available, i.e., FILL THIS
IN.
• Objective: Maximize: FILL THIS IN
2. Candidate Greedy Strategy: On each day in order, watch the
movie that is available that you haven’t previously watched that max-
imizes wK , breaking ties arbitrarily. (If there are no such movies, you
don’t watch anything.)
Fill in the steps of the proof of the following modify-the-solution
lemma, for the case given:
Let K be the movie available on day 1 with maximum wK . Let
W atch[1..N ] be any possible schedule of movie-watching so that W atch[1] 6=
K. Then there exists a schedule W atch0 with W atch[1] = K that has
at least as large total weight.

3
Proof: Let W atch, K be as above.
Case 1: Assume that in W atch, we watch movie K on a later day,
i.e., W atch[J] = K for some J > 1.
(a) (6 points) Define W atch0 .
(b) (4 points) W atch0 is a valid solution because...
(c) (4 points) The total weights of watched movies in W atch0 is at
least that in W atch because...
The following is just to complete the proof of the modify-the-solution
lemma. You can read it if you like, but don’t have to do anything.
Proof: Let W atch, K be as above. Case 2: Assume that in W atch, we
never see movie K on a later day, i.e., W atch[J] 6= K for all J. Then
we let W atch0 be the same as W atch, except that W atch0 [1] = K.
Since we never watch K at a later date in W atch0 , we don’t watch any
movies more than once, and since K is available on day 1, we don’t
watch any movies before they are available. Thus, W atch0 meets the
constraints of the problem. In W atch0 , we replace whatever movie
L = W atch[1] we were watching on Day 1 with K. Since K is
available on Day 1, and wK is the largest weight of a movie available
on Day 1, wK ≥ wL , and the total weight in W atch0 is at least that
of W atch.

Approximation for bin filling In the bin filling problem, you have n items
of positive integral sizes a1 , ..an and m < n bins, where bin j is of size Bj .
You need to assign each item i to a bin A[i]
P , in a way to fill the maximum
number of bins, where a bin j is full if i|A[i]=j ai ≥ Bj .
Give an efficient approximation algorithm for this problem. Most of the
points will be based on your approximation ratio and the proof that it
achieves this ratio. The best possible ratio is to fill at least 1/2 as many
bins as the optimal solution.
Euclidean distances minimum spanning trees Say we have a graph where
the vertices represent points (x, y) where x and y are integers, and p the
lengths are Euclidean distances between points, d((x, y), (x0 , y 0 )) = (x − x0 )2 + (y − y 0 )2
and so in general, irrational numbers. How can we modify Kruskal’s algo-
rithm to find a minimum spanning tree while avoiding issues about how
many digits to approximate these irrational distances, i.e., in a way that
only uses integer arithmetic and comparisons? (20 points, but the answer
can just be a sentence or two.)

Graded problems

Birthday parties You have n employees, with birthdays on d1 ≤ d2 ≤ ... ≤


dn . You want to have a birthday party that celebrates each employee,

4
but employees with close birthdays can be celebrated at the same party.
The party to celebrate an employee must be within D days of their actual
birthday (up to D days before or after. Parties can be on days without
any birthdays. Days are measured from the current day, so parties can’t
be on negative numbered days.) First, clearly define this problem in the
format : Instance, Solution, Constraints, Objective. Then give an efficient
algorithm to compute a set of parties of the minimal size that meets the
constraints. (5 points formalization, 5 correct poly-time algorithm, 15
points correctness proof, 5 points time analysis and efficiency).
Minimizing time to finish grading You have n problems, with grading dif-
ficulties D1 , ..Dn > 0, and m > n IAs to grade. You can assign each IA
to grade one problem, and cannot split an IAs time between problems. If
you assign k IA’s to problem j, that problem will be completed at time
Dj /k. You want to minimize the time when all problems are graded and
you can release the grades to the class. First, clearly define this problem
in the format : Instance, Solution, Constraints, Objective. Then give an
efficient algorithm to compute an optimal solution. (5 points formaliza-
tion, 5 correct poly-time algorithm, 15 points correctness proof, 10 points
time analysis and efficiency).
Reasoning with functions and equations Now an extra-credit problem worth
10 points. Incorrect or unproven algorithms or algorithms much slower
than linear in n + m will not recieve any extra credit.
You have n variable names x1 , ..xn , and a table for a function f which
gives for each variable xi a variable xj whose value is f (xi ), i.e., we know
xj = f (xi ). We are also given a list of m equations of the form xi1 = xi2 .
From these two sets of equations, we can make further deductions using
the rule:xi1 = xi2 → f (xi1 ) = f (xi2 ), and the usual properties of equality:
transitiivity, reflexitivity, symmetry. We want to return the set of xi ’s that
we can deduce are equal to x1 . For example, if we have three variables
x1 , x2 , x3 and f (x1 ) = x2 , f (x2 ) = x3 and f (x3 ) = x2 , and we know
x1 = x2 , we can deduce x2 = f (x1 ) = f (x2 ) = x3 , so all three must have
the same value. Give an efficient algorithm to compute this set. Your
algorithm should be very close to linear time. (Hint: think of adding
equations one at a time, and keeping track of the deductions so far in a
data structure.)
Approximate minimization of a sum You are given a table F [i] for a non-
increasing function from the integers from 1 to N to the non-negative
real numbers. You wish to find an i that minimizes i + F [i]. Give an
approximation algorithm scheme for this problem that takes O(log N +1/)
time and has approximation ratio 1 + . (15 points for being within the
specified time and the time analysis (12 /15 of these for algorithms that

5
take O(logN/)), 20 points for analysis of the approximation ratio. Hint:
first find an algorithm with approximation ratio 2.)

You might also like