Question

Answer the following questions: 1. Given the code segments below with n as the problem size,...

Answer the following questions:
1. Given the code segments below with n as the problem size, answer the following questions:
//Code Segment 1 (Consider n as a power of 3)
int sum = 0;
for(int i = 1; i <= n; i = i*3)
sum++;​​​​​​​// statement1
//Code Segment 2: (Consider both possibilities for x)
if(x < 5){
for(int i = 1; i <= n; i++)   
for(int k = 1; k <= i; k++)   
sum++;​​​​​// statement2
}
else{
for(int i = 1; i <= n; i++)
​​for(int j = 1; j <= i; j++)
​​​for(int k = 1; k <= j; k++)
sum++;​​​// statement3
}
//Code Segment 3: Consider n as a power of 4.
for(int i = 1; i <= n; i = i*4)
for(int k = 1; k <= i; k++)   
sum++;​​​​​​// statement4
a. (48 points) Find the exact number of times statement1, statement2, statement3 and statement4 get executed in terms of n.
b. (12 points) Determine the Big-O complexity of these 3 program fragments.

Homework Answers

Answer #1

Solution for the problem is provided below, please comment if any doubts:

1.

a)

Exact number of times execution of given statements in terms of n:

I. Statement 1

Answer: log3(n)

Explanation:

  • Here the statement 1 is inside a for loop and the for loop repeats from 1 to n, but the increment will be in powers of 3.
  • That is the statement executs for i= 1, 3, 9, 27….n
  • Thus total number of times = log3(n)

II. Statement 2

Answer: n*(n-1)/2 , if x<5 and 0 times otherwise

Explanation:

  • Here the statement 2 is inside two for loops, but it is inside a condition check “if(x<5)”.
  • Thus if the condition “x<5” passes, it executs 1 time for i=1, 2 times for i=2,…
  • Thus total number = 1+2+3+4+..n = n*(n-1)/2, surely zero if the conditionnot passed.

III. Statement 3

Answer: n(n+1)(n+2)/6 , if x>=5 and 0 times otherwise

Explanation:

  • Here the statement 2 is inside two three “for” loops, but it is executes if condition check “if(x<5)” fails.
  • Thus if the condition “x<5” not passes, it executs 1 time for i=1, 2 times for i=2,…
  • The number of execution will be = 1+3+6+10+15…+= n(n+1)(n+2)/6

IV. Statement 4

III. Statement 3

Answer: (16(logn)-1)/15

Explanation:

  • Here the the statement is inside two “for” loops.
  • The outer for loop will run on powers of 4 values of i.
    • 1, 4, 16,….
  • The inner loop will execute as much time as that of first loop value.
  • Thus total execution = 1*1+4*4+16*16…= 1+42+44…=(A geometric series of length log 4n)=(16(logn)-1)/15

b)

Big-O complexity of program segments:

I. Segment I

Answer: O(log(n))

Explanation:

  • It contains only the statement 1 and the loops around it.
  • Now the complexity will be execution complexity of statement 1,
  • Which is O(log n).

II. Segment II

Answer: O(n3)

Explanation:

  • It contains only statement 2 and 3 in serial execution.
  • Thus the complexity will be higher order term.
  • Which is O(n3)

III. Segment III

Answer: O(42logn)

Explanation:

  • It will be the complexity of running time of statement 4.
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
(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...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Define a recurrence F(n) for the code: int a(int i, int j){ if( j <= 1)...
Define a recurrence F(n) for the code: int a(int i, int j){ if( j <= 1) return i+20; else return j + a(i+10, j-3); } This recurrence I've come up with is F(n) = 2 if j <= 1 and F(n) = 2 + F(i+10, j-3). Now I need to solve this recurrence and represent it in big O notation which I'm unsure how to do. The main thing throwing me off is that there are two variables in the...
What is the time complexity of the following code? (in big-O notaion) sum = 0 count...
What is the time complexity of the following code? (in big-O notaion) sum = 0 count = 0 var1 = 0 for: i=1 to n{ sum = sum+i for j=1 to [(3i+2i)(5n)(2i)]{ var1 = sum+count} } for m = i to j*j{ sum = sum*sum var2 = sum - (count+3)}
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 } } }
1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1:...
1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1: foo mystery() foreach i ←1 to n do mystery() foreach j ←1 to i do (A) Determine the exact number of times mystery() is called in terms of n. (B) Assume the mystery function is in O(log(n) ∗ n ^2 ). Determine the complexity of “foo” in O-notation. if i ≤ j then mystery()
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++;
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: {...
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: { 03: list<int> L; 04: for (int i = 0; i < N; ++i) 05: { 06: copy (source, source + i, front_inserter<int>(L)); 07: } 08: for (int j: L) 09: { 10: if (find(dest.begin(), dest.end(), j) != dest.end()) 11: dest.push_back(j); 12: } 13: } and the following list of complexity values: A: O(1), B: O(log N), C: O(N), D: O(N log N), E: O(N2),...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
(C++ program) Use the code segment below to answer the two questions that follow. … int...
(C++ program) Use the code segment below to answer the two questions that follow. … int size = 0; cout << “Enter size: “; cin >> size; int sizeCode = size / 10; if (sizeCode == 0)    cout << “extra small”; else if(sizeCode == 1 )    cout << “small”; else if (sizeCode == 2)    cout << “medium”; else if (sizeCode == 3)    cout << “large”; else    cout << “extra large”; //end if … What would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT