0% found this document useful (0 votes)
14 views

Introduction To DS

ghdjmmt

Uploaded by

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

Introduction To DS

ghdjmmt

Uploaded by

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

Chapter: 1

Introduction to Data Structures

1.1 DS Concept: Data structure is a representation of logical relationship


existing between individual elements of data. In other words, a data structure
defines a way of organizing all data items that considers not only the elements
stored but also their relationship to each other. The term data structure is used
to describe the way data is stored.

To develop a program of an algorithm we should select an appropriate data


structure for that algorithm. Therefore, data structure is represented as:

Algorithm + Data structure = Program

Data: Collection of raw facts.


Data structure (DS) is representation of the logical relationship existing between
individual elements of data.
Definition of DS: Data structure is a particular way of organizing and storing data
in computer memory efficiently that considers not only the elements stored but
also their relationship to each other.

Why DS
• Human requirement with computer are going to complex
day by day. To solve the complex requirements in
efficient way we need this study.
• Provide fastest solution of human requirements.
• Provide efficient solution of complex problems in terms
of ...
– Space
– Time
What is the need of DS
1. It helps us to understand the relationship of one data
element with other data element.
2. It helps us to analyze the data, store it and organize in a
logical or mathematical manner.
3. To store the data in a linear manner
(eg. Month of a year)
4. To store the data in a historical manner
(eg. Historical data)

Abstract Data Type (ADT):

 Sometimes Built-in data types are not enough to handle the complex DS. So
its programmers responsibility to create their own data types (user defined
data types) such custom data type is called as ADT(Abstract Data Types).
 The data type made by user or programmer for performing any specific task
of the program called as ADT. Therefore it is represented as
ADT=Type+ function name +behavior of function
Examples of ADT: Stack, Queue, List etc.

For example, a stack is a typical abstract data type. Items stored in a stack can
only be added and removed in certain order – the last item added is the first
item removed. We call these operations, pushing and popping. In this definition,
we haven’t specified have items are stored on the stack, or how the items are
pushed and popped. We have only specified the valid operations that can be
performed.
For example, if we want to read a file, we wrote the code to read the physical
file device. That is, we may have to write the same code over and over again. So
we created what is known
today as an ADT. We wrote the code to read a file and placed it in a library for a
programmer to use.

1.2 Classification of DS:


 Data structure are normally divided into two broad
categories:
◦ Primitive Data Structure
◦ Non-Primitive Data Structure
Primitive Data Structures (Basic or Built-in DS) are the basic data structures
that directly operate upon the machine instructions. They have different
representations on different computers. Integers, floating point numbers,
character constants, string constants and pointers come under this
category.

Non-primitive data structures (Derived DS) are more complicated data


structures and are derived from primitive data structures. They emphasize on
grouping same or different data items with relationship between each data
item. Arrays, lists and files come under this category. Figure
1.1 shows the classification of data structures.

Figure 1. 1 Classification of Data Structures

Data structures are normally divided into two broad categories:


1) Primitive Data Structure
2) Non-Primitive Data Structure
Classification of Ds is as shown in above figure.
1) Primitive Data Structure: It is also called as Built-in or basic ata structures
and directly operated upon by the machine instructions. Data structures
that are directly operated upon the machine-level instructions are known
as primitive data structures.
Example: Integer, Floating-point number, Character etc.
2) Non-Primitive Data Structure: It is also called as Derived data structures
The Data structures that are derived from the primitive data structures are
called Non-primitive data structure. The non-primitive data structures
emphasize on structuring of a group of homogeneous (same type) or
heterogeneous (different type) data items.
Non-Primitive Data Structures are classified in to two types
1) Linear Data structures: (also called as Sequential DS)
Linear Data structures are kind of data structure that has homogeneous
elements. The data structure in which elements are in a sequence and form a
linear
series. Linear data structures are very easy to implement, since the memory of
the computer is also organized in a linear fashion. Some commonly used linear
data structures are Stack, Queue and Linked Lists.
Stack :s a linear data structure which follows a particular order in which
the operations are performed.
Insertion of element into stack is called PUSH and deletion of element from
stack is called POP.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).

queue: is an ordered collection of items where an item is


inserted at one end called the “rear” and an existing item is
removed at the other end, called the “front”.
Queue is also called as FIFO list i.e. First-In First- Out.
In the queue only two operations are allowed
enqueue and dequeue.

