Unit-2 Array Representation
Unit-2 Array Representation
Array
Linear Data Structure
Data
Data and
and File
File Structure
Structure
Topics to be covered
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
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 22
Classification of Data Structure
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 33
Non-primitive Data Structures
Array : Array is an ordered set which consists of fixed number of
objects.
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 44
One Dimensional Array
• An array is a collection of similar data elements.
• These data elements have the same data type.
• The elements of the array are stored in consecutive memory locations
and are referenced by an index (also known as the subscript).
• Declaring an array means specifying three things:
Data type - what kind of values it can store. For example, int, char, float
Name - to identify the array
Size- the maximum number of values that the array can hold
• Arrays are declared using the following syntax.
type name[size];
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 55
One Dimensional Array
Simplest data structure that makes use of computed address to
locate its elements is the one-dimensional array or vector.
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 66
One Dimensional Array
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
element element element element element element element element element element
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Here
A is the array
k is the index of the element of which we have to calculate the address
BA is the base address of the array A
w is the word size of one element in memory, for example, size of int is 2
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 77
One Dimensional Array
99 67 78 56 88 90 34 85
Here,
lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 88
One Dimensional Array (From textbook)
Vector A with subscript lower bound of “one” is represented as
below….
• L0 is the address of the first word
allocated to the first element of
L0
vector A
i-1 • C words is size of each element or
L0+(i-1)C node
• The address of element Ai is
A[i] Loc(Ai)=L0+(C*(i-1))
• 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))
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 99
Two Dimensional Array
A two dimensional array is specified using two subscripts where one
subscript denotes row and the other denotes column.
data_type array_name[row_size][column_size];
int marks[3][5];
Col 0 Col 1 Col2 Col 3 Col 4
Rows/Columns
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 10
10
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[2,1], A[1,2], A[2,2],
A[1,3], A[2,3], A[1,4], A[2,4]
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 11
11
Column major order matrix
Col-1 Col-2 Col-3 Col-4
Row 1 [1,1] [1,2] [1,3] [1,4]
Loc (A [ i , j ]) = L0 + (j-1)*n + (i – 1)
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 12
12
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]
[2,1] [2,2] [2,3] [2,m] b1 = lower bound subscript of row
u1 = upper bound subscript of row
n = u1 – b1 + 1
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 13
13
Overview of Two Dimensional Array
Address(A[i][j]) = Base_Address + w{m (j - 1) + (i - 1)}, if the array
elements are stored in column major order
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 14
14
Applications of Array
1. Symbol Manipulation (matrix representation of polynomial
equation)
2. Sparse Matrix
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 15
15
Representation of Polynomial equation
Y Y2 Y3 Y4
X XY XY2 XY3 XY4
X2 X2Y X2Y2 X2Y3 X2Y4
X3 X3Y X3Y2 X3Y3 X3Y4
X4 X4Y X4Y2 X4Y3 X4Y4
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 16
16
Sparse matrix
An mXn 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 - 8
Column - 3
Column - 5
Column - 7
Column - 6
Column - 4
Column - 1
Column - 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
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
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 17
17
Sparse matrix Cont…
To construct matrix structure from linear representation we need
to record
• Original row and columns of each non zero entries
• No 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
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 18
18
Sparse matrix Cont…
1 2 3 4 5 6 7 Linear representation of Matrix
1 0 0 6 0 9 0 0
2 0 0 7 8 0 4
Row Column A
2
3 10 0 0 0 0 0 0
A= 4 0 0 12 0 0 0 0 1 3 6
5 0 0 0 0 0 0 0
1 5 9
6 0 0 0 3 0 0 5
6x7 2 1 2
2 4 7
Memory Space required to store
6x7 matrix 2 5 8
2 7 4
42 x 2 = 84 bytes
3 1 10
4 3 12
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 19
19
Sparse matrix Cont…
Linear Representation of Matrix Linear Representation of Matrix
Row Column A Column A
1 3 6 1 3 6
5 5 9
1 9 Row 2
1 1 2
2 2 3
4 1 1 4
2 7 4 7
5 2 3 5
2 8 5 8
7 3 7 7
2 4 6 4
1 4 8 1
3 10 7 10
3 0 3
5
4 4 12 8 4 12
6 9
6 7 3 9 7 3
6 5 10 5
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 20
20
A super market conducting a study of the mix items purchased by
its customers.
For this study data are gathered for the purchase made by 1000
customers.
These data are organized into a matrix, purchases with
purchases(i,j) being the quantity of item i purchased by customer
j.
Suppose that the super market has an inventory of 10,000
different items.
The purchase matrix is therefore a 10,000 x 1,000 matrix
If the average customer buys 20 different items only about 20,000
of 1,00,000,000 matrix entries are nonzero
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 21
21
Some Useful Links:
https://www.youtube.com/watch?v=IxFChpxFztg
Unit
Unit –– 2:
2: Linear
Linear Data
Data Structure
Structure -- 11 22
22