0% found this document useful (0 votes)
19 views15 pages

BMC205 DSAA Unit1 Array Notes

The document provides an overview of arrays in data structures, detailing their definitions, types (one-dimensional and multidimensional), and initialization methods. It explains memory storage techniques for 2D and 3D arrays, including Row Major Order and Column Major Order, along with formulas for calculating the address of elements in these arrays. Additionally, it includes examples for better understanding of the concepts presented.

Uploaded by

Shivam Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

BMC205 DSAA Unit1 Array Notes

The document provides an overview of arrays in data structures, detailing their definitions, types (one-dimensional and multidimensional), and initialization methods. It explains memory storage techniques for 2D and 3D arrays, including Row Major Order and Column Major Order, along with formulas for calculating the address of elements in these arrays. Additionally, it includes examples for better understanding of the concepts presented.

Uploaded by

Shivam Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Hindustan Institute of Management & Computer Studies

Data Structures & Analysis of Algorithms (BMC205)


Unit -1
(Array)

Definition

Array is a linear data structure that is a collection of data elements of same types. Arrays
are stored in contiguous memory locations. It is a static data structure with a fixed size.

There are mainly two types of the array:


 One Dimensional (1D) Array or Single Dimensional Array
 Multidimensional Array

One Dimensional Array:


 It is a list of the variable of similar data types.
 It allows random access and all the elements can be accessed with the help of their
index.
 The size of the array is fixed.
 For a dynamically sized array, vector can be used in C++.

Representation of 1D array:

Syntax
The general form of declaring 1-dimensional arrays is shown below:
type arr_name[size];
 type: Type of data to be stored in the array.
 arr_name: Name assigned to the array.
 size: Size of dimension.

Initialization of 1D Array
We can initialize a 1D array by using a list of values enclosed inside ‘{ }’ and separated by
a comma as shown in the example below:
int arr[4] = {0, 1 ,2 ,3 }
Multidimensional Array:
A multi-dimensional array can be defined as an array that has more than one dimension.
Having more than one dimension means that it can grow in multiple directions. Some
popular multidimensional arrays are 2D arrays and 3D arrays.

Syntax
The general form of declaring N-dimensional arrays is shown below:
type arr_name[size1][size2]….[sizeN];
 type: Type of data to be stored in the array.
 arr_name: Name assigned to the array.
 size1, size2,…, sizeN: Size of each dimension.

Size of Multidimensional Arrays


The total number of elements that can be stored in a multidimensional array can be
calculated by multiplying the size of all dimensions. Consider the array arr[10][20]:
 The array int arr[10][20] can store total of (10*20) = 200 elements.
To get the size in bytes, we multiply the size of a single element (in bytes) by the total
number of elements in the array.
 The size of array int arr[10][20] = 10 * 20 * 4 = 800 bytes, where the size of int is
4 bytes.

Types of Multidimensional Arrays


In C, there can be many types of arrays depending on their dimensions but two of them are
most commonly used:
1. Two-Dimensional Array (2D Array)
2. Three-Dimensional Array (3D Array)

Two Dimensional Array:


 It is a list of lists of the variable of the same data type.
 It also allows random access and all the elements can be accessed with the help of their
index.
 It can also be seen as a collection of 1D arrays. It is also known as the Matrix.
 2D array is the most common multidimensional array.

Representation of 2D array:

Initialization of 2D Arrays
We can initialize a 2D array by using a list of values enclosed inside ‘{ }’ and separated by
a comma as shown in the example below:
int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
or
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};

Memory storage of 2D Arrays


As an array, the elements of the 2D array have to be stored contiguously in memory. As the
computers have linear memory addresses, the 2-D arrays must be linearized so as to enable
their storage. There are two ways to achieve this: Row Major Order and Column Major
Order.

Three-Dimensional Array:
A Three-Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It
can be visualized as multiple 2D arrays stacked on top of each other.

Initialization of 3D Array in C
Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number
of dimensions increases so the number of nested braces will also increase.
int arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11}
or
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4, 5 } },{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };

Memory storage of 3D Arrays


