Question

In: Computer Science

Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested...

  1. Be able to determine the runtime complexity of:
  1. A Java assignment statement
  2. A single loop
  3. Nested loop

  1. Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).  Show that: an expression such as the following                                                              

4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.   

  

  1. Which of the following statements are true?                                             
  1. The running time of a loop is, at most, the running time of the statements inside the loop times the number of iterations.  
  2. Consider the Java statement:

            int x = 250;

This statement has time complexity of T(n) = O(1).                           

  1. Consider the Java statement:

            int x = 250;

This statement has time complexity of T(n) = O(n).

Solutions

Expert Solution

Sol:

PART-I

Runtime Complexity: The runtime (or) time complexity of an algorithm is the amount of execution time taken by the program for a given input of size(n). The time complexity of an algorithm is defined as the number of times the program statements are executed. This is also known as "Frequency Count f(n)".

Section:1. The Time complexity of a Java assignment statement: The assignment statement in any programming language is executed only once i.e frequency count =1 or assignment statement takes one unit of time. With Big-Oh Notation, It is expressed as

T(n)=O(1)

Section 2: Time complexity of a Single for loop: Consider the following Single java "for loop" example

for(i=0 ; i<n ;i++)

{

System.out.println("i="+i); //Printing the value of "i"

}

Let us break down the for loop in to number of parts to find frequency count of every part

a) Loop initialization variable "i=0" is executed only once ,there fore ------> f(n) =1

b) Loop condition is executed  "n+1" times i.e "n" times for condition "True" & "1" time for condition"False",there fore -----> f(n) =n+1

c) Print statement inside loop is exceuted "n" times because for loop is repeated for "n" times,there fore -----> f(n) =n

d) Loop updation statement "i++" is exceuted "n" times,therefore -----> f(n) =n

Together total frequency count f(n) = 1+n+1+n+n =3n+2

If the frequency count is "3n+2",we can ignore the foloowing

  • The constant line "2" in "3n+2"
  • The leading multiplies like "3" in "3n

So we can represent final frequency count f(n) = n , With big-Oh notation T(n) = O(n)

Section 3:Time complexity of a Nested for loop: Consider the following java "Nested for loop" example

for(i=0 ; i<n ;i++) --------------->frquency count of outer for loop 2n+2

{

for(j=0 ; j<n ;j++) --------------->frquency count of inner for loop 2n+2

{

System.out.println("i="+i",j="+j); //Printing the value of "i,j" -------->frquency count n

}

}

  • Inner for loop total frequency count is given as =1+n+1+n+n =3n+2
  • Outer for loop total frequency count is given as =2n+2

Together total frequency count f(n) = (2n+2)(3n+2) =6n2+4n+6n+4 = 6n2+10n+4

  • Ignoring constant like "4" ,the remaining equation is 6n2+10mn
  • Ignoring 10n,because 10n is grow less , when compared to 6n2,the remaining equation is 6n2
  • Ignoring leading multiplies like "6" ,the remaining equation is n2

So we can represent final frequency count f(n) = n2

With big-Oh notation T(n) = O(n2)

PART-II

  • Big Oh Notation:According to big Oh notation "f(n) = O(g(n)), where f,g are two non negative integers function there exists C,k constants such that f(n) <= C.g(n) n >= k and C>0"
  • The simple meaning is function g(n) start grawing faster than f(n) at particualr C,k values
  • Given 4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.   

Let us consider f(n) = 4x3 + 7x2 + 12 & g(n) = O(x3)

Case1: Consider C=1 & k=1 then according to Big Oh notation ,Here "k" is the value of "x" chosen for one particular case

  • 4(1)3+7(1)2+12 <= 1.(1)3
  • 4+7+12<= 1
  • 23<=1 --> False

Therefore Case1 Failed

Case2: Consider C=5 & k=2 then according to Big Oh notation

  • 4(2)3+7(2)2+12 <= 5.(2)3
  • 32+28+12<= 40
  • 72<= 40 --> False

Therefore Case2 is also Failed

Case3: Consider C=10 & k=2, then according to Big Oh notation

  • 4(2)3+7(2)2+12 <= 10.(2)3
  • 32+28+12<=80
  • 72<= 80 --> True

At C=10 and k=2 funtion g(n) grows faster than f(n)

Therefore witness is C=10 & k=2

PART-III

a) According to Section2, Statement inside for loop are executed “n” times, if for loop is iterated n+1 times

Ans :YES

b) According to Section1 Java statement: int x = 250; time complexity is O(1)

Ans :YES

c) According to Section1 Java statement: int x = 250; time complexity T(n)=O(n) is wrong

Ans :No


Related Solutions

Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested...
Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested loop Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).              Show that:            an expression such as the following                                                               4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.      Which of the following statements are true?                                              The running time of a loop is, at most, the running time of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
1.If one statement is nested within another statement, as in a loop or recursive call, what...
1.If one statement is nested within another statement, as in a loop or recursive call, what is the running time of the two statements when considered as a block? the running time for the inner statement added to the running time for the outer statement the running time for the inner statement multiplied by the running time for the outer statement the running time for the inner statement divided by the running time for the outer statement the running time...
JAVA Write nested while loop that will print out this pattern, based upon a number entered...
JAVA Write nested while loop that will print out this pattern, based upon a number entered by the user. User enters 4: 1234 1234 1234 1234 User enters 2: 12 12
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ...
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ College, the student must meet the following requirements: GPA must be 3.75 or higher. Family income must be more than $60,000 Applicant must be a New Jersey resident. XYZ college asked you to write a program to implement the above requirements. Write the program using compound logic (AND, OR) whichever is applicable. Modify the program using nested if statement. Submit: Source code for both...
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ...
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ College, the student must meet the following requirements: GPA must be 3.75 or higher. Family income must be more than $60,000 Applicant must be a New Jersey resident. XYZ college asked you to write a program to implement the above requirements. Write the program using compound logic (AND, OR) whichever is applicable. Modify the program using nested if statement. This means there will be...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
JAVA!!!! int[][] BingoCardArray = new int[5][5]; Use a nested for-loop, to populate the 2-D array representing...
JAVA!!!! int[][] BingoCardArray = new int[5][5]; Use a nested for-loop, to populate the 2-D array representing 5 columns for B – I – N – G – O, where row 1 =         col 1 = random # 1- 15         col 2 = random # 16 – 30         col 3 = random # 31 – 45         col 4 = random # 46 – 60         col 5 = random # 61 – 75 row 2 =         col 1 = random # 1-...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT