Friday, January 25, 2013

Procedure for Transpose of Sparse Matrix

Transpose of sparse matrix

Consider a matrix
        
         4    0   2   0
A=    0    6   0   0    
         0    0   5   0
         7    0   0   8
         4    0   0   7
AT=  0    6   0   0    
         2    0   5   0
         0    0   0   8




R
C
value


R
C
value
 0
3
4
6

0
3
4
6
1
1
1
4

1
1
1
4
2
1
2
2

2
1
4
7
3
2
2
6

3
2
2
6
4
3
3
5

4
3
1
2
5
4
1
7

5
3
3
5
6
4
4
8

6
4
4
8











From the above two 3 – tuples we observe that in A-3 tuples are in ascending order and columns are in random way whereas in AT – 3 tuples columns are in ascending order and rows become irregular and actual changes depending on the row and column position.

Procedure Transpose(A: array[0..30, 1..3] of integers)
  B: array[0..30, 1..3] of integers
  I, j, k: integers
Begin
    B[0,1]= A[0,2];  { Copy Ist  row from A into B  such that size MXN becomes  
                               NXM }
    B[0,2]= A[0,1];
    B[0,3]= A[0,3]
    j=1;                         { J points to B 3-tuples   }
    for k= 1 to A[0,2]    {  k represents passes }
    begin
        for i=1 to A[0,3]  { i is a pointer for A 3-tuples }
          begin
              if A[I,2]= k then
                begin
                   B[j,1]= A[i,2];
                   B[j,2]= A[I,1];
                   B[j,3]= A[I,3];
                   J=j+1;
                end
          end
    end
end


Program for Transpose matrix

Program TransposeMatrix()
    A:array[1..30,1..30] of integers ;
    B,C:array[0..30,1..3] of integers ;
    i, j, m,n: integers;
begin
   writeln(“enter size of matrix’);
   read(m,n);
   writeln(“enter elements into matrix”);
   for I =1 to m do
   for j = 1 to n do
    read(A[I,j]);
 
   createsparematrix(A,m,n,B);
   Transpose(B,C);
   writeln(“resultant of Transpose is as follows”);
   for i=1 to C[0,3] do
    begin
      for j= 1 to 3 do
        begin
         write(C[i,j]);
         end
        writeln;
    end

end

Procedure for adding two Sparse Matrices

Adding of 2 Matrices i.e., C=A+B       
        
         4    0   2   0
A=    0    6   0   0    
         0    0   5   0

         0    3   1   0
B=    0    2   0   8    
         0    7   0   4

         4    3   3   0
C=    0    8   0   8    
         0    7   5   4





R
C
value


R
C
value


R
C
value
 0
3
4
4

0
3
4
6

0
3
4
8
1
1
1
4

1
1
2
3

1
1
1
4
2
1
2
2

2
1
3
1

2
1
2
3
3
2
2
6

3
2
2
2

3
1
3
3
4
3
3
5

4
2
4
8

4
2
2
8





5
3
2
7

5
2
4
8





6
3
4
4

6
3
2
7










7
3
3
5










8
3
4
4































Procedure to Add two spare Matrices.

Procedure sparseAdd(A,B: Array[0..30,1..3] of integers)
   C: Array[0..30,1..3] of integers
   i, j, k,temp: integers
begin
    if  (A[0,1]=B[0,1]) and (A[0,2]=B[0,2]) then
      begin
         C[0,1]=A[0,1];
         C[0,2]=B[0,2];
         i=1; j=1; k=0;
         while (i<= A[0,3]) and (j<=B[0,3])
           begin
              if (A[i,1]<B[j,1]) or (A[i,1]=B[j,1] and A[i,2]<B[j,2]) then
                 k=k+1;
                 C[k,1]=A[i,1];
                 C[k,2]=A[I,2];
                 C[k,3]=A[I,3];
                  i=i+1;
              else if (B[j,1]<A[i,1]) or (B[j,1]=A[i,1] and B[j,2]<A[i,2]) then
                 k=k+1;
                 C[k,1]=B[i,1];
                 C[k,2]=B[I,2];
                 C[k,3]=B[I,3];
                  j=j+1;
              else if  (A[i,1]=B[j,1] and A[i,2]=B[j,2]) then
                 temp= A[i,3]+B[j,3];
                 if temp=0 then
                   begin
                      i=i+1;
                      j=j+1;
                   end
                 else
                   begin
                      k=k+1;
                      C[k,1]=A[i,1];
                      C[k,2]=B[j,2];
                      C[k,3]=temp;
                      i=i+1;
                      j=j+1;       
                   end
              end
        end { End of while loop }
       { B is exhausted  }
         while (i<=A[0,3])
           begin
              K=k+1;
              C[k,1]=A[i,1];
              C[k,2]=A[I,2];
              C[k,3]=A[I,3];
             i=i+1;
           end
       { A is exhausted  }
         while (j<=B[0,3])
           begin
              K=k+1;
              C[k,1]=B[i,1];
              C[k,2]=B[I,2];
              C[k,3]=B[I,3];
             j=j+1;
           end
     end { End of first if }
    else
      begin
         writeln(“Matrices cannot be added. Since sizes are not same”);
      end
end

  

Main program for addition of two matrix’s

Program SparseAddition
  A,B: array[1..30, 1..30] of integers
  C,D,E: array[0..30, 0..3] of integers;
  m,n,i,j,p,q: integers;
Begin
   writeln(“enter size of first matrix”);
   read(m,n);
   writeln(“enter elements into first sparse matrix’);
   for i=1 to m do
   for j= 1 to n do
   read(A[i,j]);

   writeln(“enter size of second matrix”);
   read(p,q);
   writeln(“enter elements into second sparse matrix’);
   for i=1 to p do
   for j= 1 to q do
   read(B[i,j]);

   createsparematrix(A,m,n,C);
   createsparematrix(B,p,q,D);
   sparseAdd(C,D,E);
   writeln(“resultant of 3- tuple is as follows”);
   for i=1 to A[0,3] do
    begin
      for j= 1 to 3 do
        begin
         write(E[i,j]);
         end
        writeln;
    end
end