SlideShare a Scribd company logo
DATA STRUCTURES
ALGORITHM AND ANALYSIS
Rajdeep Chatterjee
Assistant Professor
School of Computer Engineering
KIIT University
OVERVIEW
 Algorithm
 Analysis of Algorithm
 Space Complexity
 Time Complexity
 Step Counts
 Asymptotic Notations
 Big-oh Notations
 Rate of Growth
 Types of Time Complexity
 Best Case Complexity
 Worst Case Complexity
 Average Case Complexity
ALGORITHM
 Algorithm is a finite set of well defined
computational instructions written in a proper
sequence in order to solve a problem.
 Criteria-
 Input
 Output
 Definiteness
 Finiteness
 Effectiveness
EXAMPLES
 ADD: INTEGER a, b, c
1. Read a & b
2. add a & b
3. store the sum of a & b to c
4. print c
 In C, exp(1)
void add(int a, int b){
int c;
c=a+b;
printf(“%d”,c);
}
In C, exp(2)
int add(int a, int b){
int c;
c=a+b;
return c;
}
ANALYZING AN ALGORITHM
 Why is it important?
 Predict the feasibility requirement of your code
(algorithm).
 Usual requirements
 Execution time
 Memory space
 The complexity of the algorithm is determined in
terms of space and time.
 Space complexity ← execution time
 Time complexity ← memory space
SPACE COMPLEXITY
 Amount of computer memory required during
program execution.
 Instruction space – space required to store the
code.
 Fixed space requirement – input independent
 Variable space requirement – input dependent
 S = F(I) otherwise S = C
EXAMPLES
 Exp(1)
int main(){
printf(“KIIT UNIVERSITY”);
}
 Exp(2)
void bubble(int a[], int n){
int i,j;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
swap (a[j],a[j+1])
}
}
TIME COMPLEXITY
 The amount of computer time that it needs to run
to completion.
 It is determined without considering the following
information-
 The machine we are executing,
 Its machine language instruction set,
 The time required by each machine instruction,
 The translation, a compiler/interpreter will make from
the source code to machine language.
EXAMPLES
 Exp(1)
x=x+1;
 Exp(2)
for(i=1 ; i<=n ; i++)
x=x+1;
 Exp(3)
