![Page 1: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/1.jpg)
Introduction to AlgorithmsIntroduction to Algorithms
Lecture 1
![Page 2: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/2.jpg)
IntroductionIntroduction
• The methods of algorithm design form one of the core practical technologies of computer science.
• The main aim of this lecture is to familiarize the student with the framework we shall use through the course about the design and analysis of algorithms.
• We start with a discussion of the algorithms needed to
solve computational problems. The problem of sorting is used as a running example.
• We introduce a pseudocode to show how we shall
specify the algorithms.
![Page 3: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/3.jpg)
AlgorithmsAlgorithms
• The word algorithm comes from the name of a Persian mathematician Abu Ja’far Mohammed ibn-i Musa al Khowarizmi.
• In computer science, this word refers to a special method useable by a computer for solution of a problem. The statement of the problem specifies in general terms the desired input/output relationship.
• For example, sorting a given sequence of numbers into nondecreasing order provides fertile ground for introducing many standard design techniques and analysis tools.
![Page 4: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/4.jpg)
The problem of sortingThe problem of sorting
![Page 5: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/5.jpg)
Insertion SortInsertion Sort
![Page 6: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/6.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 7: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/7.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 8: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/8.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 9: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/9.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 10: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/10.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 11: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/11.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 12: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/12.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 13: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/13.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 14: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/14.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 15: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/15.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 16: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/16.jpg)
Example of Insertion SortExample of Insertion Sort
![Page 17: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/17.jpg)
Analysis of algorithmsAnalysis of algorithms
The theoretical study of computer-programperformance and resource usage.
What’s more important than performance?• modularity
• correctness• maintainability• functionality• robustness
• user-friendliness• programmer time
• simplicity• extensibility
• reliability
![Page 18: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/18.jpg)
Analysis of algorithmsAnalysis of algorithms
Why study algorithms and performance?
• Algorithms help us to understand scalability.
• Performance often draws the line between what is feasible and what is impossible.
• Algorithmic mathematics provides a language for talking about program behavior.
• The lessons of program performance generalize to other computing resources.
• Speed is fun!
![Page 19: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/19.jpg)
Running TimeRunning 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.
![Page 20: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/20.jpg)
Kinds of analysesKinds 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.
![Page 21: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/21.jpg)
Machine-Machine-IIndependent timendependent 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.
![Page 22: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/22.jpg)
Machine-independent timeMachine-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 )(
![Page 23: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/23.jpg)
Machine-independent time: An exampleMachine-independent time: An example
A pseudocode for 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 – 15 while i > 0 and A[i] > key6 do A[i+1] A[i]7 i i – 18 A[i +1] key
![Page 24: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/24.jpg)
Analysis of INSERTION-SORT(contd.)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
27
26
25
4
2
1
nckeyiA
tcii
tciAiA
tckeyiAandi
ncji
njA
jA
ncjAkey
ncAlengthj
nj j
nj j
nj j
do
while
do
tofor
![Page 25: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/25.jpg)
Analysis of INSERTION-SORT(contd.)Analysis of INSERTION-SORT(contd.)
)1()1()1()(2
62
5421
n
jj
n
jj tctcncnccnT
).1()1( 82
7
nctcn
jj
The total running time is
![Page 26: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/26.jpg)
Analysis of INSERTION-SORT(contd.)Analysis of INSERTION-SORT(contd.)
The best case: The array is already sorted. (tj =1 for j=2,3, ...,n)
)1()1()1()1()( 85421 ncncncncncnT
).()( 854285421 ccccnccccc
![Page 27: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/27.jpg)
Analysis of INSERTION-SORT(contd.)Analysis of INSERTION-SORT(contd.)
•The worst case: The array is reverse sorted
(tj =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/( 87654212
765
2
)1(1
nnj
n
j
cbnannT 2)(
![Page 28: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/28.jpg)
Growth of FunctionsGrowth 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.
![Page 29: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/29.jpg)
Asymptotic NotationAsymptotic 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
...,2,1,0N
![Page 30: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/30.jpg)
O-notationO-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
ncnfngO
))(()( ngOnf
)(ncg)(nf
![Page 31: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/31.jpg)
ΩΩ--Omega Omega notationnotation
• 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 c s.t.
is always for large enough n.
)(ng ))(( ng
0
0
allfor )()(0
s.t.and constants positiveexist there:)())((
nnnfncg
ncnfng
))(()( ngnf
)(nf )(ncg
![Page 32: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/32.jpg)
--Theta Theta notationnotation
• 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 c1 and c2 s.t. for large enough n.
)(ng ))(( ng
021
021
allfor )()()(c0
s.t.and,, constants positiveexist there:)())((
nnngcnfng
nccnfng
)(nf ))(( ng1c 2c
)(1 ngc )(2 ngc
Θ
))(()( ngnf )()()( 21 ngcnfngc
![Page 33: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/33.jpg)
Asymptotic notationAsymptotic notation
Graphic examples of and . ,, O
![Page 34: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/34.jpg)
22
221 3
2
1ncnnnc
213
2
1c
nc
Example 1. Example 1.
Show that
We must find c1 and c2 such that
Dividing bothsides by n2 yields
For
)(32
1)( 22 nnnnf
)(32
1,7 22
0 nnnn
![Page 35: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/35.jpg)
Theorem Theorem
• For any two functions and , we have
if and only if
)(ng
))(()( ngnf
)(nf
)).(()( and ))(()( ngnfngOnf
![Page 36: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/36.jpg)
Because :
)2(5223 nnn
Example 2.Example 2.
)2(5223)( nnnnf
)2(5223 nOnn
![Page 37: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/37.jpg)
Example 3. Example 3.
610033,3forsince)(61003 2222 nnncnOnn
![Page 38: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/38.jpg)
Example 3. Example 3.
3when61003,1forsince)(61003
610033,3forsince)(610032332
2222
nnnncnOnn
nnncnOnn
![Page 39: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/39.jpg)
Example 3. Example 3.
cnncncnOnn
nnnncnOnn
nnncnOnn
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
22
2332
2222
![Page 40: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/40.jpg)
Example 3. Example 3.
100when610032,2forsince)(61003
when3,any forsince)(61003
3when61003,1forsince)(61003
610033,3forsince)(61003
2222
22
2332
2222
nnnncnnn
cnncncnOnn
nnnncnOnn
nnncnOnn
![Page 41: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/41.jpg)
Example 3. 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
![Page 42: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/42.jpg)
Example 3. 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
![Page 43: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/43.jpg)
Example 3. 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
![Page 44: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/44.jpg)
Example 3. 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
![Page 45: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/45.jpg)
Example 3. 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
![Page 46: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/46.jpg)
oo-notation-notation
• We use (small o) o-notation to denote an upper bound that is not asymptotically tight.
• We formally define as the set))(( ngo
0
0
allfor )()(0
s.t.0 constants aexist there
0constant positiveany for :)(
))((
nnncgnf
n
cnf
ngo
0)(
)(lim
ng
nf
n
![Page 47: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/47.jpg)
Example 4. Example 4.
)(2)(
But
)(2)(
isThat
ally tightasymptoticnot Is:)(2)(
ally tightAsymptotic:)(2)(
22
2
2
22
nonnf
nonnf
nOnnf
nOnnf
![Page 48: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/48.jpg)
ωω-notation-notation
• We use ω-notation to denote an upper bound that is not asymptotically tight.
• We formally define as the set))(( ng
0
0
allfor )()(0
s.t.0 constants aexist there
0constant positiveany for :)(
))((
nnnfncg
n
cnf
ng
∞=
=relation The
∞→ g(n)
f(n)lim
tmplies thaω(g(n)) i f(n)
n
![Page 49: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/49.jpg)
Example Example
)(2
)(
But
)(2
)(
22
2
nn
nf
nn
nf
![Page 50: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/50.jpg)
Standard notations and common functionsStandard notations and common functions
• Floors and ceilings
11 xxxxx
![Page 51: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/51.jpg)
Standard notations and common functionsStandard notations and common functions
• Modular arithmetic
For any integer a and positive integer n
nnaana /mod
![Page 52: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/52.jpg)
Standard notations and common functionsStandard notations and common functions
• Polynomials:
Given a nonnegative integer d, a polynomial in n of degree d is
d
i
i
i nanp0
)(
![Page 53: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/53.jpg)
Standard notations and common functionsStandard notations and common functions
• Exponentials:
!3!2
132 xx
xe x
![Page 54: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/54.jpg)
Standard notations and common functionsStandard notations and common functions
• Logarithms:
)lg(lglglg
)(loglog
logln
loglg 2
nn
nn
nn
nn
kk
e
![Page 55: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/55.jpg)
Standard notations and common functionsStandard notations and common functions
• Logarithms:
For all real a>0, b>0, c>0, and n
b
aa
ana
baab
ba
c
cb
b
n
b
ccc
ab
log
loglog
loglog
loglog)(log
log
![Page 56: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/56.jpg)
Standard notations and common functionsStandard notations and common functions
• Logarithms:
ba
ca
aa
a
b
ac
bb
bb
log
1log
log)/1(logloglog
![Page 57: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/57.jpg)
Standard notations and common functionsStandard notations and common functions
• Series expansion:
For
For
5432
)1ln(5432 xxxx
xx
1x
1x
xxx
x
)1ln(
)1(
![Page 58: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/58.jpg)
Standard notations and common functionsStandard notations and common functions
• Factorials
For the Stirling approximation:
ne
nnn
n1
12!
0n
)lg()!lg(
)2(!
)(!
nnn
n
nonn
n
![Page 59: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/59.jpg)
Designing algorithms Designing algorithms
There are many ways to design algorithms:
• Insertion sort uses an incremental approach• Merge sort uses divide-and-conquer approach
![Page 60: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/60.jpg)
Insertion sort analysisInsertion sort analysis
![Page 61: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/61.jpg)
Merge SortMerge Sort
![Page 62: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/62.jpg)
Merge SortMerge Sort
MERGE_SORT(A,p,r)
1if p<r2 then q← (p+r)/2 3 MERGE_SORT(A,p,q)4 MERGE_SORT(A,q+1, r)5 MERGE(A,p,q,r)
![Page 63: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/63.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 64: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/64.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 65: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/65.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 66: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/66.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 67: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/67.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 68: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/68.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 69: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/69.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 70: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/70.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 71: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/71.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 72: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/72.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 73: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/73.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 74: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/74.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 75: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/75.jpg)
Merging two sorted arraysMerging two sorted arrays
![Page 76: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/76.jpg)
Analyzing merge sortAnalyzing merge sort
![Page 77: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/77.jpg)
Recurrence for merge sortRecurrence for merge sort
![Page 78: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/78.jpg)
Recursion treeRecursion tree
![Page 79: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/79.jpg)
Recursion treeRecursion tree
![Page 80: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/80.jpg)
Recursion treeRecursion tree
![Page 81: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/81.jpg)
Recursion treeRecursion tree
![Page 82: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/82.jpg)
Recursion treeRecursion tree
![Page 83: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/83.jpg)
Recursion treeRecursion tree
![Page 84: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/84.jpg)
Recursion treeRecursion tree
![Page 85: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/85.jpg)
Recursion treeRecursion tree
![Page 86: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/86.jpg)
Recursion treeRecursion tree
![Page 87: Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The](https://reader035.vdocuments.us/reader035/viewer/2022070410/56649eb75503460f94bc103a/html5/thumbnails/87.jpg)
Recursion treeRecursion tree