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 following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for ( int i = n; i > 0; i /= 2 ) { for ( int j = 1; j < n; j += 2 ) { for ( int k = 0; k < n; k += 2 ) { ... // constant number of operations } } }
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 =...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 =...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 = 0; sum5 = 0; for(i=1; i<=n/2; i++) { sum2 = sum + 2; } for(j=1; j<=n*n; j++) { sum5 = sum + 5; } i think it is O(n^2) since big oh finds the order of worst case which is the the second loop being n^2 complexity. but im not sure. 2.) Give an efficient algorithm along with running time analysis to find the...
An algorithm has a running time like the following: T(n) = T(n-1) + an + b;...
An algorithm has a running time like the following: T(n) = T(n-1) + an + b; for n> 0 and a, b: the constant T(0) = 0 Determine the complexity of T(n)
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.
What is the tilde time complexity for the following code fragments: 1) int sum = 0;...
What is the tilde time complexity for the following code fragments: 1) int sum = 0; for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) sum++; 2) int sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < i; j+=2) sum++;
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for (int i = n; i >= n; i = i/2)           some O(1) time statements; end for