(40 points)
Consider the following real-world budget problem that challenges
your instructor,
and many other government/academic bureaucrats.
The input
consists of a collection of accounts
,
and a collection of events
.
Each account Ai has a start date ri and
an end date di.
Further each account Ai contains an amount ai of money.
Each event Ej happens on a date ej and
has a cost of cj dollars.
The cost cj for event Ej, or some portion thereof,
can be charged to any account
Ai that is open during the date ej, that is, any account Ai where
.
Note that an event can be paid for by more one account;
For example an event of cost
$3 can be paid by $1 from one account and $2 from another account.
Obviously, the account Ai can not contribute more than
ai dollars towards paying for events.
The problem is to determine which costs should be charged to which
accounts. The goal is to find an algorithm that will find a feasible
method of paying for the costs of the events, if it is possible to do so.
We consider greedy algorithms for this problem that pay for the
events by non-decreasing order of date. For simplicity assume
that the events are ordered by date, that is,
.
So our greedy algorithms first
determine how to pay for event E1, then determine how to
pay for event E2, then determine how to pay for event E3, etc.
Note that we are often forced to use such a greedy algorithm
in practice because
we are not aware of events until they happen.
The algorithms differ in how they pick the accounts to pay for the
current event. Consider the following two algorithms, Early and Level:
- Early
- Assume that events
have been paid for,
and we now wish to pay for event Ej.
- While Ej has not been fully paid for:
- Among the accounts that are open at time ej and
that have not yet been fully depleted, let Ai be an account
with the earliest end date.
- Deduct from Ai the smaller of the remaining unpaid portion of
the event, and the amount of money left in the account.
The intuition of Early is that you should pay from the account that
will expire soonest.
- Level
- Assume that events
have been paid for,
and we now wish to pay for event Ej.
Partition the cost of Ej among the open accounts in such a way
that the amount of money remaining, in the open account with the most money
remaining, is as small as possible.
- If you need to know, this can be accomplished in the following manner
(although how this is accomplish is not really germane to the
correctness issue at hand):
The open account with the largest balance pays for
part of Ej until the balance in this biggest open account
is equal to the balance in the second biggest open account;
Then these two open accounts pay for the remaining portion of cost equally
until their balances equals the balance in the third biggest
open account; Then these three open accounts pay for the remaining portion of
cost equally until .
The intuition of Level is that you should not deplete any account unless
it is necessary to do so.
As an example of the algorithms assume that we have two events
E1 and E2, with e1 = 5 and e2=7, and
with c1=60 and c2=50.
So event E1 can only charged to
accounts Ai satisfying
,
and
event E2 can only charged to
accounts Ai satisfying
.
Assume that accounts A7 and A9 have the property that
r7 < r9 < e1 = 5 < e2 = 7 < d7 < d9, so accounts
A7 and A9 can pay for events E1 and E2. Assume
all other accounts Ai either have di < 5 or
ri>7, so they are not eligible to pay for either event E1 or
event E2.
Assume that the initial balance in A7 is a7=$100,
and that the initial balance in A9 is a9=$80.
First Early will charge all of the cost of $60 for E1 to
A7 since d7 < d9.
Then Early will charge the first $40 for E2 to
A7 since d7 < d9. But at that time A7 will be depleted, and
then Early will charge the last $10 of the cost of E2 to
A9.
First Level will charge $40 of the cost of E1 to A7 and
$20 of the cost of E1 to A9, leaving both account
balances at $60. The $50 cost of E2 will
be split evenly by A7 and A9, that is, each
of these account pays $25, leaving the account balance
of each account at $35.
For each of the algorithms Early and Level, prove or disprove
that the algorithm correctly solves this problem