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

chap1

The document provides an introduction to data structures and algorithms, covering basic terminology such as data items, data types, variables, records, and entities. It discusses the characteristics and efficiency of algorithms, including time and space complexity, as well as the classification of data structures into static and dynamic types. Additionally, it describes various data structures like arrays, linked lists, trees, graphs, queues, and stacks, along with their operations and representations in memory.

Uploaded by

raagaqaran1988
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

chap1

The document provides an introduction to data structures and algorithms, covering basic terminology such as data items, data types, variables, records, and entities. It discusses the characteristics and efficiency of algorithms, including time and space complexity, as well as the classification of data structures into static and dynamic types. Additionally, it describes various data structures like arrays, linked lists, trees, graphs, queues, and stacks, along with their operations and representations in memory.

Uploaded by

raagaqaran1988
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

DATA STRUCTURE & ALGORITHM

Chapter 01:
Introduction to Data Structure
& Algorithm
BASIC TERMINOLOGY: ELEMENTARY DATA ORGANIZATION

Data and Data Item


Data are simply collection of facts and
figures. Data are values or set of values.
A data item refers to a single unit of
values. Data items that are divided into
sub items are group items; those that are
not are called elementary items. For
example, a student’s name may be divided
into three
sub items – [first name, middle name and last name] but the ID of a student
would normally be treated as a single item.
In the above example (ID, Age, Gender, First, Middle, Last, Street, Area ) are
elementary data items, whereas (Name, Address ) are group data items.
Data Type
Data type is a classification identifying one of various types
of data, such as floating-point, integer, or Boolean, that
determines the possible values for that type; the operations
that can be done on values of that type; and the way values
of that type can be stored. It is of two types: Primitive and
non-primitive data type. Primitive data types - includes byte
, short , int , long , float , double , boolean and char. Non-
primitive data types - such as String , Arrays and Classes
Primitive data type is the basic data type that is provided by
the programming language with built-in support.
Variable
It is a symbolic name given to some known or
unknown quantity or information, for the
purpose of allowing the name to be used
independently of the information it represents.
A variable name in computer source code is
usually associated with a data storage
location and thus also its contents and these
may change during the course of program
execution.
Record
Collection of related data items is known as record. The
elements of records are usually called fields or members.
Records are distinguished from arrays by the fact that
their number of fields is typically fixed, each field has a
name, and that each field may have a different type.
Program
A sequence of instructions that a computer can interpret
and execute is termed as program.
Entity
An entity is something that has certain attributes or
properties which may be assigned some values.
The values themselves may be either numeric or non-
numeric.
Example
Entity Set
An entity set is a group of or set of similar
entities. For example, employees of an
organization, students of a class etc.
Each attribute of an entity set has a range of
values, the set of all possible values that
could be assigned to the particular attribute.
The term “information” is sometimes used for
data with given attributes, of, in other words
meaningful or processed data.
Field
A field is a single elementary unit of information
representing an attribute of an entity, a record is the
collection of field values of a given entity and a file is the
collection of records of the entities in a given entity set.
File
File is a collection of records of the entities in a given
entity set. For example, file containing records of
students of a particular class.
Key
A key is one or more field(s) in a record that take(s)
unique values and can be used to distinguish one record
from the others.
ALGORITHM
A well-defined computational procedure that takes some
value, or a set of values, as input and produces some
value, or a set of values, as output. It can also be defined
as sequence of computational steps that transform the
input into the output.
An algorithm can be expressed in three ways: -
– in any natural language such as English, called pseudo
code.
– in a programming language or
– in the form of a flowchart.
EFFICIENCY OF AN ALGORITHM
In computer science, algorithmic efficiency are the properties
of an algorithm which relate to the amount of resources used
by the algorithm.
It can be of various types:
– Worst case efficiency: It is the maximum
number of steps that an algorithm can take for
any collection of data values.
– Best case efficiency: It is the minimum number
of steps that an algorithm can take any
collection of data values.
– Average case efficiency: It can be defined as
• the efficiency averaged on all possible inputs
• must assume a distribution of the input
• We normally assume uniform distribution (all keys
are equally probable)
Characteristics of an Algorithm
An algorithm should have the following characteristics : −
• Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should be
clear and must lead to only one meaning.
• Input − an algorithm should have 0 or more well-defined inputs.
• Output − an algorithm should have 1 or more well-defined
outputs, and should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of
steps.
• Feasibility − should be feasible with the available resources.
• Independent − an algorithm should have step-by-step directions,
which should be independent of any programming code.
TIME AND SPACE COMPLEXITY
• Complexity of algorithm is a function of size of input of a given
problem instance which determines how much running time/memory
space is needed by the algorithm in order to run to completion.
• Time Complexity: Time complexity of an algorithm is the amount of
time it needs in order to run to completion.
• Space Complexity: Space Complexity of an algorithm is the amount
of space it needs in order to run to completion.
• There are two points which we should consider about computer
programming: -
– An appropriate data structure and
– An appropriate algorithm.
ABSTRACT DATA TYPE (ADT)
It can be defined as a collection of data items together
with the operations on the data.
The word “abstract” refers to the fact that the data and
the basic operations defined on it are being studied
independently of how they are implemented.
An implementation of ADT consists
of storage structures to store the data
items and algorithms for basic
operation. All the data structures i.e.
array, linked list, stack, queue etc are
examples of ADT.
DATA STRUCTURE
In computer science, a data structure is a particular way of
storing and organizing data in a computer’s memory so that it
can be used efficiently.
Data may be organized in many different ways; the logical or
mathematical model of a particular organization of data is
called a data structure.
The choice of a particular data model depends on the two
considerations first; it must be rich enough in structure to
mirror the actual relationships of the data in the real world. On
the other hand, the structure should be simple enough that
one can effectively process the data whenever necessary.
Need of data structure
• It gives different level of organization data.
• It tells how data can be stored and accessed in its
elementary level.
• Provide operation on group of data, such as adding
an item, looking up highest priority item.
• Provide a means to manage huge amount of data
efficiently.
• Provide fast searching and sorting of data.
Selecting a data structure
• Selection of suitable data structure involve following steps

