Basics of Algorithms and Analysis of algorithm is in there, which includes Time complexity , space complexity, three cases ( best, average, worst) and analysis of Insertion sort.
*For knowledge purpose only*
*Hope you'll come up with better one*
2. Part 1 Basics of Algorithm
1. Introduction
2. Characteristics
3. Use of Algorithms
Analysis and Design of Algorithms 2
3. 1. Introduction
I. The word Algorithm come from the name of 9th century Persian mathematician
Al-Khwārizmī, Whose work built upon the work of 7th century Indian
mathematician Brahmagupta.
II. Ada Lovelace invented 1st Algorithm !
III. 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.
IV. The Internet enables people all around the world to quickly access and retrieve
large amount of information. In order to do so, clever algorithms are employed
to manage and manipulate this large volume of data. Example of that is,
finding good routes on which the data will travel.
Analysis and Design of Algorithms 3
4. o If all Algorithms are performing same procedure, all of them are
equal and if not then what differentiates them?
1. Correctness of Algorithm
I. Produce correct answer always.
II. If it is taking lot of time and
resources, We build our
algorithms as to give the best
possible answer.
2. Efficiency of Algorithm
I. Time it takes
II. Complexity
III. Space requirement
Analysis and Design of Algorithms 4
5. 2. Characteristics
• Modularity
• Correctness
• Maintainability
• Functionality
• Robustness
• User-friendliness
• Simplicity
• Programmer time
• Extensibility
• Reliability
Analysis and Design of Algorithms 5
6. 3. Use of Algorithms
I. Algorithms help us to understand scalability.
II. Public-key cryptography and digital signatures are among the core
technologies used and are based on numerical algorithms.
III. Algorithmic mathematics provides a language for talking about
program behaviour.
IV. Speed is fun!
V. Local-area and wide-area networking.
Analysis and Design of Algorithms 6
7. Part 2 Analysis of Algorithms
1. Overview
2. Time Base Analysis
3. Space Base Analysis
4. Three Cases
Analysis and Design of Algorithms 7
8. Figure : Types of analysis
Analysis and Design of Algorithms 8
Analysis
Time Complexity Space Complexity
9. 1. Time Complexity
I. In computer science, the time complexity of an algorithm
quantifies the amount of time taken by an algorithm to run as
a function of the length of the string representing the input.
II. The time complexity of an algorithm is commonly expressed
using big O notation, which excludes coefficients and lower
order terms.
Analysis and Design of Algorithms 9
10. 2. Space Complexity
I. Space complexity is a measure of the amount of working
storage an algorithm needs. That means how much memory,
in the worst case, is needed at any point in the algorithm.
II. As with time complexity, we're mostly concerned with how the
space needs grow, in big-Oh terms, as the size N of the input
problem grows.
Analysis and Design of Algorithms 10
11. To find Time Complexity of a program
Analysis and Design of Algorithms 11
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
12. Three Cases
1. Best Case
2. Average Case
3. Worst Case
Analysis and Design of Algorithms 12
13. 1. Best case
I. Cheat with a slow algorithm that works fast on some input
II. Provides a lower bound on running time
III. Input is the one for which the algorithm runs the fastest
Analysis and Design of Algorithms 13
Lower Bound RunningTime Upper Bound
14. 2. Average case
I. T(n) = expected time of algorithm over all inputs of size n.
II. Need assumption of statistical distribution of inputs.
III. Provides a prediction about the running time.
IV. Assumes that the input is random.
Analysis and Design of Algorithms 14
15. 3. Worst case
I. T(n) = maximum time of algorithm on any input of size n.
II. Provides an upper bound on running time.
III. An absolute guarantee that the algorithm would not run
longer, no matter what the inputs are.
Analysis and Design of Algorithms 15
16. Analysis of INSERTION sort
16
1]1[8
)1(17
)1(][]1[6
][05
114
10]11[sequence
sortedtheinto][Insert3
1][2
][21
timescostSORT(A)-INSERTION
8
27
26
25
4
2
1
nckeyiA
tcii
tciAiA
tckeyiAandi
ncji
njA
jA
ncjAkey
ncAlengthj
n
j j
n
j j
n
j j
do
while
do
tofor
Analysis and Design of Algorithms
17. Analysis of INSERTION sort
I. Total Running Time
II. Best Case: The array is already sorted (tj =1 for j=2,3, ...,n)
17
)1()1()1()(
2
6
2
5421
n
j
j
n
j
j tctcncnccnT ).1()1( 8
2
7
nctc
n
j
j
)1()1()1()1()( 85421 ncncncncncnT
).()( 854285421 ccccnccccc
Analysis and Design of Algorithms
18. Analysis of INSERTION sort
• Worst Case: The array is reverse sorted (tj =j for j=2,3, ...,n)
18
)12/)1(()1()( 521 nncncncnT
)1()2/)1(()2/)1(( 876 ncnncnnc
ncccccccnccc )2/2/2/()2/2/2/( 8765421
2
765
2
)1(
1
nn
j
n
j
cbnannT 2
)(
Analysis and Design of Algorithms
19. Part 3 Asymptotic Notation
1. Big-O Notation - ⃝
2. Theta Notation - Ө
3. Omega Notation - Ω
4. Example
Analysis and Design of Algorithms 19
20. Analysis and Design of Algorithms 20
1. Big-O Notation
For a given function g(n), we denote by O(g(n)) the set of functions.
The above statement implies that if, f(n)=O(g(n)) then F(n) is always
We use the O-notation to give an asymptotic upper bound of a
function, to within a constant factor.
)(ncg
0
0
allfor)()(0
s.t.andconstantspositiveexistthere:)(
))((
nnncgnf
ncnf
ngO
22. 2. Ω-Omega Notation
I. For a given function , ,we denote by the set of
functions.
II. The above statement implies that, if f(n)=O(g(n)) then F(n) is
always
III. We use Ω-notation to give an asymptotic lower bound on a
function, to within a constant factor.
Analysis and Design of Algorithms 22
)(ng ))(( ng
)(ncg
0
0
allfor)()(0
s.t.andconstantspositiveexistthere:)(
))((
nnnfncg
ncnf
ng
24. 3. Ө-Theta Notation
• For a given function , we denote by the set of
functions
• A function belongs to the set if there exist positive
constants c1 and c2 such that it can be “sand- wiched” between
and
• The above statement implies that, if f(n)=O(g(n)) then F(n) is always
Analysis and Design of Algorithms 24
)(ng ))(( ng
021
021
allfor)()()(c0
s.t.and,,constantspositiveexistthere:)(
))((
nnngcnfng
nccnf
ng
)(nf ))(( ng
)(1 ngc
)(2 ngc
)()()( 21 ngcnfngc
26. 26
Examples (Big-O)
•30n+8=O(n).
•30n+8 cn, n>n0 .
•Let c=31, n0=8. Assume n>n0=8. Then
cn = 31n = 30n + n > 30n+8, so 30n+8 < cn.
Analysis and Design of Algorithms
27. 27
• Note 30n+8 isn’t
less than n
anywhere (n>0).
• But it is less than
31n everywhere to
the right of n=8.
n>n0=8
Big-O example, graphically
Increasing n
Valueoffunction
n
30n+8
cn =
31n
30n+8
O(n)
Analysis and Design of Algorithms
28. 28
• Subset relations between order-of-growth sets.
Relations Between Different Sets
RR
( f )O( f )
( f )
• f
Analysis and Design of Algorithms