Question

Write the recurrence equation for ternary search and solve it.

Write the recurrence equation for the Worst Case complexity function for Ternary
Search (counting 4 comparisons per recursive call, and assuming W(1) = 1) and solve this
equation. You may assume that n is a power of 3.
0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

The recurrence for normal binary search is T2(n) = T2(n=2)+1. For ternary search, we make two comparisons on elements that partition the list into three sections with roughly n=3 elements and recurse on the appropriate partition. Thus analogously, the recurrence for the number of comparisons made by this ternary search is T3(n) = T3(n/3) + 2.

However, just as for binary search the second case of the Master Theorem applies. We therefore conclude that T3(n) 2 € Θ(log(n)). We now consider a slightly modified take on a ternary search in which only one comparison is made which creates two partitions, one of roughly n=3 elements and the other of 2n=3.

Here the worst case arises when the recursive call is on the larger 2n=3-element partition. Thus the recurrence corresponding to this worst-case number of comparisons is

                                   T3w(n) = T3w(2n=3) + 1

But again the second case of the Master Theorem applies to place T3w € Θ (log n). The best case is exactly the same. It is interesting to note that we will get the same results for general k-ary search as n approaches infinity.

The recurrence becomes one of Tk(n) = Tk(n/k) + k – 1 depending on whether we are talking about part (a) or part (b). For each recurrence case, two of the Master Theorem applies.


answered by: Gavin
Add a comment
Know the answer?
Add Answer to:
Write the recurrence equation for ternary search and solve it.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Binary Search (Ternary Search)

    Ternary Search is a generalization of Binary Search that can be used to find an element in an array. Itdivides the array withnelements into three parts and determines, with two comparisons, which partmay contain the value we are searching for. For instance, initially, the array is divided into three thirdsby taking mid1=(n−1)/3 and mid2=((2(n−1))/3.  Write a recurrence for the running time of Ternary Search and solve this recurrence.

  • Consider an ordered array A of size n and the following ternary search algorithm for finding...

    Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...

  • Suppose that, even unrealistically, we are to search a list of 700 million items using Binary...

    Suppose that, even unrealistically, we are to search a list of 700 million items using Binary Search, Recursive (Algorithm 2.1). What is the maximum number of comparisons that this algorithm must perform before finding a given item or concluding that it is not in the list “Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into n subinstances of size n/3, and the dividing and combining steps take linear time. Write a...

  • Example from screen cast (a) Write the recurrence relation for Binary Search, using the formula T(n)...

    Example from screen cast (a) Write the recurrence relation for Binary Search, using the formula T(n) = aT(n/b) + D(n) + C(n). (We'll assume T(1) = C, where c is some constant, and you can use c to represent other constants as well, since we can choose c to be large enough to work as an upper bound everywhere it is used.) (b) Draw the recursion tree for Binary Search, in the style shown in screencast 2E and in Figure...

  • Write a recurrence relation describing the worst-case running time of each of the following algor...

    Write a recurrence relation describing the worst-case running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution by using substitution or a recursion tree. You may NOT use the Master Theorem. 上午1:46 3月21日周四 令52%. " 5. endfor 6. return (r); function func4(A, n) *Aarray of n integers */ 1. if n s 20 then return (A[n]); 4. while (i < n/2) do 7. endwhile 8. x...

  • Given the following find-min function, write the recurrence and solve it using the master theorem. Assume...

    Given the following find-min function, write the recurrence and solve it using the master theorem. Assume the worst case, i.e. the tree is imbalanced and skewed to the left. int find_min (TreeNode* tree) // returns the minimum value in a binary search tree { if (tree == NULL)    throw (EmptyTreeException());  else   if (tree -> left == NULL)      return(tree->info);  else  { return find_min(tree->left); } }

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • Write a recurrence relation describing the worst case running time of each of the following algorithms,...

    Write a recurrence relation describing the worst case running time of each of the following algorithms, and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution by using substitution or a recursion tree. You may NOT use the Master Theorem. Simplify your answers, expressing them in a form such as O(nk) or (nklog n) whenever possible. If the algorithm takes exponential time, then just give an exponential lower bound using the 2 notation. function...

  • In Java Language Write a recurrence equation expressing the time complexity of the following algorithm. Explain...

    In Java Language Write a recurrence equation expressing the time complexity of the following algorithm. Explain your answer. Assume that n is a power of 2. Algorithm rec(n) Input: Integer value n ≥ 0         if n = 0 then return 1         else {                       c ← 0                        For i ← 0 to n−1 do c ← c + i                        c ← c + rec(n/2)                        return c                 }       

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT