Friday, January 25, 2013

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




No comments:

Post a Comment