SlideShare a Scribd company logo
1 of 25
Download to read offline
Introduction to Data Structure and
Algorithms
Manoj Kumar Rana
Research Asst. Professor
Dept. of Computing Technologies
SRM Institute of Science and Technology
Data structure and Algorithms
• Algorithm
▫ A step-by-step timely instruction or outline of a
computational procedure
• Program
▫ Implementation of an algorithm by using a
programming language
• Data Structure
▫ Organization of data needed to solve a problem
2
Data structure and Algorithms (2)
• Object
▫ An instance of a set of heterogeneous data, defining
behavior or character of a real-life entity (e.g. A Car
object which contains data like color, size price, etc.)
• Object oriented program
▫ A program where objects can interact with each other
to ease the implementation of the algorithm
• Relation between data structure and object
▫ Object is also one data type.
▫ Multiple objects can be organized by following a
specific data structure
3
Algorithmic problem
• Infinite set of input instances satisfying the
specification. For example, a sorted
non-increasing sequence of numbers of finite
length:
▫ 100000, 20000, 1000, 900, 20, 1.
Specification
of input
Specification of
output as a
function of
input
?
4
Algorithmic solution
• Algorithm describes the action on input instance
• Infinitely many correct algorithms for a
particular problem
Input instance
adhering to the
specification
Output as a
function of
input
Algorithm
5
Criteria of a good algorithm
• Efficient
▫ Running time
▫ Space taken
• Efficiency is measured as a function of input size
▫ Number of bits used to represent input data
element
▫ Number of data elements
6
Measuring the running time
• How could we measure the running time of an
algorithm?
▫ Experimental study
● Write the program that implements the algorithm
● Run the program with various data sizes and types
● Use the function System.CurrentTimeInMillis
() to get the accurate running time of the program
7
Limitation of experimental study
• It is required to implement and test the
algorithm in order to determine its running
time.
• Experiment is always done on limited set of
inputs, not done on other inputs.
• This study depends on the hardware and
software used in the experiment. To compare
running times of two algorithms, same hardware
and software environments must be used.
8
Beyond experimental study
• We will develop a general methodology for
analyzing running time of algorithms
▫ Uses a high-level description of the algorithm,
instead of using one of its implementation
▫ Takes into account all possible inputs
▫ Independent of hardware and software
environment
9
Pseudo code
• A mixture of natural languages and high-level
programming concepts that describes the main
idea behind a generic implementation of an
algorithm or data structure
▫ Eg:ArrayMax (A, n)
Input: Array A and number of elements in A, n
Output: Maximum element in A
CurrMax A[1]
for i 2 to n do
if CurrMax < A[i] then CurrMax A[i]
return CurrMax
10
Analysis of Algorithm
• Primitive Operation: Low-level operations in
pseudo code, independent of programming
languages
▫ data movement (assign)
▫ control (branch, sub-routine call, return, etc.)
▫ arithmetic and logical operations (addition,
comparison, etc.)
• By inspecting pseudo code, we can count
number of primitive operations executed by an
algorithm
11
Example: Sorting
Sort
Input
Sequence of numbers
Output
A permutation of the
sequence of numbers
a1
,a2
,a3
, ………., an
b1
,b2
,b3
, ………., bn
Correctness (Requirements for the
output)
For any given input the algorithm
halts with the following output:
b1
<b2
<b3
< ………… < bn
b1
, b2
, b3
, ……….., bn
is the
permutation of a1
, a2
, a3
, ……., an
Running time depends on
number of elements
how partially sorted they are
the algorithm
12
Insertion Sort
4 7 2 10 7 3 8
1 n
i
j
Strategy:
Insert an element in the right
position of a set of already
sorted elements
Continue until all the
elements are sorted
Input: A[1…n], an array of integers
Output: a permutation of A, such that
A[1]≤A[2] ≤A[3] ≤……………. ≤ A[n]
for j 2 to n do
key A[j]
Insert key into the sorted sequence A[1, j-1]
i j-1
while i > 0 and A[i] > key
do A[i+1] A[i]
i--
A[i+1] key
13
Analysis of Insertion Sort
primitive operations cost times
for j 2 to n do c1
n
key A[j] c2
n-1
Insert key into the sorted sequence A[1, j-1] 0 n-1
i j-1 c3
n-1
while i > 0 and A[i] > key c4
do A[i+1] A[i] c5
i-- c6
A[i+1] key c7
n-1
Total time = n (c1
+ c2
+ c3
– c5
– c6
+ c7
) + (c4
+
c5
+c6
) - (c2
+c3
+ c7
)
14
Best, worst and Average cases
• Best case: elements already sorted, tj
= 1,
running time = f(n) i.e. linear time.
• Worst case: elements are in decreasing order,
tj
= j, running time = f(n2
) i.e. quadratic time
• Average case: tj
= j/2, running time = f(n2
), i.e.
quadratic time
Total time = n (c1
+ c2
+ c3
– c5
– c6
+ c7
) + (c4
+ c5
+c6
) - (c2
+c3
+ c7
)
15
Best/worst/average case
For a specific input size n, investigate running
times of different instances
Running
time
(min)
Input instances
best case
worst case
average case
16
Asymptotic Analysis
• Goal: simplifying analysis of running time by
getting rid of “details” which may be affected by
specific implementation and hardware
▫ like, rounding 10,000.01 ≈ 10,000
▫ 6n2
≈ n2
• How the running time of an algorithm increases
with the size of the input in a limit
▫ asymptotically more efficient algorithms are best
for all but small inputs
17
Asymptotic notation
• The “big-oh” O-notation
▫ asymptotic upper bound
▫ f(n) = O(g(n)), if there
exists constants c and n0
,
s.t. f(n) ≤ cg(n) for n ≥ n0
▫ f(n) and g(n) are functions
over non-negative integers
• Used for worst-case
analysis
cg(n)
f(n)
Input size
Running
time
n0
18
Asymptotic notation
• n2
is not O(n) because there is no c and n0
s.t.:
n2
≤ cn for n ≥ n0
▫ no matter how large a c is chosen there exist an n big
enough s.t. n2
> cn
• Simple rule: drop lower order terms and constant
factors
▫ 40 n log n is O(n log n)
▫ 3n-7 is O(n)
▫ 8n2
log n +n2
+ 7n +6 is O(n2
log n)
• Note: although (40 n2
) is O(n5
), it is expected that
such an approximation be as small an order as
possible
19
Asymptotic analysis of running time
• The O-notation expresses number of primitive
operations executed as function of input size
• Comparing asymptotic running times
▫ an algorithm running in O(n) time is better than an
algorithm running in O(n2
) time
▫ Similarly O(log n) is better than O(n)
▫ hierarchy: log n < n < n2
< n3
< 2n
• Caution! Beware of very large constant factor. An
algorithm running in 10000000 n time is still O(n),
but might be less efficient than an algorithm running
in time 2n2
time, which is O(n2
)
20
Example of Asymptotic Analysis
Algorithm prefixAverages1 (X)
Input: An n element array X of integer numbers
Output: An n element array A of integer numbers
where A[i] is the average of elements X[1], X[2],
……, X[i]
for i 1 to n do
a 0
for j 1 to i do
a a + X[j]
A[i] a/i
return A
Analysis: running time is O(n2
)
i iterations
with i = 1, 2,
3, ….., n
n iterations
21
A better Algorithm
Algorithm prefixAverages2 (X)
Input: An n element array X of integer numbers
Output: An n element array A of integer numbers
where A[i] is the average of elements X[1], X[2], ……,
X[i]
m 0
for i 1 to n do
m m + X[i]
A[i] s/i
return A
Analysis: running time is O(n)
22
Asymptotic Notation (terminology)
• Special classes of algorithms:
▫ Logarithmic: O(log n)
▫ Linear: O(n)
▫ Quadratic: O(n2
)
▫ Polynomial: O(nk
), k ≥ 1
▫ Exponential: O(an
), a > 1
• Abuse of notation: f(n) = O(g(n)) actually means
f(n) ε O(g(n))
23
Exercises
1.
2.
3. f(n) = Log n!, f(n) ε O(?)
24
Thank You!
25

More Related Content

Similar to Data Structure & Algorithms - Introduction

Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithmDevaKumari Vijay
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and NotationsAbid Kohistani
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2Kumar
 
02 order of growth
02 order of growth02 order of growth
02 order of growthHira Gul
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptxEnosSalar
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysisAkshay Dagar
 

Similar to Data Structure & Algorithms - Introduction (20)

Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
 
Analysis.ppt
Analysis.pptAnalysis.ppt
Analysis.ppt
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and Notations
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
 
02 order of growth
02 order of growth02 order of growth
02 order of growth
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Chapter two
Chapter twoChapter two
Chapter two
 
Algorithms
Algorithms Algorithms
Algorithms
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 

More from babuk110

Deployment Models-Internet of things Materials
Deployment Models-Internet of things MaterialsDeployment Models-Internet of things Materials
Deployment Models-Internet of things Materialsbabuk110
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
 
Data Structure & Algorithms - Matrix Multiplication
Data Structure & Algorithms - Matrix MultiplicationData Structure & Algorithms - Matrix Multiplication
Data Structure & Algorithms - Matrix Multiplicationbabuk110
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in Cbabuk110
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referentialbabuk110
 

More from babuk110 (6)

Deployment Models-Internet of things Materials
Deployment Models-Internet of things MaterialsDeployment Models-Internet of things Materials
Deployment Models-Internet of things Materials
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
Data Structure & Algorithms - Matrix Multiplication
Data Structure & Algorithms - Matrix MultiplicationData Structure & Algorithms - Matrix Multiplication
Data Structure & Algorithms - Matrix Multiplication
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

Data Structure & Algorithms - Introduction

  • 1. Introduction to Data Structure and Algorithms Manoj Kumar Rana Research Asst. Professor Dept. of Computing Technologies SRM Institute of Science and Technology
  • 2. Data structure and Algorithms • Algorithm ▫ A step-by-step timely instruction or outline of a computational procedure • Program ▫ Implementation of an algorithm by using a programming language • Data Structure ▫ Organization of data needed to solve a problem 2
  • 3. Data structure and Algorithms (2) • Object ▫ An instance of a set of heterogeneous data, defining behavior or character of a real-life entity (e.g. A Car object which contains data like color, size price, etc.) • Object oriented program ▫ A program where objects can interact with each other to ease the implementation of the algorithm • Relation between data structure and object ▫ Object is also one data type. ▫ Multiple objects can be organized by following a specific data structure 3
  • 4. Algorithmic problem • Infinite set of input instances satisfying the specification. For example, a sorted non-increasing sequence of numbers of finite length: ▫ 100000, 20000, 1000, 900, 20, 1. Specification of input Specification of output as a function of input ? 4
  • 5. Algorithmic solution • Algorithm describes the action on input instance • Infinitely many correct algorithms for a particular problem Input instance adhering to the specification Output as a function of input Algorithm 5
  • 6. Criteria of a good algorithm • Efficient ▫ Running time ▫ Space taken • Efficiency is measured as a function of input size ▫ Number of bits used to represent input data element ▫ Number of data elements 6
  • 7. Measuring the running time • How could we measure the running time of an algorithm? ▫ Experimental study ● Write the program that implements the algorithm ● Run the program with various data sizes and types ● Use the function System.CurrentTimeInMillis () to get the accurate running time of the program 7
  • 8. Limitation of experimental study • It is required to implement and test the algorithm in order to determine its running time. • Experiment is always done on limited set of inputs, not done on other inputs. • This study depends on the hardware and software used in the experiment. To compare running times of two algorithms, same hardware and software environments must be used. 8
  • 9. Beyond experimental study • We will develop a general methodology for analyzing running time of algorithms ▫ Uses a high-level description of the algorithm, instead of using one of its implementation ▫ Takes into account all possible inputs ▫ Independent of hardware and software environment 9
  • 10. Pseudo code • A mixture of natural languages and high-level programming concepts that describes the main idea behind a generic implementation of an algorithm or data structure ▫ Eg:ArrayMax (A, n) Input: Array A and number of elements in A, n Output: Maximum element in A CurrMax A[1] for i 2 to n do if CurrMax < A[i] then CurrMax A[i] return CurrMax 10
  • 11. Analysis of Algorithm • Primitive Operation: Low-level operations in pseudo code, independent of programming languages ▫ data movement (assign) ▫ control (branch, sub-routine call, return, etc.) ▫ arithmetic and logical operations (addition, comparison, etc.) • By inspecting pseudo code, we can count number of primitive operations executed by an algorithm 11
  • 12. Example: Sorting Sort Input Sequence of numbers Output A permutation of the sequence of numbers a1 ,a2 ,a3 , ………., an b1 ,b2 ,b3 , ………., bn Correctness (Requirements for the output) For any given input the algorithm halts with the following output: b1 <b2 <b3 < ………… < bn b1 , b2 , b3 , ……….., bn is the permutation of a1 , a2 , a3 , ……., an Running time depends on number of elements how partially sorted they are the algorithm 12
  • 13. Insertion Sort 4 7 2 10 7 3 8 1 n i j Strategy: Insert an element in the right position of a set of already sorted elements Continue until all the elements are sorted Input: A[1…n], an array of integers Output: a permutation of A, such that A[1]≤A[2] ≤A[3] ≤……………. ≤ A[n] for j 2 to n do key A[j] Insert key into the sorted sequence A[1, j-1] i j-1 while i > 0 and A[i] > key do A[i+1] A[i] i-- A[i+1] key 13
  • 14. Analysis of Insertion Sort primitive operations cost times for j 2 to n do c1 n key A[j] c2 n-1 Insert key into the sorted sequence A[1, j-1] 0 n-1 i j-1 c3 n-1 while i > 0 and A[i] > key c4 do A[i+1] A[i] c5 i-- c6 A[i+1] key c7 n-1 Total time = n (c1 + c2 + c3 – c5 – c6 + c7 ) + (c4 + c5 +c6 ) - (c2 +c3 + c7 ) 14
  • 15. Best, worst and Average cases • Best case: elements already sorted, tj = 1, running time = f(n) i.e. linear time. • Worst case: elements are in decreasing order, tj = j, running time = f(n2 ) i.e. quadratic time • Average case: tj = j/2, running time = f(n2 ), i.e. quadratic time Total time = n (c1 + c2 + c3 – c5 – c6 + c7 ) + (c4 + c5 +c6 ) - (c2 +c3 + c7 ) 15
  • 16. Best/worst/average case For a specific input size n, investigate running times of different instances Running time (min) Input instances best case worst case average case 16
  • 17. Asymptotic Analysis • Goal: simplifying analysis of running time by getting rid of “details” which may be affected by specific implementation and hardware ▫ like, rounding 10,000.01 ≈ 10,000 ▫ 6n2 ≈ n2 • How the running time of an algorithm increases with the size of the input in a limit ▫ asymptotically more efficient algorithms are best for all but small inputs 17
  • 18. Asymptotic notation • The “big-oh” O-notation ▫ asymptotic upper bound ▫ f(n) = O(g(n)), if there exists constants c and n0 , s.t. f(n) ≤ cg(n) for n ≥ n0 ▫ f(n) and g(n) are functions over non-negative integers • Used for worst-case analysis cg(n) f(n) Input size Running time n0 18
  • 19. Asymptotic notation • n2 is not O(n) because there is no c and n0 s.t.: n2 ≤ cn for n ≥ n0 ▫ no matter how large a c is chosen there exist an n big enough s.t. n2 > cn • Simple rule: drop lower order terms and constant factors ▫ 40 n log n is O(n log n) ▫ 3n-7 is O(n) ▫ 8n2 log n +n2 + 7n +6 is O(n2 log n) • Note: although (40 n2 ) is O(n5 ), it is expected that such an approximation be as small an order as possible 19
  • 20. Asymptotic analysis of running time • The O-notation expresses number of primitive operations executed as function of input size • Comparing asymptotic running times ▫ an algorithm running in O(n) time is better than an algorithm running in O(n2 ) time ▫ Similarly O(log n) is better than O(n) ▫ hierarchy: log n < n < n2 < n3 < 2n • Caution! Beware of very large constant factor. An algorithm running in 10000000 n time is still O(n), but might be less efficient than an algorithm running in time 2n2 time, which is O(n2 ) 20
  • 21. Example of Asymptotic Analysis Algorithm prefixAverages1 (X) Input: An n element array X of integer numbers Output: An n element array A of integer numbers where A[i] is the average of elements X[1], X[2], ……, X[i] for i 1 to n do a 0 for j 1 to i do a a + X[j] A[i] a/i return A Analysis: running time is O(n2 ) i iterations with i = 1, 2, 3, ….., n n iterations 21
  • 22. A better Algorithm Algorithm prefixAverages2 (X) Input: An n element array X of integer numbers Output: An n element array A of integer numbers where A[i] is the average of elements X[1], X[2], ……, X[i] m 0 for i 1 to n do m m + X[i] A[i] s/i return A Analysis: running time is O(n) 22
  • 23. Asymptotic Notation (terminology) • Special classes of algorithms: ▫ Logarithmic: O(log n) ▫ Linear: O(n) ▫ Quadratic: O(n2 ) ▫ Polynomial: O(nk ), k ≥ 1 ▫ Exponential: O(an ), a > 1 • Abuse of notation: f(n) = O(g(n)) actually means f(n) ε O(g(n)) 23
  • 24. Exercises 1. 2. 3. f(n) = Log n!, f(n) ε O(?) 24