for(i=1 ; i<=n ; i++)
for(j=1 ; j<=n ; j++)
x=x+1;
Our concern should be the order of magnitude/ growth of an algorithm
for an input n.
Exp(4)
int main(){
printf(“KIIT UNIVERSITY”);
}
STEP COUNT
Instructions / code Step Count
x=x+1 constant, c
int main(){
printf(“KIIT UNIVERSITY”);
return 0;
}
?
for(i=1 ; i<=n ; i++)
x=x+1;
n
for(i=1 ; i<=n ; i++)
for(j=1 ; j<=n ; j++)
x=x+1;
n2
EXAMPLE - 1
1. int i, f=1, n=5; -------------- 1 time
2. for(i=1 ; i<=n ; i++) -------------- n+1 times
3. f=f*i; -------------- n times
4. printf(“%d”,f); -------------- 1 time
n i f=f*i , f=1
5 1 1*1=1
2 1*2=2
3 2*3=6
4 6*4=24
5 24*5=120
6
T(n) = 1+(n+1)+n+1 = 2n+3 ≈ O(n)
if n=0 then T(n) = 1+1 ≈ O(1)
EXAMPLE - 2
1. int a=0,b=1,c, i, n=5; ------------- 1
2. printf(“%d %d”,a,b); ------------- 1
3. for(i=1 ; i<=n-2 ; i++ ){ ------------- (n-2)+1 = (n-1)
4. c=a+b; ------------- (n-2)
5. printf(“%d”,c); ------------- (n-2)
6. a=b; ------------- (n-2)
7. b=c; } ------------- (n-2)
i a b c=a+b
1 0 1 1
2 1 1 2
3 1 2 3
4
T(n)= 1+1+(n-1)+2*(n-2) = 2+n-1+2n-4 = 3n-3 ≈ O(n)
EXAMPLE - 3
1. int i, j, n=5, a[5]={12, 2, 51, 35, 7};
2. for(i=0;i<n-1;i++){
3. for(j=0;j<n-1-i;j++)
4. if(a[j]>a[j+1]){
5. a[j] = a[j] + a[j+1];
6. a[j+1] = a[j] - a[j+1];
7. a[j] = a[j] - a[j+1]; }
8. for(i=0 ; i<n ; i++)
9. printf(“%d”, a[i]);
T(n) = ?
ASYMPTOTIC NOTATIONS
 O (Big-oh) Notation
Consider a function f(n) which is non-negative for all
integers n=0. we say that f(n)is Big-oh g(n), which we
write f(n)=O(g(n)), if there exits an integer n0 and a
constant c>0 such that for all integers n=n0, f(n)=cg(n).
 In other words,
O(g(n))={f(n): ∃ positive constants c and n0 ∋ 0 ≤ f(n) ≤cg(n) ∀ 𝑛 ≥ 𝑛0 }
 O-notation to give an upper bound on a function to within a
constant factor.
EXAMPLE
 Two students, X & Y
 Let, performance of X & Y are TX and TY respectively,
 Conclusion is for a large n, TX(n) ≻ TY(n)
 So, who is a better professional ?
 Ans. X
 When X outperforms Y and by what factor ?
 Ans. n0 and c
# Events X Y TX ∼ TY
1 10th 70% 85% TX(1) ≺ TY(1)
2 (10+2)th 65% 78% TX(2) ≺ TY(2)
3 B.Tech 8.8 cgpa 8.7 cgpa TX(3) ≻ TY(3)
4 M.Tech GATE rank 120 GATE rank 1700 TX(4) ≻ TY(4)
5 Ph.D 5 publications 2 publications TX(5) ≻ TY(5)
6 Job 20 lakhs/p.a. 10 lakhs/p.a. TX(6) ≻ TY(6)
EXAMPLE
 Suppose C=1 then, f(n)=6n+135
f(n) = cn2
 6n +135 = cn2 = n2 (since c=1)
 0 = n2 - 6n -135
 0 = (n-15)(n+9)
Since (n+9) > 0 for all values n ≥ 0, then (n-15)=0
 n0=15
 Find n0 when c=2 & c=4.
NOTATIONS
Big-oh : 0≤ f(n) ≤ cg(n)
Theta : 0≤ 𝑐1 𝑔 𝑛 ≤ f(n) ≤ c2 𝑔 𝑛 )
Omega : 0 ≤ cg(n) ≤ f(n)
RATE OF GROWTH
𝒍𝒐𝒈 𝟐 𝒏 n n𝒍𝒐𝒈 𝟐 𝒏 𝒏 𝟐 𝒏 𝟑 𝟐 𝒏
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
TYPES OF TIME COMPLEXITY
 Best Case Time complexity
 Exp(1) – Linear search/Binary search
 Linear search, key=3
 a[0]==key, T(n)=O(1)
 Binary search, key=24
 m=(0+4)/2 = 2
 a[2]==key, T(n)=O(1)
3 5 24 45 78
TYPES OF TIME COMPLEXITY
 Worst Case Time complexity
 Exp(2) – Linear search/Binary search
 Linear search, key=78 / 80
 a[0]==key, T(n)=O(n) / O(n+1) ≈ O(n)
 Binary search, key=78 / 80
 m=(0+4)/2 = 2 …
 a[2]!=key,…
 finally T(n)=O(log2 𝑛) ….(*)
3 5 24 45 78
TYPES OF TIME COMPLEXITY
 Average Case Time complexity
 Exp(3) – Linear search/Binary search
 Linear search, key=24
 a[0]==key, T(n)=O((n+1)/2) ≈ O(n)
 There are n cases that can occur, i.e. find at the first place,
the second place, the third place and so on up to the nth
place. If found at the ith place then i comparisons are
required. Hence the average number of comparisons over
these n cases is:
 average = (1+2+3.....+n)/n = (n+1)/2
 where the result was used that 1+2+3 ...+n is equal to
n(n+1)/2.
 Binary search, key=5
 m=(0+4)/2 = 2, m=(0+1)/2=0, m=(1+1)/2=1
 a[1]==key, finally T(n) ≈ O(log2 𝑛) ….(*)
3 5 24 45 78
SORTING ALGORITHMS
Algorithm
Data
Structure
Time
Complexity:
Best
Time
Complexity:
Average
Time
Complexity:
Worst
Space
Complexity:
Worst
Quick Sort Array O(n log(n)) O(n log(n)) O(n2) O(log(n))
Merge sort Array O(n log(n)) O(n log(n)) O(n log(n)) O(n)
Heap sort Array O(n log(n)) O(n log(n)) O(n log(n)) O(1)
Smooth sort Array O(n) O(n log(n)) O(n log(n)) O(1)
Bubble sort Array O(n) O(n2) O(n2) O(1)
Insertion
sort
Array O(n) O(n2) O(n2) O(1)
Selection
sort
Array O(n2) O(n2) O(n2) O(1)

More Related Content

