document_1676338680430
document_1676338680430
Hitesh Gupta
Assistant Professor Department of CSE
LNCT BHOPAL
Introduction to Data Structures
• A data structure is a way of storing data in a
computer so that it can be used efficiently and
it will allow the most efficient algorithm to be
used.
• A data structure should be seen as a logical
concept that must address two fundamental
concerns.
I. First, how the data will be stored, and
II. Second, what operations will be performed on it.
2
Classification of Data Structures
• Data structures can be classified as
i. Simple data structure
ii. Compound data structure
iii. Linear data structure
iv. Non linear data structure
5
Linear and Non-linear Data Structures
i. Add an element
ii. Delete an element
iii. Traverse
iv. Sort the list of elements
v. Search for a data element
6
Abstract Data Type
• An abstract data type, sometimes abbreviated
ADT, is a logical description of how we view
the data and the operations that are allowed
without regard to how they will be
implemented.
• By providing this level of abstraction, we are
creating an encapsulation around the data. The
idea is that by encapsulating the details of the
implementation, we are hiding them from the
user‘s view. This is called information hiding.
7
Algorithm Definition
• An Algorithm may be defined as a finite
sequence of instructions each of which has a clear
meaning and can be performed with a finite
amount of effort in a finite length of time.
• The word algorithm originates from the Arabic
word Algorism which is linked to the name of the
Arabic Mathematician AI Khwarizmi.
• AI Khwarizmi is considered to be the first
algorithm designer for adding numbers.
8
Structure of an Algorithm
• An algorithm has the following
structure:
– Input Step
– Assignment Step
– Decision Step
– Repetitive Step
– Output Step
9
Properties of an Algorithm
• Finiteness:- An algorithm must terminate after
finite number of steps.
• Definiteness:-The steps of the algorithm must be
precisely defined.
• Generality:- An algorithm must be generic
enough to solve all problems of a particular
class.
• Effectiveness:- The operations of the algorithm
must be basic enough to be put down on pencil
and paper.
10
Input-Output:- The algorithm must have certain
initial and precise inputs, and outputs that may be
generated both at its intermediate and final steps
Algorithm Analysis and Complexity
• The performances of algorithms can be
measured on the scales of Time and Space.
• The Time Complexity of an algorithm or a
program is a function of the running time of
the algorithm or a program.
• The Space Complexity of an algorithm or a
program is a function of the space needed by
the algorithm or program to run to completion.
12
Algorithm Analysis and Complexity
• The Time Complexity of an algorithm can be computed
either by an
– Empirical or Posteriori Testing
– Theoretical or Apriori Approach
• The Empirical or Posteriori Testing approach calls for
implementing the complete algorithm and executes
them on a computer for various instances of the
problem.
• The Theoretical or Apriori Approach calls for
mathematically determining the resources such as time
and space needed by the algorithm, as a function of
parameter related to the instances of the problem
considered.
13
Algorithm Analysis and Complexity
• Apriori analysis computed the efficiency of the
program as a function of the total frequency
count of the statements comprising the
program.
• Example: Let us estimate the frequency count
of the statement x = x+2 occurring in the
following three program segments A, B and C.
14
Total Frequency Count of Program Segment A
15
Total Frequency Count of Program Segment B
….………………. ……………………
Total Frequency Count 2n+1
18
Asymptotic Notations
• Omega(Ω): f(n) = Ω(g(n)) ( read as f of n is
omega of g of n), if there exists a positive
integer n0 and a positive number c such that
|f(n)| ≥ c |g(n)| for all n ≥ n0.
f(n) g(n)
21
Time
Complexity
Complexity Notation Description
Constant O(1) Constant number of operations, not depending on the
input data size.
Logarithmic O(logn) Number of operations proportional of log(n) where n
is the size of the input data.
Linear O(n) Number of operations proportional to the input data
size.
Quadratic O(n2 ) Number of operations proportional to the square of
the size of the input data.
Cubic O(n3 ) Number of operations proportional to the cube of the
size of the input data.
Exponential O(2n) Exponential number of operations, fast growing.
O(kn )
O(n!)
22
Time Complexities of various
Algorithms
23