-
Notifications
You must be signed in to change notification settings - Fork 0
Measuring the complexity
Devrath edited this page Jul 16, 2021
·
15 revisions
Contents |
Notation |
---|---|
Measuring the complexity | |
Constant time | O(1) |
Linear time | O(n) |
Quadratic time | O(n2) |
Logarithmic time | O(logn) |
Exponential time | O(2^n) |
- Time and space complexity is measured using the
Big-O
notation. - Sometimes we can improve time
complexity
by thetrading off
withspace complexity
and vice-versa.
- This is used to measure the
time complexity
, meaning the performance of an algorithm. - Using this analysis, we can determine if the program is scalable or not.
- Say, you write a program and it runs quickly on your computer does not mean it will run quickly when the size of the input grows.
- Certain operations are more or less costly depending on what data structures we use.
-
Accessing an element
in anarray
is fast but if you want to constantlyremove
an element from anarray
or add an element to an array, it can beexpensive
since constantly we have toresize the array
. - So if our task involves constantly removing and adding an element, we need to use a
linkedlist
But the catch here is accessing the element from alinked list
is slow
-
- Measuring the space complexity is a bit different, Consider the program
fun printData(inputData : IntArray){
// Space Complexity = O(n)
var newCopiedData : IntArray = inputData
// Space Complexity = O(1)
for(input in inputData){
println("Data$input")
}
}
- So while calculating the space complexity, We ignore the value that is passed as a parameter into a function, And we consider additional space allocated in the function, Based on the space allocated we calculate the complexity.
- Consider the snipped below where we print a value to the screen.
- Here the time complexity is
O(1)
fun printData(inputData : IntArray){
System.out.println("Data"+inputData[0])
}
- If there are two print statements, Still the time complexity is
O(1)
- This is because since we use an array and we are accessing it by index, it uses a constant amount of time
fun printData(inputData : IntArray){
System.out.println("Data"+inputData[0])
System.out.println("Data"+inputData[1])
}
- We are just trying to get the approximate amount of time and not the exact time since the execution time differs from system to system.
The time complexity increases constantly as the size of the input increases
The time complexity increases Linearly as the size of the input increases
- Consider a for loop and in each iteration, we are printing a value. Now if the loop maximum size is
5
then there arefive
iterations and if the loop size isn
thus there aren
iterations. - Consider there are
2 for loops
in a function from a single input then, itsn+n
, finally the result isn
- Consider there are
2 for loops
in a function and this time there are 2 inputs to functions then, itsm+n
, still finally the result isn
- Consider we have a function that is having one input parameter of collection, now for this collection say we have nested loops
- Here the time complexity will be
n*n
=n2
- If there are three nested loops then the complexity will be
n*n*n
=n3
- When we compare
logarithmic growth
withlinear growth
, we can see thatlinear growth
grows at the same rate, but thelogarithmic growth
slows down at some point. - An algorithm that runs in
logarithmic time
is more efficient and scalable than an algorithm that runs inquadratic
orlinear
time. - Once classic example for this is the
linear search
andbinary search
comparison. - Say there is a collection of numbers from
1 to 10
. The number10
is at the end. Now we need to find the number 10. If we use a linear search strategy maximum of 10 iterations but if we use the binary search strategy, on every iteration the collection is reduced by half meaning time to calculate the item reduces continuously, we can binary search has logarithmic time complexity.
- This is exactly opposite to the constant time and should be avoided at all costs.