Question: Sometimes, a seemingly reasonable greedy idea might not work. Your friend is trying to solve the Positive Interval Covering problem: In an array A of
Sometimes, a seemingly reasonable greedy idea might not work. Your friend is trying to solve the Positive Interval
Covering problem:
In an array A of integers, call an interval i j positive if Ai Ai Aj Your friend
wishes to cover the positive integers of the array with disjoint intervals ie the intervals may not
overlap An element is covered if it is part of one of the intervals. Given an array A containing n
integers which may be positive, negative, or zero your friend wants to find the smallest number of
disjoint positive intervals which can cover all the positive elements of A Note that negative elements
and copies of may be uncovered, or they may be part of intervals.
Your greedy friend loves using greedy algorithms, and has a greedy idea to solve this problem. Their idea is:
starting from the leftmost positive element, keep a running sum of the elements in the interval as you move the
right endpoint to the right. Move it to the right until the interval becomes nonpositive, then make everything to
the left the interval.
Your friends idea seems quite reasonable on the surface. Being the great friend you are, youve decided to help
your friend write some pseudocode for their idea. Of course, being the amazing coder that you are, youve perfectly
implemented your friends idea in the following perfectly perfect pseudocode which is perfect:
: function PositiveCoveringAn
: startInd
: while There is a positive element in A at startInd or later do
: while AstartInd do : startInd:
: endInd startInd
: sum AstartInd
: while sum do
: if endInd n then : endInd
: break
: sum AendInd
: endInd
: Add startIndendInd to list of intervals : startInd endInd return list of intervals
Your friend knows that every greedy idea needs a proof of correctness, and they wrote the following attempt at a
proof of correctness:
Spoof. We prove the claim using the greedy stays ahead format.
Let OPT be an optimal solution, and let ALG be the solution generated by our algorithm. We will show that for all n
after the nth interval reading from lefttoright ALG has covered at least as many positive elements as OPT.
We proceed by induction on n
Base Case: n
The leftmost interval must cover the leftmost positive element. ALG chooses the longest positive interval that
includes that first element as it runs until the interval becomes invalid so it must have at least as many elements
covered as OPT does.
IH: Suppose that the claim holds for n k
IS: Let ij be the endpoints of the k st interval from lefttoright of OPT. We claim that ALG will cover at
least through index j
Case : ALG was testing starting from i that is index i contained the leftmost uncovered positive element
Since OPT takes i j as an interval, it must be a positive interval. Thus ALG will consider ij as a valid interval.
It will only choose a different interval if it is longer, since we only ever increase endInd. Thus ALG covers at least
as many elements as OPT.
Case : ALG was testing starting from an index l i
By IH ALG covered at least as many elements as OPT in the first k intervals. Thus we have l i Observe that any
elements at or after i but strictly before l ie those that OPT covers but ALG does not will not include any positive
entries, as ALG starts at the first uncovered positive element. Thus, the sum Ai Aj is at most Al Aj
Since OPT considers ij a positive interval, l j is also a positive interval, which means that ALG will consider
that interval. Thus ALG also will choose an ending point j or larger.
In both cases, we have shown the claim holds for k as required
a One of your TAs notices your pseudocode and tells you that this greedy idea doesnt work. Find a coun
terexample for this algorithm, where your friends seemingly reasonable greedy idea does not work, by
describing an integer array. You should also describe what intervals the provided pseudocode returns, and
describe a better set of intervals ie a way to cover all the positive elements of the array using less disjoint
positive intervals than the greedy solution You do not need to explain how you found your solution.
b If the idea doesnt work, then your friends proof must be wrong! Clearly describe the error in the proof.
A description cannot just be the counterexample shows it must be wrong. You need to find an incorrect
leap in logic, invalid assumption, missing case, or similar error. Nonetheloess, it will probably help to run the
algorithm on your example with an eye toward the proof to see where it breaks down.
Our explanation is only a few sentences, but its ok if yours is a bit longer.
Part c in image
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