Like 2D arrays, the elements of a 3D array should also be stored contiguously in memory.
Representation of Arrays: Row Major Order and Column Major Order
When it comes to organizing and accessing elements in a multi-dimensional array, two
prevalent methods are Row Major Order and Column Major Order. These approaches
define how elements are stored in memory and impact the efficiency of data access in
computing.

1. Row Major Order: The technique stores the 2D array row after row. It means that the
first row is stored first in the memory, then the second row of the array, then the third
row, and so on.
2. Column Major Column: This technique stores the 2D array column after column. It
means that first column is stored first in the memory, then the second column, then the
third column, and so on.

Row Major Order vs Column Major Order


Aspect Row Major Order Column Major Order

Elements are stored row by Elements are stored column


row in contiguous by column in contiguous
Memory Organization locations. locations.

For a 2D array A[m][n]:


For the same array: [A[0][0],
[A[0][0], A[0][1], ..., A[m-
A[1][0], ..., A[m-1][n-1]]
Memory Layout Example 1][n-1]]

Moves through the entire Moves through the entire


row before progressing to column before progressing to
Traversal Direction the next row. the next column.

Efficient for row-wise Efficient for column-wise


access, less efficient for access, less efficient for row-
Access Efficiency column-wise access. wise access.

Commonly used in Commonly used in languages


Common Use Cases languages like C and C++. like Fortran.

Suitable for row-wise Suitable for column-wise


operations, e.g., image operations, e.g., matrix
Applications processing. multiplication.

Calculating the address of any element In the 1-D array:


A 1-dimensional array (or single-dimension array) is a type of linear array. Accessing its
elements involves a single subscript that can either represent a row or column index.

To find the address of an element in an array the following formula is used-


Address of A[Index] = B + W * (Index – LB)
Where:
 Index = The index of the element whose address is to be found (not the value of the
element).
 B = Base address of the array.
 W = Storage size of one element in bytes.
 LB = Lower bound of the index (if not specified, assume zero).

Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the size
of each element is 2 bytes in the memory, find the address of A[1700].
Solution:
Given:
 Base address (B) = 1020
 Lower bound (LB) = 1300
 Size of each element (W) = 2 bytes
 Index of element (not value) = 1700

Formula used: Address of A[Index] = B + W * (Index – LB)

Address of A[1700] = 1020 + 2 * (1700 – 1300)


= 1020 + 2 * (400)
= 1020 + 800
= 1820

Calculate the address of any element in the 2-D array using row-major and column-
major order:
The 2-dimensional array can be defined as an array of arrays. The 2-Dimensional arrays are
organized as matrices which can be represented as the collection of rows and columns as
array[M][N] where M is the number of rows and N is the number of columns.

Example:

To find the address of any element in a 2-Dimensional array, there are the following two
ways-
1. Row Major Order
2. Column Major Order

Row Major Order


Row major ordering assigns successive elements, moving across the rows and then down
the next row, to successive memory locations. In simple language, the elements of an array
are stored in a Row-Wise fashion.

To find the address of the element using row-major order uses the following formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))

I = Row Subset of an element whose address to be found,


J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix (If not given assume it as
zero),
LC = Lower Limit of column/start column index of the matrix (If not given assume it
as zero),
N = Number of column given in the matrix.

Example:
Given an array, arr[1………10][1………15] with base value 100 and the size of each
element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major
order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1 = 15 – 1
+ 1 = 15

Formula: Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))

Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))


= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
= 210

Column Major Order


If elements of an array are stored in a column-major fashion means moving across the
column and then to the next column then it’s in column-major order.

To find the address of the element using column-major order use the following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))

I = Row Subset of an element whose address to be found,


J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it
as zero),
M = Number of rows given in the matrix.

Example:
Given an array arr[1………10][1………15] with a base value of 100 and the size of each
element is 1 Byte in memory find the address of arr[8][6] with the help of column-major
order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1 = 10 – 1 +
1 = 10

Formula: Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))

Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))


= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
= 157

From the above examples, it can be observed that for the same position two different
address locations are obtained that’s because in row-major order movement is done across
the rows and then down to the next row, and in column-major order, first move down to the
first column and then next column. So both the answers are right.

So it’s all based on the position of the element whose address is to be found. For some
cases, same answers are obtained with row-major order and column-major order and for
some cases, different answers are obtained.

