Introduction to Algorithm and its Complexity Lecture 1: 18 slides Introduction to Algorithm and its Complexity Lecture 1: 18 slides By: Dr. Deepti Malhotra Assistant Professor Computer Science & IT Dept. 4/6/2019 1
Contents. 1. Introduction. 2. Algorithm. 3. Good Algorithm. 4 Contents 1 Introduction 2 Algorithm 3 Good Algorithm 4 Time and Space Complexity 5 Big Oh Notation 6 Analysis of Iterative Algorithm 4/6/2019 2
Algorithm? An algorithm is a sequence of steps to solve a problem, there may be more than one algorithm to solve a problem. Outline, the essence of a computational procedure, step by step instructions. Program: an implementation of an algorithm in some programming langauge. 4/6/2019
Informal Description of the solution is called as Algorithm Let us consider P1 as any problem in Computer Science which needs to be solved. Problem P1 can be solved by writing a program in any programming language . For e.g. C. For writing a program, we need a algorithm. 4/6/2019
Contd… Lets say that P1 has many solutions to solve i.e. A1 , A2, A3…….. P1 A1 A2 A3.. 4/6/2019
Before implementing any algorithm as a program, we have to find out which among the algorithm is good in terms of: Time: which one could execute faster and Memory: which one will take less memory. 4/6/2019
What is Good Algorithm? The choice of particular algorithm depends on the following considerations:- Time Complexity (Performance Requirement) Space Complexity ( Memory Requirement) 4/6/2019
Contd… Performance requirements are usually more critical than memory requirement, hence in general it is not necessary to worry about memory unless they grove faster than performance requirement. Therefore, the algorithms are analyzed only on the basis of performance requirements i.e running time efficiency. 4/6/2019
Time Complexity Time complexity of an algorithm is the amount of time it needs to run to completion. It is the execution time of an algorithm irrespective of the computer on which it will be use. 4/6/2019
Expressing Space and Time Complexity The space and/or time complexity is usually expressed in the form of a function f(n), where n is the input size for a given instance of the problem being solved. The 4/6/2019
Contd… Expressing space and/or time complexity a s a function f(n) is important because of the following reasons:- We may be interested to predict the rate of growth of complexity as the size of the problem increases. To compare the complexities of two or more algorithms solving the same problem in order to find which is more efficient. 4/6/2019
Big Oh Notation The most important notation used to express the function f(n) is Big Oh notation (which provides the upper bound for the complexity. f(n) = O (g(n)) “ f of n is big oh of g of n” or f(n) is of the order of g(n)” 4/6/2019
Algorithm Iterative Recursive Iterative Recursive A() A(n) { { { { For i =1 to n if( condition) Max (a,b) A(n/2) } } 4/6/2019
Contd.. Time taken by Iterative Algorithm: Count the number of time loop gets executed. Time taken by Recursive Algorithm: Count the number of time the function gets executed. If algorithm is neither iterative nor recursive, then it means there is no dependency of running time on input size, whatever input size is running time is always constant. O(1) : constant Time 4/6/2019
Analysis of Iterative Program { Int i For (i= 1 to n) Printf(“ Sita”) } Here total n times Sita will be printed. Therefore, complexity is f(n) = O(n) 4/6/2019
A( ) { int i, j for ( i=1 to n ) for ( j= 1 to n) Printf(“ Sita”) Complexity is O (n2) 4/6/2019
A ( ) int i ,j, k, n for ( i= 1; 1< = n ; i++) { for (j=1 ; j < = I; j++) { for (k =1; k<= 100; k++) { printf (“ Sita”) } } } 4/6/2019
Contd.. Outer loop runs 1 to n tomes Inner loop runs ( depends on the value of i) Inner most loop runs 100 times i.e independent of i and j. Dry Run i = 1 i = 2 i = 3 i = 4 i = 5 i = n j = 1 time j = 2times j = 3 times j = 4 times j = 5 times j = n times K = 100 times K= 2*100 K= 3*100 K= 4*100 K= 5*100 K= n*100 Total time Printf is Executed =100+2*100+3*100+4*100+…..+n*100 =100(1+2 +3 +………+n) =100(n(n+1))/2 = O (n2 ) 4/6/2019