

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


ECS122A: Algorithm Design and Analysis Solutions Homework Assignment #
Solution 2: There is a better algorithm. The general idea is to go through the activities in order of start time, assigning each to any hall that is available at that time. To do this, move through the set of events consisting of activities starting and activities finishing, in order of event time. Maintain two lists of lecture halls. Halls that are busy at the current event-time t (because they have been assigned an activity i that started at si ≤ t but won’t finish until fi > t) and halls that are free at time t. 1 When t = the start time of some activity, assign that activity to a free hall and move the hall from the free list to the busy list. When t = the finish time of some activity, move the acitivity’s hall from the busy list to the free list. (The activity is certainly in some hall, because the event times are processed in order and activity must have started before its finish time t, hence must have been assigned to a hall.) To avoid using more halls than necessary, always pick a hall that has already had an activity assigned to it, if possible, before picking a never-used hall. (This can be done by always working at the front of the free-halls list – putting freed halls onto the front of the list and taking halls from the front of the list – so that a new hall doesn’t come to the front and get chosen if there are previously-used halls.) This guarantees that the algorithm uses as few lecture halls as possible: The algorithm will terminate with a schedule requiring m ≤ n lecture halls. Let activity i be the first activity scheduled in lecture hall m. The reason that i was put in the mth lecture hall is that the first m − 1 lecture halls were busy at time si. So at this time there are m activites occuring simultaneously. Therefore any schedule must use at least m lecture halls, so the schedule returned by the algorithm is optimal. Run time:
Total: O(n + time to sort). (^1) As in the activity-selection problem in section 16.1 we are assuming that activity time intervals are half open, i.e., that if si ≥ fj , activities i and j are compatible.
C =
n
[ti 1 + (ti 1 + ti 2 ) + (ti 1 + ti 2 + ti 3 ) + · · ·]
=
n
[n ti 1 + (n − 1)ti 2 + (n − 2)ti 3 + · · ·]
n
∑^ n
k=
(n − k + 1)tik = (1 +
n
∑^ n
k=
tik −
n
∑^ n
k=
k · tik.
Note that the first term is independent of the job ordering, so only the second term affects the average completion time. Let tiℓ < tim for some ℓ and m. Then it is easy to see that by swapping tiℓ and tim , the second term decreases, increasing the total average completion cost. Therefore, C is optimal.
i vi wi 1 40 100 2 35 50 3 18 45 4 4 20 5 10 10 6 2 5
The total weight W = 100.
(a) Find the greedy solutions by using following possibilities: (i) Greedy by value, i.e., at each step select from the remaining items the one with the highest value (ii) Greedy by weight, i.e., at each step select from the remaining items the one with the least weight. (iii) Greedy by the value per pound, i.e., at each step select from the remaining items with the largest ratio vi/wi. (b) Compute the solution by dynamic programming (attach a copy of your program) (c) Briefly comment your findings.