0% found this document useful (0 votes)
51 views

Representing Matrix by Using Array: Data Structures Using C Arrays and Sructures Sparse Matrices

This document discusses different representations of matrices and polynomials in C. It describes representing a matrix using a two-dimensional array and an optimized sparse matrix representation storing only non-zero elements as triples of <row, column, value>. It also covers transpose operations on matrices and provides algorithms. For polynomials, it explains representing them by defining a structure with degree and coefficient array, or by storing terms in a single array with coefficients and exponents. Addition of two polynomials is demonstrated by adding corresponding terms.

Uploaded by

Rachana Haridas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Representing Matrix by Using Array: Data Structures Using C Arrays and Sructures Sparse Matrices

This document discusses different representations of matrices and polynomials in C. It describes representing a matrix using a two-dimensional array and an optimized sparse matrix representation storing only non-zero elements as triples of <row, column, value>. It also covers transpose operations on matrices and provides algorithms. For polynomials, it explains representing them by defining a structure with degree and coefficient array, or by storing terms in a single array with coefficients and exponents. Addition of two polynomials is demonstrated by adding corresponding terms.

Uploaded by

Rachana Haridas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structures using C

Chapter-2

ARRAYS AND SRUCTURES SPARSE MATRICES: A matrix is a mathematical object which arises in many physical problems. As computer scientists, we are interested in studying ways to represent matrices so that the operations to be performed on them can be carried out efficiently.

Representing matrix by using array


m by n (m rows, n columns) use two-dimensional array space complexity S(m, n) = m * n

The first matrix has five rows and three columns, the second six rows and six columns. In general, we write m X n (read m by n) to designate a matrix with m rows and n columns. Such a matrix has mn elements. When m is equal to n, we call the matrix square.

if we look at the second matrix of figure (b) we see that it has many zero entries. Such a matrix is called sparse.

Common

characteristics
most elements are zeros inefficient memory utilization

There is no precise definition of when a matrix is sparse and when it is not, but it is a concept which we can all recognize intuitively. Above, only 8 out of 36 possible elements are nonzero and that is sparse! A sparse matrix requires us to consider an alternate form of representation.

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

This comes about because in practice many of the matrices we want to deal with are large, e.g., 1000 X 1000, but at the same time they are sparse: say only 1000 out of one million possible elements are nonzero. On most computers today it would be impossible to store a full 1000 X 1000 matrix in the memory at once. Therefore, we ask for an alternative representation for sparse matrices.

The alternative representation: store only nonzero elements using the triple <row,col,value>
We must know the number of rows the number of columns the number of non-zero elements

Sparse Matrix Representation:


sparseMatrix Create(maxRow, maxCol) ::= #define MAX_TERMS 101 typedef struct { int col; int row; int value; } term; term a[MAX_TERMS]; o a[0].row: the number of rows o a[0].col: the number of columns o a[0].value: the total number of non-zoros optimized Sparse Matrix Representation using a two dimensional Array, where each element is characterized by <ROW, COL, VALUE>

Sparse matrix stored as triples

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

space
S(n,m) t :

complexity
= 3 *t where the number of non-zeros

Sparse Matrix ADT:


A minimal set of operations includes: matrix creation addition multiplication Transpose.

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

Transpose of a Matrix:
In transpose operation the element in the i,j position gets put in the j,i position. Another way of saying this is that we are interchanging rows and columns. The elements on the diagonal will remain unchanged, since i = j.

The transpose of the example matrix looks like

Since A is organized by row, our first idea for a transpose algorithm might be for each row i do take element (i,j,val) and store it in (j,i,val) of the transpose end The difficulty is in not knowing where to put the element (j,i,val) until all other elements which precede it have been processed. (1,1,15) which becomes (1,1,15) (1,4,22) which becomes (4,1,22) (1,6, - 15) which becomes (6,1, - 15). If we just place them consecutively, then we will need to insert many new triples, forcing us to move elements down very often. We can avoid this data movement by finding the elements in the order we want them, which would be as for all elements in column j do place element (i,j,val) in position (j,i,val) end

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

This says find all elements in column 1 and store them into row 1, find all elements in column 2 and store them in row 2, etc. Since the rows are originally in order, this means that we will locate elements in the correct column order as well.

Important Note: If we want transpose operation to work efficiently then we should organize the triples so that the row indices are in ascending order.
Algorithm for Transpose of Matrix void transpose(term a[],term b[]) { int n,I,j,current;
n = a[0].value; b[0].row = a[0].col; b[0].col = a[0].row; b[0].value = n; if(n > 0) { current = 1;

for(I = 0; I < a[0].col; i+ +) for(j = 1; j <= n; j++) if(a[j].col == i) { b[current].row = a[j].col; b[current].col = a[j].row; b[current].value = a[j].value; current++; } } }

Complexity space: 3t, where time: O(colst) t: the number of non-zeros

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

Create

