In: Computer Science
4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.
int x = 250;
This statement has time complexity of T(n) = O(1).
int x = 250;
This statement has time complexity of T(n) = O(n).
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
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
}
}
Together total frequency count f(n) = (2n+2)(3n+2) =6n2+4n+6n+4 = 6n2+10n+4
So we can represent final frequency count f(n) = n2
With big-Oh notation T(n) = O(n2)
PART-II
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
Therefore Case1 Failed
Case2: Consider C=5 & k=2 then according to Big Oh notation
Therefore Case2 is also Failed
Case3: Consider C=10 & k=2, then according to Big Oh notation
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