Beruflich Dokumente
Kultur Dokumente
In the previous lecture we have seen the implementation of ordered lists with one-dimensional arrays
and the concept of sequential mapping. Later, we have seen some commonly used features for data
structures; pointers and structures. In this lecture firstly, we will discuss the representation of higher
degree arrays and sparse matrices. After then, we will see what algorithm analysis means and how we
can estimate the time required for an algorithm. We will also talk about the techniques that can be
used to reduce the running time of an algorithm.
We define R x C matrix M as a rectangular array of elements having R rows and C columns and then
refer to any particular element in the matrix by using two subscripts r and c in an expression M[r,c].
As in one-dimensional case we have an abstraction. In all programming languages we can declare
two dimensions and store and retrieve elements using indexes. But if we want to reach the elements
using pointers how we are going to know the storage structure of the two-dimensional array in
memory?
Compilers effect the implementation of arrays. Recall that memory is linear with the addresses of the
storage units running in a single increasing sequence. So a two-dimensional structure has to be
mapped onto a structure with one dimension. Most languages implement the abstraction matrix in
row-major where the two-dimensional array is stored row by row. In some languages (such as
Fortran) it is stored column-major.
Assume a row-major implementation of an array where each element occupies n bytes of
memory:
Array mRxC is mapped to m’1xL
1
CSE211 Lecture Notes – 2004/2005-II
Ex: a[3,2];
1 2 3 4 5 6 k
1 2
1,1 1,2 2,1 2,2 3,1 3,2 i,j
1 1,1 1,2
Row1 Row2
2 2,1 2,2
3 3,1 3,2
Abstract
view
For higher dimensions, the implementation is a generalization of two-dimensional case. For the three
dimensional case, third dimension is conventionally called a plane. The new subscript is added
before the one for a row.
M[plane][row][column]
2
CSE211 Lecture Notes – 2004/2005-II
Abstract view
As in 2-D case this abstract structure is also mapped onto one dimensional linear structure.
Assuming n=1 and start location=1; m[p,i,j] = m’[ (p-1)*R*C+(i-1)*C + j-1 +1]
Ex: a[2,3,2]; 1 2
2,1,1 2,1,2
2,2,1 2,2,2 2
1 2 3 4 5 6 2,3,1 2,3,2
1,1,1 1,1,2 1,2,1 1,2,2 1,3,1 1,3,2 1 1,1,1 1,1,2
2 1,2,1 1,2,2 1
Row1 Row2 Row3
3 1,3,1 1,3,2
Plane1 Plane2
Abstract view
Structures with high proportion of zeroes are common in real world. Sometimes they are too large to
fit in main memory. We have to find a way to store them efficiently. There are two cases:
IDEA: Find a way to store only nonzero elements and keep their actual position by a storage
mapping function.
3
CSE211 Lecture Notes – 2004/2005-II
We can obtain a storage structure for a lower triangular matrix by remembering how 2-D arrays are
mapped onto linear storage. We can devise a method to reach an element of the matrix.
m(1,1) …………………… 0 0 0 0
m(2,1) …………………… 0 0 0 0
. . .
. . 0 . m[i,j] = 0, where j>i
. m(i,j) .
. none zero . .
m(R,1) …………………… m(R,R) total number of elements = R2
R
total number of elements = x = R(R+1)/2
x 1
Symmetric Matrices
To see how this works, consider that for c>r we are in the upper region and we can deduce the value
by looking its diagonal.
m(i,j) = m(j,i)
We use the similar mapping function in the case of lower triangular matrix and store only the lower
triangle of the symmetric matrix
Band Matrices
Nonzero elements are concentrated on the
diagonal and within a fixed distance above w
and below it.
w
4
CSE211 Lecture Notes – 2004/2005-II
5
6
Since we assumed that the values frequently change, we need to consider the performance of
operations:
1. Element changing from zero to nonzero (adding a new item)
- if value is zero delete the item from the table (if it is in the table)
- if value is nonzero search the table for the desired index: if exists change the value, else add
(update the table)
2. Element changing from nonzero to zero (delete an item)
3. Retrieve the value of an element with a given index
- Search the table for an element with the desired index,
if successful return the value, else return 0.
4. Print entire sparse vector in order by index
Example: Implement polynomial as a data structure, allowing operations add, subtract, multiply
and divide on them. Sol.: Use a sequence of Exponent-Coefficient pair, corresponding to index-value
pair to represent a polynomial, assuming integer-valued coefficients. Assuming max. nonzero = 3
0 3
5
CSE211 Lecture Notes – 2004/2005-II
current nonzero = 3