Module1 Lecture2
Module1 Lecture2
Module 1: Lecture 2
1. Algorithms
2. How to analyze an Algorithm
3. Growth of Functions - Asymptotic Notations
Algorithms
• Expectation from an algorithm
Algorithms
• Expectation from an algorithm
• Correctness
• Less resource usage
Algorithms
• Expectation from an algorithm
❖ Correctness
⮚ Correct: Algorithms must produce correct result.
⮚ Produce an incorrect answer: Even if it fails to give correct results all the time still
there is a control on how often it gives wrong result.
⮚ Approximation algorithm: Exact solution is not found, but near optimal solution can be
found out.
❖ Less resource usage
Algorithms
• Time taken by an algorithm
o Performance measurement or Apostoriori Analysis: Implementing the
algorithm in a machine and then calculating the time taken by the system to
execute the program successfully.
o Performance Evaluation or Apriori Analysis. Before implementing the
algorithm in a system.
❑ How long the algorithm takes :-will be represented as a function of the size of the
input. f(n)→how long it takes if ‘n’ is the size of input.
❑ How fast the function that characterizes the running time grows with the input size.
“Rate of growth of running time”.
The algorithm with less rate of growth of running time is considered better.
Algorithms & Technology
• Latest processor Vs Good Algorithm
How to analyze an algorithm??
Example: Insertion Sort
Pseudo code:
for j=2 to A length -------------------------------------------------- C1
key=A[j]-----------------------------------------------------------------
C2 //Insert A[j] into sorted Array A[1.....j-1]------------------------
C3 i=j-1------------------------------------------------------------------------
C4 while i>0 & A[j]>key---------------------------------------------------
C5 A[i+1]=A[i]---------------------------------------------------------------
C6 i=i-1------------------------------------------------------------------------
C7 A[i+1]=key----------------------------------------------------------------
C8
How to analyze an algorithm??
Example: Insertion Sort
How to analyze an algorithm??
Example: Insertion Sort
Let Ci be the cost of ith line.
Since comment lines will not incur any cost C3=0
Cost No. Of times Executed
C1 n
C2 n-1
C3 0
C4 n-1
C5
C6
C7
C8 n-1
How to analyze an algorithm??
Example: Insertion Sort
Run time = C1(n) + C2 (n-1) + 0 (n-1) + C4 (n-1) + C5( ) + C6 ( ) + C7 ( ) + C8 (n-1)
How to analyze an algorithm??
Example: Insertion Sort
Run time = C1(n) + C2 (n-1) + 0 (n-1) + C4 (n-1) + C5( ) + C6 ( ) + C7 ( ) + C8 (n-1)
22
Math you need to Review
Summations
Logarithms and Exponents • properties of logarithms:
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
logb = alogbx
logba = logxa/logxb
• properties of exponentials:
a(b+c) = aba c
abc = (ab)c
Proof techniques ab /ac = a(b-c)
Basic probability b = a logab
bc = a c*logab
23
Growth of Functions - Asymptotic Notations
big-Omega
f(n) is (g(n))
if there is a constant c > 0 and an integer
constant n0 1 such that
f(n) c•g(n) for n n0
E show that f(n)=
f(n)n
f(n) = 3 − 100n + 6
g(n) =
⇒ 3 − 100n + 6 c · for some c
If c = 2 , then 3 − 100n + 6 2
Hence, 3 − 100n + 6 = ()
Growth of Functions - Asymptotic Notations
Growth of Functions - Asymptotic Notations
Theta - notation
A function f(n) is (g(n))
if there exists positive constants , and such that
g(n)f(n) g(n )
Growth of Functions - Asymptotic Notations
Growth of Functions - Asymptotic Notations
Growth of Functions - Asymptotic Notations
Growth of Functions - Asymptotic Notations
Upper and lower bounds