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

Lecture 5 ESO207 2022 TimeComplexityBigONotation MaximumSumSubarray

1. The document discusses time complexity analysis and big O notation. It provides examples of algorithms and analyzes their time complexity using big O notation. 2. The most efficient algorithm is the one with the lowest order term in its time complexity when expressed using big O notation. Constant factors and lower order terms do not affect the overall asymptotic time complexity analysis. 3. Comparing algorithms based on their time complexity expressed using big O notation allows analyzing which algorithm will perform better for large input sizes. The algorithm with the lower order term has superior time complexity.

Uploaded by

Iitian
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)
37 views

Lecture 5 ESO207 2022 TimeComplexityBigONotation MaximumSumSubarray

1. The document discusses time complexity analysis and big O notation. It provides examples of algorithms and analyzes their time complexity using big O notation. 2. The most efficient algorithm is the one with the lowest order term in its time complexity when expressed using big O notation. Constant factors and lower order terms do not affect the overall asymptotic time complexity analysis. 3. Comparing algorithms based on their time complexity expressed using big O notation allows analyzing which algorithm will perform better for large input sizes. The algorithm with the lower order term has superior time complexity.

Uploaded by

Iitian
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/ 24

Data Structures and Algorithms

(ESO207A)

Lecture 5:
• Time complexity, Big “O” notation
• Designing Efficient Algorithm
• Maximum sum subarray Problem

1
Assignment 1
Algorithms for F()mod No. of Instructions in RAM model

RFib()
?
IFib()

CleverFib() 56 + 11

• Which algorithm turned out to be the most efficient ?


• How close to reality is the RAM model of computation ?

Variation in the time of various operations


Architecture : 32 versus 64
Code optimization due to Compiler
Multitasking due to Operating system

2
Lesson 1
from Assignment 1 ?
No. of instructions executed by
algorithm in RAM model

Prop
? orti on al to

Time taken by algorithm


in real life

May
be d
iffer
?
ent f
or diffe
rent
i np u
t Dependence on input

3
Time complexity of an algorithm

Definition:
the worst case number of instructions executed
as a function of the input size

The number of bits/bytes/words


Examples to illustrate input size :
Problem Input size
Computing F()mod for any positive integers bits
Whether an array storing numbers (each stored in a word) is sorted ? words
Whether a matrix of numbers (each stored in a word) contains “14” ? words

Homework: What is the time complexity of Rfib, IFib, CleverFib ?

5
Example:
Whether an array A storing numbers (each stored in a word) is sorted ?

IsSorted(A)
{ ; time
flag true; time
while( and flag=true) 3 more instructions before each iteration
{
If (A[]< A[]) flagfalse ; times in the worst case
;
}
Return flag ; time
}

Time complexity =

𝟓 𝒋−𝟐 6
Example:
Time complexity of matrix multiplication
Each element of the matrices
Matrix-mult(C[],D[]) occupies one word of RAM.
{ for to times
{ for to times
{ M[]  ;
for to
{ M[] M[] + C[]*D[]; instructions
}
}
}
Return M time
}
Time complexity =

𝟑 𝒏𝟑 +𝟑 𝒏𝟐 +𝟐𝒏+𝟏=¿ ( ( ( 𝟑 𝒏+𝟏 )+𝟐 ) 𝒏+𝟐 ) 𝒏+𝟏 7


Lesson 2 learnt from Assignment 1 ?
Algorithm for F()mod No. of Instructions

RFib(,)

IFib(,)

CleverFib(,) 56 + 11
100 1000

Question: What would have been the outcome if


No. of instructions of CleverFib() = 100 + 1000

Answer: CleverFib would still be the fastest algorithm ...for large value of .

8
COMPARING EFFICIENCY OF ALGORITHMS

9
Comparing efficiency of two algorithms
Let A and B be two algorithms to solve a given problem.

Algorithm A has time complexity : 2 + 125


Algorithm B has time complexity : 5 + 67 + 400

Question: Which algorithm is more efficient ?

Obviously A is more efficient than B

10
Comparing efficiency of two algorithms
Let A and B be two algorithms to solve a given problem.

Algorithm A has time complexity : 2 + 125


