Your algorithm is correct.
The input is a list of n natural numbers.
The output of your algorithm is a re-arrangement of this list of
natural numbers, which I'll call S. Let S@i denote the ith element of
this list for 0 <= i < n. (Actually your output may be "run task1, run
task2" and such, but both forms are interchangeable)
If you were to write out the elements of S with their indices below
them, we can think of the indices as representing time.
Suppose that there is a re-arrangement T of the given list in which
more of the tasks finish on time than in S. It must be possible to
obtain T from S by swapping elements of S. In particular, we must swap
an element of S which finishes on time with one that does not if we
are to have any hope of increasing the number of tasks that finish on
time. Thus we are swapping an element of S, S@x with another element
S@y with S@x > x (corresponding task finishes on time) and S@y <= y
(corresponding task doesn't finish on time). Note that y > x since
according to our algorithm we "fill" S firstly with tasks whose
deadlines have not occured. If the number of tasks finished on time
increases after swapping then it must be that S@x > y and S@y > x, but
S@y <= y, so S@x > S@y, which is impossible, since y > x and we "fill"
S firstly with tasks of earliest deadline whose deadline has not
already elapsed, but S@y is a counter-example to this condition (since
at time x it's deadline has not occured and it's deadline is earlier
than the one for S@x). Thus we cannot improve on S, and so the
algorithm is optimal. []
I think that's correct anyway...
stegen123@yahoo.com (Jack Smith) wrote in message news:<20b84b19.0310222312.27184ffd@posting.google. com>...[color=blue]
> Hello, any help appreciated with following problem. I figured out the
> algorithm (I think), just having trouble proving it is optimal.
>
>
> Suppose we are given n tasks each of which takes 1 unit time to
> complete. Suppose further that each task has a deadline by which it
> is expected to finish. IF a task is not finished by the deadline, a
> standard penalty of $10 is applied. The problem is to find a schedule
> of the tasks that minimizes the penalty. Develop a greedy algorithm
> to solve the problem. Prove that the algorithm gives an optimal
> solution.
>
>
> The greedy algorithm I came up with is:
>
> you schedule tasks to run in order of earliest deadline that has NOT
> passed. Schedule the tasks whose deadline has passed to run in the
> end.
>
> let me demonstrate with an example:
>
> task1 -> d1 = 1 (d1 = deadline for task 1)
> task2 -> d2 = 1 (d2 = deadline for task 2)
> task3 -> d3 = 2 (d3 = deadline for task 3)
>
> The greedy algorithm would scedule the tasks as follows
>
> time = 0 -> run task1
> time = 1 -> run task3 (task2 has earlier deadline, but time = d2)
> time = 2 -> run task2 ($10 penalty)
>
>
> How do I prove the greedy solution is the optimal? Can I use
> induction or contradiction? If so How?
> Thanks[/color]