DSA Complexity Analysis
DSA Complexity Analysis
• Introduction to DSA
• Linked List
• Stack and Queue
• Binary Tree
• Binary Search Tree
• Heap
• Sorting
• Searching
• Hashing
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Introduction
❑ A data structure is a named location that can be used to store and organize data.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Good Computer
Program
❑ Easy to debug.
❑ Easy to modify.
❑ Easy to maintain.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Good Computer
Program
a) Algorithms
b) Data Structures
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
DSA Introduction
❑ A data structure represents the logical relationship that exists between individuals
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Algorithm??
❑ An algorithm states the actions to be executed and the order in which these actions are
to be executed.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Algorithmic
Properties
❑ It must be correct.
❑ No ambiguity.
❑ Must terminate.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Efficiency of an
algorithm.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Efficiency of an
algorithm.
❑ Efficiency of an algorithm denotes the rate at which an algorithm solves a problem of size n.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Characteristics of an
algorithm.
❑ Unambiguous
❑ Input
❑ Output
❑ Finiteness
❑ Feasibility
❑ Independent
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
How to write an
Algorithm?
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Measuring Running Time of an Algorithm
• Implement the algorithm in a language
(Dependent on s/w and h/w)
• Run it on different inputs (Limited number
of inputs)
• Record the exact running time (Not
feasible for algorithms that take longer
Experimental
time to execute)
Method
Measuring
Running Time
Analytical
Method • Analyze running time based on input size.
• Independent of software and hardware
• Consider all possible inputs
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Asymptotic Analysis
• If the running time increases very rapidly on the basis of input then that algorithm is
not considered as efficient algorithm.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
• Consider below example:
Compilation Time
Run Time
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Time Complexity of
an algorithm.
Worst Case
Average Case
Best Case
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Time Complexity of
an algorithm.
❑ Worst Case: Longest time that an algorithm will use to produce desired results.
❑ Average Case : Average time that the algorithm will use. It depends upon probability distribution.
❑ Best Case: Shortest time that the algorithm will use to produce desired results.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Space Complexity of
an algorithm.
Datatype Array[size];
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Space Complexity of
an algorithm.
❑ There are 3 different spaces considered for determining the amount of memory used by the algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Asymptotic Notations
❑ The efficiency of an algorithm is dependent on amount of time, storage and other resources. The efficiency
❑ Asymptotic notations are mathematical notations used to describe the running time of an algorithm.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Asymptotic Notations
•Big-O Notation (O-notation) --------------→ represents upper bound of running time of algo.
•Omega Notation (Ω-notation)-------------→ represents lower bound of the running time of algo.
•Theta Notation (Θ-notation)--------------→represents upper & lower bound of the running time of algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Finding Big O
• Keep the fastest growing term and discard the lower terms and constants.
• Ignore coefficients.
f(n)=23log10n=f(n) is O(logn)
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Tight and Loose Upper bounds
• For a function f(n) there can be many functions g(n) such that f(n) is O(g(n)).
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Que 1: In asymptotic analysis, the exact running time is calculated.
a) True
b) False
Question 2:
For analyzing an algorithm, we study how the running time of the algorithm
increases with the increase in input size.
a) True
b) False
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Question 3:
Growth rate of n is more than growth rate of logn.
a)True
B) False
Question 4:
For finding big O for a function, we have to keep the ____ growing term of
the function, and we can discard all the lower terms and constants.
a) Fastest
b) slowest
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Question 5:
While computing the O notation, base of the logarithm is not important.
a) True
b) False
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Big O analysis of Algorithms
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Calculating Time Complexity
• Running time is proportional to the primitive operations executed during run time.
• Primitive operations include comparisons, arithmetic operations, assignments,
input and output operations.
• To find function T – Express the number of primitive operations executed during
run time , in terms of n.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
• So, in Asymptotic Analysis we are not interested in “how much time an operation
takes to execute on a computer”.
• Basically, we have to look into operations of loops whose iterations are dependent
on n.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Big O Analysis of Algo…Examples
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Big O Analysis of Algo…Examples
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Big O Analysis of Algo…Nested Loops
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Big O Analysis of Algo…Nested Loops
Examples
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Worst, Average and Best Case Analysis
Data D1 of Running
size n time T1
Algo A.
Data D2 of Running
size n time T2
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
• Worst Case:
• Max running time that an algo will take for input size n.
• Best Case:
• Minimum running time that an algo will take for input size n.
• Average Case:
• Average running time that an algo will take for input size n.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
• Common Complexities
• O(1) Constant
• O(log n) Logarithmic
• O(n) Linear
• O(n log n) Linear Logarithmic
• O(n^2) Quadratic
• O(n^3) Cubic #matrix operations
• O(n^k) polynomial
• O(a^n) Exponential #tower of Hanoi problem
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video