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

Lecture-3_Complexity Analysis of Algorithms

The document discusses how to determine the computational complexity of algorithms through various examples. It illustrates the process of calculating time complexity using nested loops and provides specific examples with their corresponding complexities, such as O(n) and O(n^2). Additionally, it includes a comparison of time complexities for different data structures in worst-case scenarios.

Uploaded by

ovikhan753
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)
5 views

Lecture-3_Complexity Analysis of Algorithms

The document discusses how to determine the computational complexity of algorithms through various examples. It illustrates the process of calculating time complexity using nested loops and provides specific examples with their corresponding complexities, such as O(n) and O(n^2). Additionally, it includes a comparison of time complexities for different data structures in worst-case scenarios.

Uploaded by

ovikhan753
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/ 17

CSE214: Algorithms

Computational complexity

1
Today’s Contents
• How to find the Complexity of an Algorithm?

2
Let’s Find out the complexity of
Algorithms

3
Find the Complexity of the following Algorithm

Example-1:
a = 4; // This will be executed only once.
b = a + 7; // This will be executed only once

for i = 1 to n; // This will be executed n+1 times


SUM = a+b ; // This will be executed n times

4
Find the Complexity of the following Algorithm

Example-1:

So the number of operations required by this loop are


T(n) = (1 + 1 + (n + 1) + n)
= 2n + 3
= O (n)
5
Find the Complexity of the following Algorithm

Example-2:

for i = 1 to n -------------------- O (n)

for j = 1 to n -------------------- O (n)


SUM = a+b ;

So the number of operations required by this loop are

• T(n) = O (n) * O (n)


= O (n2)
6
Find the Complexity of the following Algorithm

Example-3:
Self Practice

for i = 1 to n
for j = 1 to n
for k = 1 to n
SUM = a+b ;

7
Find the Complexity of the following Algorithm

Example-4:

for i = 1 to n ------------------ O (n)

\\ SUM = a+b ;
for j = 1 to n ---------------- O (n)
\\ SUM = a+b ;

So the number of operations required by this loop are

T(n) = O (n) + O (n)


= O (2n)
= O (n) 8
Find the Complexity of the following Algorithm

Example-5:

for i = 1 to n ---------------------- O(n)

for j = 1 to 500 ---------------------- O (1)


SUM = a + b ;

So the Time Complexity


T(n) = O(n) * O(1)
= O(n)
9
Find the Complexity of the following Algorithm

Self Practice
Example-6:

Sum = 0
for i = 1 to n
SUM += A[i] ;
Print Sum

10
Find the Complexity of the following Algorithm

Self Practice
Example-6:

Sum = 0
for i = 1 to n
SUM += A[i] ;
Print Sum

11
Find the Complexity of the following Algorithm

Example-7:

MAX = A[0] ------------------ 1


for i = 1 to n ---------------- n + 1
if A[i] > MAX ---------------- n
MAX = A[i] ; ---------------- C
Print MAX ---------------- 1

So the Time Complexity


T(n) = 1+ (n +1) + n + C + 1
= (2n + 3 + C)
12
= O (n)
Find the Complexity of the following Algorithm
Example-8:

Self Practice

flag = FALSE
for i = 1 to n
if A[i] == key
flag = TRUE;

if flag == TRUE
FOUND
else
NOT FOUND
13
Some common rates of growth

Let n be the size of input to an algorithm, and k some


constant. The following are common rates of growth.

14
Rate of Growth

15
Time complexity (Worst Case) of
some data Structures

Time complexity (Worst Case)


Access Search Insertion Deletion

Array O(1) O(n) O(n) O(n)

Singly O(n) O(n) O(1) O(1)


Linked List

Stack O(n) O(n) O(n) O(n)


or or
O(1) O(1)

16
Thank you!

17

You might also like