algorithm_lec_1eregdsgdfgdgdfgdfgdfg.ppt

partho5958 24 views 52 slides May 17, 2024
Slide 1
Slide 1 of 52
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52

About This Presentation

Student of Midnapore College (Autonomous).


Slide Content

Design & Analysis of
Algorithms
Design the Algorithm
Analysis the Algorithm

Introduction
•Algorithm:Idea behind the Program
•Pseudo-Code:Natural Language + Flavour of
Programming Language (Even that can be used to
describe an algorithm)
•Program:Set of Instruction
•Process:Program in Execution

Algorithms
Finiteness:Analgorithmmustalwaysterminateaftera
finitenumberofsteps.
Definiteness:Eachstepofanalgorithmmustbeprecisely
defined;theactionstobecarriedoutmustberigorously
andunambiguouslyspecifiedforeachcase.
Input:Analgorithmhaszeroormoreinputs,i.e,quantities
whicharegiventoitinitiallybeforethealgorithmbegins.
Output:Analgorithmhasoneormoreoutputsi.e,
quantitieswhichhaveaspecifiedrelationtotheinputs.
Effectiveness:Analgorithmisalsogenerallyexpectedto
beeffective.Thismeansthatalloftheoperationstobe
performedinthealgorithmmustbesufficientlybasicthat
theycaninprinciplebedoneexactlyandinafinitelength
oftime.

The problem of sorting

Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Running Time
• The running time depends on the input: an already
sorted sequence is easier to sort.
• Parameterize the running time by the size of the
input, since short sequences are easier to sort than
long ones.
• Generally, we seek upper bounds on the running
time, because everybody likes a guarantee.

Kinds of analyses
Worst-case:(usually)
• T(n) = maximum time of algorithm on any input of
size n.
Average-case:(sometimes)
• T(n) = expected time of algorithm over all inputs of
size n.
• Need assumption of statistical distribution of inputs.
Best-case:
• Cheat with a slow algorithm that works fast on some
input.

Machine-Independent time
The RAM Model
Machine independent algorithm design depends on a
hypothetical computer called Random Acces Machine (RAM).
Assumptions:
• Each simple operation such as +,-,if ...etc takes exactly
one time step.
• Loops and subroutines are not considered simple
operations.
• Each memory acces takes exactly one time step.

