0% found this document useful (0 votes)
30 views36 pages

Data Structure

Data structures are essential for storing and organizing data efficiently, with various types like arrays, stacks, queues, and trees used in C++. They facilitate optimal algorithm implementation for tasks such as searching and sorting, enhancing program efficiency. The document also covers definitions, types, and characteristics of data structures and algorithms, including concepts like time and space complexity.

Uploaded by

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

Data Structure

Data structures are essential for storing and organizing data efficiently, with various types like arrays, stacks, queues, and trees used in C++. They facilitate optimal algorithm implementation for tasks such as searching and sorting, enhancing program efficiency. The document also covers definitions, types, and characteristics of data structures and algorithms, including concepts like time and space complexity.

Uploaded by

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

DATA STRUCTURE

INTRODUCTION

 Data structures are used to store and organize data. An array is an


example of a data structure, which allows multiple elements to be
stored in a single variable.

 C++ includes many other data structures as well, each is used to


handle data in different ways.
 Data Structures are the basic block of Computer Science
Technology. Data Structures are used to store the data in such a
way that we can access them in a very efficient way. There are a
lot of Data Structures used in the C++ Programming language
Arrays, Stacks, Queues, Linked-lists, Trees, and Graphs.
 To solve a specific problem, we require an optimal solution or
approach, and in programming, it is also called Algorithm. They
are used to manipulate the data stored in the Data Structures to
perform operations such as Searching, Sorting, and Traversal.
 Data structures are concerned with the efficient representation of
data and algorithm is related to the task. Hence, there is a close
relationship between data structures and algorithms.
 Program=Data structures + Algorithm
 The use of proper data structures to represent data makes it
possible to write better algorithms, thereby improving program
efficiency.
NEED OF DATA STRUCTURE:

 The structure of the data and the synthesis of the algorithm are relative to each other.
Data presentation must be easy to understand so the developer, as well as the user,
can make an efficient implementation of the operation.
 Data structures provide an easy way of organising, retrieving, managing, and storing
data.

 Here is a list of the needs for data.

 Data structure modification is easy.


 It requires less time.
 Save storage memory space.
 Data representation is easy.
 Easy access to the large database
ADVANTAGES OF DATA STRUCTURES

 Structured data makes it easier to access and manipulate the


information as compared to raw or unstructured data.
 2. A variety of operations can be performed on structured data.
 3. Related data can be stored together and in the required format.
 4. Better algorithms can be used on organized data. This improves
program efficiency.
 5. They provide a means to store, organize and retrieve data in an
efficient manner.
DEFINITIONS

1. Data: Data is raw, unprocessed input given to a system or


process.
The following are some examples of data
25
Pune, Mumbai, Nasik

Data can be of two types:


i) Atomic data: Atomic data is a single, elementary piece of data,
which cannot be further decomposed. For example, the character
'a', number 10 etc. are atomic.
 ii) Composite data: Composite data is a grouped data item which
consists of sub-fields, each of which is atomic. For example, a date
consists of sub fields like day, month, and year. The concept of
structure in 'C' is composite data.

2. Information: Information is meaningful or processed data. The


system or process transforms raw data into meaningful and useful
information.

data System/process
Output
 For example, in the previous example, if we associate some other
label with the raw data or process the data, it becomes meaningful.
Data Information

25 Age of person

Pune, Mumbai, Nasik Names of Cities

3) Data type: Data-type describes the format in which data is stored


and processed by a computer. It also refers to the kind of data
that variables 'hold' in a programming language.
Example: int, char, float, etc.
 4. Data object: Data object refers to a set of elements (D) of a