better algorithm by using a little more storage row_terms the number of element in each row starting_pos the starting point of each row

Fast Transpose of Sparse Matrix:


void fast_transpose(term a[],term b[]) { int row_terms[MAX_COL]; int starting_pos[MAX_COL]; int i,j; int num_cols = b[0].row = a[0].col; int num_terms = b[0].value = a[0].value; b[0].col = a[0].row; if (num_terms > 0) { for(i = 0; i < num_cols; i+ +) row_terms[i] = 0; for(i = 1; i <= num_terms; i+ +) row_terms[a[i].col]++; starting_pos[0] = 1; for(i = 1; i < num_cols; i+ +) starting_pos[i] = starting_pos[i-1] + row_terms[i1]; for(i = 1; i <= num_terms; i++) { j=starting_pos[a[i].col]++; b[j].row = a[i].col;
b[j].col = a[i].row; b[j].value = a[i].value; }}}

After the execution of the third for loop, the values of row_terms and starting_pos are:

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

Final Output:

The polynomial ADT


Orders or Linear List:
Defn: An ordered linear list is a data structure where elements are arranged in a linear manner, or in ordered form

Examples

ordered (linear) list: (item1, item2, item3, , itemn) (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) (basement, lobby, mezzanine, first, second) (1941, 1942, 1943, 1944, 1945) (a1, a2, a3, , an-1, an) Operations performed on linear list: Finding the length, n , of the list. Reading the items from left to right (or right to left). Retrieving the ith element. Storing a new value into the ith position. Inserting a new element at the position i , causing elements numbered i, i+1, , n to become numbered i+1, i+2, , n+1 Deleting the element at position i , causing elements numbered i+1, , n to become numbered i, i+1, , n-1

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

Polynomial:
Defn:

A Polynomial is a sum of terms, where each term has a form ax , where x is the variable, a is the coefficient and e is the exponent.
Two example polynomials are:
A(x) = 3x +2x +4 and B(x) = x +10x +3x +1 Assume that we have two polynomials,
20 5 4 3 2

A(x) = aixi and B(x) = bixi where x is the variable, ai is the coefficient, and
i is the exponent, then:

A(x) + B(x) = (ai + bi)xi

A(x) B(x) = (aixj (bjxj))

Similarly, we can define subtraction and division on polynomials, as well as many other operations.

The polynomial ADT

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

There are two ways to create the type polynomial in C


Representation I define MAX_degree 101 /*MAX degree of polynomial+1*/ typedef struct{ int degree; float coef [MAX_degree]; }polynomial; Drawback: It wastes more space. Explanation: if we create variables for this structure like polynomial p1, p2; The n the memory allocation for p1 and p2 looks like this P1. Degree one memory location P1.coef 101 memory locations P2.degree one memory location P2.coef 101 memory location If we have a polynomial such that, p1 as 2x + 3x + 4 and p2 as 3x + 4x + 5 Then the storage allocation for them is something like this. P1 2x + 3x + 4 P1.degree= 2 P1.coef= 0 1 2 Coef 2 3 4 P2 3x + 4x + 5 P2.degree= 3 P2.coef= 0 1 2 Coef 3 0 4
3 2 2 3

3 0

4 0

5 0

. .

98 0

99 0

100 0

3 5

4 0

5 0

. .

98 0

99 0

100 0

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

Addition of two polynomials: P3=p1+p2 P3= 3x +2x +7x+9 P3.degree = 3 P3.coef= 0 1 2 Coef 3 2 7
3 2

3 9

4 0

5 0

. .

98 0

99 0

100 0

Check the above polynomial if we have a polynomial something like this 100 2x +x+3 Then it will unnecessarily waste 98 memory locations out of 101. So we go for one more representation Representation II MAX_TERMS 100 /*size of terms array*/ typedef struct{ float coef; int expon; } polynomial; polynomial terms [MAX_TERMS]; int avail = 0;

A(x) = 2x1000+1 B(x) = x4+10x3+3x2+1

Explanation: consider the two polynomials A(x) and B(x) given above fig shows how these polynomials are stored in the array terms. The index of the first term of A and B is given by startA and startB, respectively, while finishA and finish give the index of the last term of A and B. the index of the next location in the array is given by avail. For our example: startA=0, finishA=1, startB=2, finish=5, and avail=6.

10

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

This representation does not impose any limit on the number of polynomials that we can place in terms.

Polynomial Addition
Below given is a c function which adds to polynomials. A and B represents two polynomials and addition of them gives D=A+B, where D is resulting Addition of two polynomials A and B, to produce D(x), padd() function adds A(x) and B(x) term by term.

11

SEM-II

DATA STRUCTURES USING C

DEPARTMENT OF MCA

Resulting Polynomial that D(x) contains : Avail 2 1000 6

Coef Exp

1 4 7

10 3 8

3 2 9

2 0 10

PDF to Word

12

You might also like