Você está na página 1de 33

ALGORITHM ANALYSIS AND DESIGN

Definition of Algorithms

An algorithm is a finite set of instruction that if followed accomplishes a particular task.

An algorithm should satisfy the following criteria Input zero or more quantities are externally supplied. Output at least one quantity is produced Definiteness each instruction is clear and unambiguous Finiteness the algorithm terminates after a finite number of steps.

Effectiveness every instruction must be very basic so that it can be


carried out, by a person using only pencil and paper.

A program is an expression of algorithm in a programming language.

How to devise Algorithm :Creating an algorithm is an art which may never be fully automated. There are various design techniques. By mastering these design strategies, it will become easier for you to devise new and useful algorithms.

.
How to validate AlgorithmOnce an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs independent of issue concerning the programming language it eventually be written in.

How to Analyze Algorithms

Analysis of Algorithm or performance analysis refers to the task of determining how much computing time and storage an algorithm requires. This is a challenging area which some times requires great mathematical skill

How to test a program

Testing a programs consist of two phases Debugging:- Debugging is the process of executing the programs on sample data sets to determine whether faulty result occur and if so , to correct them.

Profiling or performance measurement is the process of executing a correct program on data set and measuring the time and space it take to compute the result.

Algorithm Specification

We can use natural languages like English Graphic representation is called flow chart Another representation is Pseudocode that resembles C and Pascal

Comments begin with // 2. Blocks are indicated with matching braces {} 3. An identifier begins with a letter 4. Assignment of values to variables is done using the assignment statement < variable> :=<expression> 5. Logical operators and ,or, not and the relational operators <, , =,,>, are provided
1.

7. The following looping statements are employed: for, while, repeat-until 8. The following conditional statements can be used If. Then. if .thenelse

9. Inputs and outputs are done using the instructions read and write 10. There is only one procedure: Algorithm the heading takes the form Algorithm Name ( parameter list)

Recursive Algorithms

A function is said to be recursive if it is defend in terms of itself. Similarly An algorithm is said to be recursive if the same algorithm is invoked in the body A algorithm that calls itself is direct recursive and an algorithm A is indirect recursive if it calls another algorithm which in turn calls A

PERFORMANCE ANALYSIS

Space Complexity Time Complexity

15

Fixed Space Requirements (C) Independent of the characteristics of the inputs and outputs.
Typically includes the instruction space(space for code) space for simple variables, fixed-size structured variable, constants and so on.

Space Complexity S(P)=C+SP(I)

Variable Space Requirements (SP(I)) Space needed by component variables whose size is dependent on the particular problem instance being solved. Space needed by referenced variables.
recursive stack space, return address
16

float abc(float a, float b, float c) { return a + b + b * c + (a + b - c) / (a + b) + 4.00; } S(I) = 0

17

Time complexity

The time T(P) taken by a program is the sum of the compile time and runtime. We may assume that a compiled program will run several times without recompilation. Consequently we concern ourselves with just the run time of the program. This runtime is denoted as Tp. .

Time Complexity
T(P)=C+TP(I)

Compile time (C) independent of instance characteristics Run (execution) time TP


TP(n)=caADD(n)+csSUB(n)+cmMUL(n)+cdDIV(n)+ Where n denotes the instance of characteristics

19

Ca, cs, cm, cd , and so on respectively, denote the time needed for addition, subtraction, multiplication, division and so on, ADD, SUB, MUL, DIV and so on are functions whose values are number of addition, subtraction multiplication, division and so on, that are performed when the code for P is used on an instance with characteristic n.

Asymptotic Notation (O, ,)

Big oh:The function f(n) = O(g(n)) iff there exist positive constants c and no such that f(n) c * g(n) for all n, n no

The function 3n + 2=O(n) as 3n +2 4n, for all n 2. Here value of g(n) =n, c= 4 & no =2 3n+3 4n for all n 3.

We write O(1) to mean a computing time that is a constant O(n) is called linear O(n2) is called quadratic O(n3) is called cubic O(2n) is called Exponential

f(n) is (g(n)) if positive constants c and n0 such that 0 f(n) cg(n) n n0 Eg: 3n+3=(n) as 3n+3 3n for n 1

f(n) is (g(n)) if positive constants c1, c2, and n0 such that c1 g(n) f(n) c2 g(n) n n0

f(n) = (g(n)) if and only if f(n) = O(g(n)) AND f(n) = (g(n))

3n+2 = (n) As 3n+2 3n and 3n+2 4n for all n 3 and c1=3 and c2 =4 and n0 =3
Eg

Computation Complexity

Definition: Measure the efficiency of an algorithm in terms of time or space required. Time tends to be more important. The efficiency of an algorithm is always stated as a function of the input size The Big-O notation: O(f(n))

27

Time Complexity

We assume each operation in a program take one time unit.


int sum (int n) { int partial_sum = 0; for (int i = 1; i <= n; i++) partial_sum = partial_sum + (i * i ); return partial_sum; }

Time Units to Compute ------------------------------ 1 for the assignment. 1 assignment, n+1 tests, and n increments. n loops of 3 units for an assignment, an addition, and one multiplication. 1 for the return statement. ---------------------------------------Total: 1+(1+n+1+n)+3n+1 = 5n+4 = O(n)

28

Basic Time Complexity Functions

In an increasing order of complexity:


Constant time: O(1) Logarithmic time: O(logn) Linear time: O(n) Polynomial time: O(n2) Exponential time: O(2n

Function Values
log n
0 1 2 3 4 5

n
1 2 4 8 16 32

n logn
0 2 8 24 64 160

n2
1 4 16 64 256 1024

n3
1 8 64 512 4096 32768

2n
2 4 16 256 65536 4294967296

30

Basic Time Complexity Functions


20
Running Time

O(2^n)

O(n^2)

O(n)

15

10

O(logn)
O(1)

0
0 5 10

Input Size
15 20 25

31

PERFORMANCE MEASUREMENT

Performance measurement is concerned with obtaining the space and time requirements of a particular algorithm. These quantities depend on the compiler and options used as well as on the computer on which the algorithm is run. Do not concern with the space and time needed for compilation. But this time is important during program testing.

We do not consider measuring runtime space requirement of a program. We focus on measuring the run time of a program. We assume the existence of a program GetTime() that returns the current time in milliseconds.

Você também pode gostar