Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Array

Outline

• Basic Concepts

• One Dimensional Array

• Multi-dimensional Array

• Sparse Matrix

• Elementary Operations
Array

• An array is a set of pairs, index and value.

• For each index which is defined, there is a value associated with that
index.

• In mathematical terms, we call this a correspondence or a mapping.


Operations on Array
The operations one normally performs on any linear structure, whether it be
an array or list, include the following:

• Traversal: Processing each element in the array (list).


• Search: Finding the location of the element with a given value or the record
with key.
• Insertion: Adding a new element to the array (list).
• Deletion: Removing an element from the array (list).
• Sorting: Arranging the elements in some type of order.
• Merging: Combining two arrays (lists) into a single list.
Traversing a Linear Array

Here 𝐿𝐴 is a linear array with lower bound 𝐿𝐵 and upper bound 𝑈𝐵.
This algorithm traverses 𝐿𝐴 applying an operation 𝑃𝑅𝑂𝐶𝐸𝑆𝑆 to each
element of 𝐿𝐴.

1. [𝐼𝑛𝑖𝑡𝑖𝑎𝑙𝑖𝑧𝑒 𝑐𝑜𝑢𝑛𝑡𝑒𝑟] 𝑆𝑒𝑡 𝐾: = 𝐿𝐵


2. 𝑹𝒆𝒑𝒆𝒂𝒕 𝑆𝑡𝑒𝑝𝑠 3 𝑎𝑛𝑑 4 𝒘𝒉𝒊𝒍𝒆 𝐾 ≤ 𝑈𝐵
3. [𝑣𝑖𝑠𝑖𝑡 𝑒𝑙𝑒𝑚𝑒𝑛𝑡] 𝐴𝑝𝑝𝑙𝑦 𝑷𝑹𝑶𝑪𝑬𝑺𝑺 𝑡𝑜 𝐿𝐴[𝐾]
4. [𝐼𝑛𝑐𝑟𝑒𝑎𝑠𝑒 𝑐𝑜𝑢𝑛𝑡𝑒𝑟] 𝑆𝑒𝑡 𝐾: = 𝐾 + 1
[𝐸𝑛𝑑 𝑜𝑓 𝑆𝑡𝑒𝑝 2 𝑙𝑜𝑜𝑝]
5. 𝐸𝑥𝑖𝑡
Insertion and Deletion
• Let 𝐴 be a collection of data elements in the memory of the computer.

• Insertion refers to the operation of adding another element to the


collection 𝐴.

• Deletion refers to the operation of removing one of the elements from


𝐴.

• Inserting an element at the “end” of a linear array can be easily done


provided the memory space allocated for the array is large enough to
accommodate the additional element.
Insertion and Deletion
• If, we need to insert an element in the middle of the array then, on the
average, half of the elements must be moved downward to new
locations to accommodate the new element and keep the order of the
other elements.

• Similarly, deleting an element at the “end” of an array presents no


difficulties, but deleting an element somewhere in the middle of the
array would require the each subsequent element be moved one
location upward in order to “fill up” the array.
Insertion and Deletion
• Since, linear arrays are usually pictured extending downward, the term
“downward” refers to locations with larger subscripts, and the term
“upward” refers to locations with smaller subscripts.

• Suppose, NAME is an 8-element linear array, and five names are in the
array.

Observe that the names are


listed alphabetically, and we
want to keep the array names
alphabetical at all times.
Insertion and Deletion
• Suppose, Ford is added to the array. Then Johnson, Smith, and Wagner
must each be moved downward one location.
Insertion and Deletion
• Next, Taylor is added to the array; then Wagner must be moved
downward one location.
Insertion and Deletion
• If Davis is removed from the array, then the five names Ford, Johnson,
Smith, Taylor, and Wagner must each be moved upward one location.
Insertion into a Linear Array
𝑰𝑵𝑺𝑬𝑹𝑻 (𝑳𝑨, 𝑵, 𝑲, 𝑰𝑻𝑬𝑴)
/∗ 𝐿𝐴 𝑖𝑠 𝑎 𝑙𝑖𝑛𝑒𝑎𝑟 𝑎𝑟𝑟𝑎𝑦 𝑤𝑖𝑡𝑕 𝑁 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑎𝑛𝑑 𝐾 𝑖𝑠 𝑎 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑠𝑢𝑐𝑕 𝑡𝑕𝑎𝑡
𝐾 ≤ 𝑁. 𝑇𝑕𝑖𝑠 𝑎𝑙𝑔𝑜𝑟𝑖𝑡𝑕𝑚 𝑖𝑛𝑠𝑒𝑟𝑡𝑠 𝑎𝑛 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝐼𝑇𝐸𝑀 𝑖𝑛𝑡𝑜 𝑡𝑕𝑒 𝐾 𝑡ℎ 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛 𝑖𝑛 𝐿𝐴. ∗/

