Complexity
Complexity
Algorithms
Efficiency
The Analysis
Framework
There are two kinds of efficiency:
• Time efficiency and Space efficiency.
• Note: The research experience has shown that for most problems, we
can achieve much more spectacular progress in speed than in space.
Asymptotic
Notations
Big-Oh (O)
What about 3n – 5 ?
• 3n – 5 – n for all n therefore, 3n-5 = Ω (n)
Theta Notation (θ)
T(n) = CopC(n)
Where
• T(n) - running time of a program
• cop be the execution time of an algorithm’s basic operation on a
particular computer
• C(n) be the number of times this operation needs to be
executed for this algorithm.
Question
Time complexity
int sum (int a[], int int Rsum (int a[], int n)
n) {
{ if (n > 0)
int result; return Rsum(a, n-1)
for(int i=0; i<n; + a[n-1];
i++)
result= result return 0;
+ a[i]; }
Sp(n) = 0 result;
return constant Rsum(a, n) Rsum(a, n-1)
space
} O(1) ….Rsum(a,1), Rsum(a, 0)
therefore, depth= n+1
Sp(n) = 12 (n+1) bytes
=O(n)
Data Space Examples
i. Int *a;
a = new int [n]; Space needed 4n Bytes (n integers, each need
4 Bytes)