Calculate the address of any element in the 3-D Array using row-major and column-
major order:
A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using
three subscripts:
1. Index1
2. Index2
3. Index3
More dimensions in an array mean more data can be stored in that array.

Example:

To find the address of any element in 3-Dimensional arrays there are the following two
ways-
 Row Major Order
 Column Major Order

Find the length Li of each dimension ‘i’ of array by using the formula

Li = UBi – LB i + 1

Where LB and UB are lower and upper bounds

Now find the effective index Ei of Li for given index Ki by using the formula

Ei = Ki – LB i

Where K is the desired index for calculation

1. Row Major Order:


To find the address of the element using row-major order, use the following formula:
Address of A[i][j][k] = B + W * ((E1 * L2 + E2) * L3 + E3)
Here:
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
E1, E2 and E3 are effective index for L1, L2 and L3 respectively

Example: Given an array, arr[1:9, -4:1, 5:10] with a base value of 400 and the size of each
element is 2 Bytes in memory, find the address of element arr[5][-1][8] with the help of
row-major order?
Solution:
Given:
Base address B = 400
Storage size of one element store in any array(in Byte) W = 2
L1 = UB1 – LB1 + 1 = 9 – 1 + 1 = 9
L2 = UB2 – LB2 + 1 = 1 – (-4) + 1 = 6
L3 = UB3 – LB3 + 1 = 10 – (5) + 1 = 6
E1 = K1 – LB1 = 5 – 1 = 4
E2 = K2 – LB2 = (-1) – (-4) = 3
E3 = K3 – LB3 = 8 – 5 = 3
Formula used for row-major order:
Address of A[i][j][k] = B + W * ((E1 * L2 + E2) * L3 + E3)

Address of arr[5][-1][8] = 400 + 2 * ((4 * 6 + 3) * 6 + 3)


= 400 + 2 * (27 * 6 + 3)
= 400 + 2 * (165)
= 730

2. Column Major Order:


To find the address of the element using column-major order, use the following formula:
Address of A[i][j][k] = B + W * ((E3 * L2 + E2) * L1 + E1)
Here:
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
E1, E2 and E3 are effective index for L1, L2 and L3 respectivey

Example: Given an array arr[1:8, -5:5, -10:5] with a base value of 400 and the size of
each element is 4 Bytes in memory find the address of element arr[3][3][3] with the help
of column-major order?
Solution:
Given:
Base address B = 400
Storage size of one element store in any array(in Byte) W = 4
L1 = UB1 – LB1 + 1 = 8 – 1 + 1 = 8
L2 = UB2 – LB2 + 1 = 5 – (-5) + 1 = 11
L3 = UB3 – LB3 + 1 = 5 – (-10) + 1 = 16
E1 = K1 – LB1 = 3 – 1 = 2
E2 = K2 – LB2 = 3 – (-5) = 8
E3 = K3 – LB3 = 3 – (-10) = 13

Formula used for column-major order:


Address of A[i][j][k] = B + W * ((E3 * L2 + E2) * L1 + E1)

Address of arr[3][3][3] = 400 + 4 * ((13 * 11 + 8) * 8 + 2)


= 400 + 4 * (151 * 8 + 2)
= 400 + 4 * (1210)
= 400 + 4840
= 5240

Applications of Array Data Structure:


Arrays mainly have advantages like random access and cache friendliness over other data
structures that make them useful.
Some applications of arrays are.
 Storing and accessing data: Arrays store elements in a specific order and allow
constant-time O(1) access to any element.
 Searching: If data in array is sorted, we can search an item in O(log n) time. We can
also find floor(), ceiling(), kth smallest, kth largest, etc efficiently.
 Matrices: Two-dimensional arrays are used for matrices in computations like graph
algorithms and image processing.
 Implementing other data structures: Arrays are used as the underlying data structure
for implementing stacks and queues.
 Dynamic programming: Dynamic programming algorithms often use arrays to store
intermediate results of subproblems in order to solve a larger problem.
 Data Buffers: Arrays serve as data buffers and queues, temporarily storing incoming
data like network packets, file streams, and database results before processing.

Advantages of Array Data Structure:


 Efficient and Fast Access: Arrays allow direct and efficient access to any element in
