Professional Documents
Culture Documents
Array
Array
Outline
• Basic Concepts
• Multi-dimensional Array
• Sparse Matrix
• Elementary Operations
Array
• For each index which is defined, there is a value associated with that
index.
Here 𝐿𝐴 is a linear array with lower bound 𝐿𝐵 and upper bound 𝑈𝐵.
This algorithm traverses 𝐿𝐴 applying an operation 𝑃𝑅𝑂𝐶𝐸𝑆𝑆 to each
element of 𝐿𝐴.
• Suppose, NAME is an 8-element linear array, and five names are in the
array.
• We can calculate the address of the element of the 𝑚𝑡ℎ row and the
𝑛𝑡ℎ column in a two-dimensional array 𝑎[𝑙𝑏1: 𝑢𝑏1, 𝑙𝑏2: 𝑢𝑏2] using
the formula:
Row-major Representation
Row-major Representation
• 𝑎𝑑𝑑𝑟(𝑎[𝑚, 𝑛]) = (total number of rows present before the 𝑚𝑡ℎ row × size of
a row)
+
(total number of elements present before the 𝑛𝑡ℎ element in
the 𝑚𝑡ℎ row × size of element)
Here,
𝑢𝑏2 − 𝑙𝑏2 + 1 , 𝑢𝑏2 and 𝑙𝑏2 are the second dimensional upper and lower
bounds.
• Generalized form:
𝑎𝑑𝑑𝑟(𝑎[𝑚, 𝑛]) = ((𝑚 − 𝑙𝑏1) × (𝑢𝑏2 − 𝑙𝑏2 + 1) ×size) + ((𝑛 − 𝑙𝑏2) ×size)
• We can calculate the address of the element of the 𝑚𝑡ℎ row and the 𝑛𝑡ℎ
column as follows:
• Number of elements in a column= (𝑢𝑏1 − 𝑙𝑏1 + 1), where 𝑢𝑏1 is the first
dimensional upper bound and 𝑙𝑏1 is the first dimensional lower bound.
• Therefore,
𝑎𝑑𝑑𝑟(𝑎[𝑚, 𝑛]) = ((𝑛 − 𝑙𝑏2) × (𝑢𝑏1 − 𝑙𝑏1 + 1) × 𝑠𝑖𝑧𝑒) + ((𝑚 − 𝑙𝑏1) ×size)
Representation of One-dimensional Array in Memory
• For a linear array 𝐿𝐴, the computer does not keep track of the address
𝐿𝑂𝐶(𝐿𝐴[𝐾]) of every element 𝐿𝐴[𝐾] of 𝐿𝐴, but does keep track of
𝐵𝑎𝑠𝑒(𝐿𝐴), the address of the first element of 𝐿𝐴.
• The computer uses the formula to find the address of 𝐿𝐴[𝐾] in time
independent of 𝐾.
• Here, 𝑤 is the number of words per memory cell for the array 𝐿𝐴, and
1 is the lower bound of the index set of 𝐿𝐴.
Representation of Two-dimensional Array in Memory
• Let 𝐴 be a two-dimensional 𝑚 × 𝑛 array.
Array:
A[1:3, 1:4]
Representation of Two-dimensional Array in Memory
A similar situation also holds for any two-dimensional 𝑚 × 𝑛 array 𝐴. That is,
the computer keeps track of 𝐵𝑎𝑠𝑒(𝐴)- The address of the first element
𝐴[1,1] of 𝐴 and computes the address 𝐿𝑂𝐶(𝐴[𝐽, 𝐾]) of 𝐴[𝐽, 𝐾] using the
formula:
𝑤 denotes the number of words per memory location for the array 𝐴. Note
that the formulas are linear in 𝐽 and 𝐾, and that one can find the address
𝐿𝑂𝐶(𝐴[𝐽, 𝐾]) in time independent of 𝐽 and 𝐾.
Representation of Two-dimensional Array in Memory
Problem:
Consider a 25 × 4 matrix array 𝐴[1: 25,1: 4]. Suppose base address of 𝐴 is
200 and the word size is 4. Further, suppose the programming language
stores two-dimensional arrays using row-major order. Then calculate the
address of 𝐴[12,3].
Answer:
Address 𝐴 12,3 = 200 + 4 4 12 − 1 + 3 − 1 = 384
• The element 𝐴(0,1) and 𝐴(0,2) contain the number of rows and columns of the
matrix. 𝐴(0,3) contains the number of non-zero terms.
Representation
Sparse Matrix Operations
• Transpose operation can be applied over sparse matrix.
Transpose of a
sparse matrix
Transpose of a Sparse Matrix
[1] [2] [3]
row col value (1) For each row 𝑖, take element < 𝑖, 𝑗, 𝑣𝑎𝑙𝑢𝑒 > and
a[0] 6 6 8 store it in element < 𝑗, 𝑖, 𝑣𝑎𝑙𝑢𝑒 > of the transpose,
[1] 1 1 15 as shown below:
[2] 1 4 22
[3] 1 6 -15 1,1,15 → 1,1,15
[4] 2 2 11 1,4,22 → 4,1,22
[5] 2 3 3 1,6, −15 → 6,1, −15
[6] 3 4 -6 2,2,11 → 2,2,11
[7] 5 1 91
Move elements down very often.
[8] 6 3 28
Sparse matrix
Transpose of a Sparse Matrix
[1] [2] [3]
row col value
(2) For all element in column 𝑗, place element
b[0] 6 6 8
< 𝑖, 𝑗, 𝑣𝑎𝑙𝑢𝑒 > in element < 𝑗, 𝑖, 𝑣𝑎𝑙𝑢𝑒 >
[1] 1 1 15
[2] 1 5 91 The transpose of the sparse matrix is shown in table.
[3] 2 2 11
[4] 3 2 3
[5] 3 6 28
[6] 4 1 22
[7] 4 3 -6
[8] 6 1 -15
• The assignment in lines 8-10 takes place exactly 𝑡 times as there are only 𝑡 non-zero
terms in the sparse matrix being generated.
• The computing time is a little disturbing since, we know that in case the matrices had
been represented as two-dimensional arrays, we could have obtained the transpose of
𝑛 × 𝑚 matrix in time 𝑂 𝑛𝑚 .
• The 𝑂(𝑛𝑡) time for algorithm TRANSPOSE becomes 𝑂(𝑛2 𝑚) where 𝑡 is of the order of
𝑛𝑚.
Product of Sparse Matrices
For matrices 𝐴 and 𝐵, where 𝐴 is 𝑚 × 𝑛 and 𝐵 is 𝑛 × 𝑝, the product
matrix 𝐶 has dimension 𝑚 × 𝑝. It’s (𝑖, 𝑗) element is defined as