Question

The task was to find the recurrence relation for this function and then find the complexity...

The task was to find the recurrence relation for this function and then find the complexity class for it as well. Provided is my work and the function. My question is, I feel like I'm missing some step in the recurrence relation and complexity class. Is this correct? The following code is in JavaScript.

function divideAndConquerSum(x){
if(x.length<1){
return 0;
}
if(x.length == 1){
return x[0];
}
var third = Math.floor((x.length-1)/3);
var next = (third *2)+1;
var y = x.slice(0, third+1);
var z = x.slice(third+1, next+1);
var a = x.slice(next+1, x.length);
return divideAndConquerSum(y)+divideAndConquerSum(z)+divideAndConquerSum(a);
  
}

///

work:

1. Checks if the array has length of zero...this is a constant time so : +1

2. Checks if the array has a length of 1, if so then return. Also constant time: +1

3. Splits the array into 3 parts this is also constant no matter the size of n: +1

4. Function then adds its self three times but each time is a recursive call of 1/3 the size of n: 3T(1/3)

1+1+1+ 3T(1/3)

3T(1/3)

calls itself so we can define the relation as

3T(1/3)

9T(1/9)

27T(1/27)

this pattern can shown as

3log3n

so we have

1+1+1+ 3log3n

which is a complexity class of

O(log n)

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Since at every step the assigning is somewhat constant c

So, T(n)=3+3+3*T(n/3)=T(n/3)+6

Since 3 for assigning the first 3 assignment, next 3 for assigning value of function returned to variables.

Also T(1)=1

Consider n=3^k

So,

T(n)=3*(3*T(n/9)+6)+6=(3^2)*T(n/6)+6*(1+3)

T(n)=(3^3)*(T(n/27))+6*(1+3+3^2)

Lets say it end after k iteration then

T(n)=(3^k)*T(n/3^k)+6*(1+3+3^2+....3^(k-1))

So,

k=log3(n)

T(n)=n*T(1)+6*(1+3+....k terms)

So,

T(n)=n+6*(3^k-1)/2=3*(n-1)+n=4n-3

So,

T(n)=theta(n)

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
The task was to find the recurrence relation for this function and then find the complexity...
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
  • 3. Determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution...

    3. Determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution using expansion/substitution and upper and/or lower bounds, when necessary. You may not use the Master Theorem as justification of your answer. Simplify and express your answer as O(n*) or O(nk log2 n) whenever possible. If the algorithm is exponential just give exponential lower bounds c) T(n) T(n-4) cn, T(0) c' d) T(n) 3T(n/3) c, T() c' e) T(n) T(n-1)T(n-4)clog2n, T(0) c' 3. Determine the...

  • 1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array...

    1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array of size n is divided into 5 segments of sizes n/5. Write the recurrence relation for the time complexity and solve it. (Show all your work.)

  • For each of the following problems write a recurrence relation describing the running time of eac...

    For each of the following problems write a recurrence relation describing the running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution using substitution and carefully computing lower and upper bounds for the sums. Simplify and express your answer as Θ(n k ) or Θ(n k (log n)) wherever possible. If the algorithm takes exponential time, then just give exponential lower bounds. 5. func5 (A,n) /*...

  • 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...

  • 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...

  • For each of the following recursive methods available on the class handout, derive a worst-case recurrence...

    For each of the following recursive methods available on the class handout, derive a worst-case recurrence relation along with initial condition(s) and solve the relation to analyze the time complexity of the method. The time complexity must be given in a big-O notation. 1. digitSum(int n) - summing the digits of integer: int digitSum(int n) {                 if (n < 10)                                 return n;                 return (digitSum(n/10) + n%10); } 2. void reverseA(int l, int r) - reversing array: void...

  • I am trying to calculate the runtime complexity of a function that does not have fixed...

    I am trying to calculate the runtime complexity of a function that does not have fixed size input, but uses several helper methods that do have fixed size input. I was unsure of how to include the helper methods in my calculations. If I have an array with a fixed size of 32 indices, and I have a function that sums up the elements in that array, will that function be O(n), or O(1)? I think that a function that...

  • Given the sequence defined with the recurrence relation:

    Given the sequence defined with the recurrence relation:$$ \begin{array}{l} a_{0}=2 \\ a_{k}=4 a_{k-1}+5 \text { for } n \geq 0 \end{array} $$A. (3 marks) Terms of Sequence Calculate \(a_{1}, a_{2}, a_{3}\) Keep your intermediate answers as you will need them in the next questionsB. ( 7 marks) Iteration Using iteration, solve the recurrence relation when \(n \geq 0\) (i.e. find an analytic formula for \(a_{n}\) ). Simplify your answer as much as possible, showing your work. In particular, your final...

  • How to design an algorithm using recursion to find number of subarray contains all 0 in...

    How to design an algorithm using recursion to find number of subarray contains all 0 in an array which only contains 0 or 1? For example, we have array 00, so we return 1, if we have an array 0100, we return 2. Assume the size of array >=1. How to find the recurrence relation of the algorithm? And how to express the recurrence relation as a function of n.

  • Compute the recurrence relation, T(n), for the following function, solve it, and give a e bound....

    Compute the recurrence relation, T(n), for the following function, solve it, and give a e bound. Justify your answer public static double myPower(double r, int n) if (n1){ return 1 } else if (n % 2 == 0) { double tmp myPower (r, n/2); return tmp tmp; } else{ myPower (r, (n 1)/2); return }

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