Algorithm B has time complexity : 50 + 125

Question: Which one would you prefer based on the efficiency criteria ?
Answer : A is more efficient than B for < 25
B is more efficient than A for > 25
Time complexity is
really an issue only
when the input is of
large size

11
Rule 1

Compare the time complexities of two algorithms for


asymptotically large value of input size only

12
Comparing efficiency of two algorithms

Algorithm B with time complexity 50 + 125

is certainly more efficient than

Algorithm A with time complexity : + 125

13
A judgment question for you !
Algorithm A has time complexity f()= + + 1250
Researchers have designed two new algorithms B and C

• Algorithm B has time complexity g() = + 10


• Algorithm C has time complexity h() = + 20 + 2000

Which of B and C is an
improvement over A in the
true sense ?

= 1/5 = 0?
C is an improvement over A
in the true sense.
14
Rule 2

An algorithm X is superior to another algorithm Y if


the ratio of time complexity of X and time complexity of Y
approaches 0 for asymptotically large input size.

15
Some Observations
Algorithm A has time complexity f()= + + 1250
Researchers have designed two new algorithms B and C

• Algorithm B has time complexity g() = + 10


• Algorithm C has time complexity h() = + 20 + 2000

Algorithm C is the most efficient of all.


Observation 1:
multiplicative or additive Constants do not play any role.

Observation 2:
The highest order term governs the time complexity asymptotically.

16
ORDER NOTATIONS

A mathematical way
to capture the intuitions developed till now.
(reflect upon it yourself)

17
Order notation
Definition: Let f() and g() be any two increasing functions of n.
f() is said to be of the order of g()
if there exist constants c and such that
f() ≤ c g() for all n >

c g()

f()

𝑛0

If f() is of the order of g(), we


write f() = O(g()) 18
Order notation :
Examples
• 20 = O() 20, 1
• 100 = O() Loose 1, 160

160, 1
• = O() Loose While analyzing time complexity of an
algorithm accurately, our aim should be to
• 2000 = O(1) choose the g() which is not loose.
Later in the course, we shall refine & extend
Simple observations: this notion suitably.
 If f() = O(g()) and g() = O(h()), then
f() = O(h())
 If f() = O(h()) and g() = O(h()), then f() + g() =
?
O(h())
These observations can be helpful for simplifying time complexity.

Prove these observation as Homeworks


19
A neat description of time complexity
• Algorithm B has time complexity g() = + 10
Hence g() = O()

• Algorithm C has time complexity h() = + 20 + 2000


Hence h() = O()

• Algorithm for multiplying two matrices has time complexity


+ + 1 = O()

Homeworks:
• g() = , f() = . Is f() = O(g()) ? Give proof.
• What is the time complexity of selection sort on an array storing n elements ?
• What is the time complexity of Binary search in a sorted array of n elements ?
20
HOW TO DESIGN EFFICIENT ALGORITHM ?
(This sentence captures precisely the goal of theoretical computer science)

21
Designing an efficient algorithm

Facts from the world of algorithms:


1. No formula for designing efficient algorithms.
2. Every new problem demands a fresh approach.

3. Designing an efficient algorithm or data structure requires


1. Ability to make key observations.
2. Ability to ask right kind of questions.
3. A positive attitude and …
4. a lot of perseverance.

We shall demonstrate the above facts


during this course many times.
22
Max-sum subarray problem
Given an array A storing n numbers,
find its subarray the sum of whose elements is maximum.

4 7

A 3 -5 3 8 2 -4 9 -6 3 -2 -8 3 -5 1 7 -9

18 -2

23
Max-sum subarray problem:
A trivial algorithm
A_trivial_algo(A)
{ max A[0];
For i=0 to n-1
For j=i to n-1
{ temp  compute_sum(A,i,j);
if max< temp then max temp;
}
return max;
} Homework: Prove that its
time complexity is O()
compute_sum(A, i,j)
{ sumA[i];
For k=i+1 to j sum sum+A[k];
return sum;
} 24
Max-sum subarray problem:

Question: Can we design O() time algorithm for Max-sum subarray problem ?
Answer: Yes.

Think over it with a fresh mind ….


We shall design it together in the next class…

25

You might also like