Question

This is a probabilistic analysis of algorithms question. Consider the following randomized version of quick_select() which...

This is a probabilistic analysis of algorithms question.

Consider the following randomized version of quick_select() which returns the kth smallest element of an unsorted array:

        random_partition(A[lo..hi], rindex)
        {
                swap(A[hi], A[rindex]);
                i = lo;
                for (j = lo; j < hi; ++j)
                        if (A[j] < A[hi])
                                swap(A[i++], A[j]);
                swap(A[i], A[hi]);
                return i;
        }

        random_quick_select(A[lo..hi], k)
        {
                if (lo == hi)
                        return A[lo];
                p = random_partition(A[lo..hi], rand(lo, hi));
                prank = p - lo + 1;
                if (prank == k)
                        return A[p];
                if (prank > k)
                        return random_quick_select(A, lo, p-1, k);
                else
                        return random_quick_select(A, p+1, hi, k-prank);
        }

Show that the expected number of array element comparisons by random_quick_select() is O(n).

Homework Answers

Answer #1

So Basically, the algorithm can be summarised in below

Given array A of size n and integer k ≤ n,
   1. Pick a pivot element p at random from A.
   2. Split A into subarrays LESS and GREATER by comparing each element to p as in
           Quicksort. While we are at it, count the number L of elements going in to LESS.
   3. (a) If L = k − 1, then output p.
           (b) If L > k − 1, output QuickSelect(LESS, k).
           (c) If L < k − 1, output QuickSelect(GREATER, k − L − 1)

Proof for Expected number of comparisions
-----------------------------------------------------------
Let T(n, k) denote the expected time to find the kth smallest in an array of size n,
and let T(n) = maxk T(n, k).

We will show that T(n) < 4n.

First of all, it takes n−1 comparisons to split into the array into two pieces in Step 2.

These pieces are equally likely to have size 0 and n − 1, or 1 and n − 2, or 2 and n − 3,
and so on up to n − 1 and 0.

The piece we recurse on will depend on k, but since we are only giving an upper bound, we
can imagine that we always recurse on the larger piece.

Therefore we have:

(.......summation is over T(i) , the summation symbol below has shifted slightly above the line , assume to be inline)
T(n) ≤ (n − 1) + (2/n) T(i)
= (n − 1) + avg [T(n/2), . . . , T(n − 1)] .

We can solve this using the “guess and check” method based on our intuition above.

Assume inductively that T(i) ≤ 4i for i < n.

Then,
T(n) ≤ (n − 1) + avg [4(n/2), 4(n/2 + 1), . . . , 4(n − 1)]
       ≤ (n − 1) + 4(3n/4)
       < 4n,
and we have verified our guess

hence the Expected Number of comparisions turns out to be O(n).

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Describe recursive algorithms for the following variants of the text segmentation problem. Assume that you have...
Describe recursive algorithms for the following variants of the text segmentation problem. Assume that you have a subroutine IsWord that takes an array of characters as input and returns True if and only if that string is a “word”. (a) Given an array A[1 .. n] of characters, compute the number of partitions of A into words. For example, given the string ARTISTOIL, your algorithm should return 2, for the partitions ARTIST·OIL and ART·IS·TOIL. Do this by appropriately modifying the...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Restricted structures such as stack and queue are fast, but they do not support access in...
Restricted structures such as stack and queue are fast, but they do not support access in the key field mode. Group of answer choices True False Big O analysis evaluates an algorithm based on its _________ performance. Group of answer choices A. average-case B. best-case C. worst-case Which of the following algorithms is the fastest in speed? Group of answer choices A. Polynomial time algorithm B. Linear time algorithm C. Exponential time algorithm The following code gives an implementation of...
CRYPTOGRAPHY PROBABILITY DISTRIBUTION Bad Shuffles Consider the following card shuffling algorithm. There are three cards in...
CRYPTOGRAPHY PROBABILITY DISTRIBUTION Bad Shuffles Consider the following card shuffling algorithm. There are three cards in the deck and they are each represented by an element in {X, Y, Z}. [Bonus marks if you use the deck {W, X, Y, Z}] Algorithm Shuffle --------------------------------------------------- deck := [X,Y,Z] for i from 0 to 2 do j := RANDOM(i) swap values of deck[i] and deck[j] end for return deck --------------------------------------------------- For each of the following definitions of RANDOM(i), compute the probability distribution...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Q1: Thefollowing code is supposed to return n!, for positive n. An analysis of the code...
Q1: Thefollowing code is supposed to return n!, for positive n. An analysis of the code using our "Three Question" approach reveals that: int factorial(int n){ if (n == 0)     return 1;   else     return (n * factorial(n – 1)); } Answer Choices : it fails the smaller-caller question.     it passes on all three questions and is a valid algorithm.     it fails the base-case question.     it fails the general-case question. Q2: Given that values is of...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A): A is a list of n integers 1 for i = 1 to n-1 do 2   x=aix=ai 3 j = i − 1 4 while (j ≥≥ 0) do 5 if x≥ajx≥aj then 6 break 7 end if 8   aj+1=ajaj+1=aj 9 j = j − 1 a end while b   aj+1=xaj+1=x c end for d return A The complexity of this algorithm is O(n2)O(n2)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT