SlideShare une entreprise Scribd logo
1  sur  58
Télécharger pour lire hors ligne
UNIT I
INTRODUCTION ATO ANALYSIS OF ALGORITHMH
Dr. Parikshit N. Mahalle
M.E. (Comp. Engg.), Ph.D
Senior Member IEEE, LMISTE, LMCSI, Member ACM, Member IAENG (H.K.)
Professor and Head
Department of Artificial Intelligence and Data Science
Bansilal Ramnath Agarwal Charitable Trust's,
Vishwakarma Institute of Information Technology,
(An Autonomous Institute Affiliated to Savitribai Phule Pune University)
An ISO 9001-2015 Certified Institute Accredited with 'A' Grade By NAAC
Kondhawa (Bk),
Pune - 411048
Email: aalborg.pnm@gmail.com | parikshit.mahalle@viit.ac.in
Algorithm
• An Algorithm is a finite set of instructions that, if
followed, accomplishes a particular task.
• All algorithms must 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 : If we trace out the instructions of an algorithm, then
for all cases, the algorithm terminates after a finite number of
steps.
– Effectiveness : Every instruction must be very basic and effective.
It is not enough that each operation be definite as in criterion 3; it
also must be feasible.
Uses of Algorithm
• It gives a language independent layout of
the program.
• So using the basic layout we can develop
the program in any desired language.
• Algorithm representation is very easy to
understand.
• It facilitates or makes coding easy.
Develop an algorithm to find maximum of 10 numbers.
1. Start
2. Let count = 1
3. Read a number, say x
4. Let max = x
5. Check if count is equal to 10 , if yes go to Step 10
6. Read a number, say x
7. Add 1 to count
8. Check if x is greater than max, if yes Set max = x
9. go to step 5
10. Display max
11. Stop.
Flow chart
• A flow chart is a pictorial representation of an
algorithm.
• An algorithm is first represented in the form of
flowchart and flowchart is then expressed in some
programming language to prepare a computer
program. Since, flowcharts shows the flow of
operations in diagrammatic form, any error in the
logic of the procedure can be detected more easily
than in case of a program. Once flowchart is ready
it is very easy to write a program in terms of
statements of a programming language
Pseudo code
Pseudo code is nothing but an informal way of
writing a program. It is a combination of
algorithm written in simple English and some
programming language. In pseudo code, there is
no restriction of following the syntax of the
programming language. Pseudo codes can not be
compiled. It is just a previous step of developing a
code for given algorithm. Even sometime by
examining the pseudo code one can decide which
language has to select for implementation.
• e.g The following algorithm (Psuedo C code) finds
and returns the maximum of n numbers;
Algorithm Max (A,n)
/*A is an array of size n*/
{
Assume max as A[0] ;
for( i = 0 ; i < n ; i = i +1 )
{
if A[i] > max then Set max to A[i] ;
}
return max;
}
The study of algorithm involves four
main areas
1. How to devise algorithms
2. How to validate algorithms
3. How to analyze algorithms
4. How to test a program
1. How to devise algorithms
1. This area has attracted many because the answer
to the question cannot be put in algorithmic form.
2. Creating an algorithm is an art which may never
be fully automated. We can Study the various
design techniques that have proven to be useful
in that they have often yielded good algorithm.
3. By mastering these design strategies, it will
become easier to devise new and useful
algorithms.
2. How to validate algorithms
1. Once an algorithm is devised, it is necessary to
show that it computes the correct answer for all
possible legal inputs. We refer to this process as
algorithm validation.
2. The purpose of the validation is to assure us that
this algorithm will work correctly independently
of the issues concerning the programming
language it will eventually be written in.
3. Once the validity of the method has been shown,
a program can be written and a second phase
begins. This phase is referred to as program
proving or sometimes as program verification.
3. How to analyze algorithms
1. Analysis of algorithms or performance
analysis refers to the task of determining
how much computing time and storage an
algorithm requires.
2. The efficiency is measured in best case,
worst case and average case.
4. How to test a program
• Testing a program consists of two phases:
– Debugging : It is the process of executing
programs on sample data sets to determine
whether faulty results occur and, if so, to
correct them.
– Profiling : Profiling or performance
measurement is the process of executing a
correct program on data sets and measuring
the time and space it takes to compute the
result.
Program development cycle
• Feasibility Study
• Requirement Analysis and problem
specifications
• Design
• Implementation / Coding
• Debugging
• Testing
• Maintenance
• Documentation
Performance Analysis
There are many criteria upon which we can judge an
algorithm. For instance:
• Does it do what we want to do?
• Does it work correctly according to the original
specifications of the task?
• Is there documentation that describes how to use
it and how it works?
• Are procedures created in such a way that they
perform logical subfunctions?
• Is the code readable?
There are other criteria for judging algorithms that
have a more direct relationship to performance . These have
to do with their computing time and storage requirements.
• Space Complexity : The space complexity of an
algorithm is the amount of memory it needs to run to
completion.
• Time Complexity : The time complexity of an
algorithm is the amount of computer time it needs to run
to completion.
• Performance evaluation can be loosely divided into two
major phases :
– A Priori estimates and
– A posteriori testing
• We refer to these as performance analysis and
performance measurement respectively.
Space Complexity
The space needed by any algorithm is seen to be the sum of following
components :
• A fixed part that is independent of the characteristics (e.g.
number,size) of the inputs and outputs. This part typically
includes the instruction space (i.e. space for the code), space for
simple variables and fixed-size component variables (also called
aggregate), space for constants, and so on…
• A variable part that consists of the space needed by component
variables whose size is dependent on the particular problem
instance being solved, the space needed by referenced variables
(to the extent that this depends on instance characteristics), and
the recursion stack space.
• The space requirement S(P) of any algorithm P may therefore be
written as S(P) = c + Sp(instance characteristics), where c is a
constant.
Time Complexity
The time T(P) taken by a program P is the sum of
the compile time and the run (or execution) time.
The compile time does not depend on the instance
characteristics. Also, we may assume that a
compiled program will be run several times
without recompilation. Consequently, we concern
ourselves with just the run time of a program. This
runtime is denoted by tp(instance characteristics).
Analysis Basics
• Many algorithms that can solve a given problem
will have different characteristics that will
determine how efficiently each will operate.
• When we analyze an algorithm, we first have to
show that the algorithm does properly solve the
problem because if it doesn‟t, its efficiency is not
important.
• Next, we look at how efficiently it solves the
problem.
• Analyzing an algorithm determines the
amount of “time” that algorithm takes to
execute.
• This is not really a number of seconds or
any other clock measurement but rather an
approximation of the number of operations
that an algorithm performs.
Algorithms classification
• Repetitive algorithms
– Have loops and conditional statements as their basis, and so
their analysis will entail determining the work done in the
loop and how many times the loop executes
• Recursive algorithms
– Solve a large problem by breaking it into pieces and then
applying the algorithm to each of the pieces.
– Analyzing will entail determining the amount of work done
to produce the smaller pieces, and then putting their
individual solutions together to get the solution to the whole
problem.
– Combining this information with the number of the smaller
pieces and their sizes, we can produce a recurrence relation
for the algorithm.
– This recurrence relation can then be converted into a closed
form that can be compared with other equations.
What is analysis
• Analysis of an algorithm provides
background information that gives us a
general idea of how long an algorithm will
take for a given problem set.
• Studying the analysis of algorithms gives us
the tools to choose between algorithms.
Algorithm to find the largest of 4 nos
largest = a
if b > largest then
largest = b
end if
if c > largest then
largest = c
end if
if d > largest then
largest = d
end if
return largest
Algorithm to find the largest of 4 nos
if a > b then
if a > c then
if a > d then
return a
else
return d
end if
else
if c > d then
return c
else
return d
end if
end if
else
Continued
else
if b > c then
if b > d then
return b
else
return d
end if
else
if c > d then
return c
else
return d
end if
end if
end if
• The purpose of analysis of algorithms is not to give a
formula that will tell us exactly how many seconds or
computer cycles a particular algorithm will take.
• This is not useful information because we would then
need to talk about the type of computer, whether it
has one or many users at a time, what processor it
has, how fast its clock is, whether it has a complex or
reduced instruction set processor chip, and how well
the compiler optimizes the executable code.
• All of those will have an impact on how fast a
program for an algorithm will run.
• To talk about analysis in those terms would mean that
by moving a program to a faster computer, the
algorithm would become better because it now
completes its job faster.
• That‟s not true, so , we do our analysis without regard
to any specific computer.
What to count and consider
• Deciding what to count involves two steps
– Choosing the significant operation or
operations
– Deciding which of those operations are integral
to the algorithm and which are overhead.
• Two classes of operations
– Comparison
– Arithmetic (additive , multiplicative operators)
Input classes
• Input plays an important role in analyzing
algorithms because it is the input that
determines what the path of execution
through an algorithm will be.
• When looking at the input, we will try to
break up all the different input sets into
classes based on how the algorithm behaves
on each set.
Cases to consider
• Choosing what input to consider when analyzing
an algorithm can have a significant impact on how
an algorithm will perform.
• We will actually look for those input set that allow
an algorithm to perform the most quickly and most
slowly.
• We will also consider an overall average
performance of the algorithm as well.
Best Case
• Is the input that requires the algorithm to take the
shortest time.
• This input is the combination of values that causes
the algorithm to do the least amount of work.
• Because the best case for an algorithm will usually
be a very small and frequently constant value, we
will not do a best-case analysis very frequently.
Worst Case
• Important analysis because it gives us an idea of
the most time an algorithm will ever take.
• Worst case analysis requires that we identify the
input values that cause an algorithm to do the most
work.
• The worst case gives us an upper bound on how
slowly parts of our programs may work based on
our algorithm choices.
Average Case
• Toughest to do because there are a lot of details
involved.
• The basic process begins by determining the
number of different groups into which all
possible input sets can be divided.
• The second step is to determine the probability
that the input will come from each of these
groups.
• The third step is to determine how long the
algorithm will run for each of these groups.
Average Case
• All of the input in each group should take the
same amount of time ,and if they do not, the
group must be split into two separate groups.
• When all of this has been done, the average case
time is given by the following formula:
m
• A(n) = ∑ i=1 pi * ti
• Where
– n : size of the input
– m : no. of groups
– pi : probability that the input will be from group i
– ti : time that the algorithm takes for input from
group i.
Frequency Count
• It is the number of times the instruction (Step in
algorithm) is executed in program (Algorithm).
• Total Computational time for an instruction =
Instruction execution time * Frequency count.
• Now instruction execution time depends upon
– speed of processor (instruction per millisecond)
– instruction set of the processor
– time required to execute instruction
– compiler for translating the instruction into machine language.
• All these factors independent of the algorithm and
external to it. Hence performance of an algorithm (time
complexity) is measured in terms of Frequency count.
Hence only Frequency count is important in deciding
the performance (time complexity) of an algorithm.
Stm
t.
No
Statement s / e Frequency Total Steps
1 Algorithm Sum(a,n) 0 -- 0
2 { 0 -- 0
3 s = 0; 1 1 1
4 for i = 1 to n do 1 n+1 n + 1
5 s = s + a[i]; 1 n n
6 return s; 1 1 1
7 } 0 -- 0
Total 2n + 3
St
m
t
.
N
o
Statement
1 Algorithm Add(a,b,c,m,n)
2 {
3 for i = 1 to m do
4 for j = 1 to n do
5 c[i,j] = a[i,j] + b[i,j];
6 }
Total
Stmt
.
N
o
Statement s / e Frequency Total Steps
1 Algorithm Add(a,b,c,m,n) 0 -- 0
2 { 0 -- 0
3 for i = 1 to m do 1 m+1 m+1
4 for j = 1 to n do 1 m(n+1) mn+m
5 c[i,j] = a[i,j] + b[i,j]; 1 mn mn
6 } 0 -- 0
Total 2mn + 2m + 1
Determine the frequency count for each statement in
the following piece of code. Obtain its time
complexity in terms of „n‟
for( i=1; i ≤ n ; i++)
{
for( j=1; j ≤ i ; j++)
{
for( k=1 ; k ≤ j ; k++)
{
s = x + 1;
}
}
}
Determine the frequency count for each statement in
the following piece of code. Obtain its time
complexity in terms of „n‟
i=0
while(i ≤ n)
{
j = i;
while( j ≤ i +1 )
{
a = a * a;
j ++;
}
i++;
}
Stmt
.
N
o
Statement s / e Frequency Total Steps
n = 0 n > 0 n = 0 n > 0
1 Algorithm RSum(a,n) 0 -- -- 0 0
2 { 0 -- -- 0 0
3 if (n <= 0) then 1 1 1 1 1
4 return 0.0; 1 1 0 1 0
5 else 0 -- -- -- --
6 return RSum(a,n-1) + a[n]; 1 + x 0 1 0 1 + x
7 } 0 -- -- 0 0
Total 2 2 + x
Where x = tRsum(n-1)
Mathematical Background
N
∑ 1 = N
i = 1
N
∑ C = C x N
i = 1
N
∑ i = N(N+1) / 2
i = 1
N
∑ i2 = N(N+1)(2N+1) / 6
i = 1
Mathematical Background
N
∑ 2i = 2N+1 - 1
i = 0
N
∑ Ai = ( AN+1 - 1) / ( A – 1 )
i = 1
N
∑ i 2i = (N-1) 2N+1 + 2
i = 1
N
∑ 1 / i = ln N
i = 1
Mathematical Background
N
∑ lg i ≈ N lg N – 1.5
i = 1
Asymptotic Notation ( Ο Ω Θ )
[Big “oh” ] : This notation is used to
express an upper bound on computing time
of an algorithm. When we say that time
complexity of Selection sort algorithm is
Ο(n2) , means that for sufficiently large
values of „n‟ , computation time will not
exceed some constant time * n2 i.e
proportional to n2 (Worst Case).
[Big “oh”] Ο
• Definition : The function f(n) = Ο( g(n) ) (read as “f of n
is big oh of g of n”) iff (if and only if ) there exist
positive constants c and n0 such that f(n) <= c * g(n)
for all n, n >= n0.
• Example:
• The function 3n+2 = Ο(n) as 3n + 2 <= 4n for all n >= 2
• The function 3n+3 = Ο(n) as 3n + 3 <= 4n for all n >= 3
• The function 100n+6 = Ο(n) as 100n + 6 <= 101n for all
n >= 6
• The function 10n2+4n+2 = Ο(n2) as 10n2+4n+2 <= 11n2
for all n >= 5
• The function 1000n2+100n-6 = Ο(n2) as 1000n2+100n-6
<= 1001n2 for all n >= 100
• The function 6*2n + n2 = Ο(2n) as 6*2n + n2 <= 7*2n for
all n >= 4
[Omega] Ω
This notation is used to express an lower
bound on computing time of an algorithm.
When we say that best case time complexity
of insertion sort is Ω (n), means that for
sufficiently large values of „n‟, minimum
computation time will be some constant
time * n i.e proportional to n.
[Omega] Ω
• Definition : The function f(n) = Ω( g(n) ) (read as “f of n
is omega of g of n”) iff there exist positive constants c
and n0 such that f(n) >= c * g(n) for all n, n >= n0.
• Example:
• The function 3n+2 = Ω (n) as 3n + 2 >= 3n for all n >= 1
• The function 3n+3 = Ω (n) as 3n + 3 >= 3n for all n >= 1
• The function 100n+6 = Ω (n) as 100n + 6 >= 100n for all
n >= 1
• The function 10n2+4n+2 = Ω (n2) as 10n2+4n+2 >= n2 for
all n >= 1
• The function 6*2n + n2 = Ω (2n) as 6*2^n + n2 >= 2n for
all n >= 1
[Theta] Θ
• This notation is used to express time complexity
of an algorithm when it is same for worst & Best
cases. For example best and worst case time
complexities for selection sort is Ο(n2) & Ω (n2)
i.e. it can be expressed as Θ (n2).
• Definition : The function f(n) = Θ( g(n) ) (read as
“f of n is theta of g of n”) iff there exist positive
constants c1, c2 and n0 such that c1 *g(n) <= f(n)
<= c2 * g(n) for all n, n >= n0.
• Example:
• The function 3n+2 = Θ (n) as 3n + 2 >= 3n
for all n >= 2 and 3n + 2 <= 4n for all n >= 2.
• The function 3n+3 = Θ (n)
• The function 10n2+4n+2 = Θ (n2)
• The function 6*2n + n2 = Θ (2n)
Recurrence Relations
• Recurrence relations can be directly derived from a recursive
algorithm, but they are in a form that does not allow us to
quickly determine how efficient the algorithm is.
• To do that we need to convert the set of recursive equations into
what is called closed form by removing the recursive nature of
the equations.
• This is done by a series of repeated substitutions until we can
see the pattern that develops.
• The easiest way to see this process is by a series of examples.
• Examples :
– T(n) = 2 T(n-2) – 15
T(2) = 40
T(1) = 40
– T(n) = 4 if n ≤ 4
= 4 T(n/2) – 1 otherwise
Consider the following recurrence relation
– T(n) = 2 T(n-2) – 15
T(2) = 40
T(1) = 40
• Substitution
T(n - 2) = 2 T (n – 2 – 2 ) – 15 = 2 T(n-4) – 15
T(n - 4) = 2 T(n-6) - 15
T(n - 6) = 2 T(n-8) – 15
T(n - 8) = 2 T(n-10) – 15
T(n - 10) = 2 T(n-12) – 15
Now we begin to substitute back into the original equation.
• T(n) = 2T(n-2) – 15 = 2(2T(n-4) – 15 ) – 15
• T(n) = 4T(n-4) – 2*15 – 15
• T(n) = 4( 2T(n-6) – 15 ) – 2 * 15 - 15
• T(n) = 8T(n-6) – 4 * 15 - 2*15 – 15
• T(n) = 8(2T(n-8) – 15) – 4 * 15 - 2*15 – 15
• T(n) = 16T(n-8) – 8*15 - 4 * 15 - 2*15 – 15
• T(n) = 16(2T(n-10) – 15 ) – 8*15 - 4 * 15 - 2*15 – 15
• T(n) = 32T(n-10) – 16*15 - 8*15 - 4 * 15 - 2*15 – 15
• T(n) = 32(2T(n-12)–15) – 16*15 - 8*15 - 4 * 15 - 2*15 – 15
• T(n) = 64T(n-12)–32*15 -16*15 - 8*15 - 4 * 15 - 2*15 – 15
• How many times we have to substitute back into
this equation to get to either of two values ?
• If n is even (2 = n – (n-2))
• This seems to indicate that we would substitute
back into this equation [(n-2) / 2] – 1 times giving
n/2 – 1 terms based on -15 in the equation, and the
power of coefficient of T will be n/2 - 1.
• If n is odd, the only thing that would change is that
T would have a value of 1 instead of 2, but by our
equations, n/2 – 1 is 5 (not 6) when n is 13.
• For odd n, we will use n/2 instead of n/2 – 1.
• We will have two cases in our answer.
( n/2 – 1)
• T(n) = 2(n/2)-1 T(2) – 15 ∑ 2i if n is even
i = 0
( n/2 – 1)
• T(n) = 2(n/2)-1 * 40 – 15 ∑ 2i if n is even
i = 0
n/2
• T(n) = 2(n/2) T(1) – 15 ∑ 2i if n is odd
i = 0
n/2
• T(n) = 2(n/2)-1 * 40 – 15 ∑ 2i if n is odd
i = 0
• Now applying the equation for an even value of n
we get
T(n) = 2(n/2)-1 * 40 – 15* (2n/2 – 1)
= 2n/2 * 20 - 2n/2 *15 + 15
= 2n/2 ( 20 – 15) + 15
= 2n/2 * 5 + 15
• for an odd value of n we get
T(n) = 2n/2 * 40 – 15* (2n/2 + 1 – 1)
= 2n/2 * 40 - 2n/2 *30 + 15
= 2n/2 ( 40 – 30) + 15
= 2n/2 * 10 + 15
Consider the following recurrence relation
– T(n) = 5 if n ≤ 4
= 4 T(n / 2) - 1 otherwise
• Substitution
T(n/2) = 4T(n/4) – 1
T(n/4) = 4T(n/8) – 1
T(n/8) = 4T(n/16) – 1
T(n/16) = 4T(n/32) – 1
T(n/32) = 4T(n/64) – 1
Now we begin to substitute back into the original equation.
• T(n) = 4T(n/2) – 1 = 4 (4 T(n/4) – 1 ) – 1
• T(n) = 16T(n/4) – 4*1 – 1
• T(n) = 16( 4T(n/8) – 1 ) – 4 * 1 – 1
• T(n) = 64T(n/8) – 16 * 1 – 4 * 1 – 1
• T(n) = 64(4T(n/16) - 1) – 16 * 1 – 4 * 1 – 1
• T(n) = 256T(n/16) – 64*1 – 16 * 1 – 4 * 1 – 1
• T(n) = 256(4T(n/32)-1) – 64*1 – 16 * 1 – 4 * 1 – 1
• T(n) = 1024T(n/32) – 256 * 1 – 64*1 – 16 * 1 – 4 * 1 – 1
• T(n) = 1024(4T(n/64)-1) – 256*1 – 64*1 – 16*1 – 4*1 – 1
• T(n) = 4096T(n/64) – 1024*1– 256*1–64*1–16*1–4*1–1
• We notice that the coefficient of -1
increases by a power of 4 each time we
substitute, and it is the case that the power
of 2 that we divide n by is 1 greater than the
largest power of 4 for this coefficient.
• Also, we notice that the coefficient of T is
the same power of 4 as the power of 2 that
we divide n by.
• When we have T(n/2), its coefficient will be
4i and we will have terms from –(4i-1) to -1
• Now , for what value of i can we stop the
substitution?
• Well, because we have the direct case
specified for n ≤ 4, we can stop when we
get to T(4) = T(n / 2lg n - 2).
• Putting together we get.
lg n - 2
• T(n) = 4lg n - 2 T(4) - ∑ 4i
i = 0
• Using the value for the direct case, and
main eqn we get
T(n) = 4lg n - 2 * 5 - (4lg n - 2 - 1) / (4 – 1)
= 4lg n - 2 * 5 - (4lg n - 2 - 1) / 3
= ( 15 * 4lg n - 2 - 4lg n - 2 + 1) / 3
= (4lg n - 2 * (15 – 1) + 1) / 3
= (4lg n - 2 * 14 + 1) / 3

