CS 1401
ANALYSIS OF ALGORITHMS (CS 1401)
UNIT 1:
Introduction, Review of basic concepts,
Advanced data structures like Binomial
Heaps, Fibonacci Heaps 5(L)
UNIT 2:
Divide and Conquer with examples such as
Sorting, Matrix
Multiplication, Convex hull etc 6(L)
UNIT 3:
Dynamic programming with examples such
as Kanpsack, All pair shortest paths etc
4(L)
UNIT 4:
Backtracking, Branch and Bound
with examples such as Travelling Salesman
Problem etc 6(L)
UNIT 5:
Algorithms involving Computational
Geometry 4(L)
UNIT 6:
Selected topics such as NP completeness,
Approximation algorithms, Randomized
algorithms, String Matching 5(L)
Text/Reference Books
Introduction to Algorithms by
Thomas H. Coreman, Charles E. Leiserson
and Ronald L. Rivest
A2 Super computer
108 ins/sec
A1
P.C
106 ins /sec
For n = 106
Pre: nothing
Post: numbers are read and deviation
from average printed
1 i= 0
2 Loop(all data are read)
1 I=I+1
2 read numbers into array[i]
3 sum = sum + number
3 Average = sum / I
4 Print (average)
5 J=0
6 Loop (j < i)
1 j = j+ 1
2 dev = array[j] – average
3 print (array [ j] . Dev)
7 Return
8 End deviation
Analysis vs. Design
1 application code
2 I=I + 1
1 I=1
2 Loop (I <= n)
1 Application code
2 I=I+2
• 1 I=1
• 2 loop(I <= n)
1 j=1
2 loop( j < = n)
1 application code
2 j = j *2
3 I=I+1
F(n ) = [n log n]
Dependent Quadratic
1 I=1
2 loop ( I < = n)
1 j=1
2 loop( j < = i)
1 application code
2 j=j+1
3 I=I + 1
no of iterations in the body of the inner loop is
1 + 2 + 3 + 4 +… + n I.e. n(n +1)/2
On an average = ( n+1/)2
thus total no of iterations = n (n+1)/2
Quadratic
1 i=1
2 Loop (I < = n)
1 j=1
2 Loop( j < = n)
1 application code
2 j = j+1
3 I = i+1
F(n) = n2
Insertion sort
c1 n + c2 (n-1) + c3(n-1)+
c4 tj + c5 (tj-1) +
c6 (tj-1) + c7 (n-1)
• If Cj = 1
• = O( n2 )
• average case:
xj is being inserted into the sorted sequence
x1 x2 ... x j-1
• the probability that xj is the largest: 1/j
• takes 2 data movements
• the probability that xj is the second largest : 1/j
• takes 3 data movements
:
• # of movements for inserting xj: 1 4 7 5
2 3 j 1 j 3
j j j 2
n
j 3 ( n 8)( n 1)
M O(n 2 )
j 2 2 4
Worst Case and best case
Best case: file is already sorted
in this case tj = 1
1
A(n) = (( k 1) 2 k 1 k(2 k 1))
2n 1
k as n is very large
= log n
= O(log n)
Quick sort
11 5 24 2 31 7 8 26 10 15
↑ ↑
11 5 10 2 31 7 8 26 24 15
↑ ↑
11 5 10 2 8 7 31 26 24 15
△• Recursively apply the
△ same procedure.
7 5 10 2 8 11 31 26 24 15
|← <11 →| |← > 11 →|
Best case : O(n log n)
1
= n
(T(1)+T(n1)+T(2)+T(n2)+…+T(n)+T(0))+cn, T(0)=0
1
= n
(2T(1)+2T(2)+…+2T(n1)+T(n))+cn
(n1)T(n) = 2T(1)+2T(2)+…+2T(n1) + cn2……(1)
(n2)T(n-1)=2T(1)+2T(2)+…+2T(n2)+c(n1)2…(2)
(1) (2)
T(n) T(n 1) 1 1
= c ( )
n n 1 n n 1
1 1 1 1 1
=c( n n 1 )+c( n 1 n 2 )+…+c( 2 1 )+T(1), T(1) = 0
1 1 1 1 1
=c( n n 1 2 )+c( n 1 n 2 ...1)
...
• <= 2log(n)+ constant
• Thus
• T(n) <= n log(n)
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
Worst/ best/average cases
f ( n)
lim n is finite.
g ( n)
Example
For functions f(n) f(n) = 2n + 6
and g(n) (to the
right) there are
positive constants c
and n0 such that:
f(n) ≤ c g(n)
for n ≥ n0
conclusion:
2n+6 is O(n).
Another Example
constant O(1)
logarithmic: O(log n)
linear: O(n)
quadratic: O(n2)
polynomial: O(nk), k ≥ 1
exponential: O(an), a > 1
EXAMPLE
Consider 1/3 n2 – 5n
1/3 - 5/n c
3n+2 =o(n2 )
3n 2
since lim n 2
0
n
• Intractable algorithms
• Algorithms for which there is no known
polynomial time algorithm
We will come back to this important class later in
the course
Categories of algorithm efficiency
Efficiency Big O
Constant O(1)
Logarithmic O(log n)
Linear O(n)
Linear logarithmic O(n log n)
Quadratic O(n2)
Polynomial O(nk)
Exponential O(cn)
Factorial O(n!)
-notation
For function g(n), (g(n)) is
given by:
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Problem size
10 102 103 104