ps2sol - Massachusetts Institute of Technology...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Massachusetts Institute of Technology Handout 9 6.046J/18.410J: Introduction to Algorithms February 27, 2003 Professors Piotr Indyk and Bruce Tidor Problem Set 2 Solutions Problem 2-1. Matching Nuts and Bolts (a) The problem statement does not describe exactly how the nuts and bolts are speci- fied, so we first need come up with a reasonable representation for the input. Let’s assume the nuts and bolts are provided to us in two arrays N [1 ...n ] and B [1 ...n ], where we are allowed to compare elements across, but not within, these two arrays. We use a divide-and-conquer algorithm very similar to randomized quicksort. The algorithm first performs a partition operation as follows: pick a random nut N [ i ]. Using this nut, rearrange the array of bolts into three groups of elements: first the bolts smaller than N [ i ], then the bolt that matches N [ i ], and finally the bolts larger than N [ i ]. Next, using the bolt that matches N [ i ] we perform a similar partition of the array of nuts. This pair of partitioning operations can easily implemented in Θ( n ) time, and it leaves the nuts and bolts nicely partitioned so that the “pivot” nut and bolt are aligned with each-other and all other nuts and bolts are on the correct side of these pivots — smaller nuts and bolts precede the pivots, and larger nuts and bolts follow the pivots. Our algorithm then finishes by recursively applying itself to the subarrays to the left and right of the pivot position to match these remaining nuts and bolts. We can assume by induction on n that these recursive calls will properly match the remaining bolts. To analyse the running time of our algorithm, we can use the same analysis as that of randomized quicksort. We are performing a partition operation in Θ( n ) time that splits our problem into two subproblems whose sizes are randomly distributed exactly as would be the subproblems resulting from a partition in randomized quicksort. Therefore, applying the analysis from quicksort, the expected running time of our algorithm is Θ( n log n ). Interesting side note: Although devising an e±cient randomized algorithm for this problem is not too di±cult, it appears to be very di±cult to come up with a deter- ministic algorithm with running time better than the trivial bound of O ( n 2 ). This remained an open research question until the mid-to-late 90’s, when a very compli- cated deterministic algorithm with Θ( n log n ) running time was finally discovered. This problem provides a striking example of how randomization can help simplify
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/23/2009 for the course CS CMPT 307 taught by Professor A.bulatov during the Spring '09 term at Simon Fraser.

Page1 / 4

ps2sol - Massachusetts Institute of Technology...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online