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
Outline
Looping
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 lower bound of “one” is represented as below….
L • L0 is the address of the first word allocated to the first element of vector A
0
i-1 • C words is size of each element or node
L0+(i-1)C • The address of element Ai is
Loc(Ai) = L0 + (C*(i-1))
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))
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 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 stored sequentially by
columns : A[1,1], A[1,2], A[1,3], A[1,4], A[2,4]
A[2,1], A[2,2], A[2,3],
Col-1 Col-2 Col-3 Col-4
Row 1 [1,1] [1,2] [1,3] [1,4]
Row 2 [2,1] [2,2] [2,3] [2,4]
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 4
Column major order matrix
Col-1 Col-2 Col-3 Col-4
Row 1 [1,1] [1,2] [1,3] [1,4]
Row 2 [2,1] [2,2] [2,3] [2,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)
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 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 columns
b1 [1,1] [1,2] [1,3] [1,m]
b1 = lower bound subscript of row
[2,1] [2,2] [2,3] [2,m] u1 = upper bound subscript of row
n = u1 – b1 + 1
b2 = lower bound subscript of column
u1 [n,1] [n,2] [n,3] [n,m] u2 = upper bound subscript of column
nxm m = u2 – b2 + 1
• The address element A [ i , j ] is given by
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)
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 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.
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 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
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 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.
7Column -
2Column -
4Column -
6Column -
5 olumn -
3 olumn -
Column -
Column -
C
C
8
1
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
Column 4 7 2 5 8 4 6 2 3
Row - 3 0 0 0 9 0 8 0 0 Value 2 1 6 7 3 9 8 4 5
Row - 4 0 4 5 0 0 0 0 0
Linear Representation of given matrix
4x8
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 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
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 10
Sparse matrix Cont…
1 2 3 4 5 6 7
Linear representation of Matrix
0 0 6 0 9 0 0 Row Column A
2 0 0 7 8 0 4
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 store 4
2 7
6x7 matrix
3 1 10
42 x 2 = 84 bytes
4 3 12
6 4 3
Memory Space required to store
Linear Representation 6 7 5
30 x 2 = 60 bytes Space Saved = 84 – 60 = 24 bytes
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 11
Sparse matrix Cont…
Linear Representation of Matrix Linear Representation of Matrix
Row Column A Column A
1 3 6 1 3 6
1 5 9 2 5 9
Row
2 1 2 3 1 2
1 1
2 4 7 4 4 7
2 3
2 5 8 5 5 8
3 7
2 7 4 6 7 4
4 8
3 1 10 7 1 10
5 0
4 3 12 8 3 12
6 9
6 4 3 9 4 3
6 7 5 10 7 5
Memory Space required to store Liner Representation = 26 x 2 = 42 bytes
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 2 – Linear Data Structure (Array) 12
Data Structures (DS)
GTU # 3130702
Thank
You
Dr. Pradyumansinh Jadeja
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected] +91 9879461848