Question

Consider the following algorithm that operates on a list of n integers: • Divide the n...

Consider the following algorithm that operates on a list of n integers:

• Divide the n values into n 2 pairs

• Find the max of each pair.

• Repeat until you have the max value of the list

(a) Show the steps of the above algorithm for the list (25,19,9,8,2,26,21,26,31,26,3,14).

(b) Derive and prove a tight bound on the asymptotic runtime of this algorithm

(c) Assuming you just ran the above algorithm, show that you can use the result and all intermediate steps to find the 2nd largest number in at most log2 n additional steps

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The algorithm to find a maximum element in a list of values is shown as follows:

  1. Divide the n number of values of the list into n/2 pairs.
  2. Determine the maximum of each pair.
  3. Repeat part 2 till the maximum element of the list is not found.

(a)

The steps of the given algorithm for the list (25, 19, 9, 8, 2, 26, 21, 26, 31, 26, 3, 14) can be shown as follows:

Step 1: Divide the n values of the list into n/2 pairs.

Step 2: Determine the maximum of each pair.

25 19, 9 8, 2 26, 21 26, 31 26, 3 14

   25     9     26       26       31      14

Step 3: Repeat step 2 till the maximum element is not found.

  • The maximum of 25 and 9 is 25.
  • The maximum of 26 and 26 is 26.
  • The maximum of 31 and 14 is 31.

The resulted values will be as follows:

25       26         31

     26               31

               31

The last element which is 31 is the maximum element of the given list. The total number of steps which are used to find the maximum element is 12 i.e., the number of elements of the list.

Hence, the total number of steps to find a maximum element of the list is equal to the number of values in a list.

(b)

The tight bound on asymptotic runtime of the given algorithm can be derived and proved as follows:

The asymptotic runtime of the given algorithm can be found as follows:

Since the number of steps to get the maximum element of the list is equal to the number of elements of the list. Therefore, the runtime of the algorithm will be O(n).

The recurrence relation for this runtime can be formed as follows:

T(n) = 0, for n = 1

T(n) = 1, for n = 2

T(n) = T(n/2) + T(n/2) + 2, for n > 2

The value n in the above relation can be a power of 2.

The above relation can be written in general terms as follows:

T(n) = 2T(n/2) + 2

        = 2[2T(n/2)/2 + 2] + 2 => 4T(n/4) + 6

        = 4[2T(n/4)/2 + 2] + 6 => 8T(n/8) + 14

        .

        .

        .

        = 2k-1T(2) + ∑(1≤i≤k-1) 2k

        = 2k-1 + 2k – 2

        = 3n/2 – 2 (since n = 2k)

T(n) = O(n)

Hence, the tight bound on the runtime of the given algorithm is O(n).

(c)

If an element of a list needs to be selected as maximum element of the list, then it must be compared with second maximum element of the list at some level of the comparison tree made in the part a.

The number of times the maximum element of the list is compared will be equal to the number of elements that needs to be checked for second maximum element.

So, the number of times the maximum element compared in the list will be height of the comparison tree formed in part a i.e., log2n.

The elements from which maximum element found in part a is compared are 26, 14, and 26. Out of these three elements the maximum is 26. Therefore, the second maximum element of the list is found as 26.

The maximum number of elements by which the maximum element of the list found in part a will be compared cannot be more than log2n.

Since the number of comparisons to find the maximum element in a list is equal to the number of elements of the list. Therefore, additional steps required to determine the second maximum element of the list is at most log2n.

Add a comment
Know the answer?
Add Answer to:
Consider the following algorithm that operates on a list of n integers: • Divide the n...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Computer Algorithm question 8) Give an algorithm for building a heap in O(n) 9) Prove the...

    Computer Algorithm question 8) Give an algorithm for building a heap in O(n) 9) Prove the algorithm given in 8) runs in O(n) time. 10) What is the asymptotic runtime of an algorithm represented by the following recurrence equation? 11) Suppose you have the following priority queue implemented as a (max) heap. What will the heap look like when the max node is removed and the heap is readjusted? Assume on each heapify operation the largest child node is selected...

  • Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n...

    Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n into 3 subproblems of sizes n/2 , n/4 , n/8 , respectively with O(n) time; Recursively call on these subproblems; and then combine the results in O(n) time. The recursive call returns when the problems become of size 1 and the time in this case is constant." (a) Let T(n) denote the worst-case running time of this approach on the problem of size n....

  • Question #4 (15 points) In class, we discussed a divide-and-conquer algorithm for matrix multiplication that involved...

    Question #4 (15 points) In class, we discussed a divide-and-conquer algorithm for matrix multiplication that involved solving eight subproblems, each half the size of the original, and performing a constant number of e(n) addition operations. Strassen's Algo- rithm, which we did not cover, reduces the number of (half-sized) subproblems to seven, with a constant number of e(n) addition and subtraction operations. Provide clear, concise answers to each of the following related questions. • (7 points). Express the runtime of Strassen's...

  • Following are the steps (algorithm) to obtain a solution for Queen Puzzle heuristically: a, Divide n...

    Following are the steps (algorithm) to obtain a solution for Queen Puzzle heuristically: a, Divide n by 12 Remember the remainder (n is 8 for the eight queens puzzle) b. Write a list of the odd numbers from 1 to n in order, if the remainder is 8, switch pairs (i.e. 3, 1, 7, 5. 11.9....) c. if the remainder is 2, Switch the places of 1 and 3, then move 5 to the end of the list

  • Please give me a divide and conquer algorithm that has runtime better than O(n^2) along with...

    Please give me a divide and conquer algorithm that has runtime better than O(n^2) along with justification. Also please do a runtime analysis on this algorithm. Please DONT copy and paste other's solution.THANKS 3. Give the best algorithm you can to convert an n digit number base 10 into binary. Here, we are counting operations on single digits as single steps, not arithmetic operations. You can use any of the multiplication algorithms we described in class.)

  • Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a...

    Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing and combining steps take a time in Θ(n n). Write a recurrence equation for the running time T (n) , and solve the equation for T (n) 2. Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing...

  • What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The...

    What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...

  • What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The...

    What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...

  • Consider the following problem: Input: a list of n-1 integers and these integers are in the...

    Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...

  • Your task is to design algorithms to solve the following problems. For full credit, your algorithm...

    Your task is to design algorithms to solve the following problems. For full credit, your algorithm must run in logarithmic time. Given a number n greaterthanorequalto 1 and a (user-specified) error tolerance e, you want to approximate the squareroot of n to within error tolerance e. Specifically, you want to return an x = Squareroot n that satisfies |x^2 - n| greaterthanorequalto e. For example, to compute the squareroot of n = 2 with e = 0.01, an acceptable answer...

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