Question

Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1]...

Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1] of real numbers if n = 1 return T[0] else temp ← Test (T[0..n − 2]) if temp ≥ T[n − 1] return temp else return T[n − 1] a. What does this algorithm compute? b. Set up a recurrence relation for the algorithm’s basic operation count and solve it.

Homework Answers

Answer #1

Algorithm
Test (T[0..n − 1]) //Input: An array T[0..n − 1] of real numbers
   if n = 1 return T[0]
   else temp ← Test (T[0..n − 2])

   if temp ≥ T[n − 1] return temp
   else return T[n − 1]

(a)Above Algorithm finds the maximum element in array T[0..n − 1] of real numbers .
(b)Recurrence Relation
   T(n) = T(n-1) + O(1)
solving using substitution method
   T(n) = T(n-1) + 1   -----------------(1)
   T(n-1)= T(n-2) + 1
Putting value of T(n-1) in equation (1), we get
   T(n) = T(n-2) + 1 + 1
similarly,
   T(n) = T(n-3) + 1 + 1 + 1
T(n) = (1 + 1 + 1 +....+ 1) (n times)
T(n) = n
T(n) = 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
Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n...
Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n 1)What does this algorithm compute? 2) Set up and solve a recurrence relation for the number of times the algorithm's basic operation is executed. 3) How does this algorithm compare with the non-recusive algorithm for computing thius function in terms of time efficeincy and space effeciency?
a. Design a non-recursive algorithm for computing an (discussed in the class). What is the basic...
a. Design a non-recursive algorithm for computing an (discussed in the class). What is the basic operation? How many times is the algorithm’s basic operation executed? b. Using an = a*an-1 (discussed in the class) to design a recursive algorithm for computing an . What is the basic operation? Set up and solve a recurrence relation for the number of times that algorithm's basic operation is executed. c. Using an = a*(a(n-1)/2) 2 (n is odd integer) and an =...
Consider the following recursive algorithm for computing the sum of the first ? cubes: ? (?)...
Consider the following recursive algorithm for computing the sum of the first ? cubes: ? (?) = 13 + 23 + ⋯+ ? 3 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n cubes if n = 1 return 1 else return S(n-1) + n * n * n
Design a recursive algorithm to compute 3^n based on the formula 3^n=3^(n−1) + 3^(n−1) + 3^(n−1)....
Design a recursive algorithm to compute 3^n based on the formula 3^n=3^(n−1) + 3^(n−1) + 3^(n−1). Also do the recurrence relation.
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1)...
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1) = 1; f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
Consider the recurrence relation T(1) = 0, T(n) = 25T(n/5) + 5n. (a) Use the Master...
Consider the recurrence relation T(1) = 0, T(n) = 25T(n/5) + 5n. (a) Use the Master Theorem to find the order of magnitude of T(n) (b) Use any of the various tools from class to find a closed-form formula for T(n), i.e. exactly solve the recurrence. (c) Verify your solution for n = 5 and n = 25.
a) Give a recursive algorithm for finding the max of a finite set of integers, making...
a) Give a recursive algorithm for finding the max of a finite set of integers, making use of the fact that the max of n integers is the larger of the last integer in the list and the max of the first n-1 integers in the list. Procedure power(x,n): If (n=0): return 1 Else: return power(x,n-1) · x b) Use induction to prove your algorithm is correct
The following algorithm adds all the entries in a square n × n array A. Analyze...
The following algorithm adds all the entries in a square n × n array A. Analyze this algorithm where the work unit is the addition operation. sum = 0 for i = 1 to n do for j = 1 to n do sum = sum + A[i, j] end for end for write (“Total of all array elements is”, sum)
Consider the recursive algorithm given to compute factorial. Which statement below most closely relates to the...
Consider the recursive algorithm given to compute factorial. Which statement below most closely relates to the concept of the inductive hypothesis in induction? n = 0 factorial(n-1) return n * factorial(n-1) return Which counting rule is best suited to solving the following problem: You are a pop star performing a concert for your fans. You say “Everybody put your hands up!” and you count 28 hands. Assuming everyone in your audience has 2 hands, how many audience members are there?...
Design a recursive algorithm with proofs of the following: Richest Heritage: Input: A binary tree T...
Design a recursive algorithm with proofs of the following: Richest Heritage: Input: A binary tree T in which each node x contains a field worth[x], which is a (positive, zero, or negative) monetary value expressed as a real number. Define (monetary) heritageof a node x to be the total worth of ancestors of x minus the total worth of proper descendants of x. Output: A node x in T with maximum heritage