1. [𝐼𝑛𝑖𝑡𝑖𝑎𝑙𝑖𝑧𝑒 𝑐𝑜𝑢𝑛𝑡𝑒𝑟] 𝑆𝑒𝑡 𝐽: = 𝑁


2. 𝑹𝒆𝒑𝒆𝒂𝒕 𝑆𝑡𝑒𝑝𝑠 3 𝑎𝑛𝑑 4 𝒘𝒉𝒊𝒍𝒆 𝐽 ≥ 𝐾
3. [𝑀𝑜𝑣𝑒 𝐽𝑡ℎ 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑑𝑜𝑤𝑛𝑤𝑎𝑟𝑑] 𝑆𝑒𝑡 𝐿𝐴[𝐽 + 1]: = 𝐿𝐴[𝐽]
4. 𝐷𝑒𝑐𝑟𝑒𝑎𝑠𝑒 𝑐𝑜𝑢𝑛𝑡𝑒𝑟 𝑆𝑒𝑡 𝐽: = 𝐽 − 1
5. [𝐸𝑛𝑑 𝑜𝑓 𝑆𝑡𝑒𝑝 2 𝑙𝑜𝑜𝑝]
6. [𝐼𝑛𝑠𝑒𝑟𝑡 𝑒𝑙𝑒𝑚𝑒𝑛𝑡] 𝑆𝑒𝑡 𝐿𝐴[𝐾]: = 𝐼𝑇𝐸𝑀
7. [𝑅𝑒𝑠𝑒𝑡 𝑁] 𝑆𝑒𝑡 𝑁: = 𝑁 + 1
8. 𝐸𝑥𝑖𝑡
Deletion on Linear Array
𝑫𝑬𝑳𝑬𝑻𝑬 𝑳𝑨, 𝑵, 𝑲, 𝑰𝑻𝑬𝑴
/∗ 𝐿𝐴 𝑖𝑠 𝑎 𝑙𝑖𝑛𝑒𝑎𝑟 𝑎𝑟𝑟𝑎𝑦 𝑤𝑖𝑡𝑕 𝑁 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑎𝑛𝑑 𝐾 𝑖𝑠 𝑎 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑠𝑢𝑐𝑕 𝑡𝑕𝑎𝑡
𝐾 ≤ 𝑁. 𝑇𝑕𝑖𝑠 𝑎𝑙𝑔𝑜𝑟𝑖𝑡𝑕𝑚 𝑑𝑒𝑙𝑒𝑡𝑒𝑠 𝑡𝑕𝑒 𝐾 𝑡ℎ 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑓𝑟𝑜𝑚 𝐿𝐴. ∗/

1. 𝑆𝑒𝑡 𝐼𝑇𝐸𝑀: = 𝐿𝐴[𝐾]


2. 𝑹𝑬𝑷𝑬𝑨𝑻 𝒇𝒐𝒓 𝐽 = 𝐾 𝑡𝑜 𝑁 − 1:
3. 𝑀𝑜𝑣𝑒 (𝐽 + 1)𝑠𝑡 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 𝑢𝑝𝑤𝑎𝑟𝑑. 𝑆𝑒𝑡 𝐿𝐴 𝐽 = 𝐿𝐴[𝐽 + 1]
4. [𝐸𝑛𝑑 𝑜𝑓 𝑙𝑜𝑜𝑝]
5. [𝑅𝑒𝑠𝑒𝑡 𝑡𝑕𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑁 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑖𝑛 𝐿𝐴] 𝑆𝑒𝑡 𝑁: = 𝑁 − 1
6. 𝐸𝑥𝑖𝑡
Storage Representations
• Each programming language has its own rules for declaring
multidimensional arrays. (As is the case with linear arrays, all element
in such arrays must be of same data type.)

• Suppose, DATA is a two-dimensional 4 × 8 array with elements of the


real type. C language would declare such as array as follows:

