
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sparse Matrix Multiplication in C++
Suppose we have two matrices A and B, we have to find the result of AB. We may assume that A's column number is equal to B's row number.
So, if the input is like [[1,0,0],[-1,0,3]] [[7,0,0],[0,0,0],[0,0,1]],
1 | 0 | 0 |
-1 | 0 | 3 |
7 | 0 | 0 |
0 | 0 | 0 |
0 | 0 | 1 |
then the output will be [[7,0,0],[-7,0,3]]
7 | 0 | 0 |
-7 | 0 | 3 |
To solve this, we will follow these steps −
r1 := size of A, r2 := size of B
c1 := size of A[0], c2 := size of B[0]
Define one 2D array ret of order r1 x c2
Define an array sparseA[r1] of pairs
for initialize i := 0, when i < r1, update (increase i by 1), do −
for initialize j := 0, when j < c1, update (increase j by 1), do −
if A[i, j] is not equal to 0, then −
insert { j, A[i, j] } at the end of sparseA[i]
for initialize i := 0, when i < r1, update (increase i by 1), do −
for initialize j := 0, when j < size of sparseA[i], update (increase j by 1), do −
for initialize k := 0, when k < c2, update (increase k by 1), do −
x := first element of sparseA[i, j]
if B[x, k] is not equal to 0, then −
ret[i, k] := ret[i, k] + second element of sparseA[i, j] * B[x, k]
return ret
Example
Let us see the following implementation to get better understanding −
class Solution { public: vector<vector<int<> multiply(vector<vector<int<>& A, vector<vector<int<>& B) { int r1 = A.size(); int r2 = B.size(); int c1 = A[0].size(); int c2 = B[0].size(); vector < vector <int< > ret(r1, vector <int< (c2)); vector < pair <int, int> > sparseA[r1]; for(int i = 0; i < r1; i++){ for(int j = 0; j < c1; j++){ if(A[i][j] != 0)sparseA[i].push_back({j, A[i][j]}); } } for(int i = 0; i < r1; i++){ for(int j = 0; j < sparseA[i].size(); j++){ for(int k = 0; k < c2; k++){ int x = sparseA[i][j].first; if(B[x][k] != 0){ ret[i][k] += sparseA[i][j].second * B[x][k]; } } } } return ret; } };
Input
{{1,0,0},{-1,0,3}},{{7,0,0},{0,0,0},{0,0,1}}
Output
[[7, 0, 0, ],[-7, 0, 3, ],]
- Related Articles
- C Program for sparse matrix
- C++ Program to Implement Sparse Matrix
- Matrix Multiplication and Normalization in C program
- Check if a given matrix is sparse or not in C++
- C++ Program to Perform Matrix Multiplication
- C Program for Matrix Chain Multiplication
- C++ Program to Check if it is a Sparse Matrix
- How to convert a sparse matrix into a matrix in R?
- How to create a sparse matrix in R?
- How to create a sparse Matrix in Python?
- Matrix Chain Multiplication
- Matrix multiplication algorithm
- Matrix Chain Multiplication (A O(N^3) Solution) in C++
- Algorithm for matrix multiplication in JavaScript
- Java Program To Determine If a Given Matrix is a Sparse Matrix
