Question

   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code...

  
1)T(n) = 27 T (n/3) + n3


2)Calculate the running time for the following code fragment.

Algorithm test

int counter, i, j;

counter := 0;

for (i:= 1; i < n; i*=2)

{

for (j:= 0; j < i; j++)

{

counter := counter +1

}

}


3)Let f(n) = 2lg 8n + 3 be a function.

Homework Answers

Answer #1
1)


2)
let's go through each iteration of the outer loop

i = n
    inner loop iterates n times
i = n/2
    inner loop iterates n/2 times
i = n/4
    inner loop iterates n/4 times
i = n/8
    inner loop iterates n/8 times
i = n/16
    inner loop iterates n/16 times
...
i = 1
    inner loop iterates 1 time

total number of iterations/operations = n + n/2 + n/4 + ... + 1
= n(1 + 1/2 + 1/4 + 1/8 + ...)
we know that 1 + 1/2 + 1/4 + 1/8 + ... <= 2
= n(1 + 1/2 + 1/4 + 1/8 + ...)
<= 2n
so, T(n) = O(n)
Answer: O(n)

3)
f(n) = 2lg 8n + 3
f(n) = O(log 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
Show that the running time for the following segment of code is O(N3) without using the...
Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to count each statement that takes one unit of time. sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ )             for( k = 0; k < n; k++ ) sum++;
Analyze the worst case running time of the following code in “big-Oh” notation in terms of...
Analyze the worst case running time of the following code in “big-Oh” notation in terms of the variable n. a. void fdisp1(int n) { for(int i=0; i < n; i++) { for(int j=0; j < n; j++) { for(int k=0; k < n; k++) { for(int m=0; m < n; m++) { System.out.println("Hi I am Here"); } } } } } b. voidprintAllPossibleOrderedPairs(intarr[], int size) { for (int i = 0; i < size; i++) { for (int j =...
Given the recurrence equation below, find running time T(n). T(n) = 8T(n/2) + n3 log n4...
Given the recurrence equation below, find running time T(n). T(n) = 8T(n/2) + n3 log n4 T(n) = 2T(n1/2) + log n
Consider the following function : F 1 = 2, F n = (F n-1 ) 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes F n using the recursive definition given above. ii. Describe a more efficient algorithm to calculate F n and give its running time.
Question 4.5 Consider the fragment in Algorithm 4.8 shown below. Algorithm 4.8 sum = 0; for...
Question 4.5 Consider the fragment in Algorithm 4.8 shown below. Algorithm 4.8 sum = 0; for (i =1; i <=f(n); i ++) sum += i; Here f(n) is a function call. Give a simple and tight Big-Oh upper bound on the running time of Algorithm 4.8, as a function of n, on the assumption that (a) the running time of f(n) is O(n), and the value of f(n) is n!. (b) the running time of f(n) is O(n), and the...
Generate test cases for the following code by using Basic Path Testing. void sort(int a[ ],int...
Generate test cases for the following code by using Basic Path Testing. void sort(int a[ ],int N) {     int i,j,p,t;        for(i=0;i<N-1;i++)        {     p=i;               for(j=i+1;j<N;j++)                      if(a[j]>a[p]) p=j;               if(p!=i)               {     t=a[p];  a[p]=a[i];     a[i]=t;   }        } } a)       Draw CFG. b)       How many basic paths for the CFG? c)        List the basic paths. d)       Generate test cases from it.
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid...
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid populated by the alternating dots and stars (see example below). The odd rows contain stars and even rows contain dots. Debug the code to fix all the compilation and run-time errors, so that the code generates the desired output. For instance, when the 'n' value passed to the function is 6, the output would look like the following. ****** ..... **** ... ** ....
a) Suppose that a particular algorithm has time complexity T(n) = 3 x 2^n, and that...
a) Suppose that a particular algorithm has time complexity T(n) = 3 x 2^n, and that executing an implementation of it on a particular machine takes t seconds for n inputs. Now suppose that we are presented with a machine that is 64 times as fast. How many inputs could we process on the new machine in t seconds? b) Suppose that another algorithm has time complexity T(n) = n^2, and that executing an implementation of it on a particular...
2. Write the big-O expression to describe the number of operations required for the following two...
2. Write the big-O expression to describe the number of operations required for the following two pieces of code. You need to explain your solution and show your work. (3 points) code 1: counter = 0       for (i = 1; i < n; ++i) {           for (j = 1; j < i; j++) { counter++; }    } code 2: counter = 0; for (i = 1; i < n; ++i) { for (j = 1; j <...
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.