0% found this document useful (0 votes)
3 views

Lecture 3

The document discusses time complexity analysis and asymptotic bounds, focusing on how the execution time of algorithms changes with input size. It outlines various techniques for complexity analysis, including operation counts and asymptotic complexity, and emphasizes the importance of best-case, average-case, and worst-case performance evaluations. Additionally, it presents examples of step counts and typical growth rates for different functions.

Uploaded by

ahsannouman11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 3

The document discusses time complexity analysis and asymptotic bounds, focusing on how the execution time of algorithms changes with input size. It outlines various techniques for complexity analysis, including operation counts and asymptotic complexity, and emphasizes the importance of best-case, average-case, and worst-case performance evaluations. Additionally, it presents examples of step counts and typical growth rates for different functions.

Uploaded by

ahsannouman11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Time complexity

Analysis and
Asymptotic Bounds
Algorithm Analysis

As the “size” of an algorithm’s input grows (array


length, size of queue, etc.):

 Time: How much longer does it run?


 Space: How much memory does it use?

For now, we will focus on time only.


Complexity Analysis

 A technique to Characterize the execution time


of an algorithm independently from the
machine, operating system, the language and
the compiler.

 Useful for:
 Comparing algorithms
 Evaluating the variations of execution time with
regard to the input data
Some complexity
analysis techniques
1. Operation counts
2. Step counts
3. Counting cache misses
4. Asymptotic complexity
5. Amortized complexity
6. Practical complexity

 We are typically interested in the execution


time of large instances of a problem, e.g., when
n∞, (asymptotic complexity)
Step count

 we attempt to account for the time spent in all


parts of the algorithm.

 A step is any computation unit that is


independent of the problem size.
 For example; return a+b+b*c+(a+b-c)/(a+b)+4;

x = y; is also a single step


First Example

1. int mean(int a[], size_t n)


2. {
3. int sum = 0;
4. for (int i = 0; i < n; i++)
5. sum += a[i];
6. return sum;
7. }
Analysis Cases

 In general, we are interested in three types of


performance

 Best-case / fastest
 Symbol Ω. Example; Ω(n2)

 Average-case
 Symbol θ. Example; θ(logn)

 Worst-case/ slowest
 Symbol O. Example; O(n)
Typical growth rates

Function Name

C constant
logN logarithmic
log2N Log-squared
N Linear
N logN
N2 Quadratic
N3 Cubic
2N Exponential
Normally these formulas
are very handy
Continue….
Continue…
Examples step counts

1. long fact(int n){


2. if (n==0)
3. return 1;
4. Long ans=1;
5. for(int i=1;i<=n;i++)
6. ans=ans*i;
7. return ans;
8. }
Example 2

1. long foo(int n){


2. long x=0;
3. if (n==0)
4. return 1;
5. Long ans=1;
6. for(int i=1;i<=n;i++) {
7. for (int j =0; j<n; j++)
8. x*=j;
9. ans*=i;
10. ans+=x;}
11. return ans;
}
Example 3

1. sum = 0;
2. for( i = 0; i < n; ++i )
3. for( j = 0; j < n * n; ++j ){
4. ++sum;
5. x=+1}

Note: Independent Nested Loop


Example 4

1. sum = 0;
2. for( i = 0; i < n; ++i )
3. for( j = 0; j < i; ++j )
4. ++sum;

Note: Dependent Nested Loop


Example 5

1. sum = 0;
2. for( i = 0; i < n; ++i )
3. for( j = 0; j < i * i; ++j )
4. for( k = 0; k < j; ++k )
5. ++sum;
Example 6

1. Int sum=0;
2. for (int j = 1; j < n; j = j * 2)
3. ++sum;

4. Int sum=0;
5. for (int j = 1; j < n; j = j * k)
6. ++sum;
Example 7

1. Int sum=0;
2. for (int j = 1; j < n; j = j + 2)
3. ++sum;

4. Int sum=0;
5. for (int j = n; j < 0; j = j - 2)
6. ++sum;
Example 8

1. for(int i=0; i<n; i++) {


2. for(int j=1; j<n; j=j*2){ ----}
3. }
4. ------------------------------------------------
5. for(int i=0; i<n; i++) {
6. for(int j=1; j<n/2; j++){ ----}
7. }
Example 9

1. int i=1; s=1


2. While( s<n)
3. { s=s+i; i++; }
4. ----------------------------------------------------
5. int a,b;
6. While(a!=b)
7. { if(a>b) { a=a-b; }
8. else { b=b-a; }
9. }
10.
Example 10

1. for(int i=0; i*i<n;i++) {


cout<<“Hello”; }
----------------------------------------------------
2. for(int i=0; i<n/2;i++) {
for(int j=1; j+n/2<n;i++){
for(int k=1; k<n; k=k*2){
cout<<“Time complexity??”;
}
}
}
Example 11

1. sum = 0;
2. for( i = 0; i < n; ++i ){
3. for( j = 0; j < i * 2; ++j ){
4. k=j
5. while (k>=1)
6. k=k-1;
7. }}

You might also like