Contenu connexe

Tendances

Tendances (20)

A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Randomized Algorithm
Randomized AlgorithmRandomized Algorithm
Randomized Algorithm
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
3.6 radix sort
3.6 radix sort3.6 radix sort
3.6 radix sort
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Hashing
HashingHashing
Hashing
 
Sets and disjoint sets union123
Sets and disjoint sets union123Sets and disjoint sets union123
Sets and disjoint sets union123
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 
S-DES.ppt
S-DES.pptS-DES.ppt
S-DES.ppt
 
NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 

Similaire à Algorithm Analysis.pdf

Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptxssuser586772
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptTekle12
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit onessuserb7c8b8
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptxwondmhunegn
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basicsayeshasafdar8
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
Program concep sequential statements
Program concep sequential statementsProgram concep sequential statements
Program concep sequential statementsankurkhanna
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - IntroductionMadhu Bala
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureVrushali Dhanokar
 

Similaire à Algorithm Analysis.pdf (20)

Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.ppt
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.ppt
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptx
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basics
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Program concep sequential statements
Program concep sequential statementsProgram concep sequential statements
Program concep sequential statements
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structure
 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Dernier (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Algorithm Analysis.pdf

  • 1. UNIT I INTRODUCTION ATO ANALYSIS OF ALGORITHMH Dr. Parikshit N. Mahalle M.E. (Comp. Engg.), Ph.D Senior Member IEEE, LMISTE, LMCSI, Member ACM, Member IAENG (H.K.) Professor and Head Department of Artificial Intelligence and Data Science Bansilal Ramnath Agarwal Charitable Trust's, Vishwakarma Institute of Information Technology, (An Autonomous Institute Affiliated to Savitribai Phule Pune University) An ISO 9001-2015 Certified Institute Accredited with 'A' Grade By NAAC Kondhawa (Bk), Pune - 411048 Email: aalborg.pnm@gmail.com | parikshit.mahalle@viit.ac.in
  • 2. Algorithm • An Algorithm is a finite set of instructions that, if followed, accomplishes a particular task. • All algorithms must 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 : If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps. – Effectiveness : Every instruction must be very basic and effective. It is not enough that each operation be definite as in criterion 3; it also must be feasible.
  • 3. Uses of Algorithm • It gives a language independent layout of the program. • So using the basic layout we can develop the program in any desired language. • Algorithm representation is very easy to understand. • It facilitates or makes coding easy.
  • 4. Develop an algorithm to find maximum of 10 numbers. 1. Start 2. Let count = 1 3. Read a number, say x 4. Let max = x 5. Check if count is equal to 10 , if yes go to Step 10 6. Read a number, say x 7. Add 1 to count 8. Check if x is greater than max, if yes Set max = x 9. go to step 5 10. Display max 11. Stop.
  • 5. Flow chart • A flow chart is a pictorial representation of an algorithm. • An algorithm is first represented in the form of flowchart and flowchart is then expressed in some programming language to prepare a computer program. Since, flowcharts shows the flow of operations in diagrammatic form, any error in the logic of the procedure can be detected more easily than in case of a program. Once flowchart is ready it is very easy to write a program in terms of statements of a programming language
  • 6. Pseudo code Pseudo code is nothing but an informal way of writing a program. It is a combination of algorithm written in simple English and some programming language. In pseudo code, there is no restriction of following the syntax of the programming language. Pseudo codes can not be compiled. It is just a previous step of developing a code for given algorithm. Even sometime by examining the pseudo code one can decide which language has to select for implementation.
  • 7. • e.g The following algorithm (Psuedo C code) finds and returns the maximum of n numbers; Algorithm Max (A,n) /*A is an array of size n*/ { Assume max as A[0] ; for( i = 0 ; i < n ; i = i +1 ) { if A[i] > max then Set max to A[i] ; } return max; }
  • 8. The study of algorithm involves four main areas 1. How to devise algorithms 2. How to validate algorithms 3. How to analyze algorithms 4. How to test a program
  • 9. 1. How to devise algorithms 1. This area has attracted many because the answer to the question cannot be put in algorithmic form. 2. Creating an algorithm is an art which may never be fully automated. We can Study the various design techniques that have proven to be useful in that they have often yielded good algorithm. 3. By mastering these design strategies, it will become easier to devise new and useful algorithms.
  • 10. 2. How to validate algorithms 1. Once an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs. We refer to this process as algorithm validation. 2. The purpose of the validation is to assure us that this algorithm will work correctly independently of the issues concerning the programming language it will eventually be written in. 3. Once the validity of the method has been shown, a program can be written and a second phase begins. This phase is referred to as program proving or sometimes as program verification.
  • 11. 3. How to analyze algorithms 1. Analysis of algorithms or performance analysis refers to the task of determining how much computing time and storage an algorithm requires. 2. The efficiency is measured in best case, worst case and average case.
  • 12. 4. How to test a program • Testing a program consists of two phases: – Debugging : It is the process of executing programs on sample data sets to determine whether faulty results occur and, if so, to correct them. – Profiling : Profiling or performance measurement is the process of executing a correct program on data sets and measuring the time and space it takes to compute the result.
  • 13. Program development cycle • Feasibility Study • Requirement Analysis and problem specifications • Design • Implementation / Coding • Debugging • Testing • Maintenance • Documentation
  • 14. Performance Analysis There are many criteria upon which we can judge an algorithm. For instance: • Does it do what we want to do? • Does it work correctly according to the original specifications of the task? • Is there documentation that describes how to use it and how it works? • Are procedures created in such a way that they perform logical subfunctions? • Is the code readable?
  • 15. There are other criteria for judging algorithms that have a more direct relationship to performance . These have to do with their computing time and storage requirements. • Space Complexity : The space complexity of an algorithm is the amount of memory it needs to run to completion. • Time Complexity : The time complexity of an algorithm is the amount of computer time it needs to run to completion. • Performance evaluation can be loosely divided into two major phases : – A Priori estimates and – A posteriori testing • We refer to these as performance analysis and performance measurement respectively.
  • 16. Space Complexity The space needed by any algorithm is seen to be the sum of following components : • A fixed part that is independent of the characteristics (e.g. number,size) of the inputs and outputs. This part typically includes the instruction space (i.e. space for the code), space for simple variables and fixed-size component variables (also called aggregate), space for constants, and so on… • A variable part that consists of the space needed by component variables whose size is dependent on the particular problem instance being solved, the space needed by referenced variables (to the extent that this depends on instance characteristics), and the recursion stack space. • The space requirement S(P) of any algorithm P may therefore be written as S(P) = c + Sp(instance characteristics), where c is a constant.
  • 17. Time Complexity The time T(P) taken by a program P is the sum of the compile time and the run (or execution) time. The compile time does not depend on the instance characteristics. Also, we may assume that a compiled program will be run several times without recompilation. Consequently, we concern ourselves with just the run time of a program. This runtime is denoted by tp(instance characteristics).
  • 18. Analysis Basics • Many algorithms that can solve a given problem will have different characteristics that will determine how efficiently each will operate. • When we analyze an algorithm, we first have to show that the algorithm does properly solve the problem because if it doesn‟t, its efficiency is not important. • Next, we look at how efficiently it solves the problem.
  • 19. • Analyzing an algorithm determines the amount of “time” that algorithm takes to execute. • This is not really a number of seconds or any other clock measurement but rather an approximation of the number of operations that an algorithm performs.
  • 20. Algorithms classification • Repetitive algorithms – Have loops and conditional statements as their basis, and so their analysis will entail determining the work done in the loop and how many times the loop executes • Recursive algorithms – Solve a large problem by breaking it into pieces and then applying the algorithm to each of the pieces. – Analyzing will entail determining the amount of work done to produce the smaller pieces, and then putting their individual solutions together to get the solution to the whole problem. – Combining this information with the number of the smaller pieces and their sizes, we can produce a recurrence relation for the algorithm. – This recurrence relation can then be converted into a closed form that can be compared with other equations.
  • 21. What is analysis • Analysis of an algorithm provides background information that gives us a general idea of how long an algorithm will take for a given problem set. • Studying the analysis of algorithms gives us the tools to choose between algorithms.
  • 22. Algorithm to find the largest of 4 nos largest = a if b > largest then largest = b end if if c > largest then largest = c end if if d > largest then largest = d end if return largest
  • 23. Algorithm to find the largest of 4 nos if a > b then if a > c then if a > d then return a else return d end if else if c > d then return c else return d end if end if else Continued else if b > c then if b > d then return b else return d end if else if c > d then return c else return d end if end if end if
  • 24. • The purpose of analysis of algorithms is not to give a formula that will tell us exactly how many seconds or computer cycles a particular algorithm will take. • This is not useful information because we would then need to talk about the type of computer, whether it has one or many users at a time, what processor it has, how fast its clock is, whether it has a complex or reduced instruction set processor chip, and how well the compiler optimizes the executable code. • All of those will have an impact on how fast a program for an algorithm will run. • To talk about analysis in those terms would mean that by moving a program to a faster computer, the algorithm would become better because it now completes its job faster. • That‟s not true, so , we do our analysis without regard to any specific computer.
  • 25. What to count and consider • Deciding what to count involves two steps – Choosing the significant operation or operations – Deciding which of those operations are integral to the algorithm and which are overhead. • Two classes of operations – Comparison – Arithmetic (additive , multiplicative operators)
  • 26. Input classes • Input plays an important role in analyzing algorithms because it is the input that determines what the path of execution through an algorithm will be. • When looking at the input, we will try to break up all the different input sets into classes based on how the algorithm behaves on each set.
  • 27. Cases to consider • Choosing what input to consider when analyzing an algorithm can have a significant impact on how an algorithm will perform. • We will actually look for those input set that allow an algorithm to perform the most quickly and most slowly. • We will also consider an overall average performance of the algorithm as well.
  • 28. Best Case • Is the input that requires the algorithm to take the shortest time. • This input is the combination of values that causes the algorithm to do the least amount of work. • Because the best case for an algorithm will usually be a very small and frequently constant value, we will not do a best-case analysis very frequently.
  • 29. Worst Case • Important analysis because it gives us an idea of the most time an algorithm will ever take. • Worst case analysis requires that we identify the input values that cause an algorithm to do the most work. • The worst case gives us an upper bound on how slowly parts of our programs may work based on our algorithm choices.
  • 30. Average Case • Toughest to do because there are a lot of details involved. • The basic process begins by determining the number of different groups into which all possible input sets can be divided. • The second step is to determine the probability that the input will come from each of these groups. • The third step is to determine how long the algorithm will run for each of these groups.
  • 31. Average Case • All of the input in each group should take the same amount of time ,and if they do not, the group must be split into two separate groups. • When all of this has been done, the average case time is given by the following formula: m • A(n) = ∑ i=1 pi * ti • Where – n : size of the input – m : no. of groups – pi : probability that the input will be from group i – ti : time that the algorithm takes for input from group i.
  • 32. Frequency Count • It is the number of times the instruction (Step in algorithm) is executed in program (Algorithm). • Total Computational time for an instruction = Instruction execution time * Frequency count. • Now instruction execution time depends upon – speed of processor (instruction per millisecond) – instruction set of the processor – time required to execute instruction – compiler for translating the instruction into machine language. • All these factors independent of the algorithm and external to it. Hence performance of an algorithm (time complexity) is measured in terms of Frequency count. Hence only Frequency count is important in deciding the performance (time complexity) of an algorithm.
  • 33. Stm t. No Statement s / e Frequency Total Steps 1 Algorithm Sum(a,n) 0 -- 0 2 { 0 -- 0 3 s = 0; 1 1 1 4 for i = 1 to n do 1 n+1 n + 1 5 s = s + a[i]; 1 n n 6 return s; 1 1 1 7 } 0 -- 0 Total 2n + 3
  • 34. St m t . N o Statement 1 Algorithm Add(a,b,c,m,n) 2 { 3 for i = 1 to m do 4 for j = 1 to n do 5 c[i,j] = a[i,j] + b[i,j]; 6 } Total
  • 35. Stmt . N o Statement s / e Frequency Total Steps 1 Algorithm Add(a,b,c,m,n) 0 -- 0 2 { 0 -- 0 3 for i = 1 to m do 1 m+1 m+1 4 for j = 1 to n do 1 m(n+1) mn+m 5 c[i,j] = a[i,j] + b[i,j]; 1 mn mn 6 } 0 -- 0 Total 2mn + 2m + 1
  • 36. Determine the frequency count for each statement in the following piece of code. Obtain its time complexity in terms of „n‟ for( i=1; i ≤ n ; i++) { for( j=1; j ≤ i ; j++) { for( k=1 ; k ≤ j ; k++) { s = x + 1; } } }
  • 37. Determine the frequency count for each statement in the following piece of code. Obtain its time complexity in terms of „n‟ i=0 while(i ≤ n) { j = i; while( j ≤ i +1 ) { a = a * a; j ++; } i++; }
  • 38. Stmt . N o Statement s / e Frequency Total Steps n = 0 n > 0 n = 0 n > 0 1 Algorithm RSum(a,n) 0 -- -- 0 0 2 { 0 -- -- 0 0 3 if (n <= 0) then 1 1 1 1 1 4 return 0.0; 1 1 0 1 0 5 else 0 -- -- -- -- 6 return RSum(a,n-1) + a[n]; 1 + x 0 1 0 1 + x 7 } 0 -- -- 0 0 Total 2 2 + x Where x = tRsum(n-1)
  • 39. Mathematical Background N ∑ 1 = N i = 1 N ∑ C = C x N i = 1 N ∑ i = N(N+1) / 2 i = 1 N ∑ i2 = N(N+1)(2N+1) / 6 i = 1
  • 40. Mathematical Background N ∑ 2i = 2N+1 - 1 i = 0 N ∑ Ai = ( AN+1 - 1) / ( A – 1 ) i = 1 N ∑ i 2i = (N-1) 2N+1 + 2 i = 1 N ∑ 1 / i = ln N i = 1
  • 41. Mathematical Background N ∑ lg i ≈ N lg N – 1.5 i = 1
  • 42. Asymptotic Notation ( Ο Ω Θ ) [Big “oh” ] : This notation is used to express an upper bound on computing time of an algorithm. When we say that time complexity of Selection sort algorithm is Ο(n2) , means that for sufficiently large values of „n‟ , computation time will not exceed some constant time * n2 i.e proportional to n2 (Worst Case).
  • 43. [Big “oh”] Ο • Definition : The function f(n) = Ο( g(n) ) (read as “f of n is big oh of g of n”) iff (if and only if ) there exist positive constants c and n0 such that f(n) <= c * g(n) for all n, n >= n0. • Example: • The function 3n+2 = Ο(n) as 3n + 2 <= 4n for all n >= 2 • The function 3n+3 = Ο(n) as 3n + 3 <= 4n for all n >= 3 • The function 100n+6 = Ο(n) as 100n + 6 <= 101n for all n >= 6 • The function 10n2+4n+2 = Ο(n2) as 10n2+4n+2 <= 11n2 for all n >= 5 • The function 1000n2+100n-6 = Ο(n2) as 1000n2+100n-6 <= 1001n2 for all n >= 100 • The function 6*2n + n2 = Ο(2n) as 6*2n + n2 <= 7*2n for all n >= 4
  • 44. [Omega] Ω This notation is used to express an lower bound on computing time of an algorithm. When we say that best case time complexity of insertion sort is Ω (n), means that for sufficiently large values of „n‟, minimum computation time will be some constant time * n i.e proportional to n.
  • 45. [Omega] Ω • Definition : The function f(n) = Ω( g(n) ) (read as “f of n is omega of g of n”) iff there exist positive constants c and n0 such that f(n) >= c * g(n) for all n, n >= n0. • Example: • The function 3n+2 = Ω (n) as 3n + 2 >= 3n for all n >= 1 • The function 3n+3 = Ω (n) as 3n + 3 >= 3n for all n >= 1 • The function 100n+6 = Ω (n) as 100n + 6 >= 100n for all n >= 1 • The function 10n2+4n+2 = Ω (n2) as 10n2+4n+2 >= n2 for all n >= 1 • The function 6*2n + n2 = Ω (2n) as 6*2^n + n2 >= 2n for all n >= 1
  • 46. [Theta] Θ • This notation is used to express time complexity of an algorithm when it is same for worst & Best cases. For example best and worst case time complexities for selection sort is Ο(n2) & Ω (n2) i.e. it can be expressed as Θ (n2). • Definition : The function f(n) = Θ( g(n) ) (read as “f of n is theta of g of n”) iff there exist positive constants c1, c2 and n0 such that c1 *g(n) <= f(n) <= c2 * g(n) for all n, n >= n0.
  • 47. • Example: • The function 3n+2 = Θ (n) as 3n + 2 >= 3n for all n >= 2 and 3n + 2 <= 4n for all n >= 2. • The function 3n+3 = Θ (n) • The function 10n2+4n+2 = Θ (n2) • The function 6*2n + n2 = Θ (2n)
  • 48. Recurrence Relations • Recurrence relations can be directly derived from a recursive algorithm, but they are in a form that does not allow us to quickly determine how efficient the algorithm is. • To do that we need to convert the set of recursive equations into what is called closed form by removing the recursive nature of the equations. • This is done by a series of repeated substitutions until we can see the pattern that develops. • The easiest way to see this process is by a series of examples. • Examples : – T(n) = 2 T(n-2) – 15 T(2) = 40 T(1) = 40 – T(n) = 4 if n ≤ 4 = 4 T(n/2) – 1 otherwise
  • 49. Consider the following recurrence relation – T(n) = 2 T(n-2) – 15 T(2) = 40 T(1) = 40 • Substitution T(n - 2) = 2 T (n – 2 – 2 ) – 15 = 2 T(n-4) – 15 T(n - 4) = 2 T(n-6) - 15 T(n - 6) = 2 T(n-8) – 15 T(n - 8) = 2 T(n-10) – 15 T(n - 10) = 2 T(n-12) – 15
  • 50. Now we begin to substitute back into the original equation. • T(n) = 2T(n-2) – 15 = 2(2T(n-4) – 15 ) – 15 • T(n) = 4T(n-4) – 2*15 – 15 • T(n) = 4( 2T(n-6) – 15 ) – 2 * 15 - 15 • T(n) = 8T(n-6) – 4 * 15 - 2*15 – 15 • T(n) = 8(2T(n-8) – 15) – 4 * 15 - 2*15 – 15 • T(n) = 16T(n-8) – 8*15 - 4 * 15 - 2*15 – 15 • T(n) = 16(2T(n-10) – 15 ) – 8*15 - 4 * 15 - 2*15 – 15 • T(n) = 32T(n-10) – 16*15 - 8*15 - 4 * 15 - 2*15 – 15 • T(n) = 32(2T(n-12)–15) – 16*15 - 8*15 - 4 * 15 - 2*15 – 15 • T(n) = 64T(n-12)–32*15 -16*15 - 8*15 - 4 * 15 - 2*15 – 15
  • 51. • How many times we have to substitute back into this equation to get to either of two values ? • If n is even (2 = n – (n-2)) • This seems to indicate that we would substitute back into this equation [(n-2) / 2] – 1 times giving n/2 – 1 terms based on -15 in the equation, and the power of coefficient of T will be n/2 - 1. • If n is odd, the only thing that would change is that T would have a value of 1 instead of 2, but by our equations, n/2 – 1 is 5 (not 6) when n is 13. • For odd n, we will use n/2 instead of n/2 – 1. • We will have two cases in our answer.
  • 52. ( n/2 – 1) • T(n) = 2(n/2)-1 T(2) – 15 ∑ 2i if n is even i = 0 ( n/2 – 1) • T(n) = 2(n/2)-1 * 40 – 15 ∑ 2i if n is even i = 0 n/2 • T(n) = 2(n/2) T(1) – 15 ∑ 2i if n is odd i = 0 n/2 • T(n) = 2(n/2)-1 * 40 – 15 ∑ 2i if n is odd i = 0
  • 53. • Now applying the equation for an even value of n we get T(n) = 2(n/2)-1 * 40 – 15* (2n/2 – 1) = 2n/2 * 20 - 2n/2 *15 + 15 = 2n/2 ( 20 – 15) + 15 = 2n/2 * 5 + 15 • for an odd value of n we get T(n) = 2n/2 * 40 – 15* (2n/2 + 1 – 1) = 2n/2 * 40 - 2n/2 *30 + 15 = 2n/2 ( 40 – 30) + 15 = 2n/2 * 10 + 15
  • 54. Consider the following recurrence relation – T(n) = 5 if n ≤ 4 = 4 T(n / 2) - 1 otherwise • Substitution T(n/2) = 4T(n/4) – 1 T(n/4) = 4T(n/8) – 1 T(n/8) = 4T(n/16) – 1 T(n/16) = 4T(n/32) – 1 T(n/32) = 4T(n/64) – 1
  • 55. Now we begin to substitute back into the original equation. • T(n) = 4T(n/2) – 1 = 4 (4 T(n/4) – 1 ) – 1 • T(n) = 16T(n/4) – 4*1 – 1 • T(n) = 16( 4T(n/8) – 1 ) – 4 * 1 – 1 • T(n) = 64T(n/8) – 16 * 1 – 4 * 1 – 1 • T(n) = 64(4T(n/16) - 1) – 16 * 1 – 4 * 1 – 1 • T(n) = 256T(n/16) – 64*1 – 16 * 1 – 4 * 1 – 1 • T(n) = 256(4T(n/32)-1) – 64*1 – 16 * 1 – 4 * 1 – 1 • T(n) = 1024T(n/32) – 256 * 1 – 64*1 – 16 * 1 – 4 * 1 – 1 • T(n) = 1024(4T(n/64)-1) – 256*1 – 64*1 – 16*1 – 4*1 – 1 • T(n) = 4096T(n/64) – 1024*1– 256*1–64*1–16*1–4*1–1
  • 56. • We notice that the coefficient of -1 increases by a power of 4 each time we substitute, and it is the case that the power of 2 that we divide n by is 1 greater than the largest power of 4 for this coefficient. • Also, we notice that the coefficient of T is the same power of 4 as the power of 2 that we divide n by. • When we have T(n/2), its coefficient will be 4i and we will have terms from –(4i-1) to -1
  • 57. • Now , for what value of i can we stop the substitution? • Well, because we have the direct case specified for n ≤ 4, we can stop when we get to T(4) = T(n / 2lg n - 2). • Putting together we get. lg n - 2 • T(n) = 4lg n - 2 T(4) - ∑ 4i i = 0
  • 58. • Using the value for the direct case, and main eqn we get T(n) = 4lg n - 2 * 5 - (4lg n - 2 - 1) / (4 – 1) = 4lg n - 2 * 5 - (4lg n - 2 - 1) / 3 = ( 15 * 4lg n - 2 - 4lg n - 2 + 1) / 3 = (4lg n - 2 * (15 – 1) + 1) / 3 = (4lg n - 2 * 14 + 1) / 3