0% found this document useful (0 votes)
50 views5 pages

CSC-HC-3016 Data Structures Unit I

The document discusses various data structures including arrays, sparse matrices, and their representations. It provides information on: - Single and multidimensional arrays, including memory requirements and addressing formulas for 1D and 2D arrays. - Row-major and column-major ordering for representing arrays in linear storage. - Sparse matrices, which have many zero entries, and representing them efficiently using a 3-tuple format listing the row, column, and value of non-zero elements.

Uploaded by

CSC LCB
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)
50 views5 pages

CSC-HC-3016 Data Structures Unit I

The document discusses various data structures including arrays, sparse matrices, and their representations. It provides information on: - Single and multidimensional arrays, including memory requirements and addressing formulas for 1D and 2D arrays. - Row-major and column-major ordering for representing arrays in linear storage. - Sparse matrices, which have many zero entries, and representing them efficiently using a 3-tuple format listing the row, column, and value of non-zero elements.

Uploaded by

CSC LCB
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/ 5

Unit-wise class notes

UNIT 1: Arrays (5 Lectures)


Single and Multidimensional Arrays, Sparse Matrices (Array and Linked
Representation),Insertion and Deletion of elements from an Array and Amount
of Work Done

Introduction to Data Structures:

Data structure is a representation of logical relationship existing between


individual elements of data. In other words, a data structure defines a way of
organizing all data items that considers not only the elements stored but also
their relationship to each other. The term data structure is used to describe the
way data is stored.

To develop a program of an algorithm we should select an appropriate data


structure for that algorithm. Therefore, data structure is represented as:
Algorithm + Data structure = Program

A data structure is said to be linear if its elements form a sequence or a linear


list. The linear data structures like an array, stacks, queues and linked lists
organize data in linear order. A data structure is said to be non linear if its
elements form a hierarchical classification where, data items appear at various
levels.

Data structures are divided into two types:


• Primitive data structures.
• Non-primitive data structures.
Primitive Data Structures are the basic data structures that directly operate
upon the machine instructions. They have different representations on different
computers. Integers, floating point numbers, character constants, string
constants and pointers come under this category.
Non-primitive data structures are more complicated data structures and are
derived from primitive data structures. They emphasize on grouping same or
different data items with relationship between each data item. Arrays, lists and
files come under this category. Figure shows the classification of data
structures.
Data structures: Organization of data
The collection of data you work with in a program have some kind of structure
or organization.
No matter how complex data structures are they can be broken down into two
fundamental types:
• Contiguous
• Non-Contiguous.
In contiguous structures, terms of data are kept together in memory (either
RAM or in a file). An array is an example of a contiguous structure. Since each
element in the array is located next to one or two other elements. In contrast,
items in a non-contiguous structure and scattered in memory, but we linked to
each other in some way. A linked list is an example of a non-contiguous data
structure.

Array: The simplest form of array is a one-dimensional array that may be


defined as a finite ordered set of homogeneous elements, which is stored in
contiguous memory locations. For example, an array may contain all integers
or all characters or any other data type, but may not contain a mix of data
types. The amount of storage required to hold an array is directly related to its
type and size. For a single dimension array, the total size in bytes required for
the array is computed as shown below.
Memory required (in bytes) = size of (data type) X length of array

The first array index value is referred to as its lower bound and in most of the
programming languages it is always 0 and the maximum index value is called
its upper bound. The number of elements in the array, called its range is given
by upper bound-lower bound.

To find the address of an element in an one dimensional array the following


formula is use

Address of A[I] = B + W * (I – LB)

I = Index of element whose address to be found,


B = Base address,
W = Storage size of one element store in any array(in byte),
LB = Lower Limit/Lower Bound of subscript(If not specified assume zero)

2-dimensional array: 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 a
number of rows and N is a number of columns. The schematic of a two-
dimensional array of size 3 × 5 is shown

Row 0 → a[0][0] a[0][1] a[0][2] a[0][3] A[0][4]

Row1 → a[1][0] a[1][1] a[1][2] a[1][3] A[1][4]

Row2 → a[2][0] a[2][1] a[2][2] a[2][3] A[2][4]

In the case of a two-dimensional array, the following formula yields the number
of bytes of memory needed to hold it:
bytes = size of 1st index × size of 2nd index × size of (base type)

REPRESENTATION OF ARRAYS

In computing, row-major order and column-major order describe methods for


arranging multidimensional arrays in linear storage such as memory.

In row-major order, consecutive elements of the rows of the array are


contiguous in memory; in column-major order, consecutive elements of the
columns are contiguous.

The difference between row-major and column-major order is simply that the
order of the dimensions is reversed. Equivalently, in row-major order the
rightmost indices vary faster as one steps through consecutive memory
locations, while in column-major order the leftmost indices vary faster.

The schematic of row major representation of an Array is shown below


a[0][0] a[0][1] a[0][2]

a[1][0] a[1][1] a[1][2]

Row Major ordering Column major ordering


Address value Address Value
0 a[0][0] 0 a[0][0]
1 a[0][1] 1 a[1][0]
2 a[0][2] 2 a[0][1]
3 a[1][0] 3 a[1][1]
4 a[1][1] 4 a[0][2]
5 a[1][2] 5 a[1][2]

Address translation function for row-major ordering is

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.

Address translation function for column-major ordering is

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.

SPARSE MATRICES
Matrices with good number of zero entries are called sparse matrices.
Triangular matrices are sparse matrices. A tridiagonal matrix is a square
matrix in which all the elements except for the main diagonal, diagonals on the
immediate upper and lower side are zeroes. Tridiagonal matrices are also
sparse matrices.

Let us consider a sparse matrix from storage point of view. Suppose that the
entire sparse matrix is stored. Then, a considerable amount of memory which
stores the matrix consists of zeroes. This is nothing but wastage of memory. In
real life applications, such wastage may count to megabytes. So, an efficient
method of storing sparse matrices has to be looked into.

A common way of representing non zero elements of a sparse matrix is the 3-


tuple form. The first row of sparse matrix always specifies the number of rows,
number of columns and number of non zero elements in the matrix. Each non
zero element is stored from the second row, with the 1st and 2nd elements of
the row, indicating the row number and column number respectively in which
the element is present in the original matrix. The 3rd element in this row stores
the actual value of the non zero element.

0 0 0 0 0 0 0 4
For example, the 3- tuple representation of the matrix6 0 0 0 0 0 0 0 is
0 3 0 0 00 0 1
shown below:

3 8 4
0 7 4
1 0 6
2 1 3
2 7 1

You might also like