lists: (Linear linked list) can be defined as a collection of variable number of data
items called nodes. Lists are the most commonly used non- primitive data
structures. Each nodes is divided into two parts:
◦ The first part contains the information of the element.
◦ o The second part contains the memory address of the next node in
the list. Also called Link part.
2) Non-Linear Data structures:
A Non-Linear Data structures is a data structure in which data item is connected to
several other data items. Non-Linear data structure may exhibit either a
hierarchical relationship or
parent child relationship. The data elements are not arranged in a sequential
structure.
The different non-linear data structures are trees and graphs.
tree is a data structure consisting of nodes organized as a hierarchy.
 Tree is a hierarchical data structure which stores the information naturally
in the form of hierarchy style.
 It is a non-linear data structure compared to arrays, linked lists, stack and
queue.
 It represents the nodes connected by edges.

Graph is a mathematical non-linear data structure capable of representing many


kind of physical structures. A graph is a set of vertices and edges which connect
them.
A graph is a collection of nodes called vertices and the connection between them
called edges.Definition: A graph G(V,E) is a set of vertices V and a set of edges E.
1.2 Algorithm Complexity:
performance of a program is the amount of computer memory and time needed
to run a program.
Complexity are of two types:
1) Time Complexity
2) Space Complexity

1) Time Complexity: The time needed by an algorithm or program is called as


TIME COMPLEXITY of the algorithm. The time complexity of a program is the
amount of computer time it needs to run to completion.
Time Complexity is divided in to THREE Types.
a) Best Case Time Complexity: Efficiency of an algorithm for an input of size N
for which the algorithm takes the smallest amount of time.
b) Average Case Time Complexity: It gives the expected value of T(n). Average
case is used when worst case and best case doesn’t gives any necessary
information about algorithm’s behavior, then the algorithm’s efficiency is
calculated on Random input.
c) Worst Case Time Complexity: efficiency of an algorithm for an input of size N
for which the algorithm takes the longest amount of time.

Time complexity is calculated on the basis of Operation Count or frequency count:


Find the basic operation a= a * b; this code takes 1 unit of time
Example: Time complexity of below code is
Total Time Complexity= O(n2)

Statement Step Count


x = a + b; 1
N
for(i=1; i <= n; i++) n2
for(j=1; j <= n; j++)
x = a + b;

Frequency count: (means how many times a particular instruction executes.)


For example
1) x=x+1
As this statement executes only once therefore the frequency count for this
statement is 1.

2) for(I=0;I<n;I++) - this loop will be executed n times


x=x+1; - this statement will be executed 1 time
Above statement execute n times therefore the frequency count will be n.
thus the performance of a program is measured in terms of “time
complexity” i.e. total time taken by the algorithm to execute.
Let us see more program fragments and have more discussion on frequency
count.

Example 1:
Main()
{
int j,n,a=10;
cout<<“\n Enter the value of n”;
cout<<n;
for(j=0;j<n;j++)
a++;

}
Now let us analyse the above program fragment –
Concentrate on ‘for’ loop
i) j=0 will be executed one time
ii) j<n will be executed n+1 time i.e.
iii) j++ will be executed n times
iv) a++ will be executed n times.

Totally 1+(n+1)+n+n i.e. 3n+2


Generally when we sum the frequency count of all the statements in the given
program we get a polynomial. However, for the sake of analysis we are
interested in the order of magnitude of polynomial.
So we will drop the constant terms from the polynomial. Hence the order of
magnitude is ‘n’ and it is denoted as O(n). “O” is called big-oh notation.
2) Space Complexity:

The space complexity of a program is the amount of memory(space) needs to


run to completion. The space needed by a program has the following
components:
Space complexity of below program is:
S (p) = Cp + Sp
S (p) = 8 + 0
S (p) = 8
Total space needed by below program= 8 bytes.

1.4 Operations on data structures


1) Traversing: Accessing each record exactly once so that certain items in
the record may be processed.
2) Inserting: adding a new record to the structure.
3) Deleting: Removing a record from the structure. Sometimes two or
more of the operations may be used in a given situation; for example we
may want to delete the record with a given key, which may mean we
first need to search for the location of the record.
4) Searching: Finding the location of the record with a given key value, or
finding the locations of all records, which satisfy one or more conditions.
5) Sorting: Arranging the records in some logical order (for example
alphabetically according to some Name key, or in numerical order
according to some Number key. such as account number.
6) Merging: Combining the records in two different sorted files into a
single sorted file

You might also like