×
Time can be your best friend and your worst enemy depending on whether you use it or waste it.
--Your friends at LectureNotes
Close

Analysis and Design of Algorithms

by Rakesh ChaudharyRakesh Chaudhary
Type: NoteInstitute: trivuwan university Course: B.Tech Specialization: Information Technology EngineeringOffline Downloads: 41Views: 602Uploaded: 4 months ago

Touch here to read
Page-1

Analysis and Design of Algorithms by Rakesh Chaudhary

Topic:
Rakesh Chaudhary
Rakesh Chaudhary

/ 83

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Rakesh Chaudhary
Rakesh Chaudhary
DAA New Summit College(B.Sc.CSIT) UNIT:- 1 Principles of Analyzing algorithms and Problems An algorithm is a finite set of computational instructions, each instruction can be executed in finite time, to perform computation or problem solving by giving some value, or set of values as input to produce some value, or set of values as output. Algorithms are not dependent on a particular machine, programming language or compilers i.e. algorithms run in same manner everywhere. So the algorithm is a mathematical object where the algorithms are assumed to be run under machine with unlimited capacity. Examples of problems • You are given two numbers, how do you find the Greatest Common Divisor. • Given an array of numbers, how do you sort them? We need algorithms to understand the basic concepts of the Computer Science, programming. Where the computations are done and to understand the input output relation of the problem we must be able to understand the steps involved in getting output(s) from the given input(s). You need designing concepts of the algorithms because if you only study the algorithms then you are bound to those algorithms and selection among the available algorithms. However if you have knowledge about design then you can attempt to improve the performance using different design principles. The analysis of the algorithms gives a good insight of the algorithms under study. Analysis of algorithms tries to answer few questions like; is the algorithm correct? i.e. the Algorithm generates the required result or not?, does the algorithm terminate for all the inputs under problem domain? The other issues of analysis are efficiency, optimality, etc. So knowing the different aspects of different algorithms on the similar problem domain we can choose the better algorithm for our need. This can be done by knowing the resources needed for the algorithm for its execution. Two most important resources are the time and the space. Both of the resources are measures in terms of complexity for time instead of absolute time we consider growth Algorithms Properties Input(s)/output(s): There must be some inputs from the standard set of inputs and an algorithm’s execution must produce outputs(s). Definiteness: Each step must be clear and unambiguous. Finiteness: Algorithms must terminate after finite time or steps. Correctness: Correct set of output values must be produced from the each set of inputs. Effectiveness: Each step must be carried out in finite time. Here we deal with correctness and finiteness. cs By Bhupendra Saud itn ep This RAM model is the base model for our study of design and analysis of algorithms to have design and analysis in machine independent scenario. In this model each basic operations (+, -) takes 1 step, loops and subroutines are not basic operations. Each memory reference is 1 step. We measure run time of algorithm by counting the steps. al Random Access Machine Model 1 Source: www.csitnepal.com
DAA New Summit College(B.Sc.CSIT) Best, Worst and Average case Best case complexity gives lower bound on the running time of the algorithm for any instance of input(s). This indicates that the algorithm can never have lower running time than best case for particular class of problems. Worst case complexity gives upper bound on the running time of the algorithm for all the instances of the input(s). This insures that no input can overcome the running time limit posed by worst case complexity. Average case complexity gives average number of steps required on any instance of the input(s). In our study we concentrate on worst case complexity only. Example 1: Fibonacci Numbers Input: n Output: nth Fibonacci number. Algorithm: assume a as first(previous) and b as second(current) numbers fib(n) { a = 0, b= 1, f=1 ; for(i = 2 ; i <=n ; i++) { f = a+b ; a=b ; b=f ; } return f ; } Efficiency Time Complexity: The algorithm above iterates up to n-2 times, so time complexity is O(n). Space Complexity: The space complexity is constant i.e. O(1). Mathematical Foundation Since mathematics can provide clear view of an algorithm. Understanding the concepts of mathematics is aid in the design and analysis of good algorithms. Here we present some of the mathematical concepts that are helpful in our study. Exponents Some of the formulas that are helpful are: ep itn cs By Bhupendra Saud al xa xb = xa+b xa / xb = xa-b (x a)b = xab xn + xn = 2xn 2n + 2n = 2n+1 2 Source: www.csitnepal.com
DAA New Summit College(B.Sc.CSIT) Logarithmes Some of the formulas that are helpful are: 1. logab = logcb / logc a ; c>0 2. log ab = log a + log b 3. log a/b = log a - log b 4. log (ab) = b log a 5. Log x < x for all x>0 6. Log 1 = 0, log 2 = 1, log 1024 = 10. 7. a logbn = n logba Series Asymptotic Notation Complexity analysis of an algorithm is very hard if we try to analyze exact. we know that the complexity (worst, best, or average) of an algorithm is the mathematical function of the size of the input. So if we analyze the algorithm in terms of bound (upper and lower) then it would be easier. For this purpose we need the concept of asymptotic notations. The figure below gives upper and lower bound concept. cs By Bhupendra Saud itn ep When we have only asymptotic upper bound then we use O notation. A function f(x)=O(g(x)) (read as f(x) is big oh of g(x) ) iff there exists two positive constants c and x 0 such that for all x >= x0, 0 <= f(x) <= c*g(x) The above relation says that g(x) is an upper bound of f(x) Some properties: Transitivity: f(x) = O(g(x)) & g(x) = O(h(x)) _ f(x) = O(h(x)) Reflexivity: f(x) = O(f(x)) al Big Oh (O) notation 3 Source: www.csitnepal.com
DAA New Summit College(B.Sc.CSIT) O(1) is used to denote constants. For all values of n >= n0, plot shows clearly that f(n) lies below or on the curve of c*g(n) Examples 1. 2. f(n) = 3n2 + 4n + 7 g(n) = n2 , then prove that f(n) = O(g(n)). Proof: let us choose c and n0 values as 14 and 1 respectively then we can have f(n) <= c*g(n), n>=n0 as 3n2 + 4n + 7 <= 14*n2 for all n >= 1 The above inequality is trivially true Hence f(n) = O(g(n)) Prove that n log (n3) is O(√n3)). Proof: we have n log (n3) = 3n log n Again, √n3 = n √n, If we can prove log n = O(√n) then problem is solved Because n log n = n O(√n) that gives the question again. We can remember the fact that log a n is O (nb) for all a,b>0. In our problem a = 1 and b = 1/2 , hence log n = O(√n). So by knowing log n = O(√n) we proved that n log (n3) = O(√n3)). cs By Bhupendra Saud itn ep Big omega notation gives asymptotic lower bound. A function f(x) =Ω (g(x)) (read as g(x) is big omega of g(x) ) iff there exists two positive constants c and x 0 such that for all x >= x0, 0 <= c*g(x) <= f(x). The above relation says that g(x) is a lower bound of f(x). Some properties: Transitivity: f(x) = O(g(x)) & g(x) = O(h(x)) _ f(x) = O(h(x)) Reflexivity: f(x) = O(f(x)) al Big Omega (Ω) notation 4 Source: www.csitnepal.com

Lecture Notes