What's hot

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
Dr Shashikant Athawale
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
Amrinder Arora
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
Ganesh Solanke
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
Swapnil Agrawal
 
Space complexity
Space complexitySpace complexity
Space complexity
Bhanusree Koduru
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
Abhimanyu Mishra
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
Rajendra Dangwal
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
Ashim Lamichhane
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
Soujanya V
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
swapnac12
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 

What's hot (20)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Time complexity
Time complexityTime complexity
Time complexity
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Space complexity
Space complexitySpace complexity
Space complexity
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Viewers also liked

Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
Emertxe Information Technologies Pvt Ltd
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingHaitham El-Ghareeb
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
Sorting
SortingSorting
Sorting
Gopi Saiteja
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Sorting
SortingSorting
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 

Viewers also liked (10)

Sorting
SortingSorting
Sorting
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Sorting
SortingSorting
Sorting
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Sorting
SortingSorting
Sorting
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Similar to Data Structure: Algorithm and analysis

Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
YekoyeTigabuYeko
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
GauravKumar295392
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
Natarajan Angappan
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
Ain-ul-Moiz Khawaja
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
Lec1
Lec1Lec1
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
Lec1
Lec1Lec1
Lec7
Lec7Lec7
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
Agung Kurniawan
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
NikhilKatariya8
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithms
MyMovies15
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithms
snehajiyani
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
ebinazer1
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
ssuser034ce1
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 

Similar to Data Structure: Algorithm and analysis (20)

Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Alg1
Alg1Alg1
Alg1
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Lec1
Lec1Lec1
Lec1
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Lec1
Lec1Lec1
Lec1
 
Lec7
Lec7Lec7
Lec7
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithms
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithms
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 

More from Dr. Rajdeep Chatterjee

Artificial Neural Networks (ANN)
Artificial Neural Networks (ANN)Artificial Neural Networks (ANN)
Artificial Neural Networks (ANN)
Dr. Rajdeep Chatterjee
 
Genetic Algorithm (GA)
Genetic Algorithm (GA)Genetic Algorithm (GA)
Genetic Algorithm (GA)
Dr. Rajdeep Chatterjee
 
Derivative free optimizations
Derivative free optimizationsDerivative free optimizations
Derivative free optimizations
Dr. Rajdeep Chatterjee
 
EEG based Motor Imagery Classification using SVM and MLP
EEG based Motor Imagery Classification using SVM and MLPEEG based Motor Imagery Classification using SVM and MLP
EEG based Motor Imagery Classification using SVM and MLP
Dr. Rajdeep Chatterjee
 
IPR: Introduction to Trademark
IPR: Introduction to TrademarkIPR: Introduction to Trademark
IPR: Introduction to Trademark
Dr. Rajdeep Chatterjee
 
Lecture 1 introduction to computer and its organization
Lecture 1 introduction to computer and its organizationLecture 1 introduction to computer and its organization
Lecture 1 introduction to computer and its organization
Dr. Rajdeep Chatterjee
 
Roughset & it’s variants
Roughset & it’s variantsRoughset & it’s variants
Roughset & it’s variants
Dr. Rajdeep Chatterjee
 
Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...
Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...
Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...
Dr. Rajdeep Chatterjee
 

More from Dr. Rajdeep Chatterjee (8)

Artificial Neural Networks (ANN)
Artificial Neural Networks (ANN)Artificial Neural Networks (ANN)
Artificial Neural Networks (ANN)
 
Genetic Algorithm (GA)
Genetic Algorithm (GA)Genetic Algorithm (GA)
Genetic Algorithm (GA)
 
Derivative free optimizations
Derivative free optimizationsDerivative free optimizations
Derivative free optimizations
 
EEG based Motor Imagery Classification using SVM and MLP
EEG based Motor Imagery Classification using SVM and MLPEEG based Motor Imagery Classification using SVM and MLP
EEG based Motor Imagery Classification using SVM and MLP
 
IPR: Introduction to Trademark
IPR: Introduction to TrademarkIPR: Introduction to Trademark
IPR: Introduction to Trademark
 
Lecture 1 introduction to computer and its organization
Lecture 1 introduction to computer and its organizationLecture 1 introduction to computer and its organization
Lecture 1 introduction to computer and its organization
 
Roughset & it’s variants
Roughset & it’s variantsRoughset & it’s variants
Roughset & it’s variants
 
Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...
Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...
Newtonian Law Inspired Optimization Techniques Based on Gravitational Search ...
 

Recently uploaded

weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 

Recently uploaded (20)

weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 

