Escolar Documentos
Profissional Documentos
Cultura Documentos
Definition of Algorithms
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.
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.
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
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
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.
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
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)
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.
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
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
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
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
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.