-
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-Onotation. - Sometimes we can improve time
complexityby thetrading offwithspace complexityand 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 elementin anarrayis fast but if you want to constantlyremovean element from anarrayor add an element to an array, it can beexpensivesince constantly we have toresize the array. - So if our task involves constantly removing and adding an element, we need to use a
linkedlistBut the catch here is accessing the element from alinked listis 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
5then there arefiveiterations and if the loop size isnthus there areniterations. - Consider there are
2 for loopsin a function from a single input then, itsn+n, finally the result isn - Consider there are
2 for loopsin 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 growthwithlinear growth, we can see thatlinear growthgrows at the same rate, but thelogarithmic growthslows down at some point. - An algorithm that runs in
logarithmic timeis more efficient and scalable than an algorithm that runs inquadraticorlineartime. - Once classic example for this is the
linear searchandbinary searchcomparison. - Say there is a collection of numbers from
1 to 10. The number10is 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.