You are on page 1of 12
Say n boxes arrive m the order 6y,...,6n. Say each box by has a positive weight w;, and the maximum weight each truck ean carry is 1” Ta park the hoxes into. N trucks preserning the order is to assign each box to one of the trucks 1,...,.N’ so that: © No truck is overloaded: the total weight of all boxes in each truck is less or equal to Ww, # The order of arrivals is preserved: if the box by is sent before the box by (1.0. box by is assigned to truck , box bj 18 assigned to trick y, and a < y) then It must be the ease that 6; has arrived to the company earlier than 6; (ie. i <3) We prove that the greedy algorithm uses the fewest possible trucks by showing that it ‘slays ahead” of any oller sulution. Specifically, we consider auy ullier sulution aud show the following. If the greedy algorithm fits boxes by, by,...,bj into the first & trucks, and the other solution fite by,...,b: into the first & trucks, then i i, the greedy algorithin is able at least to fit all the boxes byy1,....0) imto the A truck, and it can potentially fit more. This completes the induction step, the proof of the claim, and hence the proof of optimality of the greedy algorithm. TexT3.193.591 Here 1s a greedy algorithm for this problem. Start at the western end of the road and hegin maving east. until the first moment when there's a house h exactly four miles to the west. We place a base station at this point (if we went any farther east without placing a base station, we wouldn't cover fj). We then delete all the houses covered by this base station, and iterate this process on the remaining, hanses Here’s another way to view this algorithm. For any point on the road, define its position to be the number of miles it Is from the western end. We place the first base station at the easternmost (ie largest) position « with the property that all hanses hetween Mande, will be covered by 51. In general, having placed {5),.-.,i}, we place base station i+ 1 at the Jargest position 541 with the property that all houses between s; and 5:4, will be covered hye; and ey, Let $ — {51,.-., 54} denote the full set of base station positions that our greedy algorithm. places, and let I ty, ..., ln} denote the set ot base station positions in an optimal solution, sorted in increasing order (ie from west to east) We must show that k =m We do this by showing a sense in which our greedy solution $ “stays ahead” of the optimal solution 1’. Specifically, we claim that s; >t for each 7, and prove this by induction. "The claim is true for i = 1, since we go as far as possible to the east before placing the first hase station. Assume now it is true for some value i > 1; this means that our algorithms first é centers {81,...,8:} cover all the houses covered by the first i centers {f1,...,4:}. Asa result, if we add tiy1 to {s1,-.., 8:}, we will not leave any house between s; and fi41 uncovered. But the (7+ 1)* step of the greedy algorithm chooses si41 to be as large as possible subject to the condition of covering all houses between s; and s;41; 80 we have 8:41 > ¢:41. This proves the claim by induction. Finally, if k > m, then {s1,-.-,Sm} fails to cover all houses, But sy > tra, and so {tis-estm} =U’ also fails to cover all houses, a contradiction. 3.070 Let the contestants be numbered 1,....7, and let s;,04,7; denote the swimming, biking, and running times of eontestant i Here is an algorithm to pro ice a schedule: arrange the contestants in order of decreasing ; + rj, and send them out in this order. We claim that this order minimizes the completion time. We prove this hy an exchange argument. Consider any optimal salution, and suppose it does not use this order. Then the optimal solution must contain two contestants i and J so that 7 is sent out directly atter 2, but 6; +75 < 0; rj. We will call such a pair (2,3) f'n innersian Consider the solution obtained hy swapping the orders af i and j In this swapped schedule, j completes earlier than he/she used to. Also, in the swapped schedule, 7 gets out of the pool when J previously got out of the pool; but since bj +r; < 0;-+ 5, 1 finighes sooner in the swapped schedule than j finished in the previaus schedule Hence our swapped schedule does not have @ greater completion time, and so it too is optimal. ‘Continuing in this way, we can eliminate all inversions without increasing the completion time At the end of this process, we will have a schedule in the order produced by our algorithm, whose completion time is no greater than that of the original optimal order we considered. ‘Lhus the order produced by our algorithm must also be optimal Tex983.429.9 In tins problem, you basically have a set of m poms (the account events) and a set of intervals (the “error hars” around the suspicions transactions, ie. [ty — es, t+ 4), and want to know if there is a perfect matching between points and intervals so that each point lies in its corresponding interval). Without loss of generality, let us assume xy < 22S... atj1) in M. Then we have Ge STi Se SG +e; Suter, so in M we can instead match 2, to t and match x;41 to t; to have a new perfect matching M', which agrees with the algorithm. M’ agrees with the output of the greedy algorithm on the first ¢ | 1 points, contradicting our choice of é. ‘To bound the running time, note that if we simply enumerate all unmatched intervals in each iteration of the for loop, it will take O(n) time to find the unmatched one that ends carlicat. There are n itcrations, s0 the algorithm takes O(n2) time. Tex8.628.375, ‘Lo design an optimal solution, we apply @ general techmique that 1s known as Leferred Merge Fmbedding (DMR) hy researchers in the VEST community. It’s a greedy algorithm that works as follows. Let v denote the root, with v! and v" its two children. Let d’ denote the maximum root-to-leal distance over all leaves that are descendants of v’, and let d” denote the mavimum root-ta-leaf distance over alll leaves that are descendants af 1!” Now elf dl > dl", we add d! — dl" to the length of the v-ten” edge and add nothing to the length of the v-to-v! edge. © If a” > d’, we add a” ~ a’ to the length of the v-to-v' edge and add nothing to the length of the v-to-v" edge. ed! =d" we add nothing to the length of either edge helaw ». We now apply this procedure recursively to the subtrees rooted at each of vf and Let T be he complete bimay (rer in Ue problem, We first develop lwo basic facts about the optimal solution, and then use these in an “exchange” argument to prove that the DMI algorithin is optimal {i) Let w be an internal node in T, and let c',€" be the two edges directly below w. Ifa solution adds non-zero length to both e’ and e“, then it 1s not optimal. Proof. Suppose that s' > 0 and 6” > 0 are added to ¢.r and é.» respectively. Let 6 = min(3', 8”), Then the solution which adds 5" 5 and 4" 4 to the lengtho of these edgea must also have zero skew, and uses less total length. m= (ii) Let w be a node in 1’ that is neither the root nor a leaf. Ifa solution increases the length of every path from w to a leaf below w, then the solution is not optimal. Proof. Suppose that 21,...2r¢ are the leaves below w. Consider edges © in the subtree below w with the followmg property: the solution mereases the length of e, and it does not increase the length af any edge an the path from m tae Tet F he the set af all such edges: wwe observe two facts about *. First, for each leaf zr, the first edge on the w=r; path whose Jength has been mcreased must belong to F, (and no other edge on this path can belong to: F); thus there is evactly one edge from Fan every mr; path Serond, |F| > 2, since a path in the left subtree below w shares no edges with a path in the right subtree below w, and yet each contams an edge of £* Toot 2. he the edge entering m from its parent (recall that wig not the raat) Tet & he the minimum amount of length added to any of the edges in I. If we subtract 6 from the length added to each edge in F’, and add 9 to the edge above w, the length of all root-to-leat paths remains the same, and so the tree remaing zernskew Rut we have eubtracted [F|é > 94 from the total length of the tree, and added only 6, so we get a zero skew tree with less total length. We can now prove a somewhat stronger fact than what is asked for. 171 Texl79 (1m) ‘The DM algorithm produces the unique optimal solution: Proof. Consider any other solution, and let v be any node of T at which the solution does not add lengths in the way that DME would. We uac the notation from the problem, and assume without loss of generality that d’ > d’. Suppose the solution adds 4’ to the edge (v,v') and 6" to the edge (v,v") 16" 5! =a a", then it must be that 6" > 0 or clse the solution would do the same thing as DME; in this case, by (i) it is not optimal. If 6"—6’ < d’—d’, then the solution will still have to increase the length of the path from v" to each of its leaves in order to make the tree zcro-skew; 90 by (ii) it is not optimal. Similarly, if 5" 4" > d’—d", then the solution will still have to increase the length of the path from v’ to each of its leaves in order to make the tree zero-skew; so by (ii) it is not optimal. m= (a) ‘The graph on nodes v1,..-,15 with edges (v1, U2), (U1, 05): (02s U5), (Us, M4) and (v4, U5) 1m will return 2. carrespanding ta the path af edges (1,1) is such an example ‘The algari and (v2, v5), while the optimum is 3 using the path (v1, v3), (vg, vs) and (va, v5) (b) ‘The idea is to use dynamic programmmg. ‘Lhe simplest version to think of uses the suhproblems OPT(i] for the length of the longest path from 1 ta, One point ta he careful of is that not all nodes uj necessarily have a path from vy to v;. We will use the value ”— for the U1 [i] value i this case. We use U1 (1) =U as the longest path trom vy to uy has 1 edges Long-path(n) Array M(1...n] Ml) =0 For i M- For all edges (j,i) then if Mi) #-co if M 7 for which P[k] > Pij). From & onward, it 1s simply the longest Inereasing subsequence that. starts at P[t]; in ather wards, this part of the sequence has length OPT), so including Plj], the full sequence has length 1-+ OPT(k). We have thus Justified the following recurrence. OPT(j)—1 IPT (k). GD) 1 RE pg PTO) ‘The values of OPT can be built up in order of decreasing j, in time O(n—J) for iteration j, leading to a total runing time of O(n). The value we want is OPT(1), and the subsequence itself can be found by tracing back through the array of OPT values. Tex219-570.316

You might also like