Time complexity and space complexity are ways to measure how efficient an algorithm is. They help us understand how the algorithm's performance changes as the size of the problem gets bigger.
Time complexity.
The relationship between the input size and the running time or number of operations performed by an algorithm is captured by the concept of time complexity. Time complexity helps us understand how the algorithm's performance changes as the input size increases.
Why Time complexity?
For making a better program.
For comparison of the algorithm.
Complexity Notation
There are three types of complexity notation.
Big notation (O) :
Certainly! Big O notation (O) is a way to describe the upper bound or worst-case scenario of an algorithm's time or space complexity. It helps us understand how the algorithm's performance changes as the input size increases.O(n): Linear complexity. The algorithm's performance grows in direct proportion to the input size. If the input size doubles, the algorithm's time or space requirement also doubles.
O(n^2): Quadratic complexity. The algorithm's performance grows with the square of the input size. If the input size doubles, the algorithm's time or space requirement increases by four times.
O(log n): Logarithmic complexity. The algorithm's performance grows at a slower rate than the input size. As the input size increases, the algorithm's time or space requirement grows, but not as fast. This is often seen in efficient algorithms like binary search.
Omega notation (Ω): It represents the best-case scenario or the lower bound of the algorithm's time or space complexity. It provides information about the minimum performance of an algorithm. For example, Ω(n) implies a linear lower bound, Ω(n^2) represents a quadratic lower bound, and Ω(log n) indicates a logarithmic lower bound.
Theta notation (Θ): It represents the tight bound or the average-case scenario of the algorithm's time or space complexity. It provides both the upper and lower bounds, indicating that the algorithm's performance grows at a similar rate as the input size increases. For example, Θ(n) implies linear growth with the same upper and lower bounds.
Example of complexity notation.
Time Complexity in Nested Loop
//example 1
for(let i=0; i<n; i++){
for(let j=0; j<n; i++){
console.log("hello");
}
}
Solution:
if n=5
i=0 -> j=0,1,2,3,4 n
i=1 -> j=0,1,2,3,4 n
i=2 -> j=0,1,2,3,4 n
i=3 -> j=0,1,2,3,4 n
i=4 -> j=0,1,2,3,4 n
Therefore time complexity : O(n^2)
//example 2
for(let i=0; i<n; i++){ n
for(let j=0; j<m; i++){ m
console.log("hello");
}
}
}
Solution:
if n=5
i=0 -> j=0,1,2,3,4 m
i=1 -> j=0,1,2,3,4 m
i=2 -> j=0,1,2,3,4 m
i=3 -> j=0,1,2,3,4 m
i=4 -> j=0,1,2,3,4 m
Therefore time complexity : O(nm)
Time Complexity in the case of two loops.
//example 3
for(let i=0; i<n; i++){ n
console.log("hello")
}
for(let j=0; j<m; i++){ m
console.log("hello");
}
Solution:
i=0 1 2 3 4 5 --------n
j=0 1 2 3 4 5 --------n
Therefore time complexity : O(n^2)
But if n= 10^10 (very big)
m = 2 (very small)
here m is neglisible thats why
Time complexity: O(n)//example 3
for(let i=0; i<n; i++){ n
console.log("hello")
}
for(let j=0; j<m; i++){ m
console.log("hello");
}
Solution:
i=0 1 2 3 4 5 --------n
j=0 1 2 3 4 5 --------n
Therefore time complexity : O(n^2)
But if n= 10^10 (very big)
m = 2 (very small)
here m is neglisible thats why
Time complexity: O(n)
Analysing Time Complexity.
Analysis of Time Complexity.
Compare : O(n) O(n^2) O(n3)
n=1 1 1 1
n=2 2 4 8
n=3 3 9 27
n= 10^5 10^5 10^10 10^30
Note: Dynamic programming is used further for other data structures to make optimize with lesser time.
n^4 -> n^2 -> n^2 -> n^1
Space Complexity
Space complexity refers to the amount of memory or storage required by a program or algorithm to solve a problem. It measures how much additional space is needed, beyond the input itself, to perform the necessary computations.
Why Space Complexity?
- Resource Optimization: Analyzing space complexity helps optimize memory usage in algorithms or programs
Memory Constraints: Space complexity helps ensure algorithms fit within limited memory resources.
Performance Evaluation: Space complexity complements time complexity in assessing overall algorithm efficiency.
Scalability Analysis: Space complexity predicts memory usage growth with larger inputs, aiding in evaluating the scalability.
Example of Space Complexity
Space Complexity in the Case of Array