0% found this document useful (0 votes)
379 views26 pages

Abstract Data Type (ADT) : & Array

The document discusses abstract data types (ADT) and arrays. It defines an ADT as a collection of data and operations on that data without specifying implementation details. Arrays are presented as an example ADT. The document also covers array basics, address calculation for one-dimensional and two-dimensional arrays, and applications of arrays such as matrix representation and polynomial manipulation.

Uploaded by

Adhara Mukherjee
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)
379 views26 pages

Abstract Data Type (ADT) : & Array

The document discusses abstract data types (ADT) and arrays. It defines an ADT as a collection of data and operations on that data without specifying implementation details. Arrays are presented as an example ADT. The document also covers array basics, address calculation for one-dimensional and two-dimensional arrays, and applications of arrays such as matrix representation and polynomial manipulation.

Uploaded by

Adhara Mukherjee
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/ 26

Abstract Data Type (ADT)

&
Array
Objective:
•Abstract Data Type (ADT)
• To understand approach of program design
•Understanding data abstraction
•Defining ADT
•Array
•To understand array as a sequential data structure
•To learn address calculation of array index positions
•To use array for representation of polynomials and sparse matrix
What to learn
• Abstract Data Type (ADT)
• Definition
• Example 1 – Complex number
• Example 2 – Array
• Array
• Basic
• Address calculation (Row major and Column major)
• Examples related to address calculation
• Application of Arrays
• Matrix Multiplication
• Sparse Polynomial Representation and Addition
The ADT
•From Software Engineering perspective
•Abstraction ≈ think in terms of what can be done to a collection
of data, independent of how it can be done
•The process of isolating implementation details and extracting
only essential property from an entity
•Abstract Data Type (ADT) is a collection of data & a specification
on the set of operations/methods on that data; a generic
description
•ADT = Data + Operations (also called interface)
•Define range of data && operations (like add, remove, search…)
•ADT is language independent;
When to use ADT?

•In situation when operating on data that are not directly supported by
the programming language in hand
E.g. Complex Number, Natural number, Bank Account, etc
•Steps to design:
1. Plan the ADT
2. Carefully specify all operations needed
•Ignore any implementation related issues
•Better to have a few, simple operations rather than lots of
complex operations.
3. Hide unnecessary details (how many bytes in computer memory the
data type would occupy?)
ADT – Example 1

Complex number
Graphically a complex number is
represented as a point in the complex plane Operations on complex numbers
Example 2 – Array as an ADT
ADT Array:

objects: A set of pairs <index, value> where for each value of index there is a value from the
set item. Index is a finite ordered set of one or more dimensions, for example, A[1, … , n]
for one dimension, A[(0,0),(0,1),(0,2),(1,0),(1,1), (1,2), (2,0),(2,1),(2,2)] for two dimensions.
index ∈ I, value ∈ R
Functions:
for all A ∈ Array, i ∈ index, x ∈ item, j, size ∈ integer
INSERT(A, val, pos)::= Shift all elements in A[pos…n] one position forward, i.e in
positions A[pos+1…n+1] and insert the value val at position pos
DELETE(A, pos)::= Delete the character at position pos in the array A by shifting
elements A[pos+1…n] one step backward. Report error if pos is
not a valid position in L.
Item Retrieve(A, i) ::= if (i ∈ index) return the item associated with index value i in A
else return error
DISPLAY(A[1…n]) ::= Print the list elements from position 1 to position n.
end array
Array - Basic
• An array is a collection of data items, all of the same type, accessed using a common name and
by specifying index (offset) positions
• One-dimensional array is like a list; Two dimensional array is like a table / matrix
• Following declaration of an one dimensional array in C may be mapped in memory as depicted
in diagram (assume integers occupy 4 bytes memory)
int arr[12] = {11, 9, 17, 89, 1, 90, 19, 5, 3, 23, 43, 99};
x = arr[6] = *(arr+6);
Two dimensional array - row 12
a[0][0]
2
a[0][1]
5
a[0][2]

major and column major a[1][0] a[1][1] a[1][2]


31 27 4
ordering a[2][0] a[2][1] a[2][2]
18 1 6
Row major ordering
2000 2002 2004 2006 2008 2010 2012 2014 2016
12 2 5 31 27 4 18 1 6
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
Column major ordering
2000 2002 2004 2006 2008 2010 2012 2014 2016
12 31 18 2 27 1 5 4 6
a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2]
Address calculation of array elements
•Translation from array element to memory address requires two
pieces of information:
•The base address - address of the first element of the array
•The data size - number of memory cells occupied by one
element
•Assuming the Lower limit / Lower Bound of subscript is LB (0/1)
Address of an one dimensional array A[i] = B + W * ( i – LB )
•Where, B = Base address; W = Storage Size of one element
stored in the array (in byte);
Address calculation of 1-D array – Example
Example 1:

