DAA - Ch. 1
DAA - Ch. 1
(203105301)
ushil Kumar Assistant ro essor
Computer Science and Engineering
CHAPTER-1
Introduction
Outline
• Characteristics of algorithm.
• Analysis of algorithm: Asymptotic analysis of complexity bounds–
best, average and worst-case behavior;
• Performance measurements of Algorithm,
• Time and space trade-offs,
• Analysis of recursive algorithms through recurrence relations:
Substitution method
Computational problems?
• A computational problem specifies an input-output relationship
• What does the input look like?
• What should the output be for each input?
• Example:
• Input: an integer number n
• Output: Is the number even?
• Example:
• Input: A list of names of people
• Output: The same list sorted alphabetically
Problems and Solution as Algorithm
• For example, we need to solve a computational problem
“Convert a distance in kilometer to meter”
An algorithm specifies how to solve it,
I. 1. Read distance in kilometer
II. 2. Calculate distance-in-meter = distance-in-kilometer*1000
III. 3. Print distance-in-meter
What is Algorithm?
• The word algorithm comes from the name of a Persian mathematician Abu
Ja’far Mohammed ibn-i Musa al Khowarizmi.
• Algorithm is a finite set of instructions used to accomplish particular task.
Algorithm
Input
• Input may be Zero or more
Output
• At least one output will be produced..
Definiteness
• Each instruction should be cleared and unambiguous.
Finiteness
• the steps should be finite.
Characteristics of algorithm
Non –
• more than one interpretation should not be generated.
ambiguous
Best Case
• Average time required
Average Case for program execution
• Tight bound
I. Is 5n3 Q(n4) ??
Time complexity
Complexity
Space complexity
Base Case: T(n)=c , for n=0 (Here T(n) is running time and n is input size)
I. Substitution Method
II. Iterative Method
III. Recurrence Tree Method
IV. Master’s Method
Solving Recurrence Relations- Substitution Method
Substitution Method
I. T(n)=T(n-1)+1, for n>0
T(n)=1, for n=0
Solution:
T(n)=T(n-1)+1 ...........................1)
find T(n-1) and put value in equation 1)
T(n-1)=T(n-2)+1 ..........................2)
T(n-2)=T(n-3)+1 .........................3)
put the value of equation 2) and 3) in 1)
T(n)=T(n-3)+3
Solving Recurrence Relations- Substitution Method
Substitution Method
continue for k times .now equation look like,
T(n)=T(n-k)+k
to reach base case ,n-k=0 so k=n
T(n)=T(n-n)+n
T(n)=1+n
so ,T(n)=O(n)
Solving Recurrence Relations- Substitution Method
Substitution Method
II. T(n)=T(n-1)+n, for n>1
T(n)=1, for n=1
Solution:
T(n)=T(n-1)+n ......................1)
T(n-1)=T(n-2)+(n-1)
T(n-2)=T(n-3)+(n-2)
Put the value in equation 1)
T(n)=T(n-2)+(n-1)+n
T(n)=T(n-3)+(n-2)+(n-1)+n .......................2)
Solving Recurrence Relations- Substitution Method
Substitution Method
continue for k times .now equation look like,
T(n)=T(n-(k+1))+(n-k)+.......(n-2)+(n-1)+n .....................3)
To reach base case ,n-(k+1)=1 so k=n-2
Put the value in equation 2)
T(n)=T(n-(n-2+1))+(n-(n-2))+.......(n-2)+(n-1)+n
T(n)=T(1)+2+.......(n-2)+(n-1)+n
T(n)=1+2+.......(n-2)+(n-1)+n
T(n)=(n(n+1))/2
So T(n)=O(n2)
Exercise
www.paruluniversity.ac.in
Design and analysis of Algorithms
Sushil Kumar
Fig.1.2.1[1]:Recurrence Tree
• Step-3
• Lets sum up all the pre-level cost and find the final cost for the given equation.
• You can find that in given Tree:
• At Level-0
• Root will be n –(Pre-level cost at level-0 : Cn)
• Each node represent cost of sub problem it can be seen as(n/3),(2n/3)
• At level-1
• At first level Pre- level cost will be = (n/3)+(2n/3)=(3n/3)=n or cn (where c=1 in this case)
• At level-2
• At level-2 again (n/3) will be divided in to n/9 and 2n/9
• And (2n/3) will be divided in to 2n/9 and 4n/9
• Total cost at level 2(pre-level cost at level -2)= c(n/9+2n/9+2n/9+4n/9)=c9n/9=cn
• Find pre-level cost for all level (Which will eventually be: cn).
• For final cost lets sum up all pre-level cost. And Find upper bound.
Lets Analyse & Sum up
• Omit floor and ceiling functions for simplicity. As before, we let c represent the constant factor in
the O(n) term.
• When we add the values across the levels of the recursion tree, we get a value of cn for every
level.
• The longest path from the root to a leaf is n → (2/3)n → (2/3)2n → ··· → 1.Since (2/3)k n =
1 when k = log3/2 n, the height of the tree is log3/2 n.
• The solution to the recurrence to be at most the number of levels times the cost of each level, or
O(cn log3/2 n) = O(n lg n).
• The total cost is evenly distributed throughout the levels of the recursion tree. There is a
complication here: we have yet to consider the cost of the leaves. If this recursion tree were a
complete binary tree of height log3/2 n, there would be leaves.
• Since the cost of each leaf is a constant, the total cost of all leaves would then be , which is ω(n lg
n).
• This recursion tree is not a complete binary tree, however, and so it has fewer than leaves.
Moreover, as we go down from the root, more and more internal nodes are absent.
• Consequently, not all levels contribute a cost of exactly cn; levels toward the bottom contribute
less.
• Merge Sort: T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and Logba is also 1. So the solution is
Θ(n Logn)
• Binary Search: T(n) = T(n/2) + Θ(1). It also falls in case 2 as c is 0 and Logba is also 0. So the
solution is Θ(Logn)
Cont...
• Master method is mainly derived from recurrence tree method. If we draw recurrence tree of
T(n) = aT(n/b) + f(n), we can see that the work done at root is f(n) and work done at all leaves is
Θ(nc) where c is Logba. And the height of recurrence tree is Logbn.
• In recurrence tree method, we calculate total work done. If the work done at leaves is
polynomially more, then leaves are the dominant part, and our result becomes the work done at
leaves (Case 1).
• If work done at leaves and root(f(n)) is asymptotically same, then our result becomes height
multiplied by work done at any level (Case 2).
• If work done at root(f(n)) is asymptotically more, then our result becomes work done at root
(Case 3).
Case 1
• Lets take. T (n) = 9T(n/3) + n.
In given recurrence,
we have find a = 9, b = 3, f (n) = n.
so Θ(nLogba)=Θ(n2) .
Since f(n)=n , where c= 1
we can apply case 1 of the master theorem and conclude that the solution is T (n) = Θ(n2).
Case 2
• Lets take. T (n) = T (2n/3) + 1.
In given recurrence,
we have find a = 1, b = 3/2, f (n) = 1.
so Θ(nLogba)=Θ(nlog3/21) =n0 =1
Since f(n)=1.
we can apply case 2 of the master theorem and conclude that the solution is T (n) = Θ(log n).
Case 3
• T(n) = 3T(n/4) + n lg n.
we have a = 3, b = 4, f (n) = n lg n, and Θ(nLogba)= Θ(n0.793) . Since f(n)= Θ(n0.793+c)
Where c=0.217 so case 3 can be apply so
T(n)= Θ(f(n)).
For sufficiently large n, a f(n/b) = 3(n/4) lg(n/4) ≤ (3/4)n lg n = cf (n) for c = 3/4. Consequently, by
case 3, the solution to the recurrence is T(n) = Θ(n lg n).
Where master method can not apply?
• T(n/2)=2T(n/2)+n lg n