Data Structures (DS)
GTU # 3130702
Unit-2
Linear Data
Structure
Array
Dr. Pradyumansinh Jadeja
Computer Engineering
Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
+91 9879461848
Looping
Outline
Array
• Representation of arrays
• One dimensional array
• Two dimensional array
Applications of arrays
• Symbol Manipulation (matrix representation of
polynomial equation)
• Sparse matrix
Sparse matrix and its representation
One Dimensional Array
Simplest data structure that makes use of computed address to locate its
elements is the one-dimensional array or vector.
Number of memory locations is sequentially allocated to the vector.
A vector size is fixed and therefore requires a fixed number of memory
locations.
Vector A with subscript
L lower
• L isbound ofof“one”
the address is represented
the first word
0
as
allocated to the first below….
element of vector A
0
i-1 • C words is size of each element or node
• The address of element Ai is
L0+(i-
Loc(Ai) = L0 + (C*(i-1))
1)C
A[i] • Let’s consider the more general case of a vector A with lower bound for it’s
subscript is given by some variable b.
• The address of element Ai is
Loc(Ai) = L0 + (C*(i-b))
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 3
Two Dimensional Array
Two dimensional arrays are also called table or matrix
Two dimensional arrays have two subscripts
Column major order matrix: Two dimensional array in which elements
are stored column by column is called as column major matrix
Two dimensional array consisting of two rows and four columns is
A[1,1],
stored sequentially A[1,2],
by columns : A[1,3], A[1,4],
A[2,1], A[2,2], A[2,3], A[2,4]
Col- Col- Col- Col-
1[1,1] 2 3 4
Row [1,2] [1,3] [1,4]
1
Row [2,1] [2,2] [2,3] [2,4]
2
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 4
Column major order matrix
Col- Col- Col- Col-
1 2[1,2 3[1,3 4
Row [1,
[1,1 [1, [1, [1,
1 ]1] ]2] ]3] 4]
Row [2,1
[2, [2,2
[2, [2, [2,
2 ]1] ]2] 3] 4]
The address of element A [ i , j ] can be obtained by expression
Loc (A [ i , j ]) = L0 + (j-1)*2 + (i-1)
Loc (A [2, 3]) = L0 + (3-1)*2 + (2-1) = L0 + 5
In general for two dimensional array consisting of n rows and m columns
the address element A [ i , j ] is given by
Loc (A [ i , j ]) = L0 + (j-1)*n + (i – 1)
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 5
Row major order matrix
Row major order matrix: Two dimensional array in which elements are
stored row by row is called as row major matrix
b2 u2 n = no of rows, m = no of
b1 [1,1] [1,2] [1,3] [1,m] columns
b1 = lower bound subscript of
[2,1] [2,2] [2,3] [2,m] row
u1 = upper bound subscript of
row
b2
n ==u1
lower
– b1 bound
+ 1 subscript of
u1 [n,1] [n,2] [n,3] [n,m] column
nxm u2 = upper bound subscript of
column
• The address element A [ i , j ] is given by m = u2 – b2 + 1
Loc (A [ i , j ]) = L0 + (i-1)*m + (j – 1)
• The address element A [ i , j ] is given by
Loc (A [ i , j ]) = L0 + (i-b1)*(u2-b2+1) + (j –
b2)
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 6
Applications of Array
1. Symbol Manipulation (matrix representation of polynomial equation)
2. Sparse Matrix
Matrix representation of polynomial equation
We can use array for different kind of operations in polynomial equation such as
addition, subtraction, division, differentiation etc…
We are interested in finding suitable representation for polynomial so that different
operations like addition, subtraction etc… can be performed in efficient manner.
Array can be used to represent Polynomial equation.
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 7
Representation of Polynomial equation
Y Y2 Y3 Y4
X XY XY2 XY3 XY4
X2 X 3Y X2Y2 X2Y3 X2Y4
X3 X 3Y X3Y2 X3Y3 X3Y4
X4 X 4Y X4Y2 X4Y3 X4Y4
2X2 + 5XY + Y2 X2 + 3XY + Y2+Y-X
Y Y2 Y3 Y4 Y Y2 Y3 Y4
0 0 01 0 0 0 01 01 0 0
X 0 05 0 0 0 X 0
-1 30 0 0 0
X2 20 0 0 0 0 X2 10 0 0 0 0
X3 0 0 0 0 0 X3 0 0 0 0 0
X4 0 0 0 0 0 X4 0 0 0 0 0
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 8
Sparse matrix
An m x n matrix is said to be sparse if “many” of its elements are zero.
A matrix that is not sparse is called a dense matrix.
We can device a simple representation scheme whose space requirement
equals the size of the non-zero elements.
-Column
-Column
-Column
-Column
Column
Column
Column
Column
-8
-3
-5
7
6
4
-1
2
Row - 1 0 0 0 2 0 0 1 0 Terms 0 1 2 3 4 5 6 7 8
Row 1 1 2 2 2 3 3 4 4
Row - 2 0 6 0 0 7 0 0 3
Colum 4 7 2 5 8 4 6 2 3
Row - 3 0 0 0 9 0 8 0 0
n 2 1 6 7 3 9 8 4 5
Row - 4 0 4 5 0 0 0 0 0 Value
Linear Representation of given
4x8
matrix
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 9
Sparse matrix Cont…
To construct matrix structure from liner representation we need to record.
Original row and columns of each non zero entries.
Number of rows and columns in the matrix.
So each element of the array into which the sparse matrix is mapped need
to have three fields: row, column and value
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 10
Sparse matrix Cont…
1 2 3 4 5 6 7
Linear representation of Matrix
0 0 6 0 9 0 0 Row Colum A
2 0 0 7 8 0 4 n
1 3 6
A= 10 0 0 0 0 0 0
0 0 12 0 0 0 0 1 5 9
0 0 0 0 0 0 0 2 1 2
0 0 0 3 0 0 5 2 4 7
6x7
2 5 8
Memory Space required to 4
2 7
store
6x7 matrix 3 1 10
42 x 2 = 84 bytes
4 3 12
6 4 3
Memory Space required to
store 6 7 5
Linear
30 x 2Representation
= 60 bytes Space Saved = 84 – 60 = 24
bytes
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 11
Sparse matrix Cont…
Linear Representation of Matrix Linear Representation of Matrix
Row Colum A Colum A
n n
1 6 1 6
3 3
1 9 2 9
5 Row 5
2 2 3 2
1 1 1 1
2 7 4 7
4 2 3 4
2 8 5 8
5 3 7 5
2 4 6 4
7 4 8 7
3 10 7 10
1 5 0 1
4 12 8 12
3 6 9 3
6 3 9 3
4 4
6 5 1 5
7 0 7
Memory Space required to store Liner Representation = 26 x 2 = 42
bytes
#3130702 (DS) Unit 2 – Linear Data Structure
Dr. Pradyumansinh U. Jadeja 12
Data Structures (DS)
GTU # 3130702
Thank
You
Dr. Pradyumansinh Jadeja
Computer Engineering
Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected] +91 9879461848