SlideShare a Scribd company logo
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 (20)

PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PPTX
3 analysis.gtm
Natarajan Angappan
 
PPT
Data Structures and Algorithm Analysis
Mary Margarat
 
PPTX
02 Introduction to Data Structures & Algorithms.pptx
mettlehenry573
 
PDF
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
TechVision8
 
PDF
complexity analysis.pdf
pasinduneshan
 
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
PPTX
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
PPT
Basics of data structure types of data structures
kavita20193
 
PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
PPTX
Unit 1.pptx
DeepakYadav656387
 
PPT
Data Structure and Algorithms
ManishPrajapati78
 
PPT
Unit II_Searching and Sorting Algorithms.ppt
HODElex
 
PDF
DATA STRUCTURE
RobinRohit2
 
PDF
DATA STRUCTURE.pdf
ibrahim386946
 
PPTX
Unit i basic concepts of algorithms
sangeetha s
 
PPTX
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
PDF
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
3 analysis.gtm
Natarajan Angappan
 
Data Structures and Algorithm Analysis
Mary Margarat
 
02 Introduction to Data Structures & Algorithms.pptx
mettlehenry573
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
TechVision8
 
complexity analysis.pdf
pasinduneshan
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
Basics of data structure types of data structures
kavita20193
 
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
Unit 1.pptx
DeepakYadav656387
 
Data Structure and Algorithms
ManishPrajapati78
 
Unit II_Searching and Sorting Algorithms.ppt
HODElex
 
DATA STRUCTURE
RobinRohit2
 
DATA STRUCTURE.pdf
ibrahim386946
 
Unit i basic concepts of algorithms
sangeetha s
 
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 

More from babuk110 (7)

PPTX
Deployment Models-Internet of things Materials
babuk110
 
PDF
Data Structure & Algorithms - Operations
babuk110
 
PDF
Data Structure & Algorithms - Mathematical
babuk110
 
PDF
Data Structure - Dynamic Memory Allocation
babuk110
 
PDF
Data Structure & Algorithms - Matrix Multiplication
babuk110
 
PDF
Data structure & Algorithms - Programming in C
babuk110
 
PDF
Data Structure & Algorithm - Self Referential
babuk110
 
Deployment Models-Internet of things Materials
babuk110
 
Data Structure & Algorithms - Operations
babuk110
 
Data Structure & Algorithms - Mathematical
babuk110
 
Data Structure - Dynamic Memory Allocation
babuk110
 
Data Structure & Algorithms - Matrix Multiplication
babuk110
 
Data structure & Algorithms - Programming in C
babuk110
 
Data Structure & Algorithm - Self Referential
babuk110
 
Ad

Recently uploaded (20)

PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
Ad

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