the collection with constant access time, as the data is stored in contiguous memory
locations.
 Memory Efficiency: Arrays store elements in contiguous memory, allowing efficient
allocation in a single block and reducing memory fragmentation.
 Versatility: Arrays can be used to store a wide range of data types, including integers,
floating-point numbers, characters, and even complex data structures such as objects
and pointers.
 Compatibility with hardware: The array data structure is compatible with most
hardware architectures, making it a versatile tool for programming in a wide range of
environments.

Disadvantages of Array Data Structure:


 Fixed Size: Arrays have a fixed size set at creation. Expanding an array requires
creating a new one and copying elements, which is time-consuming and memory-
intensive.
 Memory Allocation Issues: Allocating large arrays can cause memory exhaustion,
leading to crashes, especially on systems with limited resources.
 Insertion and Deletion Challenges: Adding or removing elements requires shifting
subsequent elements, making these operations inefficient.
 Limited Data Type Support: Arrays support only elements of the same type, limiting
their use with complex data types.
 Lack of Flexibility: Fixed size and limited type support make arrays less adaptable
than structures like linked lists or trees.

Sparse Matrices and their representations


A matrix is a two-dimensional data object made of m rows and n columns, therefore having
total m x n values. If most of the elements of the matrix have 0 value, then it is called a
sparse matrix.

Use of Sparse Matrix instead of simple matrix


 Storage: There are lesser non-zero elements than zeros and thus lesser memory can be
used to store only those elements.
 Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements.
Example:
00304
00570
00000
02600

Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in


the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero
elements, we only store non-zero elements. This means storing non-zero elements
with triples- (Row, Column, value).

Sparse Matrix Representations can be done in two common representations:


1. Array representation
2. Linked list representation

Method 1: Using Arrays:


2D array is used to represent a sparse matrix in which there are three rows named as
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row, column)

Time Complexity: O(NM), where N is the number of rows in the sparse matrix, and M is
the number of columns in the sparse matrix.
Auxiliary Space: O(NM), where N is the number of rows in the sparse matrix, and M is the
number of columns in the sparse matrix.

Method 2: Using Linked Lists


In linked list, each node has four fields. These four fields are defined as:
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row, column)
 Next node: Address of the next node
Time Complexity: O(N*M), where N is the number of rows in the sparse matrix, and M is
the number of columns in the sparse matrix.
Auxiliary Space: O(K), where K is the number of non-zero elements in the array.

Operations on Sparse Matrix


Addition
In order to combine the matrices, a user can simply go over each element of each matrix, one
at a time, and insert the smaller element—the one with the smaller row and column values—
into the resulting matrix. If an element has the same column and row values as another
element, we add their values and insert the new data into the final matrix.

Transpose
Any row in the 1st matrix with a value equal to x and any row in the 2nd matrix (the
transposed one) with a value equal to y will contribute to the result[x][y]. The result [x][y] is
derived by multiplying all elements with col values in both matrices and only adding those
with row values of x in the original matrix and y in the 2nd transposed matrix.

Multiplication
To make our comparisons easier and keep the sorted order, we first calculate the transpose of
the 2nd matrix before multiplying the matrices. In order to obtain the resultant matrix, the
length of matrices must be traversed, and the relevant multiplied values must be added.

Types of Sparse Matrices


There are different variations of sparse matrices, which depend on the nature of the sparsity
of the matrices. Based on these properties, sparse matrices can be
o Regular sparse matrices
o Irregular sparse matrices / Non - regular sparse matrices

Regular sparse matrices


A regular sparse matrix is a square matrix with a well-defined sparsity pattern, i.e., non-zero
elements occur in a well-defined pattern. The various types of regular sparse matrices are:
o Lower triangular sparse matrices
o Upper triangular sparse matrices
o Tri-diagonal sparse matrices

Lower triangular sparse matrices


A Lower triangular sparse matrix is the one where all elements above the main diagonal are
zero value. The following matrix is a lower triangular sparse matrix.
Storing Lower triangular sparse matrices
In a lower triangular sparse matrix, the non-zero elements are stored in a 1-dimensional array
row by row.

