Skip to content

Commit 2d949ce

Browse files
committed
sparse metrix
1 parent 1f14526 commit 2d949ce

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Structure to store a non-zero element of a sparse matrix
5+
struct Element {
6+
int row;
7+
int col;
8+
int value;
9+
};
10+
11+
// Class to represent a sparse matrix
12+
class SparseMatrix {
13+
private:
14+
int rows, cols, nonZeroCount;
15+
vector<Element> elements;
16+
17+
public:
18+
SparseMatrix(int r, int c) {
19+
rows = r;
20+
cols = c;
21+
nonZeroCount = 0;
22+
}
23+
24+
// Add a non-zero element
25+
void addElement(int r, int c, int val) {
26+
if(val != 0) {
27+
elements.push_back({r, c, val});
28+
nonZeroCount++;
29+
}
30+
}
31+
32+
// Print sparse matrix
33+
void print() {
34+
vector<vector<int>> full(rows, vector<int>(cols, 0));
35+
for(auto &e : elements) {
36+
full[e.row][e.col] = e.value;
37+
}
38+
for(int i = 0; i < rows; i++) {
39+
for(int j = 0; j < cols; j++) {
40+
cout << full[i][j] << " ";
41+
}
42+
cout << endl;
43+
}
44+
}
45+
46+
// Addition of two sparse matrices
47+
SparseMatrix add(SparseMatrix &other) {
48+
if(rows != other.rows || cols != other.cols) {
49+
throw invalid_argument("Matrix dimensions must match for addition.");
50+
}
51+
SparseMatrix result(rows, cols);
52+
int i = 0, j = 0;
53+
while(i < elements.size() && j < other.elements.size()) {
54+
int pos1 = elements[i].row * cols + elements[i].col;
55+
int pos2 = other.elements[j].row * cols + other.elements[j].col;
56+
if(pos1 < pos2) {
57+
result.addElement(elements[i].row, elements[i].col, elements[i].value);
58+
i++;
59+
} else if(pos1 > pos2) {
60+
result.addElement(other.elements[j].row, other.elements[j].col, other.elements[j].value);
61+
j++;
62+
} else {
63+
int sum = elements[i].value + other.elements[j].value;
64+
result.addElement(elements[i].row, elements[i].col, sum);
65+
i++; j++;
66+
}
67+
}
68+
while(i < elements.size()) result.addElement(elements[i].row, elements[i].col, elements[i].value), i++;
69+
while(j < other.elements.size()) result.addElement(other.elements[j].row, other.elements[j].col, other.elements[j].value), j++;
70+
return result;
71+
}
72+
73+
// Subtraction of two sparse matrices
74+
SparseMatrix subtract(SparseMatrix &other) {
75+
SparseMatrix negOther = other;
76+
for(auto &e : negOther.elements) e.value = -e.value;
77+
return add(negOther);
78+
}
79+
80+
// Multiplication of two sparse matrices (naive)
81+
SparseMatrix multiply(SparseMatrix &other) {
82+
if(cols != other.rows) {
83+
throw invalid_argument("Matrix dimensions incompatible for multiplication.");
84+
}
85+
SparseMatrix result(rows, other.cols);
86+
vector<vector<int>> full1(rows, vector<int>(cols,0));
87+
vector<vector<int>> full2(other.rows, vector<int>(other.cols,0));
88+
for(auto &e : elements) full1[e.row][e.col] = e.value;
89+
for(auto &e : other.elements) full2[e.row][e.col] = e.value;
90+
91+
for(int i = 0; i < rows; i++) {
92+
for(int j = 0; j < other.cols; j++) {
93+
int sum = 0;
94+
for(int k = 0; k < cols; k++) {
95+
sum += full1[i][k] * full2[k][j];
96+
}
97+
if(sum != 0) result.addElement(i,j,sum);
98+
}
99+
}
100+
return result;
101+
}
102+
};
103+
104+
// Example usage
105+
int main() {
106+
SparseMatrix A(3, 3);
107+
A.addElement(0, 0, 1);
108+
A.addElement(0, 2, 2);
109+
A.addElement(1, 1, 3);
110+
111+
SparseMatrix B(3, 3);
112+
B.addElement(0, 0, 4);
113+
B.addElement(0, 2, 5);
114+
B.addElement(2, 2, 6);
115+
116+
cout << "Matrix A:\n"; A.print();
117+
cout << "\nMatrix B:\n"; B.print();
118+
119+
SparseMatrix C = A.add(B);
120+
cout << "\nA + B:\n"; C.print();
121+
122+
SparseMatrix D = A.subtract(B);
123+
cout << "\nA - B:\n"; D.print();
124+
125+
SparseMatrix E = A.multiply(B);
126+
cout << "\nA * B:\n"; E.print();
127+
128+
return 0;
129+
}

0 commit comments

Comments
 (0)