Pquiz2-sol

Pquiz2-sol - Introduction to Algorithms Massachusetts...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Piotr Indyk and Charles E. Leiserson December 1, 2004 6.046J/18.410J Quiz 2 Solutions Quiz 2 Solutions 108 26-30 0 0 2 3 Median = 74.5 Mean = 76.4 Std. Dev. = 22.3 18 16 14 12 Count 22 10 8 6 4 2 0 30-39 40-49 50-59 60-69 70-79 80-89 Score 90-99 100-109 110-119 120-129 0 90-99 6.046J/18.410J Quiz 2 Solutions 2 Problem 1. A Dynamic Set ¦ ¦ E XTRACT-O LDEST : Remove and return the oldest item the one inserted least recently.) ¡ © ¨§ ¡ .   § that can perform any sequence of  ¡   §  ¡ ¦ Design a data structure for  ¡ : Return (but do not remove) the item  ¡ F IND -M AX . The dynamic set . (The oldest item is for which key ¤ ¥¡ ¢   I NSERT into ¡ : Insert item has an associated key key ¤¡ ¥£¢ A dynamic set contains items, where each item supports the following operations: is maximal. operations efficiently. Solution: The goal of the problem is to come up with an efficient implementation of a queue, augmented with the additional F IND -M AX operation. Thus, it makes sense take a queue implementation 1 (e.g., a linked-list) and extend it so that it supports F IND -M AX as well. For starters, we assume that all elements are distinct. This assumption is not crucial, but simplifies the description and the analysis of the algorithms. Note that, in any case, we can break ties by replacing each element by , where is the time when was inserted, and using lexicographic ordering.  ¡ # %! #!© $"¨§ ¡ ¡ We will see that there are, essentially, three basic ways of supporting F IND -M AX :  1.Scan all elements in the list to find the maximum. This gives total worst-case running time. This solution was worth up to 8 points. A slightly better implementation maintains a pointer to the maximum element; when the element is deleted, the E XTRACT-O LDEST procedure searches for the new maximum. Although this implementation still has worst-case time bound, it gives a much better time bound 2 in the average case . Thus, this solution was worth up to 10 points. Also, this was by far the most popular solution. ) ('& §  ) 0'& § 2.Augment the queue with a search tree (description below). In this way, all three operations can be performed in time. This gives total worst-case running time. This solution was worth up to 15 points.   87@9(21 6 43 § 875021 6 43§ 3.Augment the queue with a queue-stack, that is, a hybrid of a queue and a stack. This is the best solution - it gives total time in the worst-case (although individual operations could take time). Description below.  (21 §  0BA § A remark: our “augmentation” is actually very non-invasive. That is, we do not modify the code or structures of the queue; instead, we add a new data structure that operates in parallel to the queue. 1 One can also use a search tree with elements sorted by insertion time, but this is an overkill, since the elements are not deleted in a random order. 2 E.g., when all insertions preceed all deletions, and the order of deletions and insertions is random 6.046J/18.410J Quiz 2 Solutions 3 This approach has an advantage of being simple and modular. E.g., there is no need to get into the guts of the queue code. However, modifying the queue code works fine as well. The queue with a tree. Consider any implementation of a queue, with operations I NSERT and E XTRACT-O LDEST . In addition, we will also maintain a search data structure . The data structure should support three operations in time: I NSERT , D ELETE and F IND -M AX . E.g., one can use a 2-3-tree, a Red-Black tree, or even a heap 3 . C D  87@(21 6 43§ Each element that is present in is maintained in both and (although space savings can be obtained by storing only once, and using pointers). Specifically, we implement insertions and extraction to as follows. D C ¡ ¡ Insert(Q,x): Insert(L,x) Insert(T,x) End Extract-Oldest(Q): x:=Extract-Oldest(L) Delete(T,x) Return x End Therefore, we can find the maximum as follows: Find-Maximum(Q): Return Find-Max(T) End 8759021 6 43 § time. This gives   Clearly, all three operations run in total running time bound. 875021 6 43§ The queue with a queue-stack. We will use a queue and a queue-stack . The latter supports I NSERT and E XTRACT-O LDEST (as in the queue), as well as E XTRACT-Y OUNGEST (as in the stack). For simplicity, we also assume functions O LDEST and YOUNGEST , which return the same elements as E XTRACT-O LDEST and E XTRACT-Y OUNGEST , but without removing them from . All operations are performed in constant time. E FC E C C The queue contains all elements currently in . The queue-stack contains only some of the elements of , selected in the following way. Let be the (current) elements of (and ), the oldest element first. Let denote the elements of , the oldest element first. The choice of ’s is as follows. The element is the maximum element of . For each , let be the E GC C C T HQQQ I U"SSRPH a hg e fdb c E C YVQQQ I `"SSXWV I WV a `V 3 Although in this case one needs to argue how to delete an arbitrary element in the heap, since the data structure described in CLRS, p. 127, does not enable arbitrary deletions, at least as stated. 6.046J/18.410J Quiz 2 Solutions iV $t8rpH s qi , i.e., such that . For e wvb u in , a `V a `V index of 4 is defined as the maximum element among . T H Q Q Q € xq i ƒ"SS‚yrhH In other words: as long as is in , it is the maximum element. However, when is deleted, then becomes the new maximum. And so on. Notice that the sequence is decreasing. We call it the maximal sequence of . I hV I SV QQ SSQ V©I `„pV V ) ) THQQQ I ƒ†…SR¥H  . E 8‡§ C Clearly, given , we can answer F IND -M AX in constant time: simply return , i.e., O LDEST To maintain and (that is, the sequences and ), we do the following. I hV E 8C YV©QQQ©I ``‰SSS„…V E TH©QQQ©I ƒ`„SSS„ˆH C C Insert(Q,x): Insert(L,x) While key[Youngest(L’)]<key[x] Extract-Youngest(L’) Insert(L’,x) End Extract-Oldest(Q): x:=Extract-Oldest(L) If x=Oldest(L’) then Extract-Oldest(Q) Return x End The correctness of E XTRACT-O LDEST is immediate from the definition of the maximal sequence. The correctness of I NSERT follows from the observation that the maximum sequence for contains only the elements of the maximum sequence for for which are . ¡©T HQ Q Q I "0ƒ†S…RH ¡ ‘c THQQQ I ƒ†S…‚H  To show that operations take time, we first observe that both F IND -M AX and E XTRACT-O LDEST take time in the worst-case. Moreover, the total cost of all I NSERT operations is bounded from above by the total number of operations I NSERT into (which is at most ) plus the total number of operations E XTRACT-YOUNGEST from . But, the latter is at most the total number of insertions into , which again is at most . Thus, the total time per operations is . Note that the same analysis can be done using the accounting or potential method. (’1 §  e§ $’1 E C E  C E (’1 § C Problem 2. Great Minds Need Coffee It is a beautiful autumn morning, and Professor Indyk has decided to walk from his apartment to MIT where he will give a lecture for 6.046. To prepare for this trip, the professor has gone online and downloaded a map of the roads and intersections in the Boston area. For each road connecting two intersections, the map lists the length of the road in meters. (Recall that in the Boston area, an arbitrarily large number of roads can meet at a single intersection, e.g., Davis Square.) ” “ ¤ ”¢ …—–• meters without sitting Like many great theorists, Professor Indyk cannot walk more than down and assuaging his caffeine addiction. (This is especially true before 9:30 A . M .) Fortunately, the Moonbucks Coffee Company has recently opened a large number of coffee shops throughout ˜˜˜ 77We 6.046J/18.410J Quiz 2 Solutions be the set of g fYV©QQQ p``…S…S© intersections hosting Moonbucks coffee V©IVd `„Spef™ s ) the Boston area. Let shops. 5 Thus, Professor Indyk is looking for a route from his apartment (located at one given intersection) to the Stata Center (located at another given intersection) in which he never travels more than meters without passing through an intersection with a coffee shop. Help the professor get to lecture on time by designing an efficient algorithm to find the shortest acceptable route from his apartment to the Stata Center. ˜˜˜ 7We Solution: Summary: We model the city of Boston as a graph, , with the intersections as vertices, the roads as edges and weights on the edges representing the length of the road. We call an intersection “caffeinated” if Piotr is able to acquire coffee at the intersection. For the purposes of this problem, caffeinated intersections (Note that coffee is available at each of the Moonbucks, Boston has as well as Piotr’s apartment and the Stata center). Our goal is to find the shortest path from Piotr’s apartment to the Stata center which never travels more than 1000 meters without passing through a caffeinated intersection. To do this, we create a new graph, , which has a vertex for each of the caffeinated. For each pair vertices in , if the shortest path between the corresponding vertices in , denoted , is less than 1000 meters, then there is an edge in between whose weight is . Since all street lengths in the city of Boston are non-negative, we construct the graph by running executions of Djikstra’s algorithm on (one execution starting at each of the caffeinated intersections). We then perform one more execution of Djikstra’s algorithm on to find the shortest path between Piotr’s apartment and the Stata center in (which we will show is the shortest acceptable path from Piotr’s apartment). “ h g j 'g i E h  E lh  g©b %"0§ g©b %"0§ j kg i  E –h g©b§ %"0Rm h  g©b§ %"0‚m h E j ng i h j pog i E h E Bh Algorithm: We assume that the input to our algorithm is a graph with vertices and edges represented as adjacency lists. We also assume that the first vertices (numbered 1 through ) contain Moonbucks coffee shops and that vertex is Piotr’s apartment and vertex is the Stata center. (If the vertices are not presented in this order, and we are merely given a list of vertices with coffee shops, then we can easily relabel the vertices in time). We now run Djikstra’s algorithm times where the th execution computes the single-source shortest paths from vertex to vertices . We use a Fibonacci Heap to implement the priority queue used by Djikstra’s algorithm, so the running time of each execution of Djikstra’s algorithm is . Therefore, the time for executions of Djikstra’s algorithm is . Now that we know the length of the shortest Path between all pairs of caffeinated vertices, we can construct the graph as described in the summary above. Finally, we run Djikstra’s algorithm one last time to find the shortest path from vertex to vertex in graph . Since graph has that most edges, this final step takes that most time. Therefore, the total running time of our algorithm is . Since is , this simplifies to . “ g g j p’g i e q’g i  " g§ 6 43 ‡¥7@9(21 § b j qrg i j nts„SSSe i g©QQQ© b   j vtg i ni   “§ 6 43 0¥7@u021 “§ wpi g E j ntg i e x–g i  )  w‡21 g§ g§ ‡21 )   g “§ 6 43 (ˆ75vw‡21 “ g§ h ) )  g yBwyi i g g§ r’1 E Bh “§ 6 43 (ˆ75vw‡21 “ g§  E Bh  {zi g “§ 6 43 (ˆ75vw‡21 “ g§ Correctness: We show correctness we first argue that every path from Piotr’s house (vertex ) ) in corresponds to an acceptable path in (with the same to the Stata center (vertex length). We then argue that any shortest acceptable path from vertex to vertex in e }|g i h j ~2g i E lh e w2g i j ~’g i 6.046J/18.410J Quiz 2 Solutions 6 corresponds to a path (of the same length) in . From this we conclude that by finding the shortest path from vertex to vertex in our algorithm (1) Finds an acceptable path in the original graph and (2) Finds the shortest such path in . E h h E h j }9g i e 9g i h h Consider any path in . Each edge in this path corresponds to a path of length at most 1000 meters between caffeinated vertices in . Therefore, by replacing each edge in by the corresponding path in we can construct a path of the same length in which passes through a caffeinated vertex at least once every 1000 meters. E Bh E –€ E l€ h h € h Now consider a shortest acceptable path in from vertex to . Let be the caffeinated intersections that passes through (in the order that it passes through them). Note that for each , must travel on a shortest path from to (because every subpath of a shortest path must also be a shortest path). Additionally, for each , the subpath from to must be at most 1000 meters because is acceptable. Therefore, for each , the length of the subpath from to is the same as the weight on the edge from to in . Thus if the path consisting of the vertices in has the same length as the original path . s 8„`…S…Ss„ƒ’Rr§ …‚©QQQ©I‚ s e ig j tRg i e ’Rg i h €  € I ‹a RŒ`‚ j ƒ'g i a …‚ € Š f–ˆ‡e ‰ b † Š le ‰ b † € a „‚ I ‹a RŒ`‚ I ‹a Rys‚  E Bh a „‚ a „‚ Š ƒvpte ‰ b † j ƒ‰`‰SSS`hpŽn‡§ i g s …‚©QQQ©I‚ s e i g E B€ E Bh I ‹a RŒs‚ h Grading notes: Many students attempted to use a single execution of a greedy algorithm similar to Djikstra’s algorithm find the shortest acceptable path from Piotr’s apartment to the Stata center. Such algorithms were very efficient because they visited each vertex only once, but were generally incorrect. These solutions generally received around 8 points provided that they demonstrated a solid understanding of Djikstra’s algorithm and traditional shortest path problems. The principal problem with visiting each vertex only once is as follows. Assume there is a short Path from from the source to vertex that reaches vertex after having traveled 999 meters since the last coffee shop. Additionally assume that there is a longer path from the source to vertex that reaches vertex after having traveled only 100 meters since the last coffee shop. A Djikstra-like greedy algorithm will find the former path but taking the latter path may be necessary to reach the Stata center while satisfying the caffeine requirement. That is, you may want to take a longer path to vertex in order to arrive there with more caffeine in your blood stream. (Some students even observed that the optimal path might visit visit the same intersection twice. For instance, one might first walk to vertex , then walk to nearby vertex to buy coffee, then back to vertex and onward to the Stata center. Clearly, any algorithm which visits each vertex only once will fail to find such a path). b b b b b b g b Some students used Floyd-Warshall to construct instead of using repeated executions of Djikstra’s algorithm. However, since all edge weights in this problem are non-negative (Boston has no negative length streets) it is more efficient to use repeated applications of Djikstra’s algorithm. Good solutions using Floyd-Warshall instead of Djikstra still generally received at least 20 points. E h Some creative students observed that Boston (having relatively few overpasses) is nearly planar and hence graph is sparse. This allows for a tighter analysis of the above algorithm. If one additionally assumes that no two intersections are within 1 meter of each other then one can conclude that the number of intersections within 1000 meters of a given point is a (somewhat large) constant. This allows for a very (asymptotically) efficient algorithm. Finally, two students observed that an h 6.046J/18.410J Quiz 2 Solutions 7 asymptotically more efficient solution can be found if one assumes that all street lengths are an integer number of meters. Problem 3. Radio 107.9 FM The Eccentric Motors (EM) corporation is about to roll out their 2005 year car model when they discover that many of their automobiles have faulty radio tuners that cannot access the highest frequencies in the FM spectrum. In particular, these defective radios cannot receive radio stations broadcast at 107.9 FM. Replacing the defective radios would delay the roll out of the 2005 model, which would cost the company millions of dollars in lost sales. Fortunately, not every city has a 107.9 FM station. Therefore, EM has decided to send the defective cars only to those dealerships that are not within range of any 107.9 FM station. Eccentric Motors has given you a list of their dealerships and has asked you to locate the dealerships that are out of range of all 107.9 FM stations. The list is organized so that dealership is miles east and miles north of St. Louis, Missouri. (Negative values of and denote miles west and south, respectively.) You have contacted the FCC, which has given you the location of the radio towers that broadcast at 107.9 FM. Tower is located miles east and miles north of St. Louis, Missouri, and it broadcasts at a signal strength that allows the signal to be received within a radius of miles from the tower. FCC regulations guarantee that no point is able to receive signals from two different 107.9 FM broadcast towers. ‘ – b  ¤b ‡£¢ ‘ t ¤ b¢ (’U#  ¤b (’¢ ¤ ƒg {“D ¢# g ¤ b¢ ‡£{# g ‘ ¤ 7g ¢ D ¤ ƒg hXD ¢” Design an efficient algorithm to locate the EM dealerships that are out of range of all 107.9 FM broadcast towers. Solution: Since the problems involves points and disks (that is, dealerships and tower ranges), it is a computational geometry problem. The most natural geometric technique that can be used for this problem is, the sweep-line approach. This technique was introduced in lecture to find segment intersection using a line-sweep approach. This algorithm runs in time. In this method, 4 the data is swept from left to right (West to East) by a vertical line. The sweep line keeps a data structure that maintains some information about objects (i.e., disks) that intersect the line. When a sweep-line hits a point, the data structure should be able to answer (quickly) if this point belongs to one of the disks intersected by the sweep line, or not. This suffices, since if a point belongs to a disk , then the sweep line positioned at must intersect .  " —• ˜– ‘tr§ i g ‘–‡"’1 i g§§  ™© "0§ ¡   ¡ The details are as follows. First, we need to construct a trajectory, or schedule, for the sweep-line. That is, the sequence of important points the sweep-line encounters, sorted by the -coordinate. The points are: ¡ ‘  D©¤ "‡ƒg h‚›P7g {0§ ¢”D š¤ ¢#D ‘ ¤ 7g ¢ D©¤ "r7g h‚}{7g {0§ ¢”D i¤ ¢#D The rightmost points of the disks, that is, the points ¤ 7g ¢ ¦ ¦ 4  The leftmost points of the disks, that is, the points A few people used a horizontal line moving top-down The main effect of this approach was confusing the graders; as such, this approach should be avoided as much as possible. 6.046J/18.410J Quiz 2 Solutions  ‘ t  ¤b ‡£¢ © ¤ b¢ r(’ƒ# § The dealership positions 8 . ¦ As in lecture and the book, we assume that all -coordinates are different. In particular, this implies that the radiae of the towers are non-zero (and therefore, the leftmost and rightmost points of each disks do not coincide). ¡ The data structure associated with the sweep-line is a search tree data structure . The disks in the data structure are ordered by their -coordinates (defined below). Again, we assume (for simplicity) that all such coordinates are distinct. The data structure supports insertions and deletion of elements, as well as search for a predecessor and successor of a given . All three operations can be performed in time. œ ™ ™  g o(P7@(’1 i § 6 43§  . The data structure holds Denote the sweep-line by . If is at position , we denote it by one coordinate per disk intersecting . Specifically, it is the coordinate of the center of the disk. This is done because of the following nice observation. Consider any two disjoint disks and , with centers and . Consider any “sweep-line position” , such that intersects both and . Then, if and only if all points in have -coordinates lower than all points in . ¡§ 0C  ¡ C ™  C C ™ E  C  ¡ E ™ ©E ¡ U†¨P¨§ ™© †0§ ¡   ™ E ž ‡C  E  encountered by the sweep-line is handled as follows: (with a key ) to from  Ÿ ¦ ¦ ¦ If is a dealership: œ , remove   If is the rightmost point of a disk  , add ™ ž ‡C If is the leftmost point of a disk œ ™© †0q Ÿ ¡§ s  E ™ z2™ ‰ Given the above, each new point Ÿ Ÿ   –Find , centered at , which is the predecessor of in –Find , centered at , which is the successor of in . –Check if is contained in either or . If yes, report it as “covered”. If not, report it as “not covered”. œ ™ E E ™ ©E ¡ {†5P0§  œ  ™ E U†¨E P0§ E™©E ¡  EE  EE E Ÿ Correctness. Clearly, any point reported as “covered” is indeed covered. We need to show the converse. That is, if a point is covered by , then must be either a predecessor or a sucessor of . We do it as follows. By symmetry, assume . For contradiction, assume there is another disk with center where . Clearly, does not contain , since there can be only one such disk. Then, consider the sweep line (at position ). From the earlier observation (applied to and a point ), it follows that all points in preceed (w.r.t. coordinate). In the same way, we get that all points in preceed all points in . But, this implies that all points in preceed - a contradiction.   E EE  ™ †© E ¡§ 0¡s E ™© †0¡‡Ÿ ¡§ s ™ nR¥™ ‰E   ™  ™ ¢kE {¢k{™ ‰ E™ ‰ E E {†˜E P¨§ E ™©E ¡ ¡§ 0£ˆC C s EE Ÿ  Ÿ    E EE   ¡§ ¨ž C E  Ÿ E  – g 0¥7@(’1 i § 6 43§  –  ¡§ 0›ž C  time. Processing each event takes events, we get a total time of .  " ¡§ 0›ž C ™   g l(ˆ753 i §64  g zo0P753 i §64  g l0"21 i §§ g z'0"21 i §§  Ÿ ¡  ¡§ 0¤ž C EE ¡ ¨§ E E Running time. Sorting takes time. Since there are g z'021 i § Variations. Quite a few people gave the brute force algorithm checking every tower with every dealership with running time . A few people observed that the problem becomes easier, if the disks are replaced by (smallest) squares (or -gons, for small ) containing the disks. In  gQ ¦¥021 § ! ! 6.046J/18.410J Quiz 2 Solutions 9  particular, for the case of squares, the interval formed by intersecting with a square is always the same, i.e., does not depend on ; thus, one can use interval trees to maintain the intersections. This introduces false “coverings” - a point can belong to the square but not to the disk. The solutions to this problem can be classified into the following categories: ¡§ 0C ¡ ¦ Claiming that a square (or a -gon) is a “good enough” approximation of a disk. This might be true in some cases, but is not true in general. 5 In any case, the problem statement assumed exact solution. ¦ Whenever a square covers a point , explicitely checking if is covered by the disk . This gives a correct algorithm. However, its running time could be as big as . This can happen since the fact that two disks do not intersect, does NOT imply that their squares do not intersect. See example in the figure ?? ! ¨ ¡ § Ÿ  Ÿ § ) ©0BA § Other variations included using grids, buckets or hashing. These approaches have the same problems as before: approximation or potentially large running time, which depends on the spread of the points - the ratio of largest to smallest distances. A few people gave an efficient, and exact divide and conquer algorithm. Problem 4. Isolating Kryptonite Professor Luthor has obtained a meteorite fragment that contains high levels of a useful Kryptonite isotope. The fragment is a perfect cube centimeters on each side. The professor would like to 5 However, this approach sometimes leads to very efficient approximation algorithms. E.g., see slide 6, Lecture 20 in http://theory.csail.mit.edu/ indyk/6.838/ . ª 6.046J/18.410J Quiz 2 Solutions 10 distribute this fragment to secret laboratories for study in the hopes that the Kryptonite isotope can be synthesized in large quantities. The Kryptonite is not evenly distributed throughout the meteorite, however. The density of the Kryptonite at coordinate is given by , that is, gives the amount of isotope in a cubic centimeter with origin . “    g© g©b `7%"0§ g© g©b s7%–(§   g© g©b `7%"0§  g© g©b s7%"0§ Professor Luthor has obtained access to a machine that will cut across the meteorite fragment at a given position, thereby dividing it into exactly two pieces. The machine only cuts at right angles (parallel to the -, -, or -axis) and on centimeter boundaries. Thus, each cut results in two rectangular parallelepipeds with integral dimensions. « ™ ¡ Design an efficient algorithm for dividing the meteorite into pieces so as to maximize the minimum amount of Kryptonite contained in any piece. (Partial credit will be given for solving the analogous two-dimensional problem.) “ Clarification: The machine can only be used to cut a single piece into two smaller pieces. That is, after cutting the meteorite into two pieces, you must separate the pieces; you can then perform different cuts on each piece. Also, you cannot glue pieces back together. Solution:  This problem can be solved using dynamic programming in time. The idea is to solve sub-problems of the following form: given a rectangular parallelepiped sub-section (or “piece”) of the meteorite, how can it be divided into smaller pieces so as to maximize the minimum amount of Kryptonite in any piece? We demonstrate that the problem exhibits optimal substructure, allowing each of sub-problems to be solved in time. We also give special attention to the base case ( ), yielding an algorithm for calculating the amount of Kryptonite in a given piece as a function of the amounts in smaller pieces. ¬ ) “§ (’1 g   021 “§ ­ 021 §  e§ $’1 e ®’g s Many students developed a greedy algorithm for this problem. A greedy approach does not result in the optimal series of cuts; refer to the end of this discussion for counter-examples and grading guidelines. Terminology  A piece is a rectangular parallelepiped sub-section of the meteorite with and , where , opposite corners at coordinates , and . All coordinates are integers.  ‰ –U¢˜ I™ † ¯† ¡ q–“¯˜ ‰ I¡ † « †© ) « `©  ) ™ "© ) ¡ ¨§ ) ™ "© ¡© I «© I ™© I ¡§ "s„`sU†s0w2Ÿ s ) ) I«©I ™©I „†`{"„0§ ¡ ) † « ‘knt˜ ‰ I« † † ) ™ ) Optimal Substructure Consider the problem of dividing a piece into smaller pieces. Because each cut of the machine divides a piece into exactly two smaller pieces, the optimal solution must start by cutting the piece into two pieces and (unless , in which case the problem is trivial). Consider that the optimal solution further divides into pieces and into pieces. Let denote the problem of dividing into pieces, denote the sub-problem of dividing into pieces, and denote the sub-problem of dividing into pieces. g Ÿ e e g s Ÿ I Ÿ ) € g '|g š Ÿ g I “Ÿ ) € g I “Ÿ I F€ ) g –g š Ÿ ) g Ÿ Ÿ 6.046J/18.410J Quiz 2 Solutions 11 The optimal substructure is as follows: the value of the overall solution for can never be improved by substituting a non-optimal solution for sub-problem or (or both). The proof is by contradiction. Suppose that the overall solution for did improve after changing the solution of from O PT (the optimal solution) to N ON -O PT (a non-optimal solution). In other words, the minimum amount of Kryptonite over all pieces in has increased; since we did not change the solution of , the minimum piece in N ON -O PT must be larger than the minimum piece in O PT . This contradicts the optimality of O PT . The same argument applies to . For the case in which both and are non-optimal, one can change the solution to and then the solution to ; neither change improves the overall solution. € € I °€ ) €   I G(§ €  I ‡§ € I € €  I F‡§ € € € )  I G‡§ € I ‡§ € ) I °€ € I F€ ) € ) Note that this argument is somewhat tricky because it is not the case that both sub-problems must be solved optimally in the overall optimal solution. In particular, the sub-problem that does not contain the piece with the minimum amount of Kryptonite can adopt a non-optimal solution, so long as the overall minimum is not affected. Nonetheless, the optimal substructure property shown above is enough to guarantee the correctness of our algorithm, as it shows that optimal solutions to sub-problems will always result in an optimal overall solution (i.e., one of the solutions that maximizes the minimum amount of Kryptonite in the pieces). Dynamic Programming Formulation The optimal substructure exposes the following sub-problems: for every piece in the meteorite, and for every , what is the best way to divide into pieces? The solution is comprised of optimal sub-solutions once we determine two things: 1) the best position to cut into two pieces, and 2) the best allocation of the desired pieces between the two sides of the cut. We determine these values by exhaustive search: for a given sub-problem, try every cut position and every allocation of pieces to the two sides, keeping track of the configuration that maximizes the minimum value of Kryptonite. Ÿ g Ÿ f ²†„SSS{s7Uƒ±g “©QQQ© j©ed  Ÿ g Let denote the maximum value of the minimum-Kryptonite piece across all ways of dividing into pieces. Expanding , we observe that the overall answer to the problem will be given by . Each sub-problem can be evaluated ¤g© {s“Ÿ t³ ¢ ¤g {`© ) « `© ) ™ †© ¡©I«©I ™©I "„„`†{†‰¡ tp{s“Ÿ t³ ¢ ³ s ¤g© ¢ ) ¤ 2†¥"¥†µ†´`´†´—–³ “© © © © ˜ © ˜ © ˜¢ g Ÿ 6.046J/18.410J Quiz 2 Solutions 12 as follows: ¶· · · · ™ †© ) ¡©I«©I ™©I ¡ "„„`†{†„“¨§ ) S UM -K RYPTONITE « `© :  e ®og s · · · if · · · · ) · · · · · : º »š   I« „±š « ‡§  I™ {±š ) ™ (§ I¡ ¤š ) ¡§ ¨~2g u · · if · · · · · ) · · · · · · · otherwise: · · · · ¶· · · · · · · · ¶ ¤g 7%© « †© ™©‚©I«©I ™©I †h`s„†s{†„¡ t³ ¢ ) ) ¤g 7–s© š g « †© ™ †© ) · · · · ¸ ¹ · ÉÈ 5o¼ · · · · ¿½ p¾¼ · · · · · · # À Å Á ÄI ÃÆ ÂI ¨¥Ç„# %R‹ € ¡ © I « © I ™ © ‚¢ –s„`sU†p’t³ ) · · · · · ¸ ) · · · · · s¤g R{`© · · · · · · · · · «©‚ `h`© ) ¡ © I « © I ™ © I ¡¢ –„„`s{"„£–³ ) ¤g 7–s© š g « `© ™ †© ) ÉÈ 5o¼ ‘ ÄI ¨¥Ã Æ ) ¡ © I « © ‚ © I ¡¢ –„„`p†„£–³ ¿½ p¾¼ ÂI %R‹ € · · · ¸ · · ¿½ p¾¼ ‘ Á À Å · · · · · · · · · · ÄI à  Á À ¨¥‰Y %I `i ) · · · · · · · · · · · · · · · · · · · · · · · ¶ ©¤ g© ‚ ‡ƒËp`© ™ †© ¡ © I « © I ™ © I ¡¢ "`„`s{"„£–³ ) ¤g ƒ–s© š g ) « `© ) ™ "© ) · ¹ · · · · · ¸ · ÉÈ @¾¼ · · · · ¿½ h’¼ ĨI¥Ã Æ Ê Â%IR‹ € Ê ÁÀ Å ¡ © ‚ © I ™ © I ¡¢ "h`s{"„£–³ ) ¡©I«©I ™©I "s„``{†„¡ t³ ¢ ) · ¸ ¹ ) · · ¶ ©¤ g ‡7%© ™ †© · · « `© ) · · · · · · · · · · ·¹ · · · · ·¹ The above equation has three cases. In the first case, and we sum the Kryptonite contained in the piece; we use a special procedure to do the sum efficiently (see below). In the second case, it is impossible to obtain the desired number of pieces because the volume of the parallelepiped is too small. Thus, we return negative infinity to ensure that this configuration will never be selected as the optimal (largest) value. e w g s In the third case, we evaluate all possible cuts that could be performed on the parallelepiped. The variable ranges over possible allocations of pieces between one side of the cut and the other. The variable ranges over possible locations of the cut on the -, -, and -axis. For each cut, we evaluate the minimum amount of Kryptonite in the two resulting pieces. We select the value where this minimum is maximized. g « ™ ¡ ‚ The S UM -K RYPTONITE procedure calculates the total amount of Kryptonite contained in the parallelepiped. Instead of summing over all locations (which would require time), it sums over two smaller parallelepipeds (which requires only time if the sub-problems have already been solved).  Ì ©021 §  e§ Ç21 6.046J/18.410J Quiz 2 Solutions 13  « `© ™ "© ) ¤e …S© « `© ™ †© ) ¤e …S© « `© ) ™ †© ) ) « `© ) ¡ "© ) ) « `© ) ™ © I ¡¢ "„£–»{……© ³ i¤e ) ™ †© ¡¢ £–»{‰S© ³ i¤e ) ¡©I« "`„`© ) ¤e …S© ¡©I«©I ™ –„„``U†© ) ™©e †7š ) ) ™ "© ) e ‘š ¡ vs Í Ž¡ I ) ¡ © I « © I ™ © I ¡¢ "„„`†{†‰£–³ e ‘š ™ vs Í 9{™ I ) ™ †© ) ¡©I«©I ™©I ¡ "s„`sU†„“¨§ ) ¡ © I « © I ™ © I ¡¢ "„„``{†„£–³ ) « `© ) ) ) «©e `7š ) « © I ™ © I ¡¢ `s{"„£–»U‰S7š ³ i¤e©e S UM -K RYPTONITE 1 if then return 2 elseif then return 3 elseif then return 4 else return ¡ © I « © I ™ © I ¡¢ "s„``{†„£–³ ) e š ) « us Í 9« I ¤I «©I ™©I ¡ DŽ`†{†„£¢ The S UM -K RYPTONITE procedure first tries to split the piece along the -axis. However, if the piece is only 1 cm wide, then it tries to split along the -axis, and subsequently the -axis. If the piece is a single cubic centimeter, then it returns the density as given by . ¡ « ™  Order of Evaluation A bottom-up dynamic programming algorithm solves sub-problems in a specific order to guarantee that solutions are ready before they are needed by other sub-problems. In our case, each subproblem corresponds to an element of , and we solve it by evaluating the equation for given previously. There are several legal orderings. We consider pieces in order of their size; for example, pieces, then all pieces, then all pieces, and so we first evaluate all on. Our toplevel algorithm is as follows: ³ ³ Ï ¤}–e Î e Î j y–}–e Î e Î e ±»e Î e Î   F IND -C UTS 1 Initialize all entries of to 2 for to 3 do for to 4 do for to 5 do for to 6 do for to 7 do for to 8 do for to 9 do evaluate © ‡†¥0§ “© º dš ³ e e e ¡ –± Ð š ™ t} Ð š « '} Ð š “ e ˜  'tÐ ™  'Ð « ˜ ˜  r–Ð ¡  ¡  '™  «  og ¤g© « Us´'`‚tB†–»t"w`R"£–³ Ð i «© ™ Ð i ™© ¡ Ð i ¡© «© ™© ¡¢ This iteration order is correct because, in S UM -K RYPTONITE and the equation for , a given piece is evaluated only in terms of strictly smaller pieces, i.e., in terms of pieces which have lesser or equal width, height, and depth. Because the loops on , , and correspond to the width, height, and depth, respectively, they impose a lexicographic ordering and guarantee that strictly smaller pieces are evaluated before strictly larger pieces. ³ « 'Ð ™ tÐ ¡ –Ð The iteration order, in combination with the optimal substructure presented previously, forms the basis of the algorithm’s correctness. Every piece is evaluated for the best cut and the global optimum is selected. Constructing the Cuts . To reconstruct the actual seThe value of the optimal solution is given by quence of cuts corresponding to this solution, one can maintain an auxiliary array (with the ™ ¤ 2"¥†µ†¥†w`´`´’t³ “© © © © ˜ © ˜ © ˜¢ 6.046J/18.410J Quiz 2 Solutions 14 same dimensions as ) that tracks the values of and corresponding to the maximum for a given sub-problem. One can trace back through the matrix in the standard fashion to construct the cuts. ‚ g ³ ™ Runtime Analysis  There are distinct sub-problems, which is evident from the dimensions of (as well as the nested loops in F IND -C UTS ). For each sub-problem, we have to evaluate an element of . In the base case, this evaluation is , as S UM -K RYPTONITE performs a constant amount of work. ³ ­  021 “§ ³  e§ Ç21 However, the general case requires more work. First, we vary the allocation of cuts between pieces ; this requires iterations. On each iteration, we evaluate all possible cut from to positions on the -, -, and -axis. Since there are three dimensions with at most positions on each, we consider cuts per allocation of pieces. Thus, the total amount of time to evaluate a sub-problem is .  “§ (21 e tѓ š «   02qs § 1  ™ e ¡ ‡’1 ϧ (’1 “§ “§ (’1 to solve, so the total runtime is ¬ )  (21 “§ sub-problems and each requires   ­ X (’1 “§ There are A Faster Algorithm!  A few students achieved a runtime of by improving the search for the best cut position. They observed that the maximum value is a “mountain function” with respect to the position of the cut: if moving the cut in one direction worsens the score, then moving it further in that direction is fruitless, as the score will only continue to go downhill. @‚R ) 63 ­ “§ (’1  Thus, rather than evaluating cut positions for a given sub-problem, one can do a binary search on intervals of cuts and reduce the runtime to . Four points within each interval are sampled; if all scores are distinct, then three of these points (including an endpoint) must form a monotonic slope and the endpoint can be eliminated. The algorithm recurses on the interval spanned by the remaining three points, maintaining the invariant that the optimal score (the “peak” of the mountain) is always contained within the current interval. The runtime of the search is ; by the Master Theorem, . governed by the recurrence (21 §  85021 63§   8@(’us 63§ 1  0–D §  e§ $2»i 1  ÓÒ „‡s ϧ D (D § However, it appears that this algorithm requires the scores to be distinct. For instance, if all scores except for one are equal, then the algorithm must resort to exhaustive search. Nonetheless, there are many cases in which this algorithm would perform better than the one detailed previously. Greedy Approaches More than one-quarter of the class took a greedy approach to this problem. A common solution was a top-down algorithm that finds the most Kryptonite-heavy piece and cuts it into two pieces so as to minimize the difference in Kryptonite between sides of the cut. While this algorithm is efficient and might serve as a good heuristic, it does not guarantee an optimal division of the meteorite. A counter-example is shown in Figure 1. About 5 students suggested a bottom-up greedy algorithm, in which each cubic centimeter of the meteorite is initially considered as a distinct piece. At every step of the algorithm, the piece with the minimum amount of Kryptonite is fused with its lowest-Kryptonite neighbor (assuming some . 6.046J/18.410J Quiz 2 Solutions m=3 greedy (top-down) max of min = 1 2 1 1 2 (a) 2 1 1 2 2 1 1 2 15 optimal max of min = 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 Figure 1: Counter-example for top-down greedy strategy. Given the input in (a) with m=3, the greedy algorithm produces the solution in (b) while the optimal solution is shown in (c). 2 1 1 2 (b ) (a) (c) (b ) greedy (bottom-up) max of min = 2 m=2 2 1 1 2 optimal max of min = 3 2 1 1 2 (c) Figure 2: Counter-example for bottom-up greedy strategy. Given the input in (a) with m=2, the greedy algorithm produces the solution in (b) while the optimal solution is shown in (c). geometrical constraints are satisfied). A counter-example illustrating the deficiency of this strategy appears in Figure 2. Other variants on greedy algorithms included 1) searching for a cut where the amount of Kryptonite on a side can be expressed as , where is an integer, 2) keeping the cut points constant but varying which piece is recursively cut, and 3) trying to shave off of the meteorite with each cut. All of these strategies have similar failure cases. In all, a greedy strategy fails because it does not consider all of the possibilities. A local choice might separate (resp. combine) two pieces that need to be combined (resp. separated) for a cascade of cuts to offer further benefits on the other side of the meteorite. g “Ò „ˆg “Ò phe Grading Notes There were several brute-force solutions that were correct but ran in exponential time. They received at most 7 points. A smaller number of solutions were on the right track algorithmically but the analysis showed exponential runtime. These received at most 12 points. Many solutions offered a greedy algorithm, which received at most 10 points. Extra points were awarded for acknowledging that greedy is sub-optimal, or for using simple dynamic programming to keep track of partial sums. Solutions with incomplete or incorrect runtime analysis received lower scores. Dynamic programming algorithms that were buggy or incomplete received at most 18 points. More than a quarter of the class designed the same algorithm as described here, and were generally awarded 19-25 points. Several solutions neglected to factor in the cost of solving the sub-problems; this resulted in a score around 21 points. Also, many students used a brute-force algorithm to sum the total Kryptonite content in a given parallelepiped, thereby leading to an runtime bottleneck that was usually unaccounted for. Solutions missing an analysis of the base case received at most 23 points.  Ô (21 § 6.046J/18.410J Quiz 2 Solutions 16 Problem 5. Finding Coolmail Users Macrohard Corporation has decided to start a free email service known as Coolmail. Coolmail users choose their favorite -digit number as their user ID and get the email address @coolmail.com. For instance, with a user might chose 314159265 as her user ID and be given the address 314159265@coolmail.com. Since many people find it hard to remember -digit numbers, Coolmail provides the following helpful service. Whenever an email is sent to an address @coolmail.com, where is not a valid user ID, Coolmail finds a nearest valid user ID , that is, an that minimizes , and sends the reply, “Sorry, but @coolmail.com is not a valid address. Did you mean to type @coolmail.com?” ¡ g Õ tg s ¡ g ™ ™ ™ Ö ©’d¡ Ö ™ š ¡ ¡ ¡ It has come to the attention of the opportunistic Professor Ralsky that many Coolmail users are paying too much interest on their mortgages. He would therefore like to send every Coolmail user a message informing him or her that “You can ref1nance your m0rtgage at the amazing rate of only 3.49%!!” Unfortunately, the professor does not know the email addresses of all Coolmail users. Let be the user ID’s of the Coolmail users, where is unknown to the professor. He has hired you as an MIT student intern to design an efficient algorithm to compute, with the help of Coolmail’s automated reply service, the email addresses of all the Coolmail users. Minimize the worst-case number of emails required by your algorithm. Y ˜e Wp'¥»ˆWWp°nt˜ ‰ Ø¡ ‰××× ‰ I¡ † Solution: We will assume that ; the helpful email service doesn’t make sense if there aren’t any users. We also assume that helpful email responses from Coolmail arrive in a bounded amount of time; otherwise we’ll never know if an email that we’ve sent was delivered successfully. e ~› c In the interest of clarity, we are not going to worry about constant factors in the number of emails sent. (We’re even going to actually send more than one email to the same address.) The idea of our solution is to maintain a set of non-empty regions of identity space that are guaranteed to contain at least one user. Given a non-empty region, we’ll query the region by sending an email to the median(s) to find a new user, and then update the set of regions on the basis of the query result. This will result in an algorithm that computes a list of all valid addresses in time, and sends emails.  ('& §  ('& §  Let NEAREST be a function that returns a nearest valid address to , for a half-integral value (that is, ). If is an integer, then the function emails , waits long enough for a helpful response to be returned, and then returns if the email was delivered successfully, and if the response “did you mean @coolmail?” was received. (Note that if there is a tie—i.e., there are two values and that are equidistant from —then NEAREST will break the tie arbitrarily.) If is non-integral, then the function returns whichever of NEAREST and NEAREST is closer to , or one arbitrarily if there is a tie. ¡ ¡ ¡ ¨§ ¡ ¡ ™ ¡ ™   ¡ 0§  ÚQ ˜ ¦´ G0§ i ¡ Ù upj  ¡ ¡ ™ ™ ÚQ ˜ Ûw’|¨§ š ¡ ¡ ¡  Let NONEMPTY ? be a boolean function taking two values that returns true if and only if there is some valid address such that . This function does the following: on input , NEAREST . Return true if and only if . Correctness of NONEMPTY ? is let immediate by inspection. V ’H ‰ V© `¥H V© H `¥‡§ V n²‘¾H † ¡ † ¤ V © H¢ †`¥’}2™   ¡ j 7Ò  V ±‡–§ i H§ sÜ ¦2™ 6.046J/18.410J Quiz 2 Solutions 17 be a function that, given two indices and and a stack . H  §©V© H sW`¥‡§  V© H †P(§ § ¤V© H †`¥’¢ § V := empty list. R := empty stack; push (0,10ˆk-1) onto R. V Finally, let PUSH - IF - NON - EMPTY , pushes onto if NONEMPTY ? // V will hold all valid addresses. // R holds non-empty regions. while (R is non-empty) { [a,b] := pop(R); median := (a+b)/2; valid := NEAREST(median); add valid to V. if valid < median push-if-non-empty(a,valid-1,R) push-if-non-empty(2*median-valid,b,R) elseif valid == median push-if-non-empty(a,median-1,R) push-if-non-empty(median+1,b,R) else (valid > median) push-if-non-empty(a,2*median-valid,R) push-if-non-empty(valid+1,b,R) } return V.   Correctness. We first note that, for a non-empty region , the value of NEAREST must be a valid email address in . This ensures that every range that we attempt to push onto the stack has . j 7Ò V 9("§ i H§ ¤V© H `†P’¢ ¤V© H †`¥—¢ ¤V© H †`¥’¢ V n’H † We claim the following loop invariant: all valid addresses are contained either in or in a region in , all regions in are disjoint from each other and from , and no element appears twice in . This invariant is satisfied at initialization by assumption. We now show that the invariant is maintained by an iteration of the loop. First note that in the case when , the invariant is trivially maintained. For the other cases, we prove maintenance for ; the final case is analogous. Observe that in this case the effect on is (i) possibly to delete regions that are empty (according to PUSH - IF - NON - EMPTY , which does not falsify the invariant by the correctness of NONEMPTY ?), and (ii) to delete the range . But (ii) cannot falsify the invariant either: since is the closest valid address to , there cannot be any valid addresses closer to than . Note that the resulting regions are disjoint subregions of , and are also disjoint from . This fact implies that the invariant is maintained. œ œ œ Ý œ æ ßá âå ("’äl(ypÞ ã s s âáà ß æ ßá âå ("’¯¤(ypÞ ã ‰ âáà ß œ ynÝ ç  • e ‡GËb š m Hè w ("’s`7¤F(yp$§ š æ ßá âå ã× j©e i âáà ß Þ æ ßá âå ("’ã âáà ß (ypÞ âáà ß (ypÞ æ ßá âå ("’ã âáà ß (ypÞ ¤V© H †`¥’¢ For termination, note that the number of addresses covered by is decreasing by at least one in every iteration. Thus the loop eventually terminates. When it does, is empty, and by the above loop invariant, we have that contains all valid email addresses. Furthermore, since we only add Ý Ý œ 6.046J/18.410J Quiz 2 Solutions 18  an address returned by NEAREST , the resulting contains only valid email addresses. Since all elements of are unique, the list contains exactly the valid addresses. œ § œ œ Running time. First, observe that NEAREST is the only place where our algorithm sends email. Now, note than an iteration of the loop makes one call to NEAREST and two to PUSH - IF - NON - EMPTY (and thus two to NONEMPTY ?, and thus two more to NEAREST ). Thus each iteration of the loop causes three calls to NEAREST , each of which may result in two emails being sent. Thus there are at most six emails sent per loop iteration. Since in each iteration, we find exactly one a new address to add to , there are precisely iterations. Thus the total number of emails sent is . Furthermore, since everything in the loop is a contant-time operation, the total running time is as well. œ  ('& §  0'& § Grading notes. The general breakdown of grading was that about half of the credit was given for getting a correct algorithm that ran in time, and half of the credit was given for correctness proofs and running-time analysis.  0'& §  A lot of people came up with the correct -time algorithms (similar to the one above), but people had significantly more trouble with rigorous proofs of correctness and running time. For example, in the divide-and-conquer recursive version of the above algorithm, you make recursive calls on two subintervals of size at most half of the size of the original interval. Since , people said that they has an algorithm linear in the number of users. This is totally wrong: the true recurrence is in terms of the size of the interval, not in terms of the number -time running time. of users. If done correctly, this analysis yields a ('& §  † 0D §   (2ud‘i § 1 s e  Y jÒ 7(pj § D ˜e§ W$’1 Scores for the running-time analysis (6.5 points) range from zero for no attempt whatsoever, to about 2–3 points for a hand-wavy solution that asserted facts about why the algorithm was efficient but didn’t prove any of them, through 6 points for a rigorous, correct proof. Scores for correctness (6.5 points) were similar. Almost all submitted solutions required a rigorous inductive proof, and very few gave one. Significant credit (about four points, depending on the details provided) was given for people who argued for correctness without using induction. ...
View Full Document

Ask a homework question - tutors are online