algorithms and complexity analysis

7

Upload: lawal

Post on 06-Nov-2015

220 views

Category:

Documents


1 download

DESCRIPTION

Every thing you want to know about algorithm and complexity analysis.- big omega- Big thetaand so on...

TRANSCRIPT

Algorithm and complexity analysis

Department of Computer Science. Federal University, Dutse. Jigawa State . 14Algorithm and complexity analysisLecture NoteMagaji Yusuf

1. Algorithms:An algorithm is a set of unambiguous instructions design to perform a particular task. Algorithm may have the following characteristics. It has zero or more quantities as an input. At least one quantity is computed as an output. Each instruction is computable. It terminates with answer or telling us no answer exists.

Algorithm Analysis: Is the analysis of the resource (such as time and memory space) usage of given algorithm.

2. Motivationwhy do we study analysis of algorithm? Efficient algorithm leads to efficient programs Efficient programs sells better Efficient programs make better use of hardware. Programmers who write efficient programs sells better

3. ComplexityComplexity refers to the rate at which time or space growth as a function of the problem size (for example, the size of the data on which an algorithm operate). The machine used to execute the program, the compiler used to construct the program and many other factors determine the absolute growth. Our main concentration will be on describing the inherent complexity of a program independent of machine/compiler considerations.

4. Asymptotic AnalysisAsymptotic analysis is based on the idea that as the problem size grows, the complexity will eventually settle down to a simple proportionality to some known function. It is used to explore the behavior of a function, or a relationship between functions as some parameter of a function tends towards an asymptotic value. This type of analysis shows that two functions are roughly equal to each other, or the value of some functions grow asymptotically at a greater rate than some other functions.Asymptotic analysis is based on the following notations: Big O (Upper bound) Big Omega (Lower bound) Big Theta (Tight bound)We will discuss this notations in details in the next part of this lecture. But now, let us analyze algorithm from machine language for better understanding of the above notations.5. Analysis of Algorithm from machine languageBy looking at how Java compiles code in to a machine language we can easily view the number of instructions produce by a particular code using Java debugger. First suppose that we invent a function Get(N) that computes the number of machine code instructions executed by algorithm a when run on the worst-case of problem of size N. The functions take an integer as a parameter(the problem size) and returns an integer as result (number of machine code instructions executed).For example, the following code computes the maximum value of an array of integerint max = Integer. MIN_VALUE; for(int i =0; i< a.length; i++) if a[i]>max; max= a[i];

The debugger will produce the following java code interspersed with machine code instructions. The code is been formatted for better understanding. int max = Integer.MIN_VALUE :A051E6B3D: 1209 Ldc (#9) 051E6B3F: 3D i_store_2

for(int i=0; i=a.length; i++) :B051E6B40: 04 iconst_0 //get 0051E6B41: 3E istore_3 //store it in i051E6B42: A70011 goto 0x51E6B53 // goto test near bottom

if(a[i]>max) :C051E6B45: 2B aload_1 //get a's base address051E6B46: 1D iload_3 //get i051E6B47: 2E aiload //get a[i]051E6B48: 1C iload_2 //get max051E6B49: A40007 if_icmple go to 0x051E6B53 //go to test near bottom if