B = 200, W = 4 bytes, LB = 0
• Formula - Address of A[i] = B + W * ( i – LB )
• Address of A[2] = 200 + 4 * (2 – 0) = 200 + 8 = 208
• Address of A[4] = 200 + 4 * (4 – 0) = 200 + 16 = 216
4000 4001 4002 4003 4004 4005 4006 4007 4008
Example 2: C S E - T I U W B
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
B = 4000, W = 1 byte, LB = 1
• Address of a[3] = 4000 + 1 * (3 – 1) = 4000 + 2 = 4002
• Address of a[8] = 4000 + 1 * (8 – 1) = 4000 + 7 = 4007
Address calculation of 2-D array
Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as
given: A[M][N]
Row Major order: Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc )]
Column Major order: Address of A [ I ][ J ] = B + W * [( I – Lr ) + M * ( J – Lc )]
Where,
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix
Lc = Lower limit of column/start column index of matrix
M = Number of row of the given matrix
N = Number of column of the given matrix
Example 1:
Let A be a two-dimensional array declared as follows:
A[1 …. 10] [1 …… 15] of elements;
Assuming that each element takes one memory location, the array is
stored in row-major order and the first element of the array is stored at
location 100, what is the address of the element A[i][j]?
Answer: Row Major order: Given, Lr = 1; Lc = 1; M = 10; N = 15; W = 1
Address of A [ i ][ j ] = B + W * [ N * ( i – Lr ) + ( j – Lc )]
= 100 + 1* [15*(i – 1) + (j – 1)]
= 100 + [15 i – 15 + j – 1]
= 100 + [15 i + j – 16]
= 84 + 15 i + j
= 15 i + j + 84
Example 2:
An array X [-15……….10, 15……………40] requires one byte of
storage. If beginning location is 1500 determine the location of X
[15][20]. Assume row major ordering.
Answer: The given values are:
B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)]
= 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5]
= 1500 + 785
= 2285
Practice Questions:
1. A matrix A[m][m] is stored in the memory with each element
requiring 4 bytes of storage. If the base address at A[1][1] is 1500 and
the address of A[4][5] is 1608, determine the order of the matrix
when it is stored in Column Major Wise.
2. An array AR[-4 … 6, -2 … 12], stores elements in Row Major Wise,
with the address AR[2][3] as 4142. If each element requires 2 bytes of
storage, find the Base address.
3. An array VAL[1…15][1…10] is stored in the memory with each
element requiring 4 bytes of storage. If the base address of the array
VAL is 1500, determine the location of VAL[12][9] when the array VAL
is stored (i) Row wise (ii) Column wise.
Answers:
Q.1 - Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
⇒ 1608 = 1500 + 4[m(5 – 1) + (4 – 1)]
⇒ 1608 = 1500 + 4[m(4) + 3]
⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 16m = 96 ⇒ m = 6.
Q.2. - Number of columns, C = 12 – (-2) + 1 = 12 + 2 + 1 = 15.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ 4142 = B + 2[15(2 – (-4)) + (3 – (-2))]
⇒ 4142 = B + 2[15(2 + 4) + (3 + 2)]
⇒ 4142 = B + 2[15(6) + 5] ⇒ 4142 = B + 2[90 + 5]
⇒ 4142 = B + 2[95]
⇒ 4142 = B + 190; ⇒ B = 3952.
Answers:
Q.3. Given: VAL[1…15][1…10]; W = 4; base = 1500; C = (10-1+1) = 10; R = (15-1+1) =
15;
Lr = Lc =1
( i ) Row Major: Address of an element (I,J) in row major = B + W ( C (I-Lr) + (J – Lc))
VAL [12][9] = 1500 + 4 (10 * (12-1) + (9-1))
= 1500 + 4 (10 * 11+8)
= 1500 + 4 (118) = 1500 + 472 = 1972.
( ii ) Column Major: Address of VAL(I,J) in column major = B + W ( (I-Lr) + R(J – Lc))
VAL [12][9] = 1500 + 4 ((12-1) +15 * (9-1))
= 1500 + 4 (11 + 15 * 8)
= 1500 + 4 ( 11+ 120)
= 1500 + 4 * 131 = 1500 + 524 = 2024.
Applications of array:
•Stores similar data types in a compact way so that algorithms like
searching, sorting etc. can be implemented conveniently
•Can be used to represent stack, queue, strings…
•Forms the basis of complex data structures such as heap, hash tables,
binary trees etc.
•⁞
•Matrix representation and manipulations
•Polynomial representation and manipulations
Array as matrix

Row major ordering


2000 2002 2004 2006 2008 2010 2012 2014 2016
12 2 5 31 27 4 18 1 6
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
Sparse vs. Dense Matrix
Dense Matrix Sparse Matrix
12 9 8 1 13 17 1 0 32 8 1 2 1 0 0 0 0 0 0 2
31 8 8 1 6 8 0 1 6 1 0 1 0 0 0 0 0 0 0 1
22 8 6 7 8 4 3 0 12 76 0 0 0 0 0 0 0 1 3 2
34 23 1 2 6 42 99 1 9 1 0 0 0 5 0 0 1 8 0 0
32 87 8 0 5 3 4 2 6 9 0 0 0 0 0 0 0 0 0 1
9 1 7 6 5 3 8 9 1 23 1 0 0 0 0 0 0 0 1 0
0 3 1 2 4 5 6 7 5 8 0 0 0 0 1 3 2 0 0 1
9 8 4 5 6 22 1 5 88 0 1 0 0 0 0 0 3 1 0 0
4 5 3 2 6 8 6 9 0 1 2 1 0 7 0 0 0 0 0 0
77 6 2 55 89 8 3 1 4 0 1 3 3 0 0 0 0 0 0 0

Sparse Matrix ≈ No. of Non-zero entries << No. of zero


entries
Sparse matrix representation – application of arrays
Triplet Representation Rows Columns Values
0 0 1
• 1 1 2
2 4 1
3 3 1
4 2 2
5 5 2
Matrix multiplication – application of array

Matrix multiplication – algorithm

Sparse Polynomial Representation – application of array
• A simple but often good representation of polynomial - array of coefficients
• 4x3 + 3x2 - 5x + 4 can be represented as 3 2 1 0
4 3 -5 4

• Exponents are NOT explicitly stored; implicit in its position (index) in the array
• This representation is not good when the polynomial is SPARSE (has relatively
few non-zero terms compared to its degree: eg. x1000 + 1
• To store x1000 + 1, would take 1001 slots, even though there are only two terms
• Explicitly store both coefficients and exponents, but only for terms where the
coefficient is non-zero

1000 Exponent 0 1000


x +1
Coefficient 1 1

You might also like