Matrix Multiplication Using Pairs

I am considering alternative ways of doing matrix multiplication. Instead of storing my matrix as a two-dimensional array, I use a vector like

vector<pair<pair<int,int >,int > > 

to save my matrix. The pair inside my pair (s) stores my indices (i, j), and the other int stores the value for this pair (i, j). I thought I would be able to implement my sparse array this way.

The problem is that I am trying to multiply this matrix by myself.

If it was an implementation of an array of the 2nd array, I would multiply the matrix as follows:

   for(i=0; i<row1; i++)
    {
        for(j=0; j<col1; j++)
        {
          C[i][j] = 0;
         for(k=0; k<col2; k++) 
           C[i][j] += A[i][j] * A[j][k];
      }
    }

Can someone point out a way to achieve the same result using my pair pair vector?

thank

+3
1

. , .

map<pair<int, int>, int> . , first map.

, :

typedef pair<int, int> mx_coord;
struct reverse_compare {
    bool operator() (mx_coord const &l, mx_coord const &r) const
        { return l.second < r.second? true :
                 r.second < l.second? false : l.first < r.first; }
};

typedef map< mx_coord, int > matrix;
typedef map< mx_coord, int, reverse_compare > matrix_transpose;

A B, B A B, , , , , .

B:

matrix_transpose b_t( b.begin(), b.end() ); // complexity: n log n
+1

Source: https://habr.com/ru/post/1739941/


All Articles