Unit - 2
Array
Linear Data Structure
Data and File 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 – 2: Linear Data Structure - 1 2
Classification of Data Structure
Unit – 2: Linear Data Structure - 1 3
Non-primitive Data Structures
Array : Array is an ordered set which consists of fixed number of
objects.
List: List is an ordered set consisting of a variable number of
elements to which insertions and deletions can be made.
File: File is typically a large list that is stored in a external memory
of a computer.
Unit – 2: Linear Data Structure - 1 4
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 – 2: Linear Data Structure - 1 5
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.
Unit – 2: Linear Data Structure - 1 6
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]
Address of data element, A[k] = BA(A) + w( k – lower_bound)
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 – 2: Linear Data Structure - 1 7
One Dimensional Array
99 67 78 56 88 90 34 85
Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
1000 1002 1004 1006 1008 1010 1012 1014
Address(Marks[4]) = 1000 + 2(4 – 0) Address(Marks[1]) = 1000 + 2(1 – 0)
= 1000 + 2(4) = 1000 + 2(1)
= 1008 = 1002
Length = upper_bound – lower_bound + 1
Where upper_bound is the index of the last element
and lower_bound is the index of the first element in the array
Here,
lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8
Unit – 2: Linear Data Structure - 1 8
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
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))
Unit – 2: Linear Data Structure - 1 9
Two Dimensional Array
A two dimensional array is specified using two subscripts where one
subscript denotes row and the other denotes column.
A two dimensional array is declared as:
data_type array_name[row_size][column_size];
Therefore, a two dimensional mn array is an array that contains m n
data elements and each element is accessed using two subscripts, i and j
where i<=m and j<=n
int marks[3][5];
Col 0 Col 1 Col2 Col 3 Col 4
Rows/Columns
Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4]
Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4]
Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4]
Unit – 2: Linear Data Structure - 1 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]
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]
Unit – 2: Linear Data Structure - 1 11
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)
Unit – 2: Linear Data Structure - 1 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
u1 [n,1] [n,2] [n,3] [n,m] b2 = lower bound subscript of column
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)
Unit – 2: Linear Data Structure - 1 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
Address(A[I][J]) = Base_Address + w{n (i - 1) + (j - 1)}, if the array
elements are stored in row major order.
Where w is the number of words stored per memory location
m is the number of columns
n is the number of rows
i and j are the subscripts of the array element
Unit – 2: Linear Data Structure - 1 14
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
Unit – 2: Linear Data Structure - 1 15
Representation of Polynomial equation
Y Y2 Y3 Y4
X XY XY2 XY3 XY4
X2 X2 Y X2Y2 X2Y3 X2Y4
X3 X3 Y X3Y2 X3Y3 X3Y4
X4 X4 Y 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 0
1 01 0 0
X 0 05 0 0 0 X 0
-1 30 0 0 0
X2 02 0 0 0 0 X2 1
0 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
Unit – 2: Linear Data Structure - 1 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 – 2: Linear Data Structure - 1 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 – 2: Linear Data Structure - 1 18
Sparse matrix Cont…
1 2 3 4 5 6 7 Linear representation of Matrix
1 0 0 6 0 9 0 0
2 2 0 0 7 8 0 4
Row Column A
3 10 0 0 0 0 0 0
A= 1 3 6
4 0 0 12 0 0 0 0
5 0 0 0 0 0 0 0 9
1 5
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
Memory Space required to store 6 4 3
Linear Representation 6 7 5
30 x 2 = 60 bytes
Space Saved = 84 – 60 = 24 bytes
Unit – 2: Linear Data Structure - 1 19
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 = 52 bytes
Unit – 2: Linear Data Structure - 1 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 – 2: Linear Data Structure - 1 21
▪ Some Useful Links:
https://www.youtube.com/watch?v=IxFChpxFztg
Unit – 2: Linear Data Structure - 1 22