specific data type. This set may be finite or infinite. If the set is
infinite, we have to devise some mechanisms to represent that set
in memory, because available memory is limited.
 Example: A set of integer numbers is infinite
 D=(0,+1,+2...1}
 A set of English alphabets is finite
 D= {'A', 'B'... 'Z', 'a', 'b', ‘z')
 5. Data structure: A data structure consists of data objects. their
properties and the set of legal operations which may be applied to
the elements of the data object.
 Definition
 A data structure is a set of domains D, a designated domain de D,
a set of functions F and a set of axioms A.
 The triple (D. F, A) denotes the data structure.
 D denotes the data object i.e. set of elements.
 F denotes the set of operations that can be carried out on the
elements.
 A - describes the properties and rules of the operations.
 6. Abstract Data Type: The ADT is a mathematical or conceptual definition of a
data structure. The data structure i.e. the triple (D, F. A) is called the Abstract
Data Type because it is simply a conceptual specification of the data structure.
 It only gives the "logical properties" and operations of the data type without
giving any details about the implementation. Hence, it is just an abstract
concept.
 Example of ADT
 Consider integer as an abstract type. We need to specify the data object i.e.
which elements belong to the "integer" type, the operations on integers and
rules for these operations. Its ADT can be written as follows:
 D={0,±1, ±2...}
 F= {+, -, *, /, %, <, > ...}
 A = {arithmetic rules which apply to the operations}
TYPES OF DATA STRUCTURES

1. Logical data structure and physical data structure


2. Linear and non-linear data structures
3. Static and dynamic data structures
4. Sequential organization and linked organization
LOGICAL DATA AND PHYSICAL DATA
STRUCTURE
Logical data structure: The ADT is a logical data
structure because it specifies the "logical properties"
of the data type. Hence, it is just a mathematical or
abstract concept, which defines the data type.

Physical data structure: A physical data structure is


the implemented ADT which can be used to store
elements and perform operations.
LINEAR AND NON-LINEAR DATA
STRUCTURES
 Linear: The elements of a linear data structure are arranged
in a line' ie. they form a sequence or a list. There is an
ordering among them such that there is a "first" element,
second and so on. The elements have a one-to-one
relationship with other elements.
 Example: Array, Linked list.
A1 A2 An
 Non-linear: In a non-linear data structure, the elements have a
'one to many' or 'many to many relationship between them. The
elements do not form a specific sequence. For example, to
represent the directory structure or a road network, we need a
non-linear structure.
 Example: Tree, Graph
STATIC AND DYNAMIC DATA
STRUCTURES
 Static: A static data structure is one in which the size of the
data structure does not change during the entire program.
The memory is allocated during compile-time and remains
allocated even when there are no elements in the data
structure.
 Example: Array
 Dynamic: In a dynamic data structure, the size of the data
structure grows and shrinks as elements are added and
removed dynamically i.e. during run-time. In most real-time
applications, we cannot predict the number of elements in
advance and hence, dynamic data structures are needed.
Dynamic memory allocation is required in this case.
 Example: Stack, Queue, Linked List etc.
SEQUENTIAL ORGANIZATION AND
LINKED ORGANIZATION
 Sequential organization:- In the sequential organization, data
elements are stored at a fixed distance. This means that if an
element A, is stored at location L, in memory, then the element A
will be stored at location Lese where e is a constant.
 Example: Array uses sequential organization.

A1 A2 ……… An

 Li Li+c …………
Li+(n-1)c
 Linked organization: In this organization, the elements are stored
in memory at any memory location. Their sequence is maintained
by linking the elements. The physical location of the element does
not determine its order in the set of elements.
 For example: Linked list uses linked organization.

A1 A2 An

Li Lj
Lk
ALGORITHM

 Any computer program accepts some input data,


performs operations and gives some useful output. An
algorithm is related to the operation or manipulation of
data. It is a method that can be used by a computer for
the solution of a problem.
 Definition
 An algorithm is a finite set of instructions that, if
followed, accomplishes a particular task.
 For example, let us consider the task to calculate the
area of a circle. The algorithm can be written as follows:
 Finite set of steps to solve a problem is called algorithm.
 Analysis is process of comparing two algorithm with respect to
time ,space etc.
 Algorithm to calculate area of circle
 Step 1: Start
 Step 2: Accept radius
 Step 3: Calculate area = pi radius radius
 Step 4: Display area
 Step 5: Stop
CHARACTERISTICS OF AN
ALGORITHM
 All algorithms must satisfy the following criteria.
1. Finiteness: If we trace the steps of an algorithm, the algorithm
must terminate after a finite number of steps for all cases.
2. Definiteness: Each step of an algorithm must be precisely
defined ie, each instruction must be clear and unambiguous.
3. Effectiveness: Each instruction must be sufficiently basic that
it can be done exactly and in a finite time by a person using
only paper and pencil.
Example: Add num1 to num2 is an effective operation if
num1 and num2 are integers, but if they were matrices, then
the instruction is not effective.
4. Input: An algorithm takes zero or more inputs, quantities
that are given to it initially before the algorithm begins or
dynamically as the algorithm runs.
5. Output: An algorithm generates one or more outputs,
quantifies that have a specified relation to the inputs.
ALGORITHM ANALYSIS

 There are many criteria upon which an algorithm can be


evaluated, for example:
1. Does it perform the desired task?
2. Does it work correctly according to the original specifications of
the task?
3. Does it contain documentation describing how it works?
4. Are procedures created in such a way that they perform logical
sub functions?
5. Is the code readable?
These criteria are important but they are "qualitative". However, it
is not always possible to measure these criteria. The performance
must be evaluated using some criteria which can be measured and
SPACE COMPLEXITY

 The space complexity of an algorithm/program is the amount of


memory a program needs to execute.
 Components of Space Complexity
 The space needed by an algorithm is the sum of the following two
components.
 1. Fixed part: This part is independent of the characteristics of the
inputs and outputs, It includes the memory space for simple
variables, component variables, constants etc.
 2. Variable part: It consists of the space needed by variables whose
size depends on the particular problem instance being solved, the
space needed by reference variables and the recursion stack space.
 The space complexity S(P) for an algorithm is written as
 S(P) = c + Sp
 where, c is a constant and is the fixed part.
 S, is the variable part which depends on the instance
characteristics i.e. the program instance being run.
4.2 TIME COMPLEXITY

 It is defined as the number of steps required to solve the entire


problem using some efficient algorithm.
 The time complexity of a program is defined as the amount of time
the program requires to execute. However, it is very difficult to
calculate the exact running time. Also, the exact running time
depends on the underlying hardware and software.
 Hence, instead of calculating exact running time, a rough estimate
can be made. To do this, we have to identify the active operations
in the program (which are a part of the main program). The other
operations like initialization, assignments, input and output etc.
are called book keeping operations and will not execute as many
times as the active operations. Thus, they need not be considered.
 The total number of active operations is defined as the frequency
count. We can say that, the execution time is proportional to the
frequency count of the program. The frequency count is written as
a function f(n) where n is usually the input size.
 T(p)=c+tp(instance)

 T(p)->time complexity of program p


 C->compile time
 Tp(instance)->run time
BEST CASE, AVERAGE CASE AND
WORST CASE
 An algorithm must be analyzed for three types of cases:
 1. Best Case: The term 'best-case' is used to analyze an algorithm
under optimal conditions. The best case performance is the
minimum number of steps that can be executed for the given
input values. For example, the best case for the linear search
algorithm occurs when the element to be searched is the first in
the list.
 If we run the algorithm n times with x1,x2,……xn as the probable
sets of inputs, then the best case performance is calculated as:
Best Case Tn=Min(Tx1,Tx2,……,Txn).
 Worst Case: The worst case step count is the maximum number of
steps that can be executed for the given parameters. This is the
slowest possible time an algorithm may take and it indicates the
upper bound. It is important to calculate the worst case behavior
because we know that the running time will not exceed this limit. If
x1,x2,……xn probable sets of inputs:
 Worst Case Tn=Max(Tx1,Tx2,…..Txn)
 Average Case: Average-case running time assumes that all inputs
of a given size are equally likely. The average case step count is
the average number of steps executed. If x1,x2,……xn are the n
probable sets of inputs, then the average case step count is
calculated as:
1
Average case Tn= Σ T(x1,x2,
……,xn)
all possible inputs

Usually the average case and worst-case step count is the same.
ASYMPTOTIC NOTATIONS (Ο,Ω,Θ)

 After calculating the frequency count of a program - f(n), the time


complexity is expressed using an asymptotic notation like "Big O",
Ω, Θ etc. The complexity of an algorithm can be expressed in
several ways:
1. The algorithm never takes more than f(n) operations.
2 The algorithm always takes less than f(n) operations.
3 The algorithm's running time is of the order of f(n).
The above can be expressed using Asymptotic notations O. Dand 9.
They are called asymptotic became they apply for large values of n
 O Notation (Big-Oh Notation)
 This notation is used to denote upper bounds (worst case
behavior). It is expressed an O(f(n)) which means that the time
taken never exceeds roughly f(n) operations. It is the most
commonly used notation.
 Definition: The function f(n) = O(g(n)) iff there exists positive
constants c and n0 such that f(n)<=c*g(n) for all n, n >= n0.
 Ω Notation (Omega Notation)
 This is used to denote lower bounds (best case behavior). It is
written as
 Tn-Ω (f(n)) which indicates that Tn takes at least about f(n)
operations.
 Definition: The function f(n) = Ω (g(n)) iff there exists positive
constants c and no such that f(n) >= c*g(n) for all n, n >= no.
 θ Notation (Theta Notation)
 This notation denotes both, upper and lower bounds of f(n) and
written as T(n) = θ(g(n)). It indicates the running order of an
algorithm.
 Definition: The function f(n) = θ (g(n)) iff there exist positive
constants c₁, c2, and no such that c₁*g(n) ≤ f(n) ≤c2* g(n) for all n,
n >= n0.

You might also like