psmakeupsol - 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 Erik Demaine and Shafi Goldwasser December 2, 2002 6.046J/18.410J Handout 28 Problem Set ¡ Solutions Problem -1. Optimal scheduling (a) For (1) the schedule in which task 1 runs first, followed by task 2: and average completion time For (2) the schedule in which task 2 runs first, followed by task 1: and average completion time (b) Run tasks in increasing order of processing time. This can be done by sorting the elements using heap sort or merge sort and then scheduling them in the order of increasing processing times. This algorithm takes . This algorithm uses a greedy strategy. It is shown to be optimal as follows: This cost can also be expressed as . Note that is included times, appears times, etc. As a result, should have the shortest processing time, then , etc. Otherwise, you could cut and paste in a shorter processing time and produce a faster algorithm. As a result, the greedy property holds and our algorithm is correct. (c) This problem also exhibits the greedy property which can be exploited by running tasks in increasing order of remaining processing time. Use a priority queue which prioritizes based on the amount of time remaining. Each time a new task comes up, insert it into the queue and if it would take less time to do that task then the one you are on, do the shorter task. Each time you finish a task, the next task you should do is the one with the least remaining time until completion. The priority queue can be maintained in time. This algorithm minimizes the average completion time and the proof is similar to the one in the previous part. If we do not schedule using the greedy algorithm based on remaining processing time, then we will be able to swap two time slots which would then improve the sum of the completion times and thus result in a contradiction. For example, assume you have two tasks at time , where task has processing time remaining and has processing time remaining where . Assume for the purposes of contradicction that the optimal answer has task running before task . If is done before then and . The average completion time is . However if were done before , then and . The average completion time is now which is less the average completion time for the “optimal” solution since . As a result, the task with the lowest time remaining should be done first. d e — ¥k 3b¢ ™ ˜ ef hg™ ˜ ™ e — e ¥¦ij¢ ™ — —  g qo g m ˜ n  f ¥k l¢ ef r$™ ™ — ¥i tj¢ ( Q US Q y–Tq# P e d d d  onm pg f g f ˜  •§ 4“ ¨”Q  ’§ Q ƒ…D‚ƒ©„9ƒh‚€©9€y§ x ( † §   §  £ §#  (  §  £ §#  £ ¥ aYX cb`¤¢ ( Q US Q WVTR# P £ ©§ £ w £ y§ ‘ `( w  FF‡ § ‡‡ ‡‡ ©‰©„ˆ FF‡  §  £ §# w v d sss g p d g i d g e qhg uutrqhfRhRfd 8 [email protected]¥716)&4645¥312)0(¢'&%$¥  £ ¢# ! "¥ ©¢ § £§ ¥ 8 [email protected]"¥716)GF45¥312)0(¢'&%$¥  £ ¢# ! ¥£§ § ¥£ "E©D¦¤¢ 8 ¥§ ¥ C¨¢  ¥£§ ¥£ ¦©¨¦¤¢ We assume the probabilities that each bridge will fail are independent. The probability that a path with edges of failure probabilities will not fail is . Therefore, the probability of failure is . We want to minimize for the nodes in the graph, so we can do something akin to a shortest paths algorithm, which attempts to minimize the failure probability. In fact, we can reduce the problem to a classical shortest path problem with is equivalent to minimizing nonnegative weights by observing that minimizing . Setting the weight of an edge to be , we observe that a path of minimum failure probability is a path of minimum total weight (with respect to the weights ). We could thus simply apply Dijkstra with the weights . Since we haven’t really discussed whether our computational model allows the computation of logarithms we can simply modify Dijkstra to work directly on the ’s instead of the ’s but perform exactly the same updates as Dijkstra’s algorithm would do on the ’s. Correctness will simply follow from Dijkstra’s correctness. Our variant of Dijkstra’s algorithm will maintain estimates on the failure probabilities which we will relax. Here is the pseudocode. I NITIALIZE -NF-S INGLE -S OURCE ( 1 for each vertex 2 do 3 4 ‘ Running time and correctness follow directly from Dijkstra’s algorithm as stated earlier by using ( l( S£ D#  ( 0( S£ D#  D 1 ‘ bSx c Dx Sx “ `# ( ‘ A•W “ b“ 4 1 ‘ A•W 4 4# bD•W “ b“ 4 f ‘ bS•W x x “ b# ( ‘ 4 4# NF-R ELAX ( 1 if 2 then 3 ) ( ¤£ ' ( £ S£ D# Dx d W V T ‘ A•rXGUS ( ( ¤£ ' 8# ) N OT-FALL ( 1 Initialize-NF-Single-Source( 2 3 4 while 5 do E XTRACT-M IN 6 7 for each vertex 8 do NF-R ELAX ) ) “ b# FF‡ j§ Db# 0t§ “ `# 4 ‡‡ ( “ 4 (£ 4 ¥( ¡ #U  ’6S ¡ ¢ “ © ¨ " #! ¥ §U 6S ¡ ¨ “ ¥  %    & ©  ¡ ¨ ¦ §§ £ “4 ‡‡ £ ££ FF‡ ¥§ ¤§   ©   § a'x `YS @T QRDPIG0 120 HF 1ED 67C3 B#8 4¥ A'[email protected] 98 x 1 64 753 20 1 67453 1 ‘ bSx ˜ § 1 ‘ AS•W x 4 ( )£  £ ' (i qh§ £ S£ D ‘ “ `’6S 4# U  " 1 ‘ ( tW x ¡ Problem -2. Paintball routes ¡ 2 Handout 28: Problem Set Solutions £ “ ti (¦ ¤§§ ¦  $ (a) We first note that if we could maintain a data structure that supports I NSERT , D ELETE and F IND -M IN in ( being the number of elements in the data structure), then we would be done. This is because we could then store all the elements visible through the window in this data-structure. mins can be computed by F IND -M IN , and sliding the window could be performed by a D ELETE followed by I NSERT . These operations can be performed by a balanced BST like AVL-trees or Red-Black time on a balanced BST. F IND -M IN can trees. I NSERT and D ELETE take be performed in time on a BST by going along the left spline of the tree till we can go no more. To start with, the balanced BST can be built for the first position of the window in time . And then computing mins followed by a sliding involves the 3 operations F IND -M IN , D ELETE and I NSERT , all of which can be done in time. Hence the total time to compute the array mins in the manner specified is (b) The worst case time taken by S LIDE is when the outgoing element is green. In this case the sub-routine C OLOR -R ED is executed which takes time, because it has to color all the elements in the window red. The other operations in S LIDE take time. Hence, the worst case time taken to compute the value of a single element of the array mins is . (c) We observe that each element of the entire array is colored green and then red at most once. Moreover, the C OLOR -R ED function takes time for each element that it colors red. Since each element is colored red at most once, the sum of the times taken by all the calls to C OLOR -R ED is at most . Each time S LIDE is called, its operations (other than the call to C OLOR -R ED ) take time. It can easily be seen that all other operations performed by C OMPUTE -M INS take only time on the whole. Hence the total time taken by the entire algorithm is . Thus the amortized time taken to compute the minimum for each set of consecutive elements is which is since . ¡ (4 Fb# P ( ¡# Problem -4. Weird Multiplication Q " We build three tables of boolean values, where and . The value will be if it is possible to parenthesize in such a way that the value of the resulting expression is , and otherwise. The initial conditions are iff . , where , is obtained from the multiplication table. The recurrence for, say, We note that there are three entries equal to in the table; specifically , and . So @ 89 (  6 7A%# k 7£ g  '¥  $¥  )¢ i™ d £ g Dw ¨¦ ©w §¥ " ˜ ¦ 3 ¢# i E54( %6¦c 4  (¥ " #4 (Q q# P ¥ ƒ£`Bi ( #i ¢  ˜ ‘ ux  '¥ ¢ £ !§… £ ¥ i p¤¦ £  ¢ A 2 k (Q R# 01 @ CB( 89 k ™ ¤¤@ i ™ @@ P ( P (4 &`# P US Q ¢–7R# ¢6 %# k 7£ g (Q q# P ¦ 3(#i E54j¡G¦c V P ¥ ¦( Q 5" US ¢–q# d ( ( Q US EVR# P ˜ ‘ ux  US ¢–¡# P &$¤RC€( ( 4  “ Q#  ˜ i p¤¦ £   2 k " &4 01 ‘(  i rƒ¡# k  x @ A97( 8 P 1)Q  6yT " ¢6 R# k 7£ g Q ( Q US E–q# P ( ( Q US yE–q# P ( i A%# k ‰ ¡# 4 ¥ ƒ£# ( ¦ 3 ( # i E54A%G¦ c  P US ¢VC£# ( US ¢VR#  (4 Fb# P P P ( i ¡# k  i p¤¦ £  2 k 01 (4 &b# P ¡ Problem -3. Computing partial mins  &  the relationship between ¡ Handout 28: Problem Set § Solutions and . 3 ¥ A¡2k c ( # i The recurrences for and are similar. It is easy to turn this into an algorithm which computes the entire contents of the three tables. The value we are looking for is . ( £ A%# w … ( † ¡ 4 ( R2k c ¢# i (#i j¡2k  Handout 28: Problem Set Q R# P Solutions ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online