Upload
gisa-elsa-jose
View
229
Download
0
Embed Size (px)
Citation preview
7/29/2019 AAD Module 1 S2
1/33
ALGORITHM
ANALYSIS AND DESIGN
7/29/2019 AAD Module 1 S2
2/33
Definition of Algorithms
An algorithm is a finite set of instruction
that if followed accomplishes a particular
task.
7/29/2019 AAD Module 1 S2
3/33
An algorithm should satisfy the following
criteria
Inputzero or more quantities are externally supplied.
Outputat least one quantity is produced
Definitenesseach instruction is clear and unambiguous
Finitenessthe algorithm terminates after a finite number of steps.
Effectivenessevery instruction must be very basic so that it can becarried out, by a person using only pencil and paper.
7/29/2019 AAD Module 1 S2
4/33
A program is an expression of algorithm in
a programming language.
7/29/2019 AAD Module 1 S2
5/33
How to devise Algorithm :-
Creating an algorithm is an art which may
never be fully automated. There are various
design techniques. By mastering thesedesign strategies, it will become easier for
you to devise new and useful algorithms.
7/29/2019 AAD Module 1 S2
6/33
.
How to validate Algorithm-
Once 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.
7/29/2019 AAD Module 1 S2
7/33
How to Analyze Algorithms
Analysis of Algorithm or performance
analysis refers to the task of determining
how much computing time and storage analgorithm requires. This is a challenging
area which some times requires great
mathematical skill
7/29/2019 AAD Module 1 S2
8/33
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 faultyresult occur and if so , to correct them.
7/29/2019 AAD Module 1 S2
9/33
Profiling or performance measurement is
the process of executing a correct program
on data set and measuring the time andspace it take to compute the result.
7/29/2019 AAD Module 1 S2
10/33
Algorithm Specification
We can use natural languages like English
Graphic representation is called flow chart
Another representation is Pseudocode that
resembles C and Pascal
7/29/2019 AAD Module 1 S2
11/33
1. Comments begin with //2. Blocks are indicated with matching braces
{ }
3. An identifier begins with a letter4. Assignment of values to variables is done
using the assignment statement
< variable> :=5. Logical operators and ,or, notand the
relational operators , are
provided
7/29/2019 AAD Module 1 S2
12/33
7. The following looping statements are
employed:
for, while, repeat-until
8. The following conditional statements can
be used
If. Then.
if .thenelse
7/29/2019 AAD Module 1 S2
13/33
9. Inputs and outputs are done using the
instructions readand write
10. There is only one procedure: Algorithmthe heading takes the form
AlgorithmName ( parameter list)
7/29/2019 AAD Module 1 S2
14/33
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 samealgorithm is invoked in the body
A algorithm that calls itself is direct
recursive and an algorithm A is indirectrecursive if it calls another algorithm which
in turn calls A
7/29/2019 AAD Module 1 S2
15/33
PERFORMANCE ANALYSIS
15
Space Complexity
Time Complexity
S C l it
7/29/2019 AAD Module 1 S2
16/33
16
Space Complexity
S(P)=C+SP(I) 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 structuredvariable, constants and so on.
Variable Space Requirements (SP(I))
Space needed by component variables whose size
is dependent on the particular problem instancebeing solved.
- Space needed by referenced variables.
recursive stack space, return address
7/29/2019 AAD Module 1 S2
17/33
17
float abc(float a, float b, float c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}
S(I) = 0
7/29/2019 AAD Module 1 S2
18/33
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 willrun several times without recompilation.
Consequently we concern ourselves with
just the run time of the program. Thisruntime is denoted as Tp. .
7/29/2019 AAD Module 1 S2
19/33
19
Time Complexity
Compile time (C)
independent of instance characteristics
Run (execution) time TP
T(P)=C+TP(I)
TP(n)=caADD(n)+csSUB(n)+cmMUL(n)+cdDIV(n)+
Where n denotes the instance of characteristics
7/29/2019 AAD Module 1 S2
20/33
Ca, cs, cm, cd , and so on respectively, denote the timeneeded for addition, subtraction, multiplication,division and so on, ADD, SUB, MUL,DIV and soon are functions whose values are number ofaddition, subtraction multiplication, division andso on, that are performed when the code for P isused on an instance with characteristic n.
7/29/2019 AAD Module 1 S2
21/33
Asymptotic Notation (O, ,)
7/29/2019 AAD Module 1 S2
22/33
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, forall n 2.
Here value of g(n) =n, c= 4 & no =2
3n+3 4n for all n 3.
7/29/2019 AAD Module 1 S2
23/33
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
7/29/2019 AAD Module 1 S2
24/33
f(n) is
(g(n)) if
positiveconstants c and n0such that 0
f(n) cg(n) n n0
Eg: 3n+3=(n) as 3n+3 3n for n 1
7/29/2019 AAD Module 1 S2
25/33
f(n) is
(g(n)) if
positive constantsc1, 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))
7/29/2019 AAD Module 1 S2
26/33
Eg 3n+2 = (n)
As 3n+2 3n and 3n+2 4n for all n 3 and c1=3 and c2 =4 and
n0 =3
7/29/2019 AAD Module 1 S2
27/33
27
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))
7/29/2019 AAD Module 1 S2
28/33
28
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
7/29/2019 AAD Module 1 S2
29/33
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
7/29/2019 AAD Module 1 S2
30/33
Function Values
30
log n n n logn n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 163 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
7/29/2019 AAD Module 1 S2
31/33
31
Basic Time Complexity Functions
0
5
10
15
20
0 5 10 15 20 25
R
unningTime
Input Size
O(n)
O(logn)
O(1)
O(n^2)O(2^n)
7/29/2019 AAD Module 1 S2
32/33
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.
7/29/2019 AAD Module 1 S2
33/33
We do not consider measuring runtime
spacerequirement 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.