CSCI 4020 Computer Algorithms

Embed Size (px)

Citation preview

  • 7/22/2019 CSCI 4020 Computer Algorithms

    1/6

    CSCI 4020 Computer Algorithms Spring 2011

    Problem Set 3

    Linyun Fu

    March 16, 2014

    Chapter 6, Problem 11

    Suppose youre consulting for a company that manufactures PC equipmentand ships it to distributors all over the country. For each of the next n weeks,they have a projected supply si of equipment (measured in pounds), whichhas to be shipped by an air freight carrier.

    Each weeks supply can be carried by one of two air freight companies, Aor B.

    Company A charges a fixed rate r per pound (so it costs rsi to shipa weeks supply si).

    Company B makes contracts for a fixed amountc per week, independentof the weight. However, contracts with company B must be made Inblocks of four consecutive weeks at a time.

    A schedule, for the PC company, is a choice of air freight Company (A orB) for each of the n weeks, with the restriction that company B, whenever itis chosen, must be chosen for blocks of four contiguous weeks at a time. Thecostof the schedule is the total amount paid to company A and B, according

    to the description above.Give a polynomial-time algorithm that takes a sequence of supply values

    s1, s2,...,sn and returns a scheduleof minimum cost.Example. Suppose r= 1, c= 10, and the sequence of values is

    11, 9, 9, 12, 12, 12, 12, 9, 9, 11.

    1

  • 7/22/2019 CSCI 4020 Computer Algorithms

    2/6

    Then the optimal schedule would be to choose company A for the first three

    weeks, then company B for a block of four consecutive weeks, and thencompany A for the final three weeks.

    Answer

    We define MIN COST(i) to be the lowest cost that could be achieved toship the equipment for the first i weeks, and OP T(i, j) to be the companychosen for the j-th week to achieve MIN COST(i). Since the best schedulefor the first i weeks is either achieved through choosing company A for thei-th week, or choosing company B for the i 3, i 2, i 1 andi-th week, we

    have

    MINCOST(i) = min{MINCOST(i 1) + r si, (1)

    MINCOST(i 4) + 4c} for i 4

    but for i

  • 7/22/2019 CSCI 4020 Computer Algorithms

    3/6

    end

    OPT[i][i] = Aelse

    for j from 1 to i-4 do

    OPT[i][j] = OPT[i-4][j]

    end

    OPT[i][i-3] = OPT[i][i-2] = OPT[i][i-1] = OPT[i][i] = B

    end

    end

    The final answer is stored in OPT[n][1..n], with OPT[n][i] denoting

    the company chosen for the i-th week.It takes constant time to get each MINCOST[i] value, and ioperations tostore the optimal schedule in OPT[i][1..i], so the overall time complexityis O(n2).

    Chapter 6, Problem 15

    On most clear days, a group of your friends in the Astronomy Departmentgets together to plan out the astronomical events theyre going to try ob-serving that night. Well make the following assumptions about the events.

    There are n events, which for simplicity well assume occur in sequenceseparated by exactly one minute each. Thus eventj occurs at minute

    j; if they dont observe this event at exactly minute j, then they missout on it.

    The sky is mapped according to a one-dimensional coordinate system(measured in degrees from some central baseline); event j will be takingplace at coordinate dj, for some integer value dj. The telescope startsat coordinate 0 at minute 0.

    The last event, n

    , is much more important than the others; so it isrequired that they observe event n.

    The Astronomy Department operates a large telescope that can be usedfor viewing these events. Because it is such a complex instrument, it canonly move at a rate of one degree per minute. Thus they do not expect to beable to observe all n events; they just want to observe as many as possible,

    3

  • 7/22/2019 CSCI 4020 Computer Algorithms

    4/6

    limited by the operation of the telescope and the requirement that event n

    must be observed.We say that a subset Sof the events isviewableif it is possible to observe

    each event j Sat its appointed time j, and the telescope has adequatetime (moving at its maximum of one degree per minute) to move betweenconsecutive events in S.

    The problem. Given the coordinates of each of the n events, find aviewable subset of maximum size, subject to the requirement that it shouldcontain event n. Such a solution will be called optimal.

    Example. Suppose the one-dimensional coordinates of the events are asshown here.

    Event 1 2 3 4 5 6 7 8 9Coordinate 1 -4 -1 4 5 -4 6 7 -2

    Then the optimal solution is to observe events 1, 3, 6, 9. Note that thetelescope has time to move from one event in this set to the next, even movingat one degree per minute.

    (a) Show that the following algorithm does not correctly solve this problem,by giving an instance on which it does not return the correct answer.

    Mark all events j with|dn dj|> n j as illegal (asobserving them would prevent you from observing event n)

    Mark all other events as legalInitialize current position to coordinate 0 at minute 0While not at end of event sequence

    Find the earliest legal event j that can be reached withoutexceeding the maximum movement rate of the telescope

    Add j to the set SUpdate current position to be coord.dj at minute j

    EndwhileOutput the set S

    In your example, say what the correct answer is and also what thealgorithm above finds.

    4

  • 7/22/2019 CSCI 4020 Computer Algorithms

    5/6

    (b) Give an efficient algorithm that takes values for the coordinates d1, d2,...,dn

    of the events and returns the sizeof an optimal solution.

    Answer

    Subproblem (a)

    Consider the following set of events:

    Event 1 2 3 4 5Coordinate 0 -1 2 3 2

    The algorithm will mark all the five events as legal and add event 1, 2,and 5 in order to the set S, yielding the answer {1, 2, 5}, but the correctanswer should be {1, 3, 4, 5}.

    Subproblem (b)

    We use OP T(i) to denote the size of the maximum viewable subset of thefirstievents that contains eventi. It can be achieved through observing some

    previous event such that the telescope can catch event i in time thereafter:

    OP T(i) = max1j i thenOPT[i] = 0

    else

    OPT[i] = 1

    for j from 1 to i-1 do

    if OPT[j]=0 and |didj| ij and OPT[j]+1>OPT[i] then

    5

  • 7/22/2019 CSCI 4020 Computer Algorithms

    6/6

    OPT[i] = OPT[j]+1

    endend

    end

    end

    The final answer is OPT[n].Since filling each cell in OPT[1..n] takes O(i) time, the time complexity

    of the algorithm is O(n2).

    6