ALGORITHM
www.eshikshak.co.in
Introduction to Algorithm
● An algorithm is finite set of steps defining the solution of a particular problem.
● It can be expressed in English like language, called pseudocode, in programming language or in the form of flowchart.
www.eshikshak.co.in
Characteristics
● Input – There are zero or more values which are externally supplied.
●Output – At least one value is produced●Definiteness – Each step must be clear and
unambiguous●Finiteness – If we trace the steps of an
algorithm, then for all cases, the algorithm must terminate after a finite number of steps
●Effectiveness – Each step must be sufficiently basic that it can in principal be carried out by a person using only paper and pencil
www.eshikshak.co.in
Algorithm Complexity
● There are basically two aspects of computer programming.
○ Data Organization (i.e Data & structure)○ Algorithm
● The choice of a particular algorithm depends on the following consideration :
○ Time Complexity (Performance Requirements)○ Space Complexity (Memory Requirements)
www.eshikshak.co.in
Space Complexity
● It means the amount of memory, algorithm or program needs to run to completion.
● Some of the reasons for studying space complexity are
○ If a multi user system○ Sufficient memory is available to run the
program
www.eshikshak.co.in
Space Complexity (Cont.)
● The space needed by a program consists of the following components.
○ Instruction Space : Space needed to store the executable version of the program
○ Data Space : Space needed to store all constants, variable values and has further following components
■ Constants, variables, structured variables like array and structure
■ Dynamically allocated space
www.eshikshak.co.in
Space Complexity (Cont.)
● Environmental Stack Space : Space needed to store the information needed to resume the suspended functions.
○ Return Address○ Values of all local variables and the values of
formal parameters in the function being involved.
www.eshikshak.co.in
Time Complexity
● It means the amount of time it needs to run to completion
● Reasons for studying time complexity○ Interested to know in advance that whether a
program will provide a satisfactory real time response
○ There may be several possible solutions with different time requirements
www.eshikshak.co.in
Expressing Space and Time Complexity● The space and/or time complexity is usually
expressed in form of the function f(n)○ Where n is the input size for a given instance of the
problem being solved.● Expressing space and/ or time complexity as a
function has following reasons ○ We may be interested to predict the rate of growth
of complexity as the size of problem increases.○ To Compare the complexities of two or more
algorithm○ Since in modern computers, the memory is not a
sence constraint, therefore, our analysis of algorithms will be on the basis of time complexity.www.eshikshak.co.in
Big ‘O’ Notation
● Big O is a characterization scheme that allows to measure properties of algorithms such time and space complexity.
● It is useful to set the prerequisites of algorithms and to develop and design efficient algorithms in terms of time and space complexity.
www.eshikshak.co.in
Categories of Algorithm
● Based on Big O notation, the algorithm can be categorized as follows
○ Constant time O(1)○ Logarithmic time O(log n)○ Linear time O(n)○ Polynomial time O(nk) (for k > 1)○ Exponentail time O(kn) for k > 1
www.eshikshak.co.in
Algorithm Analysis
● Different ways or algorithms to solve a problem.
● Hence, some of the algorithm may be more efficient than the others
● There are different types of time complexities which can be analyzed for an algorithm
○ Best Case Time Analysis○ Average Case Time Analysis○ Worst Case Time Analysis
www.eshikshak.co.in
Best Case Time Complexity
● It is a measure of the minimum time that the algorithm will require for an input of size ‘n’.
● The running time of many algorithms varies not only for the inputs of different sizes but also for the different inputs of same size
○ Example : Sorting or Searching
www.eshikshak.co.in
Worst Case Time Complexity
● The worst case time complexity of an algorithm is a measure of the maximum time that the algorithm will require for an input of size ‘n’.
● Example : If ‘n’ input data items are supplied in reverse order for any sorting algorithm, the algorithm will require n2 operations to perform the sort.
www.eshikshak.co.in
Average Case Time Complexity
● The time that an algorithm will require to execute a typical input data of size ‘n’ is known as average case time complexity.
www.eshikshak.co.in
Top Related