DataStructures IUnit
DataStructures IUnit
Dr. NGPASC
COIMBATORE | INDIA 1
HISTORY OF ALGORITHM
The word algorithm originates from the Arabic word algorism which is linked to the
name of the Arabic mathematician Abu Jafar Mohammed Ibn Musa Al Khwarizmi (825
A.D.).
Al Khwarizm is considered to be the first algorithm designer for adding numbers.
The algorithm designed by him and followed till today, calls for summing up the digits
occurring at specific positions and the previous carry digit, repetitively moving from the
least significant digit to the most significant digit until the digits have been exhausted.
Dr. NGPASC
COIMBATORE | INDIA 2
DEFINITION, STRUCTURE AND PROPERTIES OF 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.
STRUCTURE AND PROPERTIES:
An algorithm has the following structure:
(i) Input step
(ii) Assignment step
(iii) Decision step
(iv) Repetitive step
(v) Output step
Dr. NGPASC
COIMBATORE | INDIA 3
DEFINITION, STRUCTURE AND PROPERTIES OF 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.
STRUCTURE AND PROPERTIES:
An algorithm has the following structure:
(i) Input step
(ii) Assignment step
(iii) Decision step
(iv) Repetitive step
(v) Output step
Dr. NGPASC
COIMBATORE | INDIA 4
DEFINITION, STRUCTURE AND PROPERTIES OF ALGORITHM
Dr. NGPASC
COIMBATORE | INDIA 6
DEVELOPMENT OF ALGORITHM
● Once a clear statement of the problem is done, the model for the solution of the problem
is to be formulated.
● The next step is to design the algorithm based on the solution model that is formulated.
● It is here that one sees the role of data structures. The right choice of the data structure
needs to be made at the design stage itself since data structures influence the efficiency of
the algorithm.
● Once the correctness of the algorithm is checked and the algorithm implemented, the
most important step of measuring the performance of the algorithm is done. This is what
is termed as algorithm analysis.
● It can be seen how the use of appropriate data structures results in a better performance
of the algorithm.
● Finally the program is tested and the development ends with proper documentation.
Dr. NGPASC
COIMBATORE | INDIA 7
EFFICIENCY OF ALGORITHM
Dr. NGPASC
COIMBATORE | INDIA 8
EFFICIENCY OF ALGORITHM
● For space, it would mean looking for an algorithm that consumes or needs limited memory space for its
execution. The performance measure in such a case is termed space complexity.
● 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.
The time complexity of an algorithm can be computed either by an empirical or theoretical approach.
● The empirical or posteriori testing approach calls for implementing the complete algorithms and executing them
on a computer for various instances of the problem.
● The time taken by the execution of the programs for various instances of the problem are noted and compared.
● That algorithm whose implementation yields the least time, is considered as the best among the candidate
algorithmic solutions
Disadvantage of posteriori testing:
● The disadvantage of posteriori testing is that it is dependent on various other factors such as the machine on
which the program is executed, the programming language with which it is implemented and why, even on the
skills of the programmer who writes the program code
Dr. NGPASC
COIMBATORE | INDIA 9
EFFICIENCY OF ALGORITHM
Let us estimate the frequency count of the statement x = x + 2 occurring in the following three
program segments (A, B, C):
Dr. NGPASC
COIMBATORE | INDIA 11
APRIORI ANALYSIS – FREQUENCY COUNT OF PROGRAM SEGMENT A
Dr. NGPASC
COIMBATORE | INDIA 12
APRIORI ANALYSIS – FREQUENCY COUNT OF PROGRAM SEGMENT B
Dr. NGPASC
COIMBATORE | INDIA 13
APRIORI ANALYSIS – FREQUENCY COUNT OF PROGRAM SEGMENT C
Dr. NGPASC
COIMBATORE | INDIA 14
APRIORI ANALYSIS – FREQUENCY COUNT OF PROGRAM SEGMENT C
Dr. NGPASC
COIMBATORE | INDIA 15
ASYMPTOTIC NOTATION
● Apriori analysis employs the following notations to express the time complexity of
algorithms. These are termed asymptotic notations since they are meaningful
approximations of functions that represent the time or space complexity of a
program.
● Definition 2.1: f(n) = O(g(n)) (read as f of n is big oh 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.
Dr. NGPASC
COIMBATORE | INDIA 16
ASYMPTOTIC NOTATION
Dr. NGPASC
COIMBATORE | INDIA 17
ASYMPTOTIC NOTATION
Dr. NGPASC
COIMBATORE | INDIA 18
ASYMPTOTIC NOTATION
Dr. NGPASC
COIMBATORE | INDIA 19
DATA STRUCTURE – DEFINITION AND CLASSIFICATION
Dr. NGPASC
COIMBATORE | INDIA 20
ABSTRACT AND PRIMITIVES DATA TYPES
Dr. NGPASC
COIMBATORE | INDIA 21
CLASSIFICATION OF DATA TYPES
● The data structures are broadly classified as linear data structures and non-linear data structures.
Linear data structures are unidimensional in structure and represent linear lists. These are further
classified as sequential and linked representations. On the other hand, non-linear data structures are
two-dimensional representations of data lists.
Dr. NGPASC
COIMBATORE | INDIA 22
LINEAR DATA STRUCTURES
ii) Stacks
● Follows LIFO (Last In First Out) principle.
● Insertion (push) and deletion (pop) occur at the top.
● Example: Stack of plates, undo operation in editors.
iii) Queues
● Follows FIFO (First In First Out) principle.
● Insertion at rear and deletion from front.
● Example: Queue at a ticket counter.
iv) Priority Queues
● Elements are processed based on priority.
● Highest priority element is removed first.
● Example: Emergency patients in a hospital.
Dr. NGPASC
COIMBATORE | INDIA 24
LINEAR DATA STRUCTURES
B) Linked Representation
● In this method, elements are stored in non-contiguous memory locations and are connected
using links (pointers).
i) Linked Lists
● Collection of nodes where each node contains data and a link to the next node.
● Dynamic in size.
● Example: Playlist of songs.
ii) Linked Stacks
● Stack implemented using linked list.
● No fixed size limitation.
iii) Linked Queues
● Queue implemented using linked list.
Dr. NGPASC ● Efficient memory utilization.
COIMBATORE | INDIA 25
LINEAR DATA STRUCTURES
● An array is an ADT whose objects are sequence of elements of the same type and
the two operations performed on it are store and retrieve.
● Thus if a is an array the operations can be represented as STORE (a, i, e) and
RETRIEVE (a, i) where i is termed as the index and e is the element that is to be
stored in the array.
● These functions are equivalent to the programming language statements a[i ]: = e
and a[i ] where i is termed subscript and a the array variable name in
programming language parlance.
Dr. NGPASC
COIMBATORE | INDIA 27
ARRAYS
Dr. NGPASC
COIMBATORE | INDIA 28
ARRAYS
Dr. NGPASC
COIMBATORE | INDIA 29
ARRAY OPERATIONS
An array when viewed as a data structure supports only two operations viz.,
(i) storage of values (i.e.) writing into an array (STORE (a, i, e) ) and,
(ii) retrieval of values (i.e.) reading from an array ( RETRIEVE (a, i) )
For example, if A is an array of 5 elements then Fig. 3.2 illustrates the operations
performed on A.
Dr. NGPASC
COIMBATORE | INDIA 30
NUMBER OF ELEMENTS IN AN ARRAY
One-dimensional array:
Let A[1:u] be a one-dimensional array. The size of the array, as is evident is u and
the elements are A[1], A[2], A[u 1], A[u]. In the case of the array A[l : u] where l
is the lower bound and u is the upper bound of the index range, the number of
elements is given by (u l + 1).
The number of elements in
(i) A[1:26] = 26
(ii) A[5:53] = 49 (Q 53 5 + 1)
(iii) A[1:26] = 28
Dr. NGPASC
COIMBATORE | INDIA 31
NUMBER OF ELEMENTS IN AN ARRAY
Two-dimensional array:
Let A[1 : u1, 1 : u2] be a two-dimensional array where u1 indicates the number of
rows and u2 the number of columns in the array. Then the number of elements in A
is u1.u2 Generalizing, A[l1 : u1, l2 : u2] has a size of (u1 l1 + 1) (u2 l2 + 1)
elements. Figure 3.3 illustrates a two dimensional array and its size.
Dr. NGPASC
COIMBATORE | INDIA 32
NUMBER OF ELEMENTS IN AN ARRAY
Multi-dimensional array:
A multi-dimensional array A[1 : U1, 1 : U2,….1:Un] has a size of U1 . U2…Un
elements.
Dr. NGPASC
COIMBATORE | INDIA 33
REPRESENTATION OF ARRAYS IN MEMORY
Dr. NGPASC
COIMBATORE | INDIA 35
ONE DIMENSIONAL ARRAY
● Consider the array A(1 : u1) and let α be the address of the starting memory location
referred to as the base address of the array.
● Here as is evident, A[1] occupies the memory location whose address is α, A(2) occupies α +
1 and so on.
● In general, the address of A[i] is given by α + (i-1).
● In general, for a one-dimensional array A(l1 : u1) the address of A[i] is given by α + (i -
l1), where α is the base address.
● Example - For the array given below with base address α = 100, the addresses of the array
elements specified are computed as given below:
Dr. NGPASC
COIMBATORE | INDIA 36
REPRESENTATION OF ONE DIMENSIONAL ARRAY
Dr. NGPASC
COIMBATORE | INDIA 37
REPRESENTATION OF TWO DIMENSIONAL ARRAY
Dr. NGPASC
COIMBATORE | INDIA 38
REPRESENTATION OF TWO DIMENSIONAL ARRAY
Dr. NGPASC
COIMBATORE | INDIA 39
THREE DIMENSIONAL ARRAY
Three-Dimensional Array
● Consider a 3D array A[1:u1, 1:u2, 1:u3].
● It can be thought of as u1 two-dimensional arrays, each of size u2 × u3.
● Analogy: Imagine buildings (i = building), each with u2 floors (j = floor), and each
floor has u3 rooms (k = room).
[Link] an element A[i, j, k]:
To reach A[i, 1, 1] (first room of first floor of the ith building), you must skip:
● (i - 1) × u2 × u3 rooms from previous buildings.
● Then move to the first floor of the ith building.
Dr. NGPASC
COIMBATORE | INDIA 40
THREE DIMENSIONAL ARRAY
Dr. NGPASC
COIMBATORE | INDIA 41
THREE DIMENSIONAL ARRAY
Dr. NGPASC
COIMBATORE | INDIA 42
THREE DIMENSIONAL ARRAY
Dr. NGPASC
COIMBATORE | INDIA 43
APPLICATIONS - SPARSE MATRIX
Dr. NGPASC
COIMBATORE | INDIA 44
SPARSE MATRIX
● A matrix consumes a lot of space in memory. Thus, a 1000 * 1000 matrix needs
1 million storage locations in memory. Imagine the situation when the matrix is
sparse! To store a handful of non-zero elements, voluminous memory is allotted
and thereby wasted.
● In such a case to save valuable storage space, we resort to a triple representation
viz., (i, j, value) to represent each non-zero element of the sparse matrix.
● In other words, a sparse matrix A is represented by another matrix B [0 : t, 1 :
3] with t + 1 rows and 3 columns. Here t refers to the number of non-zero
elements in the sparse matrix.
● While rows 1 to t record the details pertaining to the non-zero elements as triple
(that is 3 columns), the zeroth row viz. B[0, 1], B[0, 2] and B[0, 3] record the
number of non-zero elements of the original sparse matrix A.
Dr. NGPASC
COIMBATORE | INDIA 45
SPARSE MATRIX
Applications: Sparse Matrices.
Sparse matrix:
A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the
matrix have 0 value, then it is called a sparse matrix.
Definition
A sparse matrix is a matrix in which most of the elements are zero, with only a few non-zero values. In other words, the number of zero
elements is much greater than the number of non-zero elements.
Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.
Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements.
Example
Array representation
A data structure that can store all information about non-zero elements namely row number i, column number j, and the value itself.
The most naive thought is a 2-d matrix having rows equal to the number of non-zero elements and three columns for storing the element and its position
coordinates as shown below.
Besides array, a linked list can also be a good choice for storing the sparse matrix in a compressed form where each node of the linked list
has exactly four entries containing row number, column number, the value of the non-zero element along with the pointer of the next node i.e.
(i, j, value, next-pointer) as shown below.
The corresponding transformation from 2-d matrix to linked list representation is as follows:
a) Row:
It is defined as the row number of the non-zero element counted from top to
bottom when it is present in the 2-d array representation.
b) Column:
It is defined as the column number of the element counted from left to right
when it is present in the 2-d array representation.
c) Value:
It is defined as the value of the non-zero element present at the (row, column)
cell of the 2-d array representation.
Dr. NGPASC 49
COIMBATORE | INDIA
SPARSE MATRIX
Transformation of Sparse Matrix to Triplet Representation
Example
Convert the following matrix into Triplet Representation.
0 0 0 6
0 0 8 0
0 0 0 0
5 0 0 4
Triplet implementation is similar to the original sparse matrix i.e. it is also a matrix but of order 3xP where P represents the number of non-
zero elements in the original matrix and 3 columns where the first column represents the row number, the second column represents the
column number corresponding to the original sparse matrix which is of the order say M x N.
● The number of non-zero elements i.e. P in the sparse matrix is calculated by traversing the original matrix in O(MxN).
● Thus, the idea is to declare a 3 x P matrix, let's call it tripletMatrix[3][P], then traverse the original sparse matrix and for some ith non-
zero element of the sparse matrix, tripletMatrix[0][i] is its row number , tripletMatrix[1][i] is its column number ,tripletMatrix[2][i] is its value
considering 0-based indexing.
● filling the tripletMatrix while traversing the original sparse matrix in O(MxN).
● Initialize a variable k to zero which keeps track of the non-zero element count hence 0 <= k < P and while
traversing the original matrix whenever a non-zero element is encountered then asssign it to the tripleMatrix as
(tripletMatrix[0][k] = row number, tripletMatrix[1][k] = column number, tripletMatrix[2][k] = value) represents the kth
non-zero element in the sparse matrix and increment k by 1.
int main()
{
// sparse matrix of order 5x6 with 6 non-zero values
int sparseMatrix[5][6] =
{
{0 , 8 , 0 , 0 , 9, 0 },
{0 , 0 , 0 , 3 , 0, 0 },
{4 , 0 , 0 , 0 , 0, 5 },
{0 , 6 , 9 , 2 , 0, 0 },
{0 , 0 , 0 , 0 , 0, 0 }
};
cout<<"\n";
}
return 0;
Dr. NGPASC 54
}
COIMBATORE | INDIA
SPARSE MATRIX
Linked list Representation of the Sparse Matrix
This representation is another linear representation of the sparse matrix besides triplet representation in which each non-zero element of the sparse matrix
is represented by a linked list node where the information about the non-zero element of sparse matrix namely row number i counted from left to right in
the sparse matrix, column number j counted from top to down in the sparse matrix, the value of the element and the pointer to the next non-zero element
i.e. next node of linked list is stored in the linked list node.
Thus, the sparse matrix is transformed into a linked list where each node contains a non-zero element.
define the terminologies for the linked list node: a) Row:
It is defined as the row number of the non-zero element counted from
top to bottom when it is present in the 2-d array representation.
b) Column:
It is defined as the column number of the element counted from left to
right when it is present in the 2-d array representation.
c) Value:
It is defined as the value of the non-zero element present at the (row,
column) cell of the 2-d array representation.
d) Next pointer:
It is defined as the address of the next node in the linked list storing
the next non-zero value.
r = new Node();
r->row = rowIndex;
r->col = colIndex;
r->value = nonZeroValue;
r->nextPointer = NULL;
temp->nextPointer = r;
}
}
Dr. NGPASC 58
COIMBATORE | INDIA
SPARSE MATRIX
// Function prints contents of linked list
// starting from startNode
void printList(Node *startNode)
{
Node *ptr = startNode;
cout << "Row position:";
while (ptr != NULL)
{
cout << ptr->row << " ";
ptr = ptr->nextPointer;
}
cout << endl;
cout << "Column position:";
ptr = startNode;
while (ptr != NULL)
{
cout << ptr->col << " ";
ptr = ptr->nextPointer;
}
cout << endl;
cout << "Value:";
ptr = startNode;
Dr. NGPASC 59
COIMBATORE | INDIA
SPARSE MATRIX
while (ptr != NULL)
{
cout << ptr->value << " ";
ptr = ptr->nextPointer;
}
}
// Driver Code
int main()
{
// sparse matrix of order 5x6 with 6 non-zero values
int sparseMatrix[5][6] =
{
{0 , 8 , 0 , 0 , 9, 0 },
{0 , 0 , 0 , 3 , 0, 0 },
{4 , 0 , 0 , 0 , 0, 5 },
{0 , 6 , 9 , 2 , 0, 0 },
{0 , 0 , 0 , 0 , 0, 0 }
};
Here are the advantages and disadvantages of using sparse matrices in data structure:
Advantages
Disadvantages
● Sparse matrices may lose information since many elements are zero.
● Choosing the right algorithms and libraries for sparse matrix operations can be challenging.