Professional Documents
Culture Documents
Design and Analysis of Algorithm
Design and Analysis of Algorithm
Design and Analysis of Algorithm
Algorithm
6CME
Unit 1
• Algorithm-definition, Specification- pseudo code conventions,
recursive algorithms, Performance analysis – space complexity, time
complexity, asymptotic notation, practical complexities,
performance measurement, Randomized algorithms- basics of
probability theory, identifying the repeated element, primality
testing, advantages and disadvantages
Algorithm
• Informally, an algorithm is any well-defined computational
procedure that takes some value, or set of values, as input and
produces some value, or set of values, as output.
• Insertion sort works the way many people sort a hand of playing
cards. We start with an empty left hand and the cards face down on
the table. We then remove one card at a time from the table and
insert it into the correct position in the left hand. To find the correct
position for a card, we compare it with each of the cards already in
the hand, from right to left.
Insertion sort contd..
Insertion sort contd..
• The numbers that we wish to sort are also known as the keys
• Although conceptually we are sorting a sequence, the input comes to
us in the form of an array with n elements
Pseudo code and real code
• In pseudocode, we employ whatever expressive method is most
clear and concise to specify a given algorithm.
• Sometimes, the clearest method is English, so do not be surprised if
you come across an English phrase or sentence embedded within a
section of “real” code.
• Another difference between pseudocode and real code is that
pseudocode is not typically concerned with issues of software
engineering.
• Exmaple: Issues of data abstraction, modularity, and error handling
are often ignored in order to convey the essence of the algorithm
more concisely.
Insertion sort pseudocode
Conventions used in pseudocode
• Indentation indicates block structure. Using indentation instead of
conventional indicators of block structure, such as begin and end
statements, greatly reduces clutter while preserving, or even
enhancing, clarity
• The symbol “//” indicates that the remainder of the line is a
comment.
• A multiple assignment of the form i = j = e assigns to both variables i
and j the value of expression
• Variables (such as i, j , and key) are local to the given procedure.
Analyzing algorithms
• Analyzing an algorithm has come to mean predicting the resources
that the algorithm requires.
• Occasionally, resources such as memory, communication bandwidth,
or computer hardware are of primary concern.
• but most often it is computational time that we want to measure.
• Hence algorithm analysis refers to the task of determining the
computing time and storage space required for an algorithm. It is
also known as performance analysis/ efficiency of an algorithm.
Analyzing algorithm contd..
Analysis is the process of determining how much Profiling is the process of executing the correct program
computing time and storage an algorithm will require on datasets and measuring the time and space it takes
to compute the results
This is independent of machine programming language Dependent on machine programming language and the
compiler
Step count and operation count
n 4+100n2+50 approximated to n 4
Asymptotic notation
2n2= O(n 3)
N2=O(n2)
• Example:
3n+10 O(N)
2n2 -10n+100 O(N 2 )
100 O(1)
Omega notation
• Example
• 5n2 = Ω(n)
Theta notation
• Example
• 5n2
int main()
{
int n;
printf(“enter the number”);
scanf(“%d”, &n);
printf(“%d”, fact(n));
return 0;
}
Recursion
Key components of a recursive algorithm
design
Randomized algorithm
• Deterministic algorithm: include simple steps and has predefined
input and a required output. In this case running time is a function of
input.
• Randomized algorithm: random bit is introduced in the algorithm
which is designed to produced random input.
input Deterministic algorithm output
Random number
Randomized algorithm
Does not gamble with correctness. Does not gambles with runtime
Gambles with running time Gambles with correctness
MONTE CARLO
LAS VEGAS
Advantages of randomized algorithm
• The first advantage is performance; randomized algo- rithms run
faster than the best-known deterministic algorithms for many
problems.
• The second advantage is that many randomized algorithms are
simpler to describe and implement than deterministic algorithms of
comparable performance.
Disadvantages of randomized algorithm
• The disadvantage is that there isn’t a guarantee that the problem will
be solved all
• upper time limit to obtain the solution can not be guaranteed.
Example: randomized quick sort
Objective: Partition around a random element.
generator.
exercises
• Prove that 1000n^2+1000n = O(n^2)
• Prove or disprove n^3 = O(n^2)
• Prove or disprove 5n+3 = O(n)
• Prove 2n+10 = O(n)
• Prove that 2n^2 + 5n + 6 is O(n^2)