Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson November 18, 2005 6.046J/18.410J Handout 24 Problem Set 6 Solutions
Problem 6-1. Electronic Billboard You are starting a new Electronic Billboard company called E-Bill. For now, you have just one billboard and you can display one advertisement on the billboard at a time. Your advertising contract with the customers says that if you display their advertisement for one week, then they will pay you, otherwise they won’t. Customers come to you with their advertisements at arbitrary times and offer you some money to display their ad. When they come to you, you must decide immediately whether you will display their advertisement starting immediately. If you are already displaying some other advertisement, you may drop it and lose all the proﬁt due to the dropped ad. The goal is to device an algorithm to maximize your proﬁt. Formally, an advertisement arrives at time and has a proﬁt of . Upon arrival of an ad, the algorithm must decide immediately whether to display it or not. The algorithm receives the proﬁt of ad arriving at time only if it displays the ad from time up to . Assume that only one job arrives at a time. Assume also that if ad completes at time , the billboard can start displaying another ad immediately. ¢ ¤ ¢ ¡ Consider the following algorithm A D -S CHEDULE , where with proﬁt arrives, 1. if no ad is being displayed, then start displaying , ¢ ¤ is some constant. When an ad The ads that an algorithm displays for a full week are completed ads, and the ads that the algorithm starts displaying, but does not display for a full week are discarded ads. The ads that the algorithm does not accept at all are rejected ads. The algorithm earns proﬁt from only the completed ads. (a) Consider the arrival sequence shown in Figure 1. Give the execution trace of algorithm A D -S CHEDULE with , that is, show what actions occur when each ad arrives and which ads are completed, discarded, and rejected. Compute the total proﬁt earned by A D -S CHEDULE . Solution: Ad 1 and 3 are discarded. Ad 2, 5 and 6 are rejected. Ad 4, 7 and 8 are completed with total proﬁt 13.7. (b) Let OPT be the algorithm that knows the sequence of ads in advance and schedules the ads so as to maximize the proﬁt. Which of the ads in Figure 1 are completed, rejected, and discarded by OPT? What is the proﬁt of OPT?
' % # ¤ ¢ ¤ ¤ 2. if an ad of proﬁt don’t accept . is being displayed, then discard if and only if ¨ ¢ ¡ ¢ ¡ ¢ ¡ ¢ ¤ , otherwise 2 Ad
Handout 24: Problem Set 6 Solutions
Arrival Time Proﬁt 1.0 1.0 1.7 1.3 1.8 1.8 2.0 3.0 2.1 2.3 2.5 4.0 3.0 4.5 4.1 6.2 Figure 1: A sample input to A D -S CHEDULE . Solution: Ad 1, 4, 7, and 8 are completed. The others are rejected. The proﬁt of OPT is 14.7. , then A D -S CHEDULE can produce arbitrarily poor proﬁts com(c) Show that if pared with OPT. (Hint: Give a sequence of ads on which OPT does well and A D -S CHEDULE does poorly.) . For , let be the arrival time and Solution: Let be the proﬁt of job . Since and , A D -S CHEDULE discards job and starts job . By using induction on , if jobs, then A D -S CHEDULE only completes the last job giving the proﬁt there are . , the intervals , , etc. are disjoint. Therefore it is Since possible to complete jobs , , , . This means that the proﬁt of OPT is at least
¢ ¤ ! © ¨ # & $ ¢ ¤ ! © Q & ¨ $ ¢ 5 ¡ H 3 ! ¨ § ! © & # ¨ $ ¢ ¨ ¢ ¡ ¡ 3 ¡ " B 3 ¢ ¡ ¢ ¡ 9 3 ¦ ! 1 ¨ @ % ¡ % B 5 % D @ ¡ 9 ¦ § 5 D © § ¦ 5 3 ! ¢ © ! 1 © ¨ ¨ § © # ¢ # 5 ¤ 3 ¢ ¤ ¤ # If , then can be arbitrarily small, hence, OPT can produce arbitrarily good proﬁts compared to A D -S CHEDULE .
© ¨ 3 © # 3 ! © ¨ # . A subset is a solution to the billboard-scheduling Consider a set of ads problem if and only if some algorithm can complete all ads in . That is, no two ads in the solution overlap. The proﬁt of a solution is . For a sequence of ads, suppose that A D -S CHEDULE generates a solution and OPT . For any time , let be the set of ads that A D -S CHEDULE generates the solution completes by (up to and including) time and be the set of ads that A D -S CHEDULE discards by time . Similarly, let be the set of ads that OPT completes by time .
T a ` ` ` a x T ` a x u ` a u x ` T a u ` T ` ¢ T ¤ q a i ¢ ` g # e d ` Y 1 B % % % B B U # e d T c % 3 ! © ¨ # ! © ¨ @ E ¢ ' ¤ ¡ £ ¥ ¢ # ¢ 3 ¤ @ E ¢ Handout 24: Problem Set 6 Solutions
(d) Suppose that is the ad that A D -S CHEDULE is displaying at time . Prove by induction that
% ¢ ¤ £ e i D ¢ ¨ ¥ ¤ " ¢ ¤ £ ¡ i D ¢ ! 3 Solution: The proof is by induction on the ads. After the ﬁrst ad arrives, we have . A D -S CHEDULE starts displaying and it has not completed or discarded . Say an ad arrives at time , while any ads. Therefore, we have A D -S CHEDULE is displaying the ad . (If no ad is being displayed when arrives, then the inductive step is trivial.) For the inductive step, we assume that before the arrival of ad , we have . On arrival of the ad at time , if A D -S CHEDULE decides not to display it, then both and remain unchanged and we trivially prove the inductive step. If A D -S CHEDULE discards and starts displaying , then we have and . Therefore, we have . Done.
" ¤ x ¤ ` ¨ ! ¢ x Y ¤ £ U ¡ i ¢ x g ¢ ! ¤ # £ e i x ¢ g # ¨ ! ¤ ¤ ¤ ¨ ¢ ¨ " ¤ ¢ £ ¢ # ¤ e ¤ £ i £ x ¢ ¡ ¤ ¡ i g i ¢ ¢ g ¨ # g ! ! x ¤ ` # ¤ # ¢ ¤ ¤ £ ¨ ¡ ! ¢ i ¢ ¤ £ g ¡ ! i ¢ g ! # (e) Prove that OPT never needs to discard an ad after it starts displaying it. In other words, where the algorithm sometimes if an optimal algorithm OPT generates a solution discards ads, then there is another algorithm OPT* that never discards an ad and generates the same solution . Solution: OPT* starts only those ads which OPT starts and completes. (f) Show that there exists an injective mapping we have and is injective.)
# " Q ¥ H 3 ¡ § ¥ ¡ ! ¥ ¤ # Q ¥ H 3 ¤ u ` u ` such that for all , . (Remember to show that the mapping
u ` 1 ` ( & u ` $ as follows: For an ad , pick , such that Solution: Deﬁne is the latest job such that . (Note that could be the same as .) It is easy to see , otherwise would be done by the time arrived, and A D -S CHEDULE that would do . Now for this , we have , otherwise A D -S CHEDULE would are non-overlapping. discard and start . Since is in , the intervals Therefore, the function is injective. (g) Let and be the ads that OPT* and A D -S CHEDULE are displaying at time . Prove that, for all , we have
D D EF D u ( ` 1 ! B ¥ ¡ # B # ¥ ¡ 9 u ¥ ` ¤ 1 ¢ ¤ u ` ¥ ¡ " ¢ ¡ ¢ ¡ § ¥ ¡ % GI ¢ ¤ £ e i ¢ ¨ ¢ ¤ £ ¡ i ¢ ¥ ¡ ¨ ¥ ¤ " ¢ ¤ D £ e i ¢ ¨ D ¥ ¤ 4 Handout 24: Problem Set 6 Solutions
Solution: Every ad whose proﬁt is counted on the left hand side is in we have
` ¤ £ u x GI Q ¢ H 3 ¤ D D ¨ Q D ¥ H 3 ¤ EF " ¢ ¤ D D ¨ D ¥ ¤ . Therefore,
u ¢ (h) Consider the potential function
D E F D EF 3 ¡ where A D -S CHEDULE and OPT* are displaying ads and respectively at time . Using parts (d) and (g), prove that this potential function always stays positive. Alternatively, you may prove this part using induction directly, if you wish. Solution: From (g) we infer that
D D EF D EF 3 ¡ u which is by (d)
D EF D EF ! Solution: Since is positive, times the proﬁt of A D -S CHEDULE is at least times the proﬁt of OPT* and we conclude that A D -S CHEDULE is competitive.
3 3 ! ! 3 (i) Conclude that A D -S CHEDULE is
¡ -competitive. " Q D ¥ H 3 ¡ GI B e % ` i % ¢ Y GI GI 1 ¦ D ¢ ¨ U ¥ ¤ ¢ ¤ # £ ¤ e £ B ¨ x i GI D ¢ ¡ ¢ ¢ ` i ¤ ¤ GI ¢ ¨ ¢ £ D £ ¢ ¤ ¡ e x ¨ £ ¤ i i ¢ ¢ £ ¥ Notice that maps to and that . Therefore, and each are in the set is injective. Therefore, we have
" D ¢ ¡ " Q EF ¢ H 3 ¡ ! # ` D ( ! u # EF # " D ¥ # ¡ £ ¡ e ¤ ¡ i D i ¢ i ¢ ! ¢ EF ! ¨ ! ! ¥ ¤ " GI ¥ GI ¢ ¤ GI GI Q ¤ ¥ GI ¢ H £ ¨ ¥ ¤ 3 ¢ ¡ ¤ £ ¤ i ¤ ¨ ¢ e D £ £ ¨ ¢ i e e ¢ ¢ ¤ i i ! £ ¤ ¢ ¢ £ e e i ¨ ¢ i D ¢ Q D EF ¥ H 3 # ¤ ! # ! for all , and . Also notice that - Handout 24: Problem Set 6 Solutions
(j) What is the optimal value of # 5 to minimize the competitive ratio?
& % D $ £ ¡ ¢ Solution: . (k) Optional: Give an example of a sequence of ads where, if A D -S CHEDULE ’s proﬁt is , then OPT’s proﬁt is at least . , let
¡ 1 ! © # ¢ 3 ¡ 1 " " ¦ 1 § © § ¦ Solution: Let
# & $ ¢ By using induction on , A D -S CHEDULE only completes job giving the proﬁt . Notice that the intervals , , etc. are disjoint. Therefore it is . This means that the proﬁt of OPT is at least possible to complete jobs , , ,
# 5 3 ¤ % 1 & $ 5 ! © ¨ ! ¨ # ¡ ¢ ! ¡ © B ¡ ¡ ¨ 9 ¨ ! 1 % ¨ 5 % D # B ¡ & & ¡ $ 9 ¢ 3 ¤ 5 D 5 ! © ¨ (l) Optional: Consider a variation of the billboard-scheduling problem in which ad lengths may vary and proﬁt is proportional to ad length. That is, each ad has a proﬁt and must be displayed for time . Give a competitive analysis of A D -S CHEDULE for this variation of the problem. Solution: We can prove that
¢ ¤ D ¨ ¥ ¤ " ¢ ¤ D ! ¢ ¤ ¢ ¤ in a manner similar to part (d). Observe also that whenever OPT* is working, A D -S CHEDULE is working as well. Since the proﬁt is the same as the length of the job, we have
I D D EF D Therefore, both the inequalities we need to prove that the potential function remains positive are true and we can use the same potential function to prove the same competitive ratio. 3 ! % ! G ¢ ¤ £ e i 5 ¢ 3 £ ¤ e ¨ 3 i ¢ ¢ By taking arbitrarily small, the proﬁt of OPT is for large enough.
1 © 5 3 ¤ ! ! ! ¢ 3 ¤ ¢ 3 ¤ ! © ¨ # Q & $ © ¢ H 3 ¨ ¤ ¤ £ ¡ i ¢ ¢ 3 ¨ ! ¤ ¥ ¤ £ ¨ # ¡ & i ¢ $ ¢ 3 " @ ¤ ¢ E ¢ ¤ e i ¢ ¨ D ¥ @ ¤ E ¨ ¢ it rejects job it discards job because and starts job after which because . ¨ ¢ 3 ¡ ¨ § Q & $ ¢ H 3 ¡ § & 3 ¡ " ¢ 3 ¢ ¡ ! © 1 ¨ § © " ¦ and Since then
3 ¤ . If A D -S CHEDULE runs job # & $ ¢ 3 . For , let be the proﬁt of job . For be the proﬁt of job . ,
" " ¦ d ! ! ! 3 ¢ ! © ¨ # ¢ 3 d be the arrival time and be the arrival time , ¤ 6 Problem 6-2. The cost of restructuring red-black trees. Handout 24: Problem Set 6 Solutions There are four basic operations on red-black trees that modify their structure: node insertions, node deletions, rotations, and color modiﬁcations. We have seen that RB-I NSERT and RB-D ELETE use only rotations, node insertions, and node deletions to maintain the red-black properties, but they may make many more color modiﬁcations. (a) Describe how to construct a red-black tree on nodes such that RB-I NSERT causes color modiﬁcations. Do the same for RB-D ELETE . Solution: For RB-I NSERT , consider a complete red-black tree with an even number of levels in which nodes at odd levels are black and nodes at even levels are red. When a node is inserted as a child of one of the leaves, then color changes will be needed to ﬁx the colors of nodes on the path from the inserted node to the root. For RB-D ELETE , consider a complete red-black tree in which all nodes are black. If a leaf is deleted, then the ”double blackness” will be pushed all the way up to the root, with a color change at each level (case 2 of RB-D ELETE -F IXUP ), for a total of color changes. Although the worst-case number of color modiﬁcations per operation can be logarithmic, we shall prove the following theorem. Theorem 1 Any sequence of RB-I NSERT and RB-D ELETE operations on an initially empty red-black tree causes structural modiﬁcations in the worst case. (b) Examine Figures 13.4, 13.5, and 13.6 in CLRS closely. Some of the cases handled by the main loop of the code of both RB-I NSERT-F IXUP and RB-D ELETE -F IXUP are terminating: once encountered, they cause the loop to terminate after a ﬁxed, constant number of operations. For each of the cases of RB-I NSERT-F IXUP and RB-D ELETE -F IXUP , specify which are terminating and which are not. Solution: All cases except for case 1 of RB-I NSERT-F IXUP and case 2 of RB-D ELETE -F IXUP are terminating. We shall ﬁrst analyze the structural modiﬁcations when only insertions are performed. Let be a red-black tree, and let be the number of red nodes in . Assume that unit of potential can pay for the structural modiﬁcations performed by any of the three cases of RB-I NSERT-F IXUP .
§ ¡ § ¦ ¦ ¤ ¢ ¡ ¤ ¢ ¡ ¤ ¢ ¡ (c) Let
§ ¡ be the result of applying Case 1 of RB-I NSERT-F IXUP to .
¡ ¡ § . Argue that Solution: Case 1 of RB-I NSERT-F IXUP reduces the number of red nodes by one, a . fact that can be seen in Figure 13.4 in CLRS. Hence, § § § § § ¡ Handout 24: Problem Set 6 Solutions
(d) Node insertion into a red-black tree using RB-I NSERT can be broken down into three parts. List the structural modiﬁcations and potential changes resulting from T REE -I NSERT , from nonterminating cases of RB-I NSERT-F IXUP , and from terminating cases of RB-I NSERT-F IXUP . Solution: T REE -I NSERT causes one node insertion and a unit increase in potential. The nonterminating case of RB-I NSERT-F IXUP (Case 1) makes three color changes and decreases the potential by one. The terminating cases of RB-I NSERT-F IXUP (Cases 2 and 3) cause one rotation each and do not affect the potential. (e) Using part (d), argue that the amortized number of structural modiﬁcations (with re. spect to ) of RB-I NSERT is Solution: The number of structural modiﬁcations and amount of potential change resulting from T REE -I NSERT and the terminating cases of RB-I NSERT-F IXUP are constant, so the amortized cost of these parts are constant. The nonterminating case of RB-I NSERT-F IXUP may repeat up to times, but its amortized cost is 0, since by our assumption the unit decrease in the potential pays for the structural modiﬁcations needed. Therefore, the worst-case amortized cost of RB-I NSERT is constant.
! 1 ¤ ¢ ! ¡ 7 We now wish to prove the theorem for both insertions and deletions. Deﬁne
¡ ¡ ¡ ¡ ¡ if if if if
§ is red, is black and has no red children, is black and has one red child, is black and has two red children. Let the potential of a red-black tree and let be the tree that results from applying any nonterminating case of RB-I NSERT-F IXUP or RB-D ELETE -F IXUP to . (f) Show that for all nonterminating cases of RB-I NSERT-F IXUP . Argue that the amortized number of structural modiﬁcations (with respect to ) per. formed by RB-I NSERT-F IXUP is Solution: From Figure 13.5 of CLRS, we see that Case 1 of RB-I NSERT-F IXUP makes the following changes to the tree:
¡ ! ! § " ! § ¡ § ¡ § B ! ¡ ¨ i D § ! § ¦ ¦ £¤ ¤ ¤ ¤ ¤ ¤¦ ¥ ! ¡ be deﬁned as 8 Handout 24: Problem Set 6 Solutions
Changes a black node with two red children to a red node (node ), resulting in . a potential change of Changes a red node to a black node with one red child (node in the top diagram; node in the bottom diagram), resulting in no potential change. Changes a red node to a black node with no red children (node ), resulting in a potential change of . T ¡ The total change in potential is , which pays for the structural modiﬁcations performed, and thus the amortized cost of Case 1 (nonterminating case) is . Because the terminating cases of RB-I NSERT-F IXUP cause constant structural changes and is based solely on node color and the numconstant change in potential, since ber of color changes caused by termintaing cases is constant. The amortized cost of the terminating cases is at most constant. Hence, the overall amortized cost of RB-I NSERT-F IXUP is constant. for all nonterminating cases of RB-D ELETE -F IXUP . (g) Show that Argue that the amortized number of structural modiﬁcations (with respect to ) performed by RB-D ELETE -F IXUP is . Solution: Figure 13.6 of CLRS shows that Case 2 of RB-D ELETE -F IXUP makes the following changes to the tree: Changes a black node with no red children to a red node (node ), resulting in a . potential change of If is red, then it loses a black child, with no effect on potential. If is black, then it goes from having no red children to having one red child, resulting in a potential change of .
¡ ¡ ¡ ¡ ! ! § " ! § ¦ ! ¡ ¡ ¡ The total change in potential is either or , depending on the color of . In either case, one unit of potential pays for the structural modiﬁcations performed, and thus the amortized cost of Case 2 (nonterminating case) is at most . Because the terminating cases of RB-D ELETE cause constant structural changes and constant change in potenis based solely on node color and the number of color changes caused tial, since by termintaing cases is constant. The amortized cost of the terminating cases is at most constant. Hence, the overall amortized cost of RB-D ELETE -F IXUP is constant.
¦ ! ¡ (h) Complete the proof of Theorem 1. Solution: Since the amortized cost of each operation is bounded above by a constant, the actual number of structural modiﬁcations for any sequence of RB-I NSERT and RB-D ELETE operations on an initially empty red-black tree cause structural modiﬁcations in the worst case.
! ¦ ¦ ...
View Full Document
- Fall '05
- Red-black tree, IXUP, RB-D ELETE -F, RB-I NSERT-F IXUP, ELETE -F IXUP