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...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V...
array_v.h #ifndef ARRAY_V_H #define ARRAY_V_H #include <cassert> template < typename IndexType, typename BaseData > class Array_V { public: IndexType partition(IndexType lo, IndexType hi); IndexType sort(int numvals); void qsRecursive(IndexType lo, IndexType hi); IndexType getHiIndex(); IndexType getLoIndex(); void setHiIndex(IndexType index); void setLoIndex(IndexType index); Array_V(IndexType lo, IndexType hi); //constructor Array_V(int size = 0); Array_V(const Array_V< IndexType, BaseData >& initArray); //copy constructor ~Array_V(); //destructor BaseData& operator [] (IndexType); Array_V< IndexType, BaseData >& operator = (const Array_V< IndexType, BaseData >& initArray); void assign(IndexType i, const...
Question 1 Consider the following 32-bit IEEE 754 floating point number. The binary number has been...
Question 1 Consider the following 32-bit IEEE 754 floating point number. The binary number has been separated into the sign, exp and frac fields Sign = 1           exp = 1000 0001           frac= 1111 0000 0000 0000 0000 000 Convert this number to decimal. Recall that the bias is 127 for IEEE single precision. Consider the following working C code. For(i = 0; I < 3: i++)             For(j = 0; j<=I; j++)                         Arr[i][j] = i + j; Sketch arr...
JAVA - algorithm analysis and sorting Question 1 Which of the following features grows fastest? 1....
JAVA - algorithm analysis and sorting Question 1 Which of the following features grows fastest? 1. N2 2. log N 3. N log N 4. N 5. 10 Question 2 Given the following code segment: for( int i = 1; i < n; i++ ) for( int j = 1; j < i; j++ ) k = k + i + j; What is the runtime of the code segment? 1. None of the above 2. O(i*N) 3. O(N2) 4....
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 ------...
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...
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...
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...
[15 pts.] Consider a version of a binary search tree (BST) for sorted maps with integer...
[15 pts.] Consider a version of a binary search tree (BST) for sorted maps with integer keys that stores additional information as data members of each node w of the tree to account for the smallest and largest integer keys in the subtree rooted at w. Suppose that we can access this information using w.min, for the smallest integer key, and w.max, for the largest. Algorithm 4 provides the pseudocode for the constructor of a node in a binary tree...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT