0% found this document useful (0 votes)
18 views

DSA Complexity Analysis

Complexity Analysis

Uploaded by

onkarlonari71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

DSA Complexity Analysis

Complexity Analysis

Uploaded by

onkarlonari71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Course Contents:

• 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 computer program is a collection of instructions to perform a specific task. For this, a


computer program may need to store data, retrieve data, and perform computations on
the data.

❑ 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

❑ Run efficiently and correctly.

❑ User friendly interface.

❑ Easy to read and understand.

❑ 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

Program consists of 2 things:

a) Algorithms

b) Data Structures

*Good program is combination of both.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
DSA Introduction

❑ An algorithm is a step by step process to solve particular problem statement.

❑ A data structure represents the logical relationship that exists between individuals

elements of data to carry out certain tasks.

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 is a precise procedure for solving a problem in finite number of steps.

❑ 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.

❑ Composed of series of concrete steps.

❑ No ambiguity.

❑ Composed of finite number of steps.

❑ Must terminate.

❑ It takes zero or more inputs.

❑ It should use less memory space as much as possible.

❑ It results in one or more outputs.

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.

❑ Algorithms are programs in general form.

❑ It is an idea upon which program is designed.

❑ It should be independent of the programming language.

❑ Every programmer having enough knowledge and

experience should understand it.

❑ It should be applicable to inputs of all sizes.

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.

❑ Time and Space

❑ Time = no. of steps algorithm executes.

❑ Space = no. of unit memory storage it requires.

❑ Algorithm complexity calculation: Time taken & Space required.

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?

❑ It is problem and resource dependent.

❑ Algorithm are never written to support a particular programming code.

❑ Problem domain should be well defined.

❑ Example: Design an algorithm to add 2 numbers and display the result.

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

• Running time is dependent on size of input(n).


• Small input size = less running time.
• Big input size = more running time.
• Example: Array Elements Sorting.

• This kind of behavior of algorithm is studied in 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:

Input Size 2 4 7 100 1000 10000


n
Algo A. 4 8 14 200 2000 20000
(time)
Algo B. 4 16 49 10000 1000000 100000000
(time)

• Algo B will take more time as compared to Algo A.


• We will definitely choose Algo A.
• Algo B. will be very inefficient for larger input.
• To determine the efficiency of the algorithm we have to see how the algorithm behaves
when the input size is increased.
• To find the rate of growth of running time we will use Big O Notation.
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.

❑ The time taken for an algorithm is comprised of:

Compilation Time

Run Time

❑ Compile time is the time taken to compile an algorithm.

It checks syntax and semantic errors.

❑ Run time is the time to execute the compiled program.

Is runtime calculated only for executable statements or declaration statements?

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.

❑ Time complexity of an algorithm is generally classified as three types:

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.

❑ Space complexity of the program is the amount of memory consumed.

❑ Fixed amount of memory.

❑ Variable amount of memory.

❑ Memory taken by instruction not in control of programmer.

❑ Memory taken by variable is in control of programmer.

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.

1. Instruction Space: occupied by compile version of program.

2. Data Space: used to hold variables, data structures, data elements.

3. Environment Space: used for run time stack on function call.

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

is measured with the help of asymptotic notations.

❑ Asymptotic notations are mathematical notations used to describe the running time of an algorithm.

❑ Example: Bubble Sort:

1. If input array is already sorted: best case

2. If array is in reverse condition: worst case

3. Array is neither sorted nor in reverse condition: average case

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Asymptotic Notations

There are mainly three asymptotic notations:

•Big-O Notation (O-notation) --------------→ represents upper bound of running time of algo.

(worst case complexity)

•Omega Notation (Ω-notation)-------------→ represents lower bound of the running time of algo.

(best case complexity)

•Theta Notation (Θ-notation)--------------→represents upper & lower bound of the running time of algo.

(average case complexity)

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.

• If f(n) is constant then we say that f(n) is O(1).

• Base of logarithm is not important.

• Example: f(n)=8log2n = f(n) is O(logn)

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

• Express the running time as function of input size(n).


• T(n) is running time in terms of n.

• Find Big O for function T(n).

• We have to find the rate of growth.

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.

def somealgo(int n):


input #c1 T(n)=c1+c2+c3*n+c4
assignment #c2 T(n) is O(n)
for I in range(n):
comparison #c3*n

arithmetic operation #c4

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”.

• We are interested in, “How many times that operation is executed”.

• We will be concerned about while writing function T, about whose numbers


depends on input size n.

• 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

• We can ignore assignment and output


def somealgo(int n):
operation as they don’t depend on n. Last
for loop as well can be ignored because it
assignment
will execute for 6 times always.
• Arithmetic operation will execute (n-2)
for I in range(n-2,0,-1):
times.
arithmetic operation
• So, T(n) = (n-2)+n
output
• = n+n-2
for I in range (n):
• = 2n-2
comparison
• Ignore coefficient and constant we will get,
for I in range (6):
• T(n) = n
assignment

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

• Outer loop will execute n times.


def somealgo(int n):
• For each iteration of outer loop the inner
loop will execute n times.
input
• Total iterations will be: n+n+n….n times
for I in range(n):
= n*n =n^2
assignment
T(n)=n+n^2
T(n) is O(n^2)
for j in range (n):
comparison
arithmetic operation
output

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.

• Usually worst case is done which is very informative.


• Best case is not informative.
• Average case is very difficult.

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

You might also like