0% found this document useful (0 votes)
22 views73 pages

Unit-II (SparseMatrix Polynomial) Updated

The document covers the fundamentals of linear data structures, focusing on arrays as abstract data types, their organization, and memory representation. It explains operations on arrays, including retrieval, storage, and address calculation for both one-dimensional and multi-dimensional arrays. Additionally, it discusses the concept of ordered lists and their representation using arrays.

Uploaded by

Ayush Patil
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)
22 views73 pages

Unit-II (SparseMatrix Polynomial) Updated

The document covers the fundamentals of linear data structures, focusing on arrays as abstract data types, their organization, and memory representation. It explains operations on arrays, including retrieval, storage, and address calculation for both one-dimensional and multi-dimensional arrays. Additionally, it discusses the concept of ordered lists and their representation using arrays.

Uploaded by

Ayush Patil
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/ 73

Fundamentals of Data Structures

S. Y. B. Tech CSE Semester – III

SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY


Linear Data Structures
Linear Data Structures: Array as an Abstract Data Type, Sequential Organization, Storage
Representation and their Address Calculation: Row major and Column Major, Multidimensional Arrays:
Concept of Ordered List, Single Variable Polynomial: Representation using arrays, Polynomial as array
of structure, Polynomial addition, Polynomial evaluation and Polynomial multiplication. Sparse Matrix:
Sparse matrix representation using array, Sparse matrix addition, Transpose of sparse matrix- Simple
and Fast Transpose, Time and Space tradeoff.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 2


Applications of
an Array

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 3


Array as an Abstract Data Type
▪Array is set of pairs – index and
value.
▪For each index, there is a value
associated with it.
▪Arrays are often stored in
consecutive set of memory locations.
▪Mainly two operations perform on
array are retrieve and store values.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 4


Array as an Abstract Data Type
▪ The function CREATE produce a
structure ARRAY (value, index) new empty array.
Declare CREATE() -> array ▪ RETRIEVE takes as input an array
and an index and either returns the
RETRIEVE(array, index) -> appropriate values or an error.
value ▪ STORE is used to enter new index-
STORE(array, index, value) -> value pairs
array;
for all A ∈ array, i, j ∈ index, x ∈ value let
RETRIEVE(CREATE() , i) ::= error
RETRIEVE(STORE(A, i, x) , j) ::= val
end
end ARRAY
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 5
Sequential Organization
▪In sequential organization, elements are stored in consecutive
memory locations. Ex- array
▪Properties of sequential data structure are:
▪Simple to use
▪Simple to define
▪Constant access time
▪Mapping by compiler
▪An array element can be accessed by a[i] where a is the name of the
array and i is the index.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 6


Sequential Organization
▪Compiler maps a[i] to its physical location in memory.
▪Address of a[i] is given by
▪starting address of a + i * size of array elements in bytes
▪This mapping is carried out in constant time, irrespective of which
element is being accessed.
▪Limitations:
▪Size of an array is defined at the time of programming
▪Insertion and deletion is time consuming
▪Requires contiguous memory

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 7


Memory Representation and
Address Calculation
▪1-D Array
▪1-D array is a list of elements or simply a row of elements.
▪In mathematic, we often deal with variable that are simple scripted

Where xi refers to the ith element of x variables can be expressed as – x[0], x[1],
…. X[4]

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 8


Memory Representation and
Address Calculation
Example: 1-D array A[7] where A is array name and 7 is size.
Syntax: int A[7];
index value Memory address

[0] 10 1024
[1] 20 1028
[2] 30 1032
[3] 40 1036
[4] 50 1040
[5] 60 1044
[6] 70 1048

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 9


Memory Representation and Address
Calculation
▪Address of 1st element of an array is called Base
Address (BA).
▪Address of ith element = BA+ offset of the ith element from
BA
▪Where offset =( number of element before ith element)* size of each
element

Ex: consider an array : int a[10]; and BA = 403, size of element = 4


bytes. Calculate address of a[3]?
a[3] = 403 + (3*4) = 403 + 12 = 415

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 10


Memory Representation and Address
Calculation
▪2-D Array
▪2-D arrays can be thought of as a table consisting of rows and
columns.
▪It is also called as matrix.
▪1st dimension is referred as row and 2nd dimension is referred as
column.
▪The elements of 2D array may be arranged either row wise or
column wise.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 11


Memory Representation and Address
Calculation
▪Ex: int a[3][4]; //declares an integer array of 3 rows and 4
columns
int a[3][4]; Col 0 Col 1 Col 2 Col 3

