Arrays - UniMAP Portalportal.unimap.edu.my/portal/page/portal30/Lecturer Notes...Representation of...

Post on 14-Mar-2018

231 views 2 download

Transcript of Arrays - UniMAP Portalportal.unimap.edu.my/portal/page/portal30/Lecturer Notes...Representation of...

Arrays

Chapter 3

Outline

● Introduction● Array Operations● Number of Elements in an array

– One-dimensional array– Two dimensional array– Multi-dimensional arrays

● Representation of Arrays in Memory– One-dimensional array– Two-dimensional arrays– Three dimensional arrays– N-dimensional array

● Applications– Sparse matrix – Ordered lists

● ADT for arrays

Arrays

● Def: An array is an ADT whose objects are sequence of elements of the same type and the two operations performed on it are store and retrieve.

● ARRAY OPERATIONS

– An array when viewed as a data structure supports only two operations:

● storage of values (i.e.) writing into an array (STORE (a, i, e) ) and,

● retrieval of values (i.e.) reading from an array ( RETRIEVE (a, i) )● Arrays could be of one-dimension, two dimension, three-

dimension or in general multi-dimension.

Number of elements in an array

● Definition of an array A - A[l : u]; where l is the lower bound and u is the upper bound of the index range, the number of elements is given by (u – l + 1).

● An array A - A[l1 : u1, l2:u2] has a size of (u1-l1+1) * (u2-l2+1) elements.

● Multidimensional array representation

– A[1:u1, 1:u2, 1:u3, ….., 1:un] has a size of u1*u2*u3 …..*un-1*un elements i.e

– Generalizing the array A[l1:u1, l2:u2, l3:u3, …, 1:un] has the size

∏i=1

n

ui

∏i=1

n

(u i−li+1)

Exercise

● Find the number of elements for the below arrays

– A[-1:3, 3:4, 2:6]

– A[0:2, 3:4, -1:2]

Representation of Arrays in Memory

● The arrays are stored in memory in one of the two ways, viz., row major order (lexicographic order) or column major order. In this class we use row major order.

One dimensional array

● For the array A(1:u1) let α be the address of the starting memory location referred to as the base address of the array.

– Here as is evident, A[1] occupies the memory location whose address is α, A(2) occupies α +1 and so on. In general, the address of A[i] is given by α +(i-1).

Exercises

● Exercise: calculate the addresses of elements specified assume the base address α is 100:-

– Array - A[1:7] element A[7]

– Array – A[-2:23] element A[16]

Two dimensional arrays

● Consider the array A[1:u1, 1:u2] which is to be stored in memory. It is helpful to imagine this array as u1 number of one-dimensional arrays of length u2. Thus if A[1,1] is stored in address α, the base address, then A[i,1] has address α + (i-1)*u2, and A[i, j] has address α + (i-1)u2 + (j-1)

● In general for two dimensional array A[l1:u1, l2:u2] the address A[i,j] is given by

– α + (i-1)(u2 - l2 + 1) + (j - l2)

Exercise

● For the arrays given with α =220 as the base address , compute the address of the specified elements

– Array A[1:10, 1:5] element A[8,3]

– Array A[-2:4, 6:10] element A[3,-5]

Three dimensional array

● Consider the 3D array A[1:u1, 1:u2, 1:u3], imagine it is u1 of two dimensional arrays of u2 u3. To access A[i,1,1] the address for that element is α + (i-1)u2u3. Address A[i,j,1] is α + (i-1)u2u3 + (j – 1)u3. Address A[i,j,k] is α + (i-1)u2u3 + (j – 1)u3 + (k-1). In general A[l1:u1, l2:u2, l3:u3]

– α + (i – l1)(u2 - l2 + 1)(u3 - l3 + 1) + (j – l2)(u3 - l3 + 1) + (k - l3)

Exercises

● For the arrays given below with the base address α =110 compute the address of :

– A[1:5, 1:2, 1:3] element A[2,1,3]

– A[-2:4,-6,10:1:3] element A[-1, -4,2]

Sparse matrix

● A matrix is a mathematical object which finds its applications in various scientific problems. A matrix is an arrangement of m X n elements arranged as m rows and n columns. The Sparse matrix is a matrix with zeros as the dominating elements. There is no precise definition for a sparse matrix. In other words, the “sparseness” is relatively defined

● A matrix consumes a lot of space in memory. Thus, a 1000 X 1000 matrix needs 1 million storage locations in memory. Imagine the situation when the matrix is sparse! To store a handful of non-zero elements, voluminous memory is allotted and there by wasted!

Cont..

● To save valuable storage space, we resort to a 3-tuple representation viz., (i, j, value) to represent each non-zero element of the sparse matrix.

● In other words, a sparse matrix A is represented by another matrix B[0:t, 1:3] with t+1 rows and 3 columns. Here t refers to the number of non-zero elements in the sparse matrix.

● While rows 1 to t record the details pertaining to the non-zero elements as 3-tuples(that is 3 columns), the zeroth row viz. B[0,1] , B[0,2] and B[0,3] record the number of non-zero elements of the original sparse matrix A.

Example sparse matrix

Order list or linear list

● One of the simplest and useful data objects in computer science is an ordered list or linear list.

● An ordered list can be either empty or non empty. In the latter case, the elements of the list are known as atoms, chosen from a set D. The ordered lists provide a variety of operations such as retrieval, insertion, deletion, update etc.

● The most common way to represent an ordered list is by using a one-dimensional array. Such a representation is termed sequential mapping through better forms of representation have been presented in the literature.

Example an ordered list

ADT for Arrays

● Data objects:

– A set of elements of the same type stored in a sequence

● Operations:

– Store value VAL in the ith element of the array ARRAY

● ARRAY[i] = VAL

– Retrieve the value in the ith element of array ARRAY as

VAL = ARRAY[i]