𝑓𝑙𝑜𝑎𝑡 𝐷𝐴𝑇𝐴 [4][8];


Row-major Representation
• We can consider a two-dimensional array as a one-dimensional array
since it has elements with a single dimension.

• As a result of this, a two-dimensional array can be assumed as a single


column with many rows and mapped sequentially. Such representation
is called row-major representation.

• 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,

• The total number of rows present before 𝑚𝑡ℎ row


= 𝑚 − 𝑙𝑏1 where 𝑙𝑏1 is a first dimensional lower bound

• Size of a row = total number of elements present in a row × size of an element


Row-major Representation

• Total number of elements in a row is calculated as:

𝑢𝑏2 − 𝑙𝑏2 + 1 , 𝑢𝑏2 and 𝑙𝑏2 are the second dimensional upper and lower
bounds.

• Generalized form:

𝑎𝑑𝑑𝑟(𝑎[𝑚, 𝑛]) = ((𝑚 − 𝑙𝑏1) × (𝑢𝑏2 − 𝑙𝑏2 + 1) ×size) + ((𝑛 − 𝑙𝑏2) ×size)

„Size‟ = Size of an element


Column-major Representation
• We can also represent a two-dimensional array as one single row of
columns and map it sequentially. Such a representation is called a column-
major representation.
Column-major Representation

• We can calculate the address of the element of the 𝑚𝑡ℎ row and the 𝑛𝑡ℎ
column as follows:

• 𝑎𝑑𝑑𝑟(𝑎[𝑚, 𝑛]) = (Total number of columns present before 𝑛𝑡ℎ column ×


column size)
+
(Total number of elements present before the
𝑚𝑡ℎ element of the 𝑛𝑡ℎ column × each element size)
Column-major Representation
• Columns which are placed before 𝑛𝑡ℎ column = (𝑛 − 𝑙𝑏2) where 𝑙𝑏2 is a
second dimensional lower bound.

• Column size = Total number of elements present in a column × element size

• 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 𝐾.

𝐿𝑂𝐶(𝐿𝐴[𝐾]) = 𝐵𝑎𝑠𝑒(𝐿𝐴) + 𝑤(𝐾 − 1)

• 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.

• Although 𝐴 is pictured as a rectangular array of elements with 𝑚 rows and


𝑛 columns, the array will be represented in a memory by a block of 𝑚 × 𝑛
sequential memory locations. Specifically, the programming language will
store the array 𝐴 either:

1. Row by row, i.e., row-major order


2. Column by column, i.e., column-major order
Representation of Two-dimensional Array in Memory

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:

(Row-major order) 𝐿𝑂𝐶(𝐴[𝐽, 𝐾]) = 𝐵𝑎𝑠𝑒(𝐴) + 𝑤[𝑛(𝐽 − 1) + (𝐾 − 1)]

(Column-major order) 𝐿𝑂𝐶(𝐴[𝐽, 𝐾]) = 𝐵𝑎𝑠𝑒(𝐴) + 𝑤[𝑚(𝐾 − 1)] + (𝐽 − 1)]

𝑤 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

For column-major order ?


Sparse Matrix
• A general matrix consists of 𝑚 rows and 𝑛 columns of numbers.

• We write 𝑚 × 𝑛 to designate a matrix with 𝑚 rows and 𝑛 columns.


Such a matrix has 𝑚𝑛 elements. When 𝑚 = 𝑛, we call the matrix
square.

• It is very normal to store a matrix in two-dimensional array, say


𝐴 1: 𝑚, 1: 𝑛 . Then, we can work with any element by writing 𝐴 𝑖, 𝑗 .
Sparse Matrix
• The second matrix of the figure has many zero entries. Such a matrix
is called sparse.

• In second matrix, only 8 out of 36 entries are non-zero and that is


sparse!
Representation of Sparse Matrix
• A specific representation is used for sparse matrix which only stores the non-zero
elements.

• Each element of a matrix is uniquely characterized by its row and column


position, say (𝑖, 𝑗). Matrix is stored as a list of 3-tuples of the form (𝑖, 𝑗, 𝑣𝑎𝑙𝑢𝑒).

• Sparse matrix 𝐴(0: 𝑡, 1: 3) where 𝑡 = 8 is the number of non-zero terms.

• 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.

• In transpose operation, we move the elements so that the element in