Row 0 a[0][0] a[0][1] a[0][2] a[0][3]


Row 1 a[1][0] a[1][1] a[1][2] a[1][3]
Row 2 a[2][0] a[2][1] a[2][2] a[2][3]

▪In memory, all elements are stored linearly using contiguous


addresses.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 12


Memory Representation and Address
Calculation
▪In order to store 2D
matrix, a 2D address
space must be mapped to
1D address space.
▪In computer memory,
matrices are stored in
either row-major form or
column-major form.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 13


Memory Representation and Address
Calculation
▪Column-major representation:
▪if the elements are stored column-wise manner then it is
called column major representation.
int A[3][4]; Column-major:
1 2 3 4 1 4 7 10
A= 5 6 7 8 A= 2 5 8 11
9 10 11 12 3 6 9 12

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 14


Memory Representation and Address
Calculation

▪Address of an element aij (row-major) =BA+


(row_index*total_no_of_col+col_index)*S; or
B + (i – L1) * (U2 – L2 + 1) * S + ( j – L2) * S
▪Where B = base address
▪L1 & U1 are lower and upper bound values for rows
▪L2 & U2 are lower and upper bound values for columns
▪S is Number of bytes taken to store element
▪Ex: int a[3][4]; BA = 100; S=4 bytes; Find location of a[2][1] row-
wise?
A[2][1] = 100 + (2-0) * (3 – 0 + 1) * 4 + (1 – 0 ) * 4 100+(2*4+1)*4
= 100 + 2 * 4* 4 + 4 100+36
= 100 + 32+ 4 136
= 136`

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 15


Memory Representation and Address
Calculation
▪Address of an element aij (column-major) =
▪BA+(col_index*total_no_of_row+row_index)*S; or
▪B + (j – L2) * (U1 – L1 + 1) * S + ( i – L1) * S
▪Where B = base address
▪L1 & U1 are lower and upper bound values for rows
▪L2 & U2 are lower and upper bound values for columns
▪S is Number of bytes taken to store element
▪Ex: int a[3][4]; BA = 100; S=4 bytes; find location of a[2][1] col-
wise?
A[2][1] = 100 + (1-0) * (2 – 0 + 1) * 4 + (2 – 0 ) * 4 100+(1*3+2)*4
= 100 + 1 * 3 * 4 + 8 100+20
= 100 + 12 + 8 120
= 120
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 16
Row -major and Column -major
Address
Example:
int a[3][4],Base address 1050 s=4,find a[2][3] in row major and column major representation
Row-major :
A[2][3]=1050+(2*4+3)*4
= 1050+44
=1094
Column-major:
A[2][3] =1050+(3*3+2)*4
= 1050+44
= 1094

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 17


Multidimensional Array
⮚ In C/C++, multidimensional arrays can be used as array of arrays.
⮚ Data in multidimensional arrays are stored in tabular form (in row major
order).
⮚ Declaration
⮚ General form of declaring N-dimensional arrays:
⮚ data_type array_name[size1][size2]....[sizeN];
e.g. int a[5][10][20];
⮚ Size of multidimensional arrays
⮚ Total number of elements that can be stored in a multidimensional array can be
calculated by multiplying the size of all the dimensions.
⮚ int a[5][10][20];
⮚ 1000 elements in an array ‘a’.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 18


Memory Representation and Address
Calculation (row-major)

▪a[S1][S2]…[SN]
▪Address of an element aij
▪Address of a[I1][I2]. . . .[lN] = B + W*[((E1S2 + E2 )S3 +E3 )S4 ….. + EN-
1)SN + EN]
▪Where B = base address
▪W represents the size of an element in bytes
▪Ei is given by Ei = li – ti, where li and ti are the calculated indexes (indices of array
element which needs to be determined) and lower bounds respectively.
▪S1, S2, S3, ………, SN are dimensions
▪l1, l2, l3, …………..lN are indices of the element whose address needs to be
calculated.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 19


Memory Representation and Address
Calculation (row-major)
▪Example
▪int A[10][20][30][40] with base address 1200
▪Find the address of element A[1][3][5][6]
▪B = 1200 and W = 4, S1 = 10, S2 = 20, S3 = 30, S4 = 40
▪Lower bounds is zero.
▪ E1 = 1 – 0 = 1
▪ E2 = 3 – 0 = 3
▪ E3 = 5 – 0 = 5
▪ E4 = 6 – 0 = 6
▪A[I1][I2]. . . .[lN] = B + W*[((E1S2 + E2 )S3 +E3 )S4 ….. + EN-1)SN + EN]
▪A[1][3][5][6] = 1200 + 4(((1 × 20 + 3)30 +5)40 + 6) = 112424

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 20


Memory Representation and Address
Calculation (column-major)
▪Address of a[I1][I2]. . . .[lN] = B + W*[((…ENSN-1+ EN-1 )SN-2 +… E3 )S2+
E2 )S1 +E1]
▪Example
▪int A[10][20][30][40] with base address 1200
▪Find the address of element A[1][3][5][6]
▪B = 1200 and W = 4, S1 = 10, S2 = 20, S3 = 30, S4 = 40
▪Lower bounds is zero.
▪ E1 = 1 – 0 = 1
▪ E2 = 3 – 0 = 3
▪ E3 = 5 – 0 = 5
▪ E4 = 6 – 0 = 6
▪A[I1][I2]. . . .[lN] = B + W*[((…ENSN-1+ EN-1 )SN-2 +… E3 )S2+ E2 )S1 +E1]
▪A[1][3][5][6] = 1200 + 4(((6*30+5)20+3)10+1) = 149324
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 21
Concept of Ordered List
⮚ One of the simplest and most commonly found data object is the ordered or linear list.
⮚ Examples
⮚ Days of the week
⮚ (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY)
⮚ The values in a card deck
⮚ (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace)
⮚ If we consider an ordered list more abstractly, we say that it is either empty or it can be written as
⮚ (a1,a2,a3, ...,an)
⮚ where the ai are atoms from some set S.
⮚ There are a variety of operations that are performed on these lists.
⮚ find the length of the list, n;
⮚ read the list from left-to-right (or right-to-left);
⮚ retrieve the i-th element, ;
⮚ store a new value into the i-th position, ;
⮚ insert a new element at position causing elements numbered i, i + 1, ...,n to become numbered i + 1,
i + 2, ...,n + 1;
⮚ delete the element at position causing elements numbered i + 1, ...,n to become numbered i, i +
1, ..., n - 1.
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 22
Representation of Ordered List using
an Array
⮚ Associate the list element ai with the array index i.
⮚ This we will refer to as a sequential mapping
⮚ Using the conventional array representation we are storing ai and ai + 1 into consecutive locations i
and i + 1 of the array.
⮚ Retrieve or modify the values of random elements in the list is done in a constant amount of time.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 23


Representation of Polynomials using
arrays

▪Polynomial is one of the classic example of an Ordered List.


▪An ordered list is set of elements where set may be empty or
it can be written as a collection of elements such as (a 1, a2, a3,
…..,an), where ai are atoms from some set S.
▪A polynomial is the sum of terms where each term consists of
variable, coefficient and exponent.
▪Ex: A(x) = 3x2 + 2x + 4 and B(x) = x4 + 10x3 + 3x2 + 1

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 24


Representation of Polynomials of Degree n
in an array

▪We have a Polynomial-


▪Cm-1 Xm-1 + Cm-2 Xm-2+ …….+ C0X0
▪Polynomial with n terms can be represented by the ordered list of
length 2n+1
▪(n, (power, coeff), (power, coeff), ……..)
▪Ex: x3 + 5x2 + 9 can be represented as –
▪(3, (3,1), (2,5), (0,9)) or 3 3 1 2 5 0 9

▪x4+59x+10
▪(3, (4, 1), (1, 59), (0, 10)) or 3 4 1 1 59 0 10

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 25


Representation of Polynomials
A (x, y)
▪If the polynomial has m non-zero terms then the polynomial can
be represented as an ordered list of 3m + 1 terms.
m, power of x, power of y, coeff, power of x, power of y, coeff , …

1st term 2nd term


▪1st entry is the number of non-zero terms. For each term, there
are 3 entries- an exponent of x, y and coefficient triple.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 26


Representation of Polynomials
A(x, y)

▪3x3y2 + 10xy -1
(3, (3, 2, 3), (1, 1, 10), (0, 0 , -1))
OR
3 3 2 3 1 1 10 0 0 -1

▪X2+5xy+y2-y-x
(5, (2,0,1), (1,1,5), (0,2,1), (0,1,-1), (1,0,-1))
OR
5 2 0 1 1 1 5 0 2 1 0 1 -1 0 0 -1

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 27


Representation of Polynomials A
(x, y, z)
▪If the polynomial has m non-zero terms then the polynomial
can be represented as an ordered list of 4m + 1 terms.
m, power of x, power of y, power of z, coeff, power of x, power of y, power of z coeff ,

1st term 2nd term


▪1st entry is the number of non-zero terms. For each term,
there are 4 entries- an exponent of x, y, z and coefficient.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 28


Representation of Polynomials A
(x, y, z)

▪5x3y2z + 3x2y3z2 + 6xyz3 + 10


(4, (3, 2, 1, 5), (2, 3, 2, 3), (1, 1, 3, 6), (0, 0, 0, 10))
OR
4 3 2 1 5 2 3 2 3 1 1 3 6 0 0 0 10

▪-8x3y3z3 + 10x3y2z + 5xyz2 + 10


(4, (3,3,3, -8), (3,2,1, 10), (1,1,2,5), (0,0,0,10))
OR
4 3 3 3 -8 3 2 1 10 1 1 2 5 0 0 0 10

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 29


Addition and Evaluation of
Polynomials
▪Add A(x) = 3x2 + 2x + 4 and B(x) = x4 + 10x3 + 3x2 + 1
▪Result C(x) = x4 + 10x3 + 5x2 + 2x + 5

▪While storing the polynomials in the arrays, we have to store


them in the descending order of the exponents.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 30


Adding Polynomials
Horizontal
Method

Vertical Method

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 31


Add the polynomials.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 32


Representing Polynomial using array of
Structure

Consider polynomial A(x) = 2x1000 + 1, representing using array of


structure: Coeff Expo
#define Max_Terms 100; [0] 2 1000
[1] 1 0
struct polynomial
{
int coef;
int exp;

};

typedef struct polynomial poly[Max_Terms];


11/27/2018 LINEAR DATA STRUCTURE (DS-I) 33
Polynomial Addition
P1(x) = 3x2 + 2x + 4
P3(x) = x4 + 10x3 + 6x2 + 5
P2(x) = x4 + 10x3 + 3x2 -2x + 1

• Case 1: exponent of p1 > exponent of p2


copy term of p1 to end of p3.
[go to next term in p1]

• Case 2: exponent of p1 < exponent of p2


copy term of p2 to end of p3.
[go to next term in p2]

• Case 3: exponent of p1 = exponent of p2


copy term in p3 with the same exponent and with the sum of the coefficients of p1 and
p2.
[go to next term in p1 & p2]
Algorithm polyadd (p1, p2, max1, max2)
{ while( i < max1 )
i = j = k = 0;
{
{
while ( i < max1 && j < max2) p3[k] = p1[i];
{
if p1[i].exp > p2[j].exp k++;
{
p3[k] = p1[i]; i++;
k++; i++;
}
} //while
else if( p1[i].exp < p2[j].exp) while( j < max2 )
{
p3[k] = p2[j]; {
k++; j++;
} p3[k] = p2[j];
else // same exponents
{ temp = p1[i].coef + p2[j].coef;
k++;
if(temp != 0) j++;
{ p3[k].exp = p1[i].exp;
p3[k].coef = temp; } //while
i++; j++; k++;
}
} // end while
Evaluation of Polynomial
"Evaluating" a polynomial is the same as evaluating anything
else: you plug in the given value of x, and figure out what the
answer will be, or, in some cases, what y is supposed to be.
For instance:
Evaluate 2x3 – x2 – 4x + 2 at x = –3
Plug in –3 for x, remembering to be careful with parentheses and
negatives:
2(–3)3 – (–3)2 – 4(–3) + 2
= 2(–27) – (9) + 12 + 2
= –54 – 9 + 14
= –63 + 14
= –49
Always remember to be careful with the minus signs!
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 36
Evaluation of Polynomials
➢Algorithm Eval_Poly
➢Step 1: Read the polynomial array A
➢Step 2: Read the value of x
➢Step 3: Initialize the variable sum to zero
➢Step4: calculate coeff * pow(x, exp) of each term and add the
result to sum
➢Step 5: display sum
➢Step 6: end

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 37


Polynomial Multiplication
⮚ Polynomial 1:
⮚ 6x3 + 10x2+ 0x^1 + 5
⮚ Polynomial 2:
⮚ 4x2 + 2x1 + 1
⮚ Multiplication of Polynomials 1 & 2:
⮚ 24x5 + 52x4 + 26x3 + 30x2 + 10x1 + 5

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 38


Algorithm polymultiplication (p1, p2, max1, max2)
{
i = j = k = 0;
while ( i < max1){
j = 0;
while( j < max2) else{
{ p3[k].exp = exp;
temp = p1[i].coef * p2[j].coef; p3[k].coef = temp;
if(temp != 0) j++; k++;
{ flag = 0; }
exp = p1[i].exp + p2[j].exp;
} // end if
for(x = 0; x < k; x++){
if(exp == p3[x].exp){ } // end while
flag = 1; i++;
break; } } // end while
}
if(flag == 1){
p3[x].coef = p3[x].coef + temp;
j++;
}
Representation of sparse matrix
➢A matrix contains m rows and n columns of elements.
Col0 Col1 Col2 Col3 Col4 Col5
Col0 Col1 Col2
Row0 15 0 0 22 0 -15
Row0 -27 3 4
Row1 0 11 3 0 0 0
Row1 6 82 -2
Row2 0 0 0 -6 0 0
Row2 109 -64 11
Row3 0 0 0 0 0 0
Row3 12 8 9
Row4 91 0 0 0 0 0
Row4 48 27 47
Row5 0 0 28 0 0 0
mat1(5*3)
mat2(6*6)
➢We write m*n to designate a matrix with m rows and n columns

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 40


Representation of sparse matrix
➢In mat1 contains 15 non-zero elements but in mat2 contains 8
non-zero elements.
➢Any m*n matrix which contains a large number of zeros is
called as sparse matrix.
➢We can characterize uniquely any element within a matrix by
using the triple <row, col, value>.
➢We can use an array of triples to represent a sparse matrix.
➢An array of triples called as compact form.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 41


Representation of sparse matrix
➢Each triple contains row, column and value of the non-zero
elements.
➢The first row of compact form contains number of rows, no
of columns and number of nonzero elements of original
sparse matrix.
➢If sparse matrix contains ‘t’ non-zero elements, then the
compact form has t+1 rows and 3 columns.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 42


Sparse matrix in triplet form
Col0 Col1 Col2 Col3 Col4 Col5 Col0 Col1 Col2

Row0 15 0 0 22 0 -15
Row0 6 6 8
Row1 0 11 3 0 0 0 Row1 0 0 15
Row2 0 0 0 -6 0 0 Row2 0 3 22
Row3 0 0 0 0 0 0 Row3 0 5 -15
Row4 91 0 0 0 0 0 Row4 1 1 11
Row5 0 0 28 0 0 0 Row5 1 2 3
Row6 2 3 -6
Matrix1[6][6] Row7 4 0 91
Row8 5 2 28
Matrix2[9][3]

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 43


Sparse matrix in triplet form
➢Matrix1 is a normal matrix and Matrix2 is a compact form of
Sparse matrix stored as triples
➢Row0 of compact form matrix contains total number of rows,
columns and non-zero elements of sparse matrix.
➢In Normal matrix representation:
6 * 6 * 2(**)= 72 bytes are required for storage.
➢In Sparse matrix (Triplet) representation:
(8+1)x3x2(**) = 54 bytes are required for storage.
➢( ** 2 bytes are required to store integer value in 32 bits
compiler)

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 44


[0] [1] [2] [3] [4] [5]
Algorithm compact(A,m,n,B)
{ //m and n are tot. no. of rows. & cols. of original matrix A[0] 15 0 0 22 0 -15
B(0,0)=m;
B(0,1)=n
A[1] 0 11 3 0 0 0
k=1; A[2] 0 0 0 -6 0 0
for i=0 to m A[3] 0 0 0 0 0 0
{ A[4] 91 0 0 0 0 0
for j=0 to n
A[5] 0 0 28 0 0 0
{
if(A(i,j)!=0) Col0 Col1 Col2
{ B[0] 6 6 8
B(k,0)=i;
B(k,1)=j; B[1] 0 0 15
B(k,2)=A(i,j); B[2] 0 3 22
k++; B[3] 0 5 -15
} //end for j
} //end for i
B[4] 1 1 11
} B[5] 1 2 3
B(0,2)=k-1; B[6] 2 3 -6
}
B[7] 4 0 91
B[8] 5 2 28

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 45


Simple Transpose
Transpose is an operation which exchanges rows and columns.
For Example:

2 0 0 2 0 0
0 0 -3 Transpose 0 0 4
0 4 0 0 -3 0

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 46


Simple Transpose
Exchanging rows and
Transpose of Sparse Matrix columns

3 3 3 3 3 3
0 0 2 0 0 2
Transpose
1 2 -3 2 1 -3
2 1 4 1 2 4

Is this a correct
matrix?
No, in a matrix values needs to stored
sequentially(row & column wise)

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 47


Simple Transpose
Now we will see how to find out simple transpose of sparse matrix.
3 3 9 1.Traverse Second column of sparse matrix
0 0 2
2.Search for column indexes in sequence from
0 1 -3 0 to number of columns-1
0 2 4
1 0 5 Transpose
1 1 6
1 2 1
3.Copy the rows (from 1 to number of non-zero
2 0 7 terms) in sequence to transpose matrix.
2 1 8
2 2 3

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 48


Simple Transpose of Sparse
Matrix
Col0 Col1 Col2 Col0 Col1 Col2
Row0 6 6 8 Row0 6 6 8
Row1 0 0 15 Row1 0 0 15
Row2 0 3 22 Row2 0 4 91
Row3 0 5 -15 Row3 1 1 11
Row4 1 1 11 Row4 2 1 3
Row5 1 2 3 Row5 2 5 28
Row6 2 3 -6 Row6 3 0 22
Row7 4 0 91 Row7 3 2 -6
Row8 5 2 28 Row8 5 0 -15

(a) (b)
(a) shows a sparse matrix and (b) shows its transpose stored as triples.
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 49
Simple Transpose of Sparse
Matrix
➢Transpose of a matrix is obtained by interchanging rows and
columns.
➢In another way, we can say that element in the i, j position gets put in
the j, i position.
➢But this is not the case with sparse matrix triple form.
➢To find transpose of sparse matrix triple form, find all elements in col0
and store then in row0 of the transpose matrix, find all elements in col1
and store then in row1 of the transpose matrix, and so on.
➢Since the original matrix ordered the rows, the columns within each
row of the transpose matrix will be arranged in ascending order as
well.
11/27/2018 LINEAR DATA STRUCTURE (DS-I) 50
[0] [1] [2] [3] [4] [5]

Algorithm of Simple Transpose of A[0] 15 0 0 22 0 -15

Sparse Matrix A[1]

A[2]
0
0
11
0
3
0
0
-6
0
0
0
0
Algorithm Transpose(A,B) A[3] 0 0 0 0 0 0
// A is a matrix represented in sparse form. B is set to be its
A[4] 91 0 0 0 0 0
transpose.
1 (m,n,t) :=(A(0,0), A(0,1), A(0,2)) A[5] 0 0 28 0 0 0
2 (B(0,0), B(0,1), B(0,2)) := (n,m,t) Col0 Col1 Col2

3 if t<=0 then return //check for zero matrix B[0] 6 6 8


4 q :=1 //q is position of next term in B B[1] 0 0 15
5 for col :=0 to n do //transpose by column B[2] 0 3 22
6 for p:=1 to t do //for all nonzero term do
7 if A(p,1) = col then //correct column
B[3] 0 5 -15
8 {(B(q,0), B(q,1), B(q,2)) := (A(p,1), A(p,0), A(p,2)) B[4] 1 1 11
9 q:=q+1} B[5] 1 2 3
10 end B[6] 2 3 -6
11 end B[7] 4 0 91
12 end Transpose B[8] 5 2 28

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 51


Difference between Fast and
Simple Transpose
Time Complexity of Simple Transpose
= O(n*t)

Time Complexity of Fast Transpose


=O(n+t)

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 52


Fast Transpose
Exchanging rows and
Transpose of Sparse Matrix columns

3 3 3 3 3 3
0 0 2 0 0 2
Transpose
1 2 -3 1 2 4
2 1 4 2 1 -3

Is this a correct
matrix?
Yes, because row and column indices are in
sequence.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 53


Fast Transpose
Now we will see how to find out fast transpose of sparse matrix.

3 3 9 1.Calculate frequency of each column value


0 0 2
2.Use frequency of each column value to find
0 1 -3 location of each row in transpose matrix.
0 2 4
1 0 5
Transpose
1 1 6
1 2 1
3.Copy row containing first zero at location zero.
2 0 7
2 1 8
2 2 3

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 54


Fast Transpose of Sparse Matrix
➢Time complexity of simple transpose, is O(columns*elements)
➢We can transpose a matrix as a sequence of triples in time (columns
+ elements)
➢Fast_Transpose proceeds by first determining the number of
elements in each column of A
➢This gives us the number of elements in each row of B. From this
information, the starting point in B of each of its rows is easily
obtained.
➢We can now move the elements of A one by one into their correct
position in B.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 55


Fast Transpose of Sparse Matrix
Col0 Col1 Col2 Col0 Col1 Col2

Row0 6 6 8 Row0 6 6 8
Row1 0 0 15 Row1 0 0 15
Row2 0 3 22 Row2 0 4 91
Row3 0 5 -15 Row3 1 1 11
Row4 1 1 11 Row4 2 1 3
Row5 1 2 3 Row5 2 5 28
Row6 2 3 -6 Row6 3 0 22
Row7 4 0 91 Row7 3 2 -6
Row8 5 2 28 Row8 5 0 -15

(a) (b)
(a) shows a sparse matrix and (b) shows its transpose stored as triples.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 56


Col0 Col1 Col2

Algorithm of Fast Transpose of Row0 6 6 8

Sparse Matrix Row1

Row2
0
0
0
3
15
22
procedure FAST_TRANSPOSE(A,B) Row3 0 5 -15
// A is a matrix represented in sparse form. B is set to be its transpose. t is non-zero terms Row4 1 1 11
declare S(n), T(n); //local array
1 (m,n,t) := (A(0,0), A(0,1), A(0,2))
Row5 1 2 3
2 (B(0,0), B(0,1), B(0,2)) := (n,m,t) Row6 2 3 -6
3 if t<=0 then return //check for zero matrix Row7 4 0 91
4 for i:=0 to n do S(i) :=0 end
5 for i:= 1 to t do //S(k) is the number of elements in row k of B Row8 5 2 28
6 S(A(i, 1)) := S(A(i, 1)) + 1 //elements in row k of B Col0 Col1 Col2
7 end
8 T(0):=1 //T(i) is the starting position of row i in B Row0 6 6 8
9 for i:=1 to n do Row1 0 0 15
10 T(i) := T(i -1 ) + S(i - 1)
11 end
Row2 0 4 91
12 for i:=1 to t do //move all t elements of A to B// Row3 1 1 11
13 j := A(i,1) //j is the row in B// Row4 2 1 3
14 (B(T(j),0), B(T(j),1), B(T(j),2)) := (A(i,1), A(i,0), A(i,2)) //store in triple//
15 T(j) :=T(j) + 1 //increase row j to next spot//
Row5 2 5 28
16 end Row6 3 0 22
17 end FAST_TRANSPOSE Row7 3 2 -6
11/27/2018 LINEAR DATA STRUCTURE (DS-I) Row8 5 0 57 -15
Additions of two Sparse Matrices
Col0 Col1 Col2

Col0 Col1 Col2 Col0 Col1 Col2 Row0 6 6 1


Row0 6 6 8 Row0 6 6 8 Row1 0 0 130
Row1 0 0 15 Row1 0 0 15 Row2 0 3 22
Row2 0 3 22 Row2 0 4 91 Row3 0 4 91
Row3 0 5 -15 Row3 1 1 11 Row4 0 5 -15
Row4 1 1 11 Row4 2 1 3 Row5 1 1 22
Row5 1 2 3 Row7 3 2 -6 Row6 1 2 3
Row6 2 3 -6 Row7 2 1 3
Row7 4 0 91
Row8 2 3 -6
Row8 5 2 28
Row9 3 2 -6
Row10 4 0 91
Row11 5 2 28

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 58


Algorithm - Addition of Sparse
Matrix
Algorithm Addition(a,b,c)
//a and b are two sparse matrices in compact form. C is the resultant sparse matrix in compact form.
if a(0,0) = b(0,0) and a(0,1) = b(0,1)
if t1=0 and t2 = 0 then stop
c(0,0) = a(0,0) ; c(0,1) = a(0,1) ;
End
i =j =k = 1
while ( i<= t1 and j <= t2)
Case A : a(i ,0) = b(j ,0) // row position
Case 1 : a(i ,1) = b(j ,1 ) //col position
temp = a(i ,2) + b(j ,2)
if temp != 0
c(k , 0) = a(i , 0)
c(k , 1) = a(i , 1)
c(k , 2) = temp
k = k+1
end if
i = i +1; j=j+1;

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 59


Algorithm - Addition of Sparse
Matrix
Case 2 : if a(i ,1) < b(j , 1)
c(k , 0) = a(i , 0)
c(k , 1) = a(i , 1)
c(k , 2) = a(i , 2)
k = k+1
i = i +1
Case 3 : if a(i ,1) > b(j , 1)
c(k , 0) = b(j , 0)
c(k , 1) = b(j , 1)
c(k , 2) = b( j , 2)
k = k+1
j = j +1
Case B : if a(i ,0) < b(j , 0)
c(k , 0) = a(i , 0)
c(k , 1) = a(i , 1)
c(k , 2) = a( i , 2)
k = k+1
i = i +1

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 60


Algorithm - Addition of Sparse
Matrix
Case C : if a(i ,0) > b(j , 0)
c(k , 0) = b(j , 0)
c(k , 1) = b(j , 1)
c(k , 2) = b( j , 2)
k = k+1
j = j +1
end while
while (i <= t1)
{
c := a
i ++; k++;
}
while (j <= t2)
{
c := b
j ++; k++;
}
c(0,2) = k-1
end Addition

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 61


FAQ
1. What is sparse matrix? List the applications?
2. Represent sparse matrix with suitable data structures?
Explain with example simple and fast transpose?
3. Explain sequential memory organization with suitable
example?
4. Explain row major and column major representation of a
matric? Show address calculation with example?

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 62


FAQ

5. Find out the addition of two sparse matrices in triplet form and also find Simple and Fast
transpose? 4 5 6
4 5 6
0 3 7 0 3 5
0 4 6 1 3 8
1 4 4 1 4
+
45 +
2 1 8 2 3 4
3 2 45 3 2 45
6. Represent the following polynomial using array: 4 1 2
4 4 21
1. 3x3y2+10xy4+10x+1
2. 21+x4-5x7+18x68
3. 5x2+10xy+y2+20
4. 5x3y2z+3x2y3z2+6xyz3+10
5. 10x3y2z-8x3y3z3+5xyz2+10

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 63


FAQ
7. Each element of an array Data[20][50] requires 4 bytes of storage. Base
address of data is 2000. determine the location of Data[10][10] when the
array is stored as i) row major and ii) column major?
8. Consider the liner array A(5:50). BA=300 and the number of words per
memory cell is 4 bytes. Find address of A[15]?
9. Consider int arr[4][5]. BA=1020. Find the address of arr[3][4] with row-
major and column major representation?
10. Consider int arr[5][4]. BA=510. Find the address of arr[3][2] with row-major
and column major representation?

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 64


FAQ
7. Each element of an array Data[20][50] requires 4 bytes of storage. Base
address of data is 2000. determine the location of Data[10][10] when the
array is stored as i) row major and ii) column major?
8. Consider the liner array A(5:50). BA=300 and the number of words per
memory cell is 4 bytes. Find address of A[15]?
9. Consider int arr[4][5]. BA=1020. Find the address of arr[3][4] with row-
major and column major representation?
10. Consider int arr[5][4]. BA=510. Find the address of arr[3][2] with row-major
and column major representation?

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 65


Examples
A farmer must add the areas of two plots of land to determine the
amount of seed to plant. The area of plot A can be represented by 3x2 +
7x – 5, and the area of plot B can be represented by 5x2 – 4x + 11.
Write a polynomial that represents the total area of both plots of land.

(3x2 + 7x – 5) Plot A.
+ (5x2 – 4x + 11) Plot B.
8x2 + 3x + 6 Combine like terms.

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 66


1 x+2 2
) 3 )
9 55
6 xx ++ 22
2x + 5 3x + 7

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 67


Write an expression that represents the area of
the shaded region in terms of x.
1 2
) 5 )
8
7 33

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 68


Add or subtract.
1. 7m2 + 3m + 4m2 11m2 + 3m

2. (r2 + s2) – (5r2 + 4s2) –4r2 – 3s2


3. (10pq + 3p) + (2pq – 5p + 6pq) 18pq – 2p
4. (14d2 – 8) + (6d2 – 2d + 1) 20d2 – 2d – 7

5. (2.5ab + 14b) – (–1.5ab + 4b) 4ab + 10b

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 69


6. A painter must add the areas of two walls to
determine the amount of paint needed. The area
of the first wall is modeled by 4x2 + 12x + 9, and
the area of the second wall is modeled by
36x2 – 12x + 1. Write a polynomial that
represents the total area of the two walls.
40x2 + 10

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 70


Practice Assignments

➢Implement Array as an Abstract Data Type using Sequential


Organization?
➢Representation of Polynomials using arrays and perform
operations
➢ Addition of two polynomials
➢Evaluation of Polynomial

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 71


Takeaway

➢Study the concept of Sequential Organization and its Memory


Representation and Address Calculation.
➢Learn Representation and operations of polynomial such as
Addition and Evaluation.
➢Study and implement sparse matrix concept and operations
such as Simple Transpose, Fast Transpose and Addition

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 72


References
1. E. Horowitz, S. Sahani, S. Anderson-Freed, "Fundamentals
of Data Structures in C", Universities Press, 2008
2. Treamblay, Sorenson, “An introduction to data structures
with applications”, Tata McGraw Hill, Second Edition
3. Aaron Tanenbaum, “Data Structures using C”, Pearson
Education
4. R. Gilberg, B. Forouzan, "Data Structures: A pseudo code
approach with C", Cenage Learning, ISBN 9788131503140

11/27/2018 LINEAR DATA STRUCTURE (DS-I) 73

You might also like