Academic Session 2025-26
ODD Semester Jul-Dec 2025
UNIVERSITY INSTITUTE OF ENGINEERING
CSE-2nd year
CS201/IT201
SEMESTER 3
Advanced Data Structures and Algorithms
(24CSH-202)
Unit No. 1 Chapter No. 1.1.1 Topic :Analysis of Algorithms
____________________________________________________
Faculty Name and E_Code: Bhavneet Kaur(E14414)
Designation: Assistant Professor, Master Subject Co-ordinator
Learning Objectives
•Understand the Importance of Algorithm
Analysis
•Analyze Time and Space Complexity
.
•Differentiate Between Complexity Cases
Analysis of Algorithms
1.Complexity Analysis of
Algorithms
2. Time Complexity
3.Space Complexity
4. Important Functions in
algorithm analysis
Complexity Analysis of Algorithms
• An algorithm is a procedural way for solving a
problem in a finite amount of time.
• Importance of algorithm analysis: Predict
performance, understand scalability
Input Algorithm Output
Considerations for Analysis
• Determine the basic operation (e.g., comparisons,
assignments)
• Input size as a variable (n)
• Worst-case vs average-case
Running Time
• Most algorithms transform input best case
average case
objects into output objects. 120
worst case
100
• The running time of an
Running Time
80
algorithm typically grows with
60
40
the input size. 20
0
1000 2000 3000 4000
• Average case time is often I nput Size
difficult to determine.
Running Time –Contd
• Focus is on the worst case running time.
• Easier to analyze
• Crucial to applications such as games,
finance and robotics
7
Time Complexity
• The number of times it takes to
execute a specific algorithm given
the length of the input is known
as its temporal complexity.
• Not a measure of how long an
algorithm takes to run: Since
operating system, processor
power, and programming
language are taken into account 8
Time Complexity
• The amount of time it takes for each
statement to be completed is the
algorithm's temporal complexity.
• Depends on the volume of the
processed data.
• Determine the efficacy and performance
evaluation of an algorithm.
9
Example #include <iostream>
using namespace std;
Here in the given code
int main()
snippet ,”Bhavneet” is printed n {
times since the value will be
int i, b ;
assigned at run time. for (i = 1; i <= b; i++) {
cout <<
So,the time complexity is linear: “Bhavneet!!!\n";
O(n) i.e. every time, a linear amount }
return 0;
of time is required to execute code. }
10
Space Complexity
• When an algorithm is run on a
computer, it occupies a specific
amount of memory space. Memory used to store
input data and
• The amount of memory used by a temporal values while
program to execute . running
• Space complexity is auxiliary and
input space.
11
Example-BINARY SEARCH
In the case of the iterative
approach, no extra space is used.
Hence, the space complexity
is O(1).
• In the worst
case, logn recursive calls are
stacked in the memory.
12
Example-BINARY SEARCH(Contd.)
• i comparisons require i recursive calls
to be stacked in memory.
• Since average time complexity analysis
has logn comparisons, the average
memory will be O(logn).
• Thus, in recursive implementation the
overall space complexity will
be O(logn). 13
Measure Time Complexity of an
Algorithm
1. Identify the Input Size: Determine the variable that
represents the size of the input. This is often denoted as n.
2. Count Basic Operations: Identify the basic operations in the Analyze loops,
algorithm, such as comparisons, assignments, and arithmetic conditionals, and
operations. Count how many times these operations are
executed relative to the input size.
recursions
3. Find the Most Significant Term: Express the count of Add up costs of all
operations as a function of the input size. Focus on the term operations
that grows the fastest as the input size increases. This term
dictates the overall time complexity. Example: Nested
4. Express in Big-O Notation: Use Big-O notation to express the loops => O(n^2)
time complexity. Big-O notation captures the asymptotic
behaviour of the algorithm, ignoring constant factors and
lower-order terms.
14
• Consider a simple example of a linear search algorithm:
Example def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
• Steps to Determine Time Complexity:
1.The input size n is the length of the array arr.
2.The basic operation here is the comparison arr[i] == target,
which occurs inside the loop. The loop runs n times in the worst
case.
3.The number of comparisons is directly proportional to the
input size n.
4. The time complexity is O(n) 15
Different cases for analysing time
complexity
• A single loop running n times has a time
complexity of O(n)
Loops:
• Nested loops, where each loop runs n times,
typically have a time complexity of O(n^2)
• Analyse the recursion tree to determine the time
Recursion: complexity.
• Use the Master Theorem for divide-and-conquer
algorithms.
• O(1): Constant time
• O(logn)O(\log n)O(logn): Logarithmic time
• O(n): Linear time
• O(nlogn): Linearithmic time (common in efficient sorting algorithm-
Common Complexities: mergesort and heapsort)
• O(n^2): Quadratic time (common in simple sorting algorithms
• like bubble sort)
• O(2^n): Exponential time
• O(n!): Factorial time 16
Measure Space Complexity of
Algorithm
1. Identify the Input Size: Determine the variable that
represents the size of the input, often denoted as n. Identify Count the
2. Count the Memory Usage: Identify the memory the Input Memory
consumed by variables, data structures, and Size Usage
function call stacks.
3. Find the Most Significant Term: Express the
memory usage as a function of the input size. Focus Find the
on the term that grows the fastest as the input size Express in
Most
increases.
Big-O
Significant
Notation
4. Express in Big-O Notation: Use Big-O notation to Term
express the space complexity, ignoring constant
factors and lower-order terms.
17
Examples Recursive Function
Consider a recursive function to compute
Simple Function with Primitive
the factorial of a number:
Variables
def factorial(n):
Consider a function that sums the elements of an
if n == 0: return 1
array:
else:
def sum_array(arr): return n * factorial(n - 1)
total = 0 for num in arr:
total += num return total Steps to Determine Space Complexity:
1.The input size n is the integer for which
Steps to Determine Space Complexity: we are computing the factorial.
1.The input size n is the length of the array arr. 2.The function uses-Space for the
2.The function uses-An integer total which takes function call stack. Each recursive call
O(1)space. adds a new frame to the stack.
3.There is no auxiliary variable used except few The depth of the recursion is n.
other variables. 4.The recursive calls are the dominant
4.The space complexity is O(1) term.
5.The space complexity is O(n)
18
Different Cases for Analyzing Space
Complexity
Static •Variables and fixed-size data structures (e.g., arrays, strings of fixed length) typically contribute O(1)space.
vs. •Dynamic data structures (e.g., lists, trees, hash maps) can contribute O(n) space depending on the number
of elements.
Dynamic
Allocatio
n:
•The space complexity includes the space for the call stack.
Rec •For a recursive function with a maximum depth of recursion d, the space complexity is often O(d)
ursi
on:
•The extra space or temporary space used by an algorithm.
Auxi •Distinguish between the input space (space taken by the inputs) and auxiliary space (extra space required by the
algorithm).
liary
Spa
ce: 19
Seven Important Functions
• Seven functions that often appear
in algorithm analysis: 1E+29
1E+27 Cubic
– Constant 1 1E+25 Quadratic
1E+23
– Logarithmic log n 1E+21 Linear
– Linear n 1E+19
1E+17
– N-Log-N n log n
T(n)
1E+15
1E+13
– Quadratic n2 1E+11
– Cubic n3 1E+9
1E+7
– Exponential 2n 1E+5
1E+3
1E+1
1E-1
• In a log-log chart, the slope of the 1E-1 1E+2 1E+5 1E+8
line corresponds to the growth rate n
of the function 20
Case Study- Optimizing Delivery Routes for a
Hyperlocal Grocery App
A startup offers a hyperlocal grocery delivery service in a metropolitan city.
As the number of daily orders increased, the backend routing system started showing
performance delays. The app initially used a greedy nearest-neighbor algorithm for route
optimization, but delivery efficiency declined during peak hours.
The development team needed to evaluate and optimize the algorithm to improve performance
and minimize total delivery time.
Outcome:35% faster route computation with A*
- 20% improvement in customer satisfaction
- Better scalability and performance understanding
Reference / Source: Skiena (2008), Algorithm Design Manual
- CLRS (2009), Chapters 15, 24, 35
- Swiggy Engineering Blog
21
Summary of the Lecture
•Algorithm Analysis helps evaluate performance by measuring
time and space complexity with respect to input size.
•Time Complexity measures how execution time
• increases with input size, while space complexity
•evaluates memory usage.
•Worst-case analysis is most commonly used due to its
predictability and is easier to evaluate compared to average-
case.
•Big-O notation is used to express the upper bounds
•of algorithm performance, focusing on dominant terms and
ignoring constants. 22
Quiz
1.Which of the following is the correct time complexity for 4. Suppose we are comparing the running times
linear search in the worst case? of two algorithms. Algorithm A has a running
a) O(1) time of 100n², and Algorithm B has a running
b) O(log n) time of 2ⁿ. Which algorithm runs faster for
c) O(n)
small input sizes, and which one scales better
d) O(n log n)
2. In recursive functions, what determines the space for large input sizes?
complexity? a) A is faster for all input sizes
a) Number of variables b) B is faster for small n, A scales better for large
b) Depth of recursive calls n
c) Size of return value c) A is faster for small n, B scales better for large
Identify
d) Numbertheofdominant
iterations term in the expression: 5n^2 n
3. + 3n log n + 10. d) A is faster for small n, B is infeasible for large
a) 5n² n
b) 3n log n Ref: Introduction to Algorithms" by Thomas H.
c) 10 Cormen,Chapter 2
d) All grow equally
Ref:NPTEL Assignments
23
References
• Mark Allen Weiss-Data Structures and Algorithm Analysis in C
• "Introduction to Algorithms" by Thomas H. Cormen, Charles
E. Leiserson, Ronald L. Rivest, and Clifford Stein, MIT Press.
• https://www.youtube.com/watch?v=7KQf7f8ti1U
Relevant learning resources
• https://nptel.ac.in/courses/106106131
• https://archive.nptel.ac.in/courses/106/105/106105164/
Thank You