the 𝑖, 𝑗 position gets put in the 𝑗, 𝑖 position, i.e., interchanging row and
column.

• The elements of diagonal will remain unchanged, since 𝑖 = 𝑗.

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

Transposed of the Sparse matrix


Sparse Matrix Operations
𝑷𝒓𝒐𝒄𝒆𝒅𝒖𝒓𝒆 𝑻𝑹𝑨𝑵𝑺𝑷𝑶𝑺𝑬 (𝑨, 𝑩)
/∗ 𝐴 𝑖𝑠 𝑎 𝑚𝑎𝑡𝑟𝑖𝑥 𝑟𝑒𝑝𝑟𝑒𝑠𝑒𝑛𝑡𝑒𝑑 𝑖𝑛 𝑠𝑝𝑎𝑟𝑠𝑒 𝑓𝑜𝑟𝑚, 𝐵 𝑖𝑠 𝑠𝑒𝑡 𝑡𝑜 𝑏𝑒 𝑖𝑡𝑠 𝑡𝑟𝑎𝑛𝑠𝑝𝑜𝑠𝑒.∗/
1. (𝑚, 𝑛, 𝑡) ← 𝐴 0,1 , 𝐴 0,2 , 𝐴 0,3
2. (𝐵(0,1), 𝐵(0,2), 𝐵(0,3)) ← (𝑛, 𝑚, 𝑡)
3. 𝒊𝒇 𝑡 ≤ 0 𝑡𝑕𝑒𝑛 𝒓𝒆𝒕𝒖𝒓𝒏 // 𝑐𝑕𝑒𝑐𝑘 𝑓𝑜𝑟 𝑧𝑒𝑟𝑜 𝑚𝑎𝑡𝑟𝑖𝑥
4. 𝑞 ← 1 //𝑞 𝑖𝑠 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛 𝑜𝑓 𝑛𝑒𝑥𝑡 𝑡𝑒𝑟𝑚 𝑖𝑛 𝐵
5. 𝒇𝒐𝒓 𝑐𝑜𝑙 ← 1 𝑡𝑜 𝑛 𝑑𝑜 //𝑡𝑟𝑎𝑛𝑠𝑝𝑜𝑠𝑒 𝑏𝑦 𝑐𝑜𝑙𝑢𝑚𝑛𝑠
6. 𝒇𝒐𝒓 𝑝 ← 1 𝑡𝑜 𝑡 𝑑𝑜 //𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛𝑜𝑛𝑧𝑒𝑟𝑜 𝑡𝑒𝑟𝑚𝑠 𝑑𝑜
7. 𝒊𝒇 𝐴(𝑝, 2) = 𝑐𝑜𝑙 //𝑐𝑢𝑟𝑟𝑒𝑛𝑡𝑐𝑜𝑙𝑢𝑚𝑛
8. 𝒕𝒉𝒆𝒏 [(𝐵(𝑞, 1), 𝐵(𝑞, 2), 𝐵(𝑞, 3)) ←
9. (𝐴(𝑝, 2), 𝐴(𝑝, 1), 𝐴(𝑝, 3))] // insert 𝑛𝑒𝑥𝑡 𝑡𝑒𝑟𝑚 𝑜𝑓 𝐵
10. 𝑞 ←𝑞+1
11. 𝑒𝑛𝑑
12. 𝑒𝑛𝑑
13. 𝑒𝑛𝑑 𝑇𝑅𝐴𝑁𝑆𝑃𝑂𝑆𝐸 Time Complexity = 𝑶(𝒏𝒕)
Sparse Matrix Operations
• For each iteration of the loop of lines 5-12, the if clause of line 7 is tested 𝑡 times. Since,
the number of iterations of the loops of lines 5-12 is 𝑛, the total time for line 7 becomes
𝑛𝑡.

• The assignment in lines 8-10 takes place exactly 𝑡 times as there are only 𝑡 non-zero
terms in the sparse matrix being generated.

• The lines 1-4 take a constant amount of time.

• The total time for the algorithm is therefore 𝑂(𝑛𝑡).

• 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

𝑐𝑖𝑗 = 𝑎𝑖𝑘 𝑏𝑘𝑗


1≤𝑘≤𝑛

For 1 ≤ 𝑖 ≤ 𝑚 and 1 ≤ 𝑗 ≤ 𝑝, the product of two sparse matrices may


no longer be sparse, for instance,

You might also like