Data Structure: Algorithm and analysis

  • 1. DATA STRUCTURES ALGORITHM AND ANALYSIS Rajdeep Chatterjee Assistant Professor School of Computer Engineering KIIT University
  • 2. OVERVIEW  Algorithm  Analysis of Algorithm  Space Complexity  Time Complexity  Step Counts  Asymptotic Notations  Big-oh Notations  Rate of Growth  Types of Time Complexity  Best Case Complexity  Worst Case Complexity  Average Case Complexity
  • 3. ALGORITHM  Algorithm is a finite set of well defined computational instructions written in a proper sequence in order to solve a problem.  Criteria-  Input  Output  Definiteness  Finiteness  Effectiveness
  • 4. EXAMPLES  ADD: INTEGER a, b, c 1. Read a & b 2. add a & b 3. store the sum of a & b to c 4. print c  In C, exp(1) void add(int a, int b){ int c; c=a+b; printf(“%d”,c); } In C, exp(2) int add(int a, int b){ int c; c=a+b; return c; }
  • 5. ANALYZING AN ALGORITHM  Why is it important?  Predict the feasibility requirement of your code (algorithm).  Usual requirements  Execution time  Memory space  The complexity of the algorithm is determined in terms of space and time.  Space complexity ← execution time  Time complexity ← memory space
  • 6. SPACE COMPLEXITY  Amount of computer memory required during program execution.  Instruction space – space required to store the code.  Fixed space requirement – input independent  Variable space requirement – input dependent  S = F(I) otherwise S = C
  • 7. EXAMPLES  Exp(1) int main(){ printf(“KIIT UNIVERSITY”); }  Exp(2) void bubble(int a[], int n){ int i,j; for(i=0;i<n-1;i++){ for(j=0;j<n-1-i;j++) if(a[j]>a[j+1]) swap (a[j],a[j+1]) } }
  • 8. TIME COMPLEXITY  The amount of computer time that it needs to run to completion.  It is determined without considering the following information-  The machine we are executing,  Its machine language instruction set,  The time required by each machine instruction,  The translation, a compiler/interpreter will make from the source code to machine language.
  • 9. EXAMPLES  Exp(1) x=x+1;  Exp(2) for(i=1 ; i<=n ; i++) x=x+1;  Exp(3) for(i=1 ; i<=n ; i++) for(j=1 ; j<=n ; j++) x=x+1; Our concern should be the order of magnitude/ growth of an algorithm for an input n. Exp(4) int main(){ printf(“KIIT UNIVERSITY”); }
  • 10. STEP COUNT Instructions / code Step Count x=x+1 constant, c int main(){ printf(“KIIT UNIVERSITY”); return 0; } ? for(i=1 ; i<=n ; i++) x=x+1; n for(i=1 ; i<=n ; i++) for(j=1 ; j<=n ; j++) x=x+1; n2
  • 11. EXAMPLE - 1 1. int i, f=1, n=5; -------------- 1 time 2. for(i=1 ; i<=n ; i++) -------------- n+1 times 3. f=f*i; -------------- n times 4. printf(“%d”,f); -------------- 1 time n i f=f*i , f=1 5 1 1*1=1 2 1*2=2 3 2*3=6 4 6*4=24 5 24*5=120 6 T(n) = 1+(n+1)+n+1 = 2n+3 ≈ O(n) if n=0 then T(n) = 1+1 ≈ O(1)
  • 12. EXAMPLE - 2 1. int a=0,b=1,c, i, n=5; ------------- 1 2. printf(“%d %d”,a,b); ------------- 1 3. for(i=1 ; i<=n-2 ; i++ ){ ------------- (n-2)+1 = (n-1) 4. c=a+b; ------------- (n-2) 5. printf(“%d”,c); ------------- (n-2) 6. a=b; ------------- (n-2) 7. b=c; } ------------- (n-2) i a b c=a+b 1 0 1 1 2 1 1 2 3 1 2 3 4 T(n)= 1+1+(n-1)+2*(n-2) = 2+n-1+2n-4 = 3n-3 ≈ O(n)
  • 13. EXAMPLE - 3 1. int i, j, n=5, a[5]={12, 2, 51, 35, 7}; 2. for(i=0;i<n-1;i++){ 3. for(j=0;j<n-1-i;j++) 4. if(a[j]>a[j+1]){ 5. a[j] = a[j] + a[j+1]; 6. a[j+1] = a[j] - a[j+1]; 7. a[j] = a[j] - a[j+1]; } 8. for(i=0 ; i<n ; i++) 9. printf(“%d”, a[i]); T(n) = ?
  • 14. ASYMPTOTIC NOTATIONS  O (Big-oh) Notation Consider a function f(n) which is non-negative for all integers n=0. we say that f(n)is Big-oh g(n), which we write f(n)=O(g(n)), if there exits an integer n0 and a constant c>0 such that for all integers n=n0, f(n)=cg(n).  In other words, O(g(n))={f(n): ∃ positive constants c and n0 ∋ 0 ≤ f(n) ≤cg(n) ∀ 𝑛 ≥ 𝑛0 }  O-notation to give an upper bound on a function to within a constant factor.
  • 15. EXAMPLE  Two students, X & Y  Let, performance of X & Y are TX and TY respectively,  Conclusion is for a large n, TX(n) ≻ TY(n)  So, who is a better professional ?  Ans. X  When X outperforms Y and by what factor ?  Ans. n0 and c # Events X Y TX ∼ TY 1 10th 70% 85% TX(1) ≺ TY(1) 2 (10+2)th 65% 78% TX(2) ≺ TY(2) 3 B.Tech 8.8 cgpa 8.7 cgpa TX(3) ≻ TY(3) 4 M.Tech GATE rank 120 GATE rank 1700 TX(4) ≻ TY(4) 5 Ph.D 5 publications 2 publications TX(5) ≻ TY(5) 6 Job 20 lakhs/p.a. 10 lakhs/p.a. TX(6) ≻ TY(6)
  • 16. EXAMPLE  Suppose C=1 then, f(n)=6n+135 f(n) = cn2  6n +135 = cn2 = n2 (since c=1)  0 = n2 - 6n -135  0 = (n-15)(n+9) Since (n+9) > 0 for all values n ≥ 0, then (n-15)=0  n0=15  Find n0 when c=2 & c=4.
  • 17. NOTATIONS Big-oh : 0≤ f(n) ≤ cg(n) Theta : 0≤ 𝑐1 𝑔 𝑛 ≤ f(n) ≤ c2 𝑔 𝑛 ) Omega : 0 ≤ cg(n) ≤ f(n)
  • 18. RATE OF GROWTH 𝒍𝒐𝒈 𝟐 𝒏 n n𝒍𝒐𝒈 𝟐 𝒏 𝒏 𝟐 𝒏 𝟑 𝟐 𝒏 0 1 0 1 1 2 1 2 2 4 8 4 2 4 8 16 64 16 3 8 24 64 512 256 4 16 64 256 4096 65536
  • 19. TYPES OF TIME COMPLEXITY  Best Case Time complexity  Exp(1) – Linear search/Binary search  Linear search, key=3  a[0]==key, T(n)=O(1)  Binary search, key=24  m=(0+4)/2 = 2  a[2]==key, T(n)=O(1) 3 5 24 45 78
  • 20. TYPES OF TIME COMPLEXITY  Worst Case Time complexity  Exp(2) – Linear search/Binary search  Linear search, key=78 / 80  a[0]==key, T(n)=O(n) / O(n+1) ≈ O(n)  Binary search, key=78 / 80  m=(0+4)/2 = 2 …  a[2]!=key,…  finally T(n)=O(log2 𝑛) ….(*) 3 5 24 45 78
  • 21. TYPES OF TIME COMPLEXITY  Average Case Time complexity  Exp(3) – Linear search/Binary search  Linear search, key=24  a[0]==key, T(n)=O((n+1)/2) ≈ O(n)  There are n cases that can occur, i.e. find at the first place, the second place, the third place and so on up to the nth place. If found at the ith place then i comparisons are required. Hence the average number of comparisons over these n cases is:  average = (1+2+3.....+n)/n = (n+1)/2  where the result was used that 1+2+3 ...+n is equal to n(n+1)/2.  Binary search, key=5  m=(0+4)/2 = 2, m=(0+1)/2=0, m=(1+1)/2=1  a[1]==key, finally T(n) ≈ O(log2 𝑛) ….(*) 3 5 24 45 78
  • 22. SORTING ALGORITHMS Algorithm Data Structure Time Complexity: Best Time Complexity: Average Time Complexity: Worst Space Complexity: Worst Quick Sort Array O(n log(n)) O(n log(n)) O(n2) O(log(n)) Merge sort Array O(n log(n)) O(n log(n)) O(n log(n)) O(n) Heap sort Array O(n log(n)) O(n log(n)) O(n log(n)) O(1) Smooth sort Array O(n) O(n log(n)) O(n log(n)) O(1) Bubble sort Array O(n) O(n2) O(n2) O(1) Insertion sort Array O(n) O(n2) O(n2) O(1) Selection sort Array O(n2) O(n2) O(n2) O(1)