0% found this document useful (0 votes)
20 views21 pages

06 Complexity and Asymtotic Analysis

This document provides an overview of complexity and asymptotic analysis techniques for analyzing algorithms. It discusses analyzing constant time statements, loops including for, do-while, and nested loops. It covers determining the time complexity of conditional statements and sequences of statements using techniques like summation series analysis. Graphical representations of different growth rates are also mentioned. The document reminds students about an upcoming lab project to work in teams to analyze an interesting data structures and algorithms topic.

Uploaded by

Haseeb Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

06 Complexity and Asymtotic Analysis

This document provides an overview of complexity and asymptotic analysis techniques for analyzing algorithms. It discusses analyzing constant time statements, loops including for, do-while, and nested loops. It covers determining the time complexity of conditional statements and sequences of statements using techniques like summation series analysis. Graphical representations of different growth rates are also mentioned. The document reminds students about an upcoming lab project to work in teams to analyze an interesting data structures and algorithms topic.

Uploaded by

Haseeb Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CS250: Data Structures and Algorithms

Complexity and Asymptotic Analysis

Instructor: Dr. Sohail Iqbal

1
Emprical Analysis
Data Analysis
How many steps?
Standard analysis techniques
• Standard analysis techniques
– Constant time statements
– Analyzing Loops
• For Loop
• Do-While Loop
• Nested Loops
– Conditional Statements
– Sequence of Statements
Constant Time Statements
Simplest case: O(1) time statements
• Assignment statements of simple data types
int x = y;
• Arithmetic operations
x = 5 * y + 4 - z;
count = count + 1;
• Array referencing / assignment
A[j] = 5;
• Most conditional tests
if (x < 12) ...
Constant Time Statements
Example: Simple If-Statement
Times
if (n < 0) 1
absval = -n 1
else
absval = n; 1

Time complexity = O (1)


Analyzing Loops
• Any loop has two parts:
1. How many iterations are performed?
2. How many steps per iteration?
int sum = 0,j;
for (j=0; j < N; j++)
sum = sum + j; Product Rule
• Loop executes N times (0….N-1)
• Four O(1) steps per iteration
• Total time is N * O(1) = O(N*1) = O(N)
Analyzing Loops
Algorithm arrayMax(A, n)
# operations

currentMax  A[0] 2
for (i =1; i<n; i++) 1+n+
n-1
if A[i]  currentMax then 2(n 
1)
currentMax  A[i] 2(n 
1)
Analyzing Loops
Example: Simple Loop
Times
i = 1; 1
sum = 0; 1
while (i <= n) { n+1
i = i + 1; n
sum = sum + i; n
}

Total Cost = 1 + 1 + (n+1) + n + n


Time complexity = O(n)
Analyzing Loops

• What about while-loops?


• Determine how many times the loop will be executed

bool done = false;


int result = 1, n;
cin >> n;

while (!done)
{
result = result * n;
n--;
if (n <= 1) done = true;
}

• Loop terminates when done == true, which happens after


N iterations. Total time: O(N)
Analyzing Loops
Nested Loops
• Treat just like a single loop and evaluate each level
of nesting as needed.
int j,k;
for (j=0; j<N; j++)
for (k=0; k<N; k++)
sum += k+j;
• Start with outer loop:
– How many iterations? N
– How much time per iteration? Need to evaluate inner loop
• Inner loop uses O(N) time

• Total time is N * O(N) = O(N*N) = O(N2)


Analyzing Loops (Nested Loops)
Example: Nested Loop
Times
i=1; 1
sum = 0; 1
while (i <= n) { n
j=1; n
while (j <= n) { n*n
sum = sum + i; n*n
j = j + 1; n*n
}
i = i +1; n
}
Total Cost = 1 + 1 + (n+1) + n + n*(n) + n*n + n*n + n
Time Complexity O(n2 )
Analyzing Loops
Nested Loops
• What if the number of iterations of one loop
depends on the counter of the other?
int i,j;
for (i=N; i > 0; i--)
for (j=N; j >= i; j--)
sum += i + j;
• Sol: Analyze inner and outer loops together
– Number of iterations of the inner and outer loop is:
1 + 2 + ... + N = O( ??? )
Sum of Series analysis
• When doing Big-O analysis, we sometimes
have to compute a series like:
1 + 2 + 3 + ... + (N-1) + N
What is the complexity of this?
Remember Gauss’ equation
n
Σi= n * (n+1) = n2 + n = O(N2) =1 + 2 + 3 + … + n
2 2
i=1
Conditional Statements
 What about conditional statements such as
if (condition)
statement1;
else
statement2;
where statement1 runs in O(N) time and statement2 runs
in O(N2) time
• We use "worst case" complexity: among all inputs of size
N, what is the maximum running time?
• Analysis of the example above is O(N2)
Sequence of Statements
• For a sequence of statements, compute their
complexity functions individually and add them up
for (j=0; j < N; j++)
for (k =0; k < j; k++)
sum = sum + j*k; O(N2)
for (i=0; i < N; i++)
sum = sum - i;
O(N)
cout << “Sum is ” << sum;
O(1)2 Sum Rule
 Total cost is O(N2) + O(N) +O(1) = O(N )
Graphical Representation of Growth Rates

19
Reminder: Lab Project
• Choose an Interesting topic relevant to DSA

• Make a team of 2-3 people.

• Two teams can work close to each other.

• Proposal Defense. 16 Oct 2023 [10 lab marks]

• Final Defense. 16 Dec 2023. [20 lab marks]


20
Questions

21

You might also like