Professional Documents
Culture Documents
Unit - I
9
Performance Analysis (Priori estimates)
• Two criteria are used to judge algorithms: (i) time complexity (ii)
space complexity.
• Space Complexity of an algorithm is the amount of space needed to
solve an instance of a computational problem.
• Time Complexity of an algorithm is the amount of time it needs to
run an Algorithm.
10
Space Complexity
• Memory space S(P) needed by a program P, consists of two
components:
• A fixed part: needed for instruction space (byte code), simple variable space,
constants space etc. c
• A variable part: dependent on a particular instance of input and output data.
Sp(instance)
• S(P) = c + Sp(instance)
11
Space Complexity: Example 1
1. Algorithm abc (a, b, c)
2. {
3. return a+b+b*c+(a+b-c)/(a+b)+4.0;
4. }
For every instance 3 computer words required to store variables: a,
b, and c. Therefore Sp()= 3. S(P) = 3.
12
Space Complexity: Example 2
1. Algorithm Sum(a[], n)
2. {
3. s:= 0.0;
4. for i = 1 to n do
5. s := s + a[i];
6. return s;
7. }
13
Space Complexity: Example 2.
• Every instance needs to store array a[] & n.
• Space needed to store n = 1 word.
• Space needed to store a[ ] = n floating point words (or at
least n words)
• Space needed to store i and s = 2 words
• Sp(n) = (n + 3). Hence S(P) = (n + 3).
14
Time Complexity
• Time required T(P) to run a program P also consists of two
components:
• A fixed part: compile time which is independent of the problem instance c.
• A variable part: run time which depends on the problem instance
tp(instance)
• T(P) = c + tp(instance)
• Fixed part is usually ignored; only the variable part tp() is measured.
15
i=1 i=1
For(i<=n)
For(i<=n)
{ i=i+2
{ i=i+1 }
}
AlgoSum(A[],m, n)
1 Algorithm Sum(a[],n) 0 - 0
2 { 0 - 0
3 S = 0.0; 1 1 1
4 for i=1 to n do 1 n+1 n+1
5 s = s+a[i]; 1 n n
6 return s; 1 1 1
7 } 0 - 0
2n+3 18
Time Complexity: Example 2
Statements S/E Freq. Total
1 Algorithm Sum(a[],n,m) 0 - 0
2 { 0 - 0
5 s = s+a[i][j]; 1 nm nm
6 return s; 1 1 1
7 } 0 - 0
2nm+2n+2 19
Orders of Growth
Classification of Time Complexities
Name of Efficiency Classes Order of Growth Examples
22
Asymptotic Notations
= n0
25
Big O Notation
• Example: f(n) = 10n2+4n+2 is O(n2) because 10n2+4n+2 <= 11n2 for all
n >=5.
• Example: f(n) = 6*2n+n2 is O(2n) because 6*2n+n2 <=7*2n for all n>=4.
• Algorithms can be: O(1) constant; O(log n) logrithmic; O(nlogn);
O(n) linear; O(n2) quadratic; O(n3) cubic; O(2n) exponential.
26
Big O Notation
• Now it is easy to compare time or space complexities of algorithms.
Which algorithm complexity is better?
• T(P1) = O(n) or T(P2) = O(n2)
• T(P1) = O(1) or T(P2) = O(log n)
• T(P1) = O(2n) or T(P2) = O(n10)
27
Omega Notation
f(n) ɛ ɵ (g(n))
Problem Solving Principle
• Understand the Problem: Problem Stmt, Known, Unknown
• Make Decisions: Suitable DS, Algorithmic Technique
• Design or Specify the Problem: Flowcharts, Natural Languages
• Verify the Algorithm: Checking the correctness of Algorithm
• Analyze the Algorithm: Space and Time
• Implement: Suitable Programming Languages
• *Testing, Documentation, Maintenance and follow up
Classification of Problem
• Sorting: Ascending, Descending: numbers, Chars, Strings: Key & Unique
• Searching: Search for desired element. Search Key, Many algorithms
• Numerical Problems: Math Eqns, System of equations, definite integrals
• Geometric Problems: Points, Lines, Polygon
• Combinatorial Problems: Solvable, Unsolvable. No Algorithms-Finite
Time
• Graph Problems: Vertices and Edges: CN-Graph Traversals, Shortest Path
Problem Solving Strategies
• Brute Force: Straight Forward, Naïve Approach
• Divide and Conquer: Break a problem in to subProblem
• Dynamic Programming: The results of smaller, re-occuring instances
are obtained to solve the problem.
• Greedy Technique: locally optimal decisions are made
• Backtracking: Trial and error
• Branch & Bound: setting bound on live branches.
Divide-and-Conquer
The most-well known algorithm design strategy:
1. Divide instance of problem into two or more smaller instances
• T(n)= T(1)
aT(n/b)+f(n)
Examples of Recurrence relation
• Void Test (n)
•{
• if (n>0)
• {
• print(“%d”, n);
• Test(n-1)
• }
•}
• Void Test (n)
•{
• if(n>0)
• {
• for(i=0; i<n;i++)
• {
• printf(“%d”, i)
• }
• Test(n-1)
• }
•}
• Void Test (n)
•{
• if(n>0)
• {
• for(i=1; i<n;i*2)
• {
• printf(“%d”, i)
• }
• Test(n-1)
• }
•}
Mergesort
• Split array A[0..n-1] into about equal halves and make
copies of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
• Repeat the following until no elements remain in one of the
arrays:
• compare the first elements in the remaining unprocessed portions
of the arrays
• copy the smaller of the two into A, while incrementing the index
indicating the unprocessed portion of that array
• Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
Pseudocode of Mergesort
Pseudocode of Merge
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
The non-recursive
version of Mergesort
3 8 2 9 1 7 4 5 starts from merging
single elements into
2 3 8 9 1 4 5 7 sorted pairs.
1 2 3 4 5 7 8 9
Quicksort
• Select a pivot (partitioning element) – here, the first element
• Rearrange the list so that all the elements in the first s
positions are smaller than or equal to the pivot and all the
elements in the remaining n-s positions are larger than or
equal to the pivot
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e., )
subarray — the pivot is now in its final position
• Sort the two subarrays recursively
Partitioning Algorithm
or i > r
< or j = l
• Improvements:
• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
• elimination of recursion
These combine to 20-25% improvement
The problem:
Multiply two matrices A and B, each of size n n
A B C
nn nn nn
56
The traditional way:
n
C ij Aik Bkj
k 1
3
T ( n) O ( n )
57
The Divide-and-Conquer way:
C11 C12 A11 A12 B11 B12
C A B
C 21 C 22 A21 A22 B21 B22
59
n
T (n) 8 T ( ) an 2
2
O(n 3 )
which n2 is for addition
62
Example: 1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
use Divide-and-Conquer way to solve it as following:
1 1 1 0 1 1 1 0 3 3 3 0
1 1 1 0 1 1 1 0 3 3 3 0
1 1 1 0 1 1 1 0 3 3 3 0
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 1 1 3 3
C11
1 1 1 1 1 0 0 0 3 3
1 1 1 0 1 0 1 0 3 0
C12
1 1 1 0 1 0 0 0 3 0
1 1 1 1 1 0 1 1 3 3
C 21
0 0 1 1 0 0 0 0 0 0
1 1 1 0 1 0 1 0 3 0
C 22
0 0 1 0 0 0 0 0 0 0
63
Strassen’s matrix multiplication:
·Discover a way to compute the Cij’s using 7 multiplications
and 18 additions or subtractions
P ( A11 A22 )( B11 B22 )
Q ( A21 A22 ) B11
R A11 ( B12 B22 )
S A22 ( B21 B11 )
T ( A11 A12 ) B22
U ( A21 A11 )( B11 B12 )
V ( A12 A22 )( B21 B22 )
C11 P S T V
C12 R T
C 21 Q S
C 22 P R Q U
64
•Algorithm :
65
else
Partition A into 4 submatrices: A11 , A12 , A; 21 , A22
Partition B into 4 submatrices: B11 , B12 , B;21 , B22
n
call Strassen ( 2
;
, A11 A22 , B11 B22 , P)
n
call Strassen ( , A21 A22 , B11 , Q);
2
n
call Strassen ( , A11 , B12 ; B22 , R )
2
n
call Strassen ( , A22 , B21 ; B11 , S )
2
n
call Strassen ( , A11 A12;, B22 , T )
2
66
n
call Strassen ( , A21 A11 , B11 ; B12 ,U )
2
n
call Strassen ( , A12 A22 , B21 ; B22 ,V )
2
C11 P S T V ;
C12 R T ;
C 21 Q S ;
C 22 P R Q U ;
end;
Young
CS 331 D&A of Algo. Topic: Divide and Conquer 67
Analysis:
n
7 T ( ) an 2 n2
T ( n) 2
b n2
n
T (n) 7 T ( ) an 2
2
n 7
7 T ( 2 ) ( )an 2 an 2
2
2 4
n 7 2 7
7 T ( 3 ) ( ) an ( ) an 2 an 2
3 2
2 4 4
Young
CS 331 D&A of Algo. Topic: Divide and Conquer 69
Assume n = 2k for some integer k
k 1 n 2 7 k 2
7 T ( k 1 ) an ( ) 1
2 4
7 k 1
( ) 1
7 k 1 b an 2 4
7
1
4
7
b 7k c n2 ( )k
4
7
7 Lg
b 7 Lgn cn 2 ( ) Lgn b 7 Lgn cn 2 (n) 4
4
b n Lg 7 cn Lg 7 (b c) n Lg 7
O(n Lg 7 ) O (n 2.81 )
70
Masters Theorem
• T(n)=aT(n/b)+nklogpn
• Condition: a>=1; b>1; k>=0; p is a real no
• Case1: if a>bk then T(n)=(nlog a) b
• Case2: if a= bk
i) if p>-1 then T(n)= (nlogba logp+1)
ii) if p=-1 then T(n)= (nlogba loglogn)
iii) if p<-1 then T(n)= (nlogba )
• Case3: if a<bk
i) if p>=0 then T(n)= (nk logpn)
ii) if p<0 then T(n)= (nk)