Machine-independent time
What is insertion sort’s worst-case time?
• It depends on the speed of our computer,
• relative speed (on the same machine),
• absolute speed (on different machines).
BIG IDEA:
• Ignore machine-dependent constants.
• Look at growth of
“Asymptotic Analysis”nnT as )(

Machine-independent time: An example
A pseudocodefor insertion sort ( INSERTION SORT ).
INSERTION-SORT(A)
1 for j2 to length [A]
2 do key A[ j]
3 Insert A[j] into the sortted sequence A[1,..., j-1].
4 i j –1
5 while i > 0 and A[i] > key
6 do A[i+1] A[i]
7 i i–1
8 A[i +1] key

Analysis of INSERTION-SORT(contd.)1]1[8
)1(17
)1(][]1[6
][05
114
10]11[ sequence
sorted theinto][Insert 3
1][2
][21
timescost SORT(A)-INSERTION
8
2
7
2
6
2
5
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 of INSERTION-SORT(contd.))1()1()1()(
2
6
2
5421  

n
j
j
n
j
j tctcncnccnT ).1()1(
8
2
7 

nctc
n
j
j
The total running time is

Analysis of INSERTION-SORT(contd.)
The best case: The array is already sorted.
(t
j=1 for j=2,3, ...,n))1()1()1()1()(
85421  ncncncncncnT ).()(
854285421 ccccnccccc 

Analysis of INSERTION-SORT(contd.)
•The worst case: The array is reverse sorted
(t
j=j for j=2,3, ...,n).)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
)(

Growth of Functions
Although we can sometimes determine the exact
running time of an algorithm, the extra precision is not
usually worth the effort of computing it.
For large inputs, the multiplicative constants and lower
order terms of an exact running time are dominated by
the effects of the input size itself.

Asymptotic Notation
The notation we use to describe the asymptotic running
time of an algorithm are defined in terms of functions
whose domains are the set of natural numbers.
Asymptotic notations are the
mathematicalnotationsusedtodescribetherunningtime
ofanalgorithmwhentheinputtendstowardsaparticular
valueoralimitingvalue. ...,2,1,0N

O-notation
•For a given function , we denote by the set
of functions
•We use O-notation to give an asymptotic upper bound of
a function, to within a constant factor.
• means that there existes some constant c
s.t. is always for large enough n. )(ng ))((ngO 







0
0
allfor )()(0
s.t.and constants positiveexist there:)(
))((
nnncgnf
ncnf
ngO ))(()( ngOnf )(ncg )(nf

Ω-Omega notation
•For a given function , we denote by the
set of functions
•We use Ω-notation to give an asymptotic lower bound on
a function, to within a constant factor.
• means that there exists some constant cs.t.
is always for large enough n. )(ng ))((ng 







0
0
allfor )()(0
s.t.and constants positiveexist there:)(
))((
nnnfncg
ncnf
ng ))(()( ngnf  )(nf )(ncg

-Theta notation
•For a given function , we denote by the set
of functions
•A function belongs to the set if there exist
positive constants and such that it can be “sand-
wiched” between and or sufficienly large n.
• means that there exists some constant c
1
and c
2 s.t. for large enough n.)(ng ))((ng 







021
021
allfor )()()(c0
s.t.and,, constants positiveexist there:)(
))((
nnngcnfng
nccnf
ng )(nf ))((ng 1c 2c )(
1ngc )(
2ngc Θ ))(()( ngnf  )()()(
21
ngcnfngc 

Asymptotic notation
Graphic examples of and .,,O

2
2
22
1 3
2
1
ncnnnc  21
3
2
1
c
n
c  Example 1.
Show that
We must find c
1 and c
2such that
Dividing bothsides by n
2yields
For )(3
2
1
)(
22
nnnnf  )(3
2
1
,7
22
0 nnnn 

Theorem
•For any two functions and , we have
if and only if)(ng ))(()( ngnf  )(nf )).(()( and ))(()( ngnfngOnf 

Because :)
2
(52
2
3 nnn 
Example 2.)
2
(52
2
3)( nnnnf  )
2
(52
2
3 nOnn 

Example 3. 610033,3forsince)(61003
2222
 nnncnOnn

Example 3. 3when61003,1forsince)(61003
610033,3forsince)(61003
2332
2222


nnnncnOnn
nnncnOnn

Example 3. cnncncnOnn
nnnncnOnn
nnncnOnn



when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
2332
2222

Example 3. 100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
2222
22
2332
2222




nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn

Example 3. 3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
3232
2222
22
2332
2222





nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn

Example 3. 100when61003,any forsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
3232
2222
22
2332
2222






nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn

Example 3. apply. and both since)(61003
100when61003,any forsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
22
3232
2222
22
2332
2222







Onnn
nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn

Example 3. applies. only since)(61003
apply. and both since)(61003
100when61003,any forsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
32
22
22
3232
2222
22
2332
2222
Onnn
Onnn
nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn









Example 3. applies. only since)(61003
applies. only since)(61003
apply. and both since)(61003
100when61003,any forsince)(61003
3when61003,3forsince)(61003
100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
2
32
22
22
3232
2222
22
2332
2222









nnn
Onnn
Onnn
nnncncnnn
nnnncnnn
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn

Standard notations and common functions
•Floors and ceilings  11  xxxxx

Standard notations and common functions
•Logarithms:)lg(lglglg
)(loglog
logln
loglg
2
nn
nn
nn
nn
kk
e



Standard notations and common functions
•Logarithms:
For all real a>0, b>0, c>0, and nb
a
a
ana
baab
ba
c
c
b
b
n
b
ccc
a
b
log
log
log
loglog
loglog)(log
log




Standard notations and common functions
•Logarithms:b
a
ca
aa
a
b
ac
bb
bb
log
1
log
log)/1(log
loglog




Standard notations and common functions
•Factorials
For the Stirling approximation:




















ne
n
nn
n
1
12! 0n )lg()!lg(
)2(!
)(!
nnn
n
non
n
n



Types of Functions
O(1) ------Constant
Ex.: f(n)= 2 or 5 or 5000 (any constant) O(1)
O(logn) ------logarithmic
Ex.: alogn+b O(logn)
O(n) Linear
Ex.: an+b =O(n)
O(n^2) Quadratic
Ex.: an^2+bn+c
O(n^3) Cubic
Ex.: an^3+bn^2+cn+d
O(a^n) like 2^n, 3^n, n^n Exponential

Compare Classes of Functions
1<logn<√n<n<nlogn< n^2<n^3<…<2^n<3^n<…<n^n

Machine Independent
•TimeComplexityofanAlgorithm:
NumberofPrimitivecomputations.
Relationshipbetweentherunningtime
ofanalgorithmandthesizeofitsinput
•DoesnotdependonSoftwareand
Hardware

A Model of the Computer: RAM
•An informal model of the random-access machine (RAM)
•Basic types in the RAM model
•◮integer and floating-point numbers
•◮limited size of each “word” of data (e.g., 64 bits)
•Basic steps in the RAM model
•◮operations involving basic types
•◮load/store: assignment, use of a variable
•◮arithmetic operations: addition, multiplication, division,
etc.
•◮branch operations: conditional branch, jump
•◮subroutine call
A basic step in the RAM model takes a constant time
Tags