Representing Matrix by Using Array: Data Structures Using C Arrays and Sructures Sparse Matrices
Representing Matrix by Using Array: Data Structures Using C Arrays and Sructures Sparse Matrices
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.
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
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
SEM-II
DEPARTMENT OF MCA
space
S(n,m) t :
complexity
= 3 *t where the number of non-zeros
SEM-II
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.
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
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++; } } }
SEM-II
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
After the execution of the third for loop, the values of row_terms and starting_pos are:
SEM-II
DEPARTMENT OF MCA
Final Output:
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
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:
Similarly, we can define subtraction and division on polynomials, as well as many other operations.
SEM-II
DEPARTMENT OF MCA
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
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;
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
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
DEPARTMENT OF MCA
Coef Exp
1 4 7
10 3 8
3 2 9
2 0 10
PDF to Word
12