Course Name: CS302-Design An Analysis of Algorithm: Credit Hours: 3
Course Name: CS302-Design An Analysis of Algorithm: Credit Hours: 3
Course Name: CS302-Design An Analysis of Algorithm: Credit Hours: 3
analysis of Algorithm
Credit Hours: 3
Week 3
Arithmetic series
n+ (n-1) +(n-2)+……1= n(n+1)/2 =O(n2)
Task
Design nested loop in which execution of inner
instructions sum to quadratic series.
For (i=1;i<=n;i++)
for (j=1;j<=i2; j++)
{ //sequence of statements
}
1+4+9+…..n2
= 2n3+3n2+n / 6
= O(n3)
Example (Nested loop)
n + n/2 + n/4 + … 1
= O log(n)
Task:
int fun(int n)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i; j > 0; j--)
count = count + 1;
return count;
}
“count = count + 1”
0 + 1 + 2 + 3 + 4 + …. + (n-1) times.
=
=(n-1)(n-1+1)/2
=(n-1)(n)/2
=O(n2)
Previous Lecture Example
for (i=0; i<n; i++)
{for (j=i; j<n;j++)
{
Sequence of statements….
}
}
n + (n-1 ) + (n-2) +…… 2+1
=
=n(n+1)/2
=O(n2)
Recurrence
Relations
The portion of the definition that does not contain T is called the base
case of the recurrence relation
The part that contains T is called the recurrent or recursive case.
Forming Recurrence Relations
For a given recursive method, the base case and the recursive case of its recurrence
relation correspond directly to the base case and the recursive case of the method.
Example 1: Write the recurrence relation for the following method.
public void f (int n) {
if (n > 0) {
System.out.println(n);
f(n-1);
}
}
The base case is reached when n == 0. The method performs one
comparison. Thus, the number of operations when n == 0, T(0), is
some constant a.
When n > 0, the method performs two basic operations and then calls
itself, using ONE recursive call, with a parameter n – 1.
Therefore the recurrence relation is:
Forming Recurrence Relations
Iteration Method
The recurrence relation for the running time of the method is:
T(1) = a if n = 1 (one element array)
T(n) = T(n / 2) + b if n > 1
Analysis Of Recursive Binary Search
Expanding:
T(n) = T(n / 2) + b
= [T(n / 4) + b] + b = T (n / 22) + 2b
= [T(n / 8) + b] + 2b = T(n / 23) + 3b
= ……..
= T( n / 2k) + kb
T(n) = T(n-1) + bn
T(0) = c
T(n) = T(n-1) + bn
= (T(n-2) + b(n-1)) + bn = T(n-2) + bn+bn-b
= T(n-3) + b(n-2)) +b(n-1) + b(n) =T(n-3)+3bn-2b-b
= T(n-4) + b(n-3)+ b(n-2)) +b(n-1) + b(n) = T(n-4)+4bn-3b-2b-b
=T(n-4)+4bn-b[3+2+1]
= T(n-k)+kbn-b{(k-1) (k-1)/2}
n-k=0 => n=k
= T(0)+bn^2+b[(n-1)(n-1)/2]
=c+ bn^2+b[(n-1)(n-1)/2]
=O(n^2)
2
solving recurrences
expanding the recurrence into a tree
summing the cost at each level
T(n/2) T(n/2)
Recursion tree
cn/2 cn/2
cn/2 cn/2
O(1)
Determining depth/height of tree
Recursion tree
cn/2 cn/2
h = log n cn/4 cn/4 cn/4 cn/4
O(1)
Recursion tree
cn/2 cn/2
h = log n cn/4 cn/4 cn/4 cn/4
O(1)
Recursion tree
cn/2 cn/2 cn
h = log n cn/4 cn/4 cn/4 cn/4
O(1)
Recursion tree
cn/2 cn/2 cn
h = log n cn/4 cn/4 cn/4 cn/4 cn
…
O(1)
Recursion tree
cn/2 cn/2 cn
h = log n cn/4 cn/4 cn/4 cn/4 cn
…
O(1) #leaves = n O(n)
Recursion tree
cn/2 cn/2 cn
h = log n cn/4 cn/4 cn/4 cn/4 cn
…
O(1) #leaves = n O(n)
Total = O(n log n)
Let's consider another example,
T(n) = T(n/3) + T(2n/3) + n.
Expanding out the first few levels, the recurrence
tree is:
Merge(a, tmp_array, 0, 4, 7 )
Merge(a, tmp_array, 0, 4, 7 )
m_sort( a, tmp_array, 0, 3) m_sort( a, tmp_array, 4, 7)
62 (0) 58 (1) 55 (2) 10 (3) 45 (4) 44 (5) 6 (6) 90 (7)
aptr bptr
cptr
aptr bptr
6 (0)
cptr
aptr bptr
6 (0) 10 (1)
cptr
aptr bptr
cptr
aptr bptr
cptr
aptr bptr
cptr
aptr bptr
cptr
aptr bptr
cptr
aptr bptr
cptr
i = 1;
while (i < n) {
tot += i;
i = i * 2;
}
Example #4: equivalent # of steps?
i = n;
while (i > 0) {
tot += i;
i = i / 2;
}
Coding example #5
int total(int n)
for( i=0 ; i < n; i++)
subtotal += i;
main()
for ( i=0 ; i<n ; i++ )
tot += total(i);
Coding example #9: Equivalent
code