KEMBAR78
18103010 algorithm complexity (iterative) | PPT
ALGORITHM COMPLEXITY
(ITERATIVE)
Dr B R Ambedkar National Institute of Technology, Jalandhar
• What is algorithm complexity?
• What is the need to analyze the complexity of an algorithm?
• Execution Speed (depending on n)
• Some common time complexities
• Analysis of Algorithms
Contents
Algorithm Complexity
•Algorithm complexity is a measure which evaluates the order
of the count of operations, performed by a given algorithm as a
function of the size of the input data.
•Complexity of algorithm is usually evaluated in three different
cases:
• Best Case
• Average Case
• Worst Case
• In simple language, algorithm complexity is rough estimation
of number of steps performed by given computation depending
on the size of the input data.
• Measured through asymptotic notation
O(g(n)) where g(n) is a function of the input data size.
• Examples:-
 Linear Complexity O(n),
 Quadratic complexity O(n2),
 Logarithmic complexity O(log n),
 Cubic complexity O(n3)
Algorithm Complexity(continued)
• It takes a lot of time for a program to process large inputs and
complete the execution of the program.
• Sometimes, it is important to analyze the execution time of
given algorithm so that it is more user efficient and user does
not need to wait a long time for the program to execute.
• So, to analyze how much time a user will require if he enters a
value, we analyze the complexity of the algorithms.
• The better the time complexity of an algorithm is, the faster the
program will be executed.
Why analyzing Algorithm complexity?
Execution Speed (depending on n)
Algorithm 10 50 100 1000 10000 100000
O(log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
O(n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
O(n*log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
O(n2) < 1 sec. < 1 sec. < 1 sec. < 1 sec. 2 sec.
4 min.
(approx.)
O(n3) < 1 sec. < 1 sec. < 1 sec. 20 sec.
5 hours
(approx.)
231 days
(approx.)
O(2n) < 1 sec.
260 days
(approx.)
hang hang hang hang
O(n!) hang hang hang hang hang hang
Some Important Points
NOTE:-
• Time complexity is always measured in worst case, i.e.,
O(g(n)).
• The time complexity is always written in the form of the
largest degree of the polynomial as a function of n
obtained. For example, if g(n) = 6n2 + 4n +7, then the time
complexity of the algorithm is O(n2).
• If there are O(1) statements under If Else statements, then
the time complexity of If Else block will always be O(1).
Some Important Points (Continued)
• For example:-
if (condition 1) {
Some O(1) statements
} else if (condition 2) {
Some O(1) statements
} else if (condition 3) {
Some O(1) statements
} else {
Some O(1) statements
}
Some Complexities
Complexity Notation Description
Constant O(1)
Constant number of operations, not
depending on the input data size,
e.g. n=10000 1-2 operations.
Logarithmic O(log n)
Number of operations proportional
to log2n where n is the size of the
input, e.g. n=10000 14
operations
Linear O(n)
Number of operations proportional
to the input data size, e.g. n =
10000 5000 operations
Analysis of Algorithms
O(1) : Time complexity of a function (or set of statements) is
considered as O(1) if it doesn’t contain loop, recursion and call
to any other non-constant time function.
• A loop or recursion that runs a constant number of times is
also considered as O(1).
Example:-
// Here c is a constant
for( i = 0 ; i < c ; i++) {
cout<<“Hello”;
}
Analysis of Algorithms
O(n): Time Complexity of a loop is considered as O(n) if the
loop variables is incremented or decremented by a constant
amount.
Example:-
for( i = 1 ; i <= n ; i++) {
cout<<“Hello”;
}
The loop will execute at most n times in the worst case. So the
time complexity of the above algorithm is O(n).
Analysis of Algorithms
O(nk): Time complexity of nested loops is equal to the number
of times the innermost statement is executed.
Example:-
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
cout<<“Good”;
}
}
The inner loop runs from j=1 to j=n, so it will run n times. This
inner loop will run each of n times for i=1 to i=n, so the total
running time will be O(n*n) which is O(n2).
Analysis of Algorithms
O(log n): Time Complexity of a loop is considered as O(log n) if
the loop variables is divided or multiplied by a constant amount.
Example:-
for (int i = 1; i <= n; i = i*2) {
cout<<“Great”;
}
The value of i will be 1, 2, 4, 8,.…..,2k until 2k <= n.
Therefore, k = log2n.
Hence, the loop will run maximum log2n times, so the time
complexity is O(log2n).
Analysis of Algorithms
Example:
int i = 1,s = 0;
while(s <=n)
{
i++;
s = s + i;
}
Therefore, s=(i*(i+1))/2
Execution will end when s > n.
Hence, n = (i*(i+1))/2
i 1 2 3 4 5
s 1 3 6 10 15
Analysis of Algorithms
Ignoring constant terms, the equation becomes
n = i2
Hence, loop will exit when i = √n.
Therefore, time complexity of the algorithm is O(√n).
Conclusion
• Different programs require execute in different time
limits.
• But to make the programs most effective and make them
execute in the shortest time, we need to find optimize
algorithm to a problem.
• Analyzing the time complexity of algorithms help us
analyze and find out the best algorithm that will solve the
problem in the least possible time.
References
Websites:-
• https://www.geeksforgeeks.org/anaylsis-of-algorithm-
set-4-analysis-of-loops/
• https://www.leda-tutorial.org/en/official/ch02s02s03.html
• https://introprogramming.info/english-intro-csharp-
book/read-online/chapter-19-data-structures-and-
algorithm-complexity/

18103010 algorithm complexity (iterative)

  • 1.
    ALGORITHM COMPLEXITY (ITERATIVE) Dr BR Ambedkar National Institute of Technology, Jalandhar
  • 2.
    • What isalgorithm complexity? • What is the need to analyze the complexity of an algorithm? • Execution Speed (depending on n) • Some common time complexities • Analysis of Algorithms Contents
  • 3.
    Algorithm Complexity •Algorithm complexityis a measure which evaluates the order of the count of operations, performed by a given algorithm as a function of the size of the input data. •Complexity of algorithm is usually evaluated in three different cases: • Best Case • Average Case • Worst Case
  • 4.
    • In simplelanguage, algorithm complexity is rough estimation of number of steps performed by given computation depending on the size of the input data. • Measured through asymptotic notation O(g(n)) where g(n) is a function of the input data size. • Examples:-  Linear Complexity O(n),  Quadratic complexity O(n2),  Logarithmic complexity O(log n),  Cubic complexity O(n3) Algorithm Complexity(continued)
  • 5.
    • It takesa lot of time for a program to process large inputs and complete the execution of the program. • Sometimes, it is important to analyze the execution time of given algorithm so that it is more user efficient and user does not need to wait a long time for the program to execute. • So, to analyze how much time a user will require if he enters a value, we analyze the complexity of the algorithms. • The better the time complexity of an algorithm is, the faster the program will be executed. Why analyzing Algorithm complexity?
  • 6.
    Execution Speed (dependingon n) Algorithm 10 50 100 1000 10000 100000 O(log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. O(n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. O(n*log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. O(n2) < 1 sec. < 1 sec. < 1 sec. < 1 sec. 2 sec. 4 min. (approx.) O(n3) < 1 sec. < 1 sec. < 1 sec. 20 sec. 5 hours (approx.) 231 days (approx.) O(2n) < 1 sec. 260 days (approx.) hang hang hang hang O(n!) hang hang hang hang hang hang
  • 7.
    Some Important Points NOTE:- •Time complexity is always measured in worst case, i.e., O(g(n)). • The time complexity is always written in the form of the largest degree of the polynomial as a function of n obtained. For example, if g(n) = 6n2 + 4n +7, then the time complexity of the algorithm is O(n2). • If there are O(1) statements under If Else statements, then the time complexity of If Else block will always be O(1).
  • 8.
    Some Important Points(Continued) • For example:- if (condition 1) { Some O(1) statements } else if (condition 2) { Some O(1) statements } else if (condition 3) { Some O(1) statements } else { Some O(1) statements }
  • 9.
    Some Complexities Complexity NotationDescription Constant O(1) Constant number of operations, not depending on the input data size, e.g. n=10000 1-2 operations. Logarithmic O(log n) Number of operations proportional to log2n where n is the size of the input, e.g. n=10000 14 operations Linear O(n) Number of operations proportional to the input data size, e.g. n = 10000 5000 operations
  • 10.
    Analysis of Algorithms O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. • A loop or recursion that runs a constant number of times is also considered as O(1). Example:- // Here c is a constant for( i = 0 ; i < c ; i++) { cout<<“Hello”; }
  • 11.
    Analysis of Algorithms O(n):Time Complexity of a loop is considered as O(n) if the loop variables is incremented or decremented by a constant amount. Example:- for( i = 1 ; i <= n ; i++) { cout<<“Hello”; } The loop will execute at most n times in the worst case. So the time complexity of the above algorithm is O(n).
  • 12.
    Analysis of Algorithms O(nk):Time complexity of nested loops is equal to the number of times the innermost statement is executed. Example:- for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { cout<<“Good”; } } The inner loop runs from j=1 to j=n, so it will run n times. This inner loop will run each of n times for i=1 to i=n, so the total running time will be O(n*n) which is O(n2).
  • 13.
    Analysis of Algorithms O(logn): Time Complexity of a loop is considered as O(log n) if the loop variables is divided or multiplied by a constant amount. Example:- for (int i = 1; i <= n; i = i*2) { cout<<“Great”; } The value of i will be 1, 2, 4, 8,.…..,2k until 2k <= n. Therefore, k = log2n. Hence, the loop will run maximum log2n times, so the time complexity is O(log2n).
  • 14.
    Analysis of Algorithms Example: inti = 1,s = 0; while(s <=n) { i++; s = s + i; } Therefore, s=(i*(i+1))/2 Execution will end when s > n. Hence, n = (i*(i+1))/2 i 1 2 3 4 5 s 1 3 6 10 15
  • 15.
    Analysis of Algorithms Ignoringconstant terms, the equation becomes n = i2 Hence, loop will exit when i = √n. Therefore, time complexity of the algorithm is O(√n).
  • 16.
    Conclusion • Different programsrequire execute in different time limits. • But to make the programs most effective and make them execute in the shortest time, we need to find optimize algorithm to a problem. • Analyzing the time complexity of algorithms help us analyze and find out the best algorithm that will solve the problem in the least possible time.
  • 17.
    References Websites:- • https://www.geeksforgeeks.org/anaylsis-of-algorithm- set-4-analysis-of-loops/ • https://www.leda-tutorial.org/en/official/ch02s02s03.html •https://introprogramming.info/english-intro-csharp- book/read-online/chapter-19-data-structures-and- algorithm-complexity/