For example: The 5 by 5 lower triangular sparse matrix as shown in the above figure is
stored in one-dimensional array B is:

B = { A11, A21, A22, A31, A32, A33, A41, A42, A43, A44, A51, A52, A53, A54, A55}

Here B [1] = A11, B [2] = A21, B [3] = A22, ……………………… B [14] = A54, B [15] = A55

To calculate the total number of non-zero elements, we need to know the number of non-zero
elements in each row and then add them. Since the number of non-zero elements in an ith row
is ‘i’ so that the total number of non-zero elements in the lower triangular sparse matrix
of n rows is:

1 + 2 + ………………… + i + ………………….. + (n-1) + n = n * (n+1) / 2

So B will contain only n * (n+1) / 2 elements which is about half as many elements
as a two dimensional n * n array.

Now we will want the formula that gives us the integer L in terms of J and K where B[L] =
AJK, where L represents the number of elements in the list up to and including AJK

Now there are 1 + 2 + ………… + (J-1) = (J-1) * J / 2 elements in the rows above
AJK and there are K elements in row J up to and including AJK.

( J  1) * J
L K
2
yields the index that accesses the value AJK from the linear array B.

Upper triangular sparse matrices


The Upper triangular sparse matrix is where all the elements below the main diagonal are
zero value. Following matrix is the Upper triangular sparse matrix.
Storing Upper triangular sparse matrices
In an upper triangular sparse matrix, the non-zero elements are stored in a 1-dimensional
array column by column.

For example, The 5 by 5 upper triangular sparse matrix, as shown in the above figure, is
stored in one-dimensional array B is:

B = {A11, A12, A22, A13, A23, A33, A14, A24, A34, A44, A15, A25, A35, A45, A55}

Here B [1] = A11, B [2] = A12, B [3] = A22, B [4] = A13,B [5] = A23 ,B [6] = A33, B [7] = A14 ,B
[8] = A24 ,B [9] = A34 ,B [10] = A44 ,B [11] = A15 ,B [12] = A25 ,B [13] = A35 , B [14] = A45, B
[15] = A55

In order to calculate the total number of non-zero elements, we need to know the number of
non-zero elements in each column and then add them. Since, the number of non-zero
elements in ith column is ‘i’ so that the total number of non-zero elements in upper triangular
sparse matrix of n columns is:

1 + 2 + ………………… + i + ………………….. + (n-1) + n = n * (n+1) / 2

Tri-diagonal sparse matrices


The tridiagonal sparse matrix where all non-zero elements lie on one of the three diagonals,
the main diagonal, above the main diagonal and below the main diagonal
Storing Tri-diagonal sparse matrices
In a tri-diagonal sparse matrix, all the non-zero elements are stored in a 1-dimensional array
row by row.

For example, The 5 by 5 tri-diagonal sparse matrix, as shown in the above figure, is stored in
one-dimensional array D is:

D = {A11, A12, A21, A22, A23, A32, A33, A34, A43, A44, A45, A54, A55}

Here D [1] = A11, D [2] = A12, D [3] = A21, D [4] = A22, D [5] = A23, D [6] = A32, D [7] =
A33, D [8] = A34, D [9] = A43, D [10] = A44, D [11] = A45, D [12] = A54, D [13] = A55

In order to calculate the total number of non-zero elements, we need to know the number of
non-zero elements along the diagonal, above the diagonal and below the diagonal. The
number of elements in a square matrix along the diagonal is 'n', above the diagonal is (n-1)
and below the diagonal is (n-1). So the total number of non-zero elements in n-square matrix
is:

n + (n-1) + (n-1)
the diagonal Above the diagonal Below the diagonal

Therefore, the total number of elements for n-square matrix = n + (n-1) + (n-1) = 3n-2

So D will contain only 3*n-2 elements.

Now we will want the formula that gives us the integer L in terms of J and K where D[L] =
AJK, where L represents the number of elements in the list up to and including AJK

Now there are 3 * (J - 2) + 2 elements in the rows above AJK and (K – J + 1) elements to the
left of AJK.

L = {3 * (J - 2) + 2} + (K – J + 1) + 1

L  2* J  K  2
yields the index that accesses the value AJK from the linear array D.

You might also like