Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Lecture 1,2,3 – Week 2

Design and Analysis of Algorithms


Farrukh Ehsan
Time complexity of an algorithm
§ Time complexity refers to the computational complexity that describes the amount of time an
algorithm takes to run as a function of the length of the input.
§ In simpler terms, it characterizes how the runtime of an algorithm increases as the size of the input
increases.
§ Think of time complexity like measuring how fast or slow an algorithm works when you give it a
task.
§ It's like asking, "How does the time taken by the algorithm change when I give it more work to do?”
§ Time complexity helps us understand how the time taken by an algorithm changes as we give it
more work. It's a way to compare algorithms and see which ones are more efficient for different
tasks.
Finding Timing Complexity of a piece of code
There are two attributes
§ Finding exact running time
§ Hardware and software dependency
§ Very time consuming
§ Error Prone
§ Finding complexity profile as a function of input size
§ When we calculate time complexity, we don’t want any kind of hardware and software
dependency, so that everyone can agree on it.
§ We need to know the time complexity based on pseudo code.
Why do we need to find time complexity?
As we discussed in the previous slides that we don’t want to calculate the running time, then what is
the purpose of finding the time complexity?
§ The reason to calculate the time complexity is that there might be more than one ways to solve a
certain problem.
§ For example, you’ve studied more than one algorithms to sort the list.
§ Now the question is which one is better algorithm out of all sorting list algorithms?
§ Now to perform this compression, we need to calculate the time complexity.
§ At the same time, we need to know which algorithm will perform better if we increase the input
size for any algorithm.
§ For example, if we want to sort a list of elements, now here size of the input will be number of elements in the list.
What does it mean by input size?
§ Input size also varies from problem to problem.
§ We will discuss the importance of input size in coming lectures.
How to find time complexity?
Operations counting technique
§ Assign each primitive operation (+,-,*,/, etc.) equal cost (1 unit)
§ Assign unit cost to object creation
§ Assign unit cost to initialization, copy and assignment of primitive objects
§ Assign unit cost to return statement of primitive objects
§ Assign unit cost to function call excluding arguments/parameters passing.
§ You can say number of units or total operation cost, both are same things.
§ So, what we do
§ Find number of operations of each line of code or operation cost
§ Find the frequency of execution of each line of code
§ So total operation cost of a particular line will be:
§ (number of operations in that line * frequency of execution)
§ At the end we will get total number of operations as a function of input size
§ We normally represents input size with n.
Examples
§ Developing Timing Equation
operations (Cost) * Frequency
1. a=2*a+3*k 4 * 1
2. a=a*k 2 * 1
§ Total cost (in terms of number of operations) T(n) = 4 + 2
§ 6 operations/units cost is the time complexity of these two lines of code when the input size is n.
§ It is constant and it does not depend on the input size.
§ Developing Timing Equation
operations (Cost) * Frequency
//for( i = 1 to n)
1. for ( int i = 1; i <= n ; ++ i) { (3+1)*2(n-1) or (1+1)+2*n
2. a=a*k 2 * (n)
3. }
§ Total cost (in terms of number of operations) T(n) = 2*n + 2*n + (1+1) => 4*n + 2
§ 4*n + 2 operations/units' cost is the time complexity of these two lines of code when the input size
is n.
§ Developing Timing Equation
operations (Cost) * Frequency
//for( i = 1 to n)
1. for ( int i = 1; i < n ; ++ i) { (3+1)*2(n-2) or (1+1)+2*n(n-1)
2. a=a*k 2 * (n-1)
3. }
§ Total cost (in terms of number of operations) T(n) = 2*(n-1) + 2*(n-1) + (1+1) => 4*n - 2
§ 4*n - 2 operations/units' cost is the time complexity of these two lines of code when the input size
is n.
§ Developing Timing Equation
operations (Cost) * Frequency
//for( i = 1 to n step 2)
1. for ( int i = 1; i <= n ; i+2) {
2. a = a * k}
§ Total cost (in terms of number of operations) T(n) =
§ operations/units' cost is the time complexity of these two lines of code when the input size is n.

You might also like