Docsity

Homework 4 Solutions for Algorithm Design | ECS 122A, Assignments of Computer Science

Toggle sidebar
bg1
ECS122A: Algorithm Design and Analysis Solutions
Homework Assignment #4
1. Do Exercise 16.1-3
Answer: Let Sbe the set of nactivities.
Solution 1: The “obvious” solution of using Greedy-Activitiy-Selector is to find a maximum-size
set S1of compatible activities from Sfor the first lecture hall, then using it again to find a
maximum-size set S2of compatible activities from S−S1for the second hall, and so on until all
the activities are assigned. This requires Θ(n2) time in the worst case (why?). Note: we will
take this as a correct answer for grading
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 ithat started at si≤tbut won’t finish until fi> t) and halls that
are free at time t.1When 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≤nlecture halls. Let activity ibe the first activity
scheduled in lecture hall m. The reason that iwas 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 mactivites occuring
simultaneously. Therefore any schedule must use at least mlecture halls, so the schedule returned
by the algorithm is optimal.
Run time:
•Sort the 2nactivity-starts/activity-ends events. (In the sorted order, an activity-ending event
should precede an activity-starting event at the same time.) O(nlg n) time for arbitrary
times, possibly O(n) if the times are restricted (e.g. to small integers).
•Process the events in O(n) times: Scan the 2nevents, doing O(1) work for each (moving a
hall from one list to the other and possibly associating an activity with it).
Total: O(n+ time to sort).
1As 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 iand jare compatible.
1
pf3

Partial preview of the text

Download Homework 4 Solutions for Algorithm Design | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

ECS122A: Algorithm Design and Analysis Solutions Homework Assignment #

  1. Do Exercise 16.1- Answer: Let S be the set of n activities. Solution 1: The “obvious” solution of using Greedy-Activitiy-Selector is to find a maximum-size set S 1 of compatible activities from S for the first lecture hall, then using it again to find a maximum-size set S 2 of compatible activities from S − S 1 for the second hall, and so on until all the activities are assigned. This requires Θ(n^2 ) time in the worst case (why?). Note: we will take this as a correct answer for grading

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:

  • Sort the 2n activity-starts/activity-ends events. (In the sorted order, an activity-ending event should precede an activity-starting event at the same time.) O(n lg n) time for arbitrary times, possibly O(n) if the times are restricted (e.g. to small integers).
  • Process the events in O(n) times: Scan the 2n events, doing O(1) work for each (moving a hall from one list to the other and possibly associating an activity with it).

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.

  1. We are given n jobs j 1 , j 2 ,... , jn, with known running time t 1 , t 2 ,... , tn, respectively. We have a single processor. We want to schedule these jobs so as to minimize the avarage completion time? We assume nonpreemptive scheduling, i.e., once a job is started, it must run to completion. Specify a greedy algorithm for the job scheduling problem, and prove the optimality. Answer: The greedy solution is to arrange the jobs by shortest job first. We can show that this yields an optimal schedule. Let the running time {ti} of n jobs be sorted such that ti 1 ≤ ti 2 ≤ ti 3 ≤ · · · ≤ tin. Then the first job finishes in time ti 1 , and the second job finishes after ti 1 + ti 2 , and the third job finishes after ti 1 + ti 2 + ti 3 , and so on. Hence the average completion time is

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.

  1. The 0-1 knapsack problem. Six items {(i, vi, wi)} (vi and wi are the value and weight of item i, resp.) are given as follows:

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.