Professional Documents
Culture Documents
Algorithm Intro
Algorithm Intro
Introduction
• The methods of algorithm design form one
of the core practical technologies of
computer science.
• Speed is fun!
Running Time
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
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
BIG IDEA:
• Ignore machine-dependent constants.
• Look at growth of
“Asymptotic Analysis”
Machine-independent time: An example
n n
T (n) c1 c2 (n 1) c4 (n 1) c5 t j c6 (t j 1)
j 2 j 2
n
c7 (t j 1) c8 ( n 1).
j 2
Analysis of INSERTION-SORT(contd.)
T (n) c1n c2 (n 1) c4 (n 1) c5 (n 1) c8 (n 1)
(c1 c2 c4 c5 c8 )n (c2 c4 c5 c8 ).
Analysis of INSERTION-SORT(contd.)
• The worst case: The array is reverse sorted
(tj =j for j=2,3, ...,n).
n n(n 1)
j
j 1 2
Show that
For
Theorem
if and only if
Example 2.
Because :
Example 3.
Example 3.
Example 3.
Example 3.
Example 3.
Example 3.
Example 3.
Example 3.
Example 3.
Standard notations and common functions
• Logarithms:
Standard notations and common functions
• Logarithms:
For all real a>0, b>0, c>0, and n
Standard notations and common functions
• Logarithms:
Standard notations and common functions
• Factorials
For the Stirling approximation: