0% found this document useful (0 votes)
33 views31 pages

5 Time & Space Complexity 1.1!03!08 2024

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)
33 views31 pages

5 Time & Space Complexity 1.1!03!08 2024

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/ 31

INTRODUCTION TO ALGORITHMS

URL: https://forms.gle/LsD6CnjK5gaVNHg98

QR CODE:
TIME AND SPACE
COMPLEXITY
TOPICS

⮚ What is an algorithm complexity?


Time complexity
Space complexity
⮚ Analysis of algorithm
⮚ Asymptotic notation
1.Big oh (O)notation
2.Big omega (Ω) notation
3.Theta(Θ) notation
What is an Algorithm Complexity?

The complexity of an algorithm in Java, or any programming language, refers to

the evaluation of its efficiency in terms of time and space requirements as a

function of the input size. As mentioned earlier, algorithm complexity is typically

categorized into two types:


a. Time Complexity: It measures the amount of time an algorithm takes to run as

a function of the input size 'n'. Time complexity is denoted using Big O notation,

which provides an upper bound on the growth rate of the algorithm's running

time. The notation is expressed as O(f(n)), where 'f(n)' represents a function

describing the worst-case time required for an algorithm.


b. Space Complexity: It measures the amount of memory space an algorithm uses

as a function of the input size 'n'. Space complexity is also denoted using Big O

notation, and it represents the upper bound on the additional memory space

required by the algorithm during execution.


Analysis of Algorithm

⮚ The goal of analysis of an algorithm is to compare algorithm in running time


and also Memory management.
⮚ Running time of an algorithm depends on how long it takes a computer to
run the lines of code of the algorithm.
Running time of an algorithm depends on
1.Speed of computer
2.Programming language
3.Compiler and translator
Examples: binary search, linear search
Asymptotic Analysis:

⮚ Expressing the complexity in terms of its relationship to know function. This


type of analysis is called asymptotic analysis.
⮚ The main idea of Asymptotic analysis is to have a measure of the efficiency
of an algorithm, that doesn’t depends on
1.Machine constants.
2.Doesn’t require an algorithm to be implemented.
3.Time taken by the program to be prepared.
Asymptotic Notation:
The mathematical way of representing the Time complexity.
⮚The notation we use to describe the asymptotic running time of an algorithm is defined in terms
of functions whose domains are the set of natural numbers.
Definition: It is the way to describe the behavior of functions in the limit or without bounds.
⮚Asymptotic growth: The rate at which the function grows…
⮚“growth rate” is the complexity of the function or the amount of resources it takes up to
compute.
⮚ Growth rate Time +memory
Classification of growth:

1.Growing with the same rate.

2.Growing with the slower rate.

3.Growing with the faster rate.


types:

They are 3 asymptotic notations are mostly used to represent the time complexity

of the algorithm.

1.Big oh (O)notation

2.Big omega (Ω) notation

3.Theta(Θ) notation
Big-O notation

Big-O notation represents the upper bound of the running time of an algorithm.

Thus, it gives the worst-case complexity of an algorithm.


Big-O notation
Big-O notation

O(g(n)) = { f(n): there exist positive constants c and n0

such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }

The above expression can be described as a function f(n) belongs to the set O(g(n)) if there

exists a positive constant c such that it lies between 0 and cg(n), for sufficiently large n.

For any value of n, the running time of an algorithm does not cross the time provided by

O(g(n)).
Big-O notation

Since it gives the worst-case running time of an algorithm, it is widely used to

analyze an algorithm as we are always interested in the worst-case scenario.


Omega(Ω) notation

Omega notation represents the lower bound of the running time of an algorithm.

Thus, it provides the best case complexity of an algorithm.


Omega(Ω) notation
Omega(Ω) notation

Ω(g(n)) = { f(n): there exist positive constants c and n0

such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }

The above expression can be described as a function f(n) belongs to the set Ω(g(n)) if

there exists a positive constant c such that it lies above cg(n), for sufficiently large n.

For any value of n, the minimum time required by the algorithm is given by Omega

Ω(g(n)).
Theta Notation (Θ-notation)

Theta notation encloses the function from above and below. Since it represents

the upper and the lower bound of the running time of an algorithm, it is used for

analyzing the average-case complexity of an algorithm.


Theta Notation (Θ-notation)
Theta Notation (Θ-notation)

For a function g(n), Θ(g(n)) is given by the relation:

Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0

such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }

The above expression can be described as a function f(n) belongs to the set Θ(g(n)) if there exist

positive constants c1 and c2 such that it can be sandwiched between c1g(n) and c2g(n), for

sufficiently large n.
Theta Notation (Θ-notation)

If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0, then f(n) is

said to be asymptotically tight bound.


INTERVIEW
QUESTIONS
Interview questions
1. What is the time complexity of the following code snippet?

void printPairs(int[] arr) {


for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
System.out.println(arr[i] + ", " + arr[j]);
}}}

Answer: The time complexity of this code is O(n^2) since it uses nested loops to print all

possible pairs of elements in the array.


Interview questions
2. How can you improve the time complexity of the previous code ?

Answer: You can improve the time complexity to O(n) by using a different approach.

Instead of nested loops, you can use a single loop and a HashSet to keep track of visited

elements.
Interview questions
3. Explain the difference between the time complexities of linear search and binary search
algorithms.

Answer: Linear search has a time complexity of O(n) since it checks each element in the

list one by one until the target element is found or the end of the list is reached. Binary

search, on the other hand, has a time complexity of O(log n) as it halves the search space

at each step, making it much more efficient for sorted arrays.


Interview questions
4. What is the space complexity of the following recursive function to calculate the factorial of a
number?
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}}
Answer: The space complexity of this function is O(n) due to the recursive calls that are pushed
onto the call stack until the base case is reached.
Interview questions
5. What is the significance of Big-O notation in algorithm analysis?

Answer: Big-O notation represents the upper bound of an algorithm's running time and

gives the worst-case complexity. It helps us understand how the algorithm's performance

scales with larger input sizes.


Interview questions
6. Explain Omega notation and its role in algorithm analysis.

Answer: Omega notation represents the lower bound of an algorithm's running time and
provides the best-case complexity. It gives us the minimum time required by the algorithm
for any input size.
THANK
YOU

+91 78150 95095 [email protected] www.codemithra.com

You might also like