Unit 1 Introduction to Data Structures
Unit 1 Introduction to Data Structures
Structures
Concept of Data:
Data is a collection of numbers, alphabets &
symbols combined to represent information.
There are two types:
Atomic data
Composite data
Atomic data:
Atomic data are non decomposable entity.
Ex. Integer value 123 or character A
It can not be further divided.
If we divide value 123 it’s meaning lost.
Composite data
It is composition of several atomic data. so
it is further divided.
Ex. Date of birth is composite data
But day, month & year are atomic data.
Data Type
A data type is a term which refers to the
kind of data variable may hold in PL.
Ex.
Linear:
Ex. Linked list, stack, queue.
Elements are arranged in linear fashion (in
sequence.)
Only one-one relation can be handled
using linear data structure.
Non Linear Data Structures
All one-many, many-one , many-many
relations are handled using non linear data
structures.
Here every data element can have number
of predecessors as well as successors.
Ex. Tree, graphs.
Static & Dynamic
Static:
In case of static data structure, m/m for objects is
allocated at the time compilation.
Amount of m/m required is determined by the compiler
during compilation.
Ex. Int a[50];
Disadvantages:
1. Wastage of m/m.
2. It may cause overflow.
3. No re-usability of allocated m/m.
4. Difficult to guess exact size of data at time of writing of
program.
Dynamic
int main()
{
char *str = NULL;
/* display string */
printf("String is %s\n", str);
/* free memory */
free(str);
return 0;
}
Persistent & Ephemeral data structures.
A data structure is said to be persistent if it can be
accessed but can not be modified.
Any operation on such data structure creates two
versions of data structures. previous version is saved &
all changes are made in new version.
Operations that changes the data will create
1. Copy of old instance of data structure with original
values.
2. Copy of new instance of data structures with updated
values.
3. Ex. Call by value functions.
If we will create data cells & modify their
content’s it mean we can create
ephemeral data structures.
These data structures change with
operations.
Advantage of it is ability to maintain state
as a shared resource among many
routines.
It is more time efficient than call by value.
Disadvantage: complexity.
Algorithm:
Characteristics:
1. j=1;
2. While (j<= n)
{
3. x=x+1;
4. j++;
}
Statement no. Frequency
1 1
2 n+1
3 n
4 n
Determine frequency count
1. for(i=1;i<=n;i++)
2. for(j=1;j<=i;j++)
3. x=x+1;
Statement no. 1 = n+1
Statement no. 2 = 2+3+…..+n+(n+1)
= (1+2+3+…..n)+n
=n(n+1) + n =1/2(n2 + 3n)
2
Statement no 3: =1+2+3+…n
=n(n+1)
2
=1/2 (n2 + n)
Measuring the running time of a program:
f(n) = O(g(n))
Average case (Big - Theta notation)
number of comparisons required to search an
element present in between 1 and n.
Big - Theta notation is used to define the average
bound of an algorithm in terms of Time Complexity.
That means Big - Theta notation always indicates
the average time required by an algorithm for all
input values.
That means Big - Theta notation describes the
average case of an algorithm time complexity.
Average case= Θ (n)
Big - Theta Notation can be defined as
follows...
Consider function f(n) the time complexity of
an algorithm and g(n) is the most significant
term. If C1 g(n) <= f(n) <= C2 g(n) for all n >=
n0, C1, C2 > 0 and n0 >= 1. Then we can
represent f(n) as Θ(g(n)).
f(n) = Θ(g(n))
Ordered List
Ordered list is a set of elements where set
may be empty or it can be written as a
collection of elements such as
(a1,a2,a3……..an) a list sometimes called as
linear list.
Ex. set of days in week.
List of one digit numbers.
Operations: Display, search, insert, delete.
Polynomials
One classic example of an ordered list is a
polynomials.
Def.-
A polynomial is the sum of terms where each
terms consists of variable, coefficient and
exponent.
Various operations –
• Addition of two polynomials
• Multiplication
• Evaluation
Polynomial representation
one dimensional array where:
// an array of struct
index represents the exponent
#define MAXSIZE 10
and the stored value is the
typedef struct poly {
corresponding coefficient
real coeff;
OR
int expo;
2x8 + 4x2 + 1 }term;
term poly1[MAXSIZE];
0 1 2 3 4 5 6 7 8 9
term poly2[MAXSIZE];
1 4 2
term poly3[MAXSIZE];
Drawbacks Of Using Array:
1. If the exponent is too large then
unnecessary the size of array will be
more. Ex. 7x999-10. Scanning such
array will be time consuming.
2. Wastage of space.
3. Can not decide what the array size
should be.
Ex.
3x4+5x3+7x2+10x-19
This type of representation of polynomials is
suitable if, the upper limit on exponent value
is reasonably high
Actual number or terms are closer to this
limiting value.
Polynomial representation
By using structure.
2 main advantages
1. There is no limit on maximum value of the exponent.
2. It requires less number of terms though there is vast
difference in max & min value of exponent.
Ex.
typedef struct poly
{
int coeff;
int expo;
}p;
p p1[10];
Ex. 7x999-10
Coeff expo
0
7 999
-10 0
1
.
.
.
9
Polynomial addition
Take polynomial A & B
3x3+2x2+x+1
5x3+7x
Polynomial evaluation
Consider the polynomial for evaluation as
-10x7+4x5+3x2
Algorithm:
Step1: Read the polynomial array A.
Step2: Read the value of x.
Step3: Initialize the variable sum to zero.
Step 4: Then calculate coeff * pow (x, exp) of each term
and add the result to sum.
Step 5: Display sum.
Step 6 : stop
Concept of Sequential Organization:
0 10
Value
1 20 stored in
Index array
used to 2 30
find 3 40
element
.
.
.
9
Two dimensional array Ex. int a[10][3];
Columns
0 1 2
0 10 20 30
row 1 40 50 60
2
.
.
.
9
The elements in two dimensional array
may be arranged either in row wise or
column wise.
If the elements are stored in row wise manner
then it is called “Row Major Representation”.
If the elements are stored in column wise
manner then it is called “Column Major
Representation”.
Row major Representation
If the elements are stored in row wise manner then
it is called “Row Major Representation”.
Ex. If we want to store elements
• 10 20 30 40 50 60
then
• In a 2D array
0 1 2
0 10 20 30
1
40 50 60
.
.
.
9
Column Major Representation
If the elements are stored in column wise manner
then it is called “Column Major Representation”.
Ex. If we want to store elements
• 10 20 30 40 50 60 then
elements will be filled up by column wise
manner (consider array a[3][2])
• In a 2D array
0 1
0
10 40
1 20 50
2 30 60
Each element is occupied at successive location
if the element is of integer type then 2 bytes of
memory will be allocated.
If it is float then 4 bytes of memory will be
allocated.
Ex.
int a[3][2]={ {10,20}
{30,40}
{50,60} }
Then in a row major matrix
a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a [2][1]
10 20 30 40 50 60
10 30 50 20 40 60
ro co valu
Use
array of triples a[] to represent
w l e
a[0] 6 6 8
a matrix. a[1] 0 0 15
a[2] 0 3 22
row by row a[3] 0 5 -15
a[4] 1 1 11
within a row, a[5] 1 2 3
a[6] 2 3 -6
column by column a[7] 4 0 91
a[8] 5 2 28
Sparse Matrices
Definition
Sparse matrix is that matrix which has a very few non
zero elements.
Representation
Ex.
• Suppose a matrix is 6X7 & number of non-zero elements
are say 8 then representation will be
Index Row No Column value
No
0 6 7 8
1 0 6 -10
2 1 0 55
3 2 5 -23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 -28
8 5 0 99
Sparse Matrices
In sparse matrix –
• (TOTAL NO. OF NON-ZERO VALUE +1)*3*2
(8+1)*3*2=9*6 =54 bytes
Sparse matrix representation saves (72-54=18 bytes) of
memory.
.
Representation for Sparse Matrices
typedef struct {
int col, row, value;
} term;
term a[MAX_TERMS];
Read Sparse
cout<<"\n Enter the size of matrix (rows,columns)”;
cin>>m>>n;
a[0][0]=m;
a[0][1]=n;
1. Start
2.Read two sparse matrices SP1 and SP2
3.The index of SP1 and SP2 will be i=1,j=1
resp. Non zero elements are t1 and t2 for
SP1 and SP2.
4.The k=1 index will be for sparse matrix
SP3 which will store the addition of two
matrices.
5. SP3[0][0]=SP1[0][0]
SP3[0][1]=SP1[0][1]
Row Col Val Row Col Val
3 3 4 3 3 4
0 0 1 0 0 1
1 0 2 0 1 2
2 0 3 1 0 3
2 1 4 2 1 4
SP1 SP2
Row Col Val
3 3 5
0 0 2
0 1 2
1 0 5
2 0 3
2 1 8
SP3
Operations: Transpose
c transpose(a) // a: m x n
matrix
//Algorithm 1:
for each row i {
take element (i, j, value) and
store it as (j, i, value).
}
row col valu row col valu
e e
a[0] 6 6 8 c[0] 6 6 8
a[1] 0 0 15 c[1 ] 0 0 15
a[2] 0 3 22 c[2] 3 0 22
a[3] 0 5 -15 c[3] 5 0 -15
Eg. a[4] 1 1 11 c[4] 1 1 11
a[5] 1 2 3 c[5] 2 1 3
a[6] 2 3 -6 c[6] 3 2 -6
a[7] 4 0 91 c[7] 0 4 91
a[8] 5 2 28 c[8] 2 5 28
Index Row No Column value
No
0 6 7 8
Find Transpose 1 0 6 -10
of this sparse
matrix. 2 1 0 55
3 2 5 -23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 -28
8 5 0 99
Index Row No Column value
No
0 7 6 8
1 0 1 55
2 0 5 99
3 1 3 67
4 3 4 14
5 4 4 -28
6 5 2 -23
7 6 0 -10
8 6 3 88
Operations: Transpose
Problem: If we just place them
consecutively, we need to do a lot of
insertions to make the
row col valuordering right.
e
c[0] 6 6 8
c[1 ] 0 0 15
c[2] 3 0 22
c[3] 5 0 -15
c[4] 1 1 11
c[5] 2 1 3
c[6] 3 2 -6
c[7] 0 4 91
c[8] 2 5 28
Alg. 2 for Transpose
Algorithm 2:
Find all elements in col. 0, and store them in row 0;
Find all elements in col. 1, and store them in row 1;
… … … … etc
if(A[Term][1]== c)
{
B[nxt][0]=A[Term][1];
B[nxt][1]=A[Term][0];
B[nxt][2]=A[Term][2];
nxt++;
}
}
}
}
Complexity of simple transpose
O(No.of columns* No. of terms)
Fast Transpose:
Determine the number of elements in
each column of the original matrix.
==>
Determine the starting positions of each
row in the transpose matrix.
a[0] 6 6 8
a[1] 0 0 15
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
[0] [1] [2] [3] [4] [5]
row_terms = 2 1 2 2 0 1
starting_pos = 1 3 4 6 8 8
void fast_transpose(term a[ ], term b[ ])
{
/* the transpose of a is placed in b */
int row_terms[MAX_COL], starting_pos[MAX_COL];
int i, j, num_cols = a[0].col, num_terms = a[0].value;
b[0].row = num_cols; b[0].col = a[0].row;
b[0].value = num_terms;
if (num_terms > 0){ /*nonzero matrix*/
columns
for (i = 0; i < num_cols; i++) //Initialise to 0.
row_terms[i] = 0;
elements
for (i = 1; i <= num_terms; i++)
row_term [a[i].col]++
starting_pos[0] = 1;
columns for (i =1; i < num_cols; i++)
starting_pos[i]=starting_pos[i-1] +row_terms [i-1];
CHAPTER 2 97
for (i=1; i <= num_terms, i++) {
j = starting_pos[a[i].col];
b[j].row = a[i].col;
elements b[j].col = a[i].row;
b[j].value = a[i].value;
starting_pos[a[i].col]++;
}
}
}
CHAPTER 2 98