▪ Analyze the problem to determine the resource constraints a
solution must meet.
▪ Determine basic operation that must be supported. Quantify
resource constraint for each operation
▪ Select the data structure that best meets these requirements.
▪ Each data structure has cost and benefits. Rarely is one data
structure better than other in all situations. A data structure
requires:
o Space for each item it stores
o Time to perform each basic operation
o Programming effort.
Classification of Data Structures
Type of data structure
Static data structure
A data structure whose organizational characteristics
are invariant throughout its lifetime. Such structures
are well supported by high-level languages and
familiar examples are arrays and records. The prime
features of static structures are
o None of the structural information need be stored
explicitly within the elements – it is often held in a distinct
logical/physical header;
o The elements of an allocated structure are physically
contiguous, held in a single segment of memory;
Cont..
o All descriptive information, other than the physical
location of the allocated structure, is determined by the
structure definition;
o Relationships between elements do not change during
the lifetime of the structure.
Dynamic data structure
• A data structure whose organizational characteristics may
change during its lifetime. The adaptability afforded by such
structures, e.g. linked lists, is often at the expense of
decreased efficiency in accessing elements of the structure.
• Two main features distinguish dynamic structures from static
data structures. Firstly, it is no longer possible to infer all
structural information from a header; each data element will
have to contain information relating it logically to other
elements of the structure. Secondly, using a single block of
contiguous storage is often not appropriate, and hence it is
necessary to provide some storage management scheme at
run-time.
Linear Data Structure
• A data structure is said to be linear if its elements form
any sequence. There are basically two ways of
representing such linear structure in memory.
• One way is to have the linear relationships between
the elements represented by means of sequential
memory location. These linear structures are called
arrays.
• The other way is to have the linear relationship
between the elements represented by means of
pointers or links. These linear structures are called
linked lists.
Non-linear Data Structure
• This structure is mainly used to represent data
containing a hierarchical relationship between
elements. E.g. graphs, family trees and table of
contents.
A BRIEF DESCRIPTION OF DATA STRUCTURES
• Array
The simplest type of data structure is a linear (or one
dimensional) array. A list of a finite number n of similar
data referenced respectively by a set of n consecutive
numbers, usually 1, 2, 3 . . . . . . . n. if we choose the
name A for the array, then the elements of A are denoted
by subscript notation or by the parenthesis notation or by
the bracket notation
Example

A linear array A[8] consisting of numbers is pictured in


following figure.
Linked List
• A linked list or one way list is a linear collection of data
elements, called nodes, where the linear order is given
by means of pointers. Each node is divided into two
parts:
– The first part contains the information of the element/node
– The second part contains the address of the next node (link
/next pointer field) in the list. There is a special pointer Start/List
contains the address of first node in the list. If this special
pointer contains null, means that List is empty.
Example:
Tree
• Data frequently contain a hierarchical relationship between
various elements. The data structure

which reflects this relationship is called a rooted tree graph or,


simply, a tree.
Graph , Queue
• Data sometimes contains a relationship between pairs
of elements which is not necessarily hierarchical in
nature, e.g. an airline flights only between the cities
connected by lines. This data structure is called Graph.
• Queue
• A queue, also called FIFO system, is a linear list in
which deletions can take place only at one end of the
list, the Font of the list and insertion can take place
only at the other end Rear.
Stack
• It is an ordered group of homogeneous items of
elements. Elements are added to and removed from
the top of the stack (the most recently added items are
at the top of the stack). The last element to be added is
the first to be removed (LIFO: Last In, First Out).
DATA STRUCTURES OPERATIONS
• The data appearing in our data structures are
processed by means of certain operations. In
fact, the particular data structure that one
chooses for a given situation depends largely in
the frequency with which specific operations are
performed.
Cont..
• The following four operations play a major role in this
text:
• Traversing: To access each and every element of the structure
exactly one’s is called traversal of DS.
• Searching: To find out the location of particular data element in the
data structure is called searching operation.
• Insertion: To add one or more data element in the data structure at
particular position is called insertion.
• Deletion: To remove an element from data structure is called
deletion .
ARRAYS: DEFINITION
• An array is a list of a finite number of homogenous data
elements.(i.e. data element of the same type)

• Arrays cab be classified as:


• One dimensional array or linear array that require only one index to
access an individual element of the array.
• Two dimensional arrays that requires two indices to access an
individual element of the array. It is also called as matrix
• Multidimensional arrays for which we need two or more indices.
Representation of One-Dimensional Array
• In Pascal language we can define array as
• VAR X: array [ 1 … N] of integer {or any other type}
• That’s means the structure contains a set of data
elements, numbered (N), for example called (X), its
defined as type of element, the second type is the
index type, is the type of values used to access
individual element of the array, the value of index is
• 1<= I =< N
Calculating the address of any element In the 1-D array:

To find the address of an element in an array the following formula is used-

• Address of A[I] = B + W * (I – LB)


• I = Subset of element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LB = Lower Limit/Lower Bound of subscript(If not specified
assume zero).
Example:
Given the base address of an array A[1300
………… 1900] as 1020 and the size of each
element is 2 bytes in the memory, find the address
of A[1700].
Given:
Base address B = 1020
Lower Limit/Lower Bound of subscript LB = 1300
Storage size of one element store in any array W = 2 Byte
Subset of element whose address to be found I = 1700

Formula used:
Address of A[I] = B + W * (I – LB)

Solution:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820
Two-Dimensional Arrays (MATRIX)
• The simplest form of the multidimensional array
is the two-dimensional array. A two- dimensional
array is, in essence, a list of one-dimensional
arrays. To declare a two-dimensional integer
array of size x,y you would write something as
follows:
Example
• A 2-dimensional array a, which contains three rows and four
columns can be shown as below:

• Thus, every element in array a is identified by an element


name of the form a[ i ][ j ], where a is the name of the array,
and i and j are the subscripts that uniquely identify each
element in a.
Representation of two dimensional arrays in memory

• A two dimensional ‘m x n’ Array A is the collection of m


X n elements. Programming language
stores the two dimensional array in one dimensional
memory in either of two ways-
· Row Major Order: First row of the array occupies the
first set of memory locations reserved for the array;
Second row occupies the next set, and so forth.
• Row Major Order: First row of the array occupies the
first set of memory locations reserved for the array;
Second row occupies the next set, and so forth.
Example
• To determine element address A[i,j]:
Location ( A[ i,j ] ) =Base Address + ( N x ( I - 1 ) ) + ( j -
1 ) For example:
Given an array [1…5,1…7] of integers.
• Calculate address of element T[4,6], where BA=900
Sol) I = 4 , J = 6
M= 5 , N= 7
Location (T [4,6]) = BA + (7 x (4-1)) + (6-1)
= 900+ (7 x 3) +5
= 900+ 21+5
= 926
Column Major Order: Order elements of first column
stored linearly and then comes
elements of next column.
Example
To determine element address A[i,j]:
Location ( A[ i,j ] ) =Base Address + ( M x ( j - 1 ) ) + ( i - 1 )

For example:
Given an array [1…6,1…8] of integers. Calculate
address element T[5,7], where BA=300
Sol) I = 5 , J = 7
M= 6 , N= 8
Location (T [4,6]) = BA + (6 x (7-1)) + (5-1)
= 300+ (6 x 6) +4
300+ 36+4
Representation of Three& Four Dimensional Array

• By the same way we can determine address of


element for three and four dimensional array
• Three Dimensional Array
To calculate address of element X[ i,j,k]
using row-major order :
Location ( X[i,j,k] )=BA + MN (k-1) + N (i-1) + (j-1)
• using column-major order
Location ( X[i,j,k] )=BA + MN (k-1) + M (j-1) + (i-1)
Cont..
• Four Dimensional Array
To calculate address of element X[ i,j,k] using row-
major order :
Location ( Y[i,j,k,l] )=BA + MNR (l-1) +MN (k-1) +N (i-1)
+ (j-1)
using column-major order
Location ( Y[i,j,k,l] )=BA + MNR (l-1) +MN (k-1) +M (j-1)
+ (i-1)
Operations on array
• Traversing: means to visit all the elements of the array
in an operation is called traversing.
• Insertion: means to put values into an array
• Deletion / Remove: to delete a value from an array.
• Sorting: Re-arrangement of values in an array in a
specific order (Ascending or Descending) is called
sorting.
• Searching: The process of finding the location of a
particular element in an array is called searching.
Traversing in Linear Array:
• It means processing or visiting each element in the
array exactly once; Let ‘A’ is an array stored in the
computer’s memory. If we want to display the contents
of ‘A’, it has to be traversed i.e. by
• accessing and processing each element of ‘A’ exactly
once. The alternate algorithm for traversing (using for
loop) is :
Example calculate & print the average of the following array of
integers
#include<iostream.h>
#define size 10 // another way int const size = 10
int main()
{
int x[10]={4,3,7,-1,7,2,0,4,2,13};
int i,sum=0,LB=0, UB=size;
float av;
for(i=LB,i<UB;i++)sum = sum + x[i];
av = (float)sum/size;
cout<< "The average of the numbers= "<<av << endl;
return 0;
}

You might also like