Skip to content

Commit 3ca0860

Browse files
Merge pull request #382 from PragatiGHub/sparse
#182 - Sparse Matrix Operation
2 parents c7d03e6 + e3274eb commit 3ca0860

File tree

1 file changed

+95
-42
lines changed

1 file changed

+95
-42
lines changed
Lines changed: 95 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,182 @@
11
#include <bits/stdc++.h>
22
using namespace std;
33

4+
/*
5+
==========================================
6+
Sparse Matrix - Basic Operations
7+
Operations: Addition, Subtraction, Multiplication
8+
Representation: Triplet (row, col, value)
9+
==========================================
10+
*/
11+
412
// Structure to store a non-zero element of a sparse matrix
513
struct Element {
614
int row;
715
int col;
816
int value;
917
};
1018

11-
// Class to represent a sparse matrix
19+
// Class to represent a Sparse Matrix
1220
class SparseMatrix {
1321
private:
14-
int rows, cols, nonZeroCount;
15-
vector<Element> elements;
22+
int rows, cols; // Matrix dimensions
23+
vector<Element> elements; // Store only non-zero elements
1624

1725
public:
26+
// Constructor to initialize the matrix with given rows and columns
1827
SparseMatrix(int r, int c) {
1928
rows = r;
2029
cols = c;
21-
nonZeroCount = 0;
2230
}
2331

24-
// Add a non-zero element
32+
// Add a non-zero element to the sparse matrix
2533
void addElement(int r, int c, int val) {
26-
if(val != 0) {
34+
if (val != 0) {
2735
elements.push_back({r, c, val});
28-
nonZeroCount++;
2936
}
3037
}
3138

32-
// Print sparse matrix
33-
void print() {
39+
// Print the full matrix (converts sparse → dense for display)
40+
void print() const {
3441
vector<vector<int>> full(rows, vector<int>(cols, 0));
35-
for(auto &e : elements) {
42+
for (const auto &e : elements) {
3643
full[e.row][e.col] = e.value;
3744
}
38-
for(int i = 0; i < rows; i++) {
39-
for(int j = 0; j < cols; j++) {
45+
for (int i = 0; i < rows; i++) {
46+
for (int j = 0; j < cols; j++) {
4047
cout << full[i][j] << " ";
4148
}
4249
cout << endl;
4350
}
4451
}
4552

46-
// Addition of two sparse matrices
47-
SparseMatrix add(SparseMatrix &other) {
48-
if(rows != other.rows || cols != other.cols) {
53+
// ------------------------
54+
// Matrix Addition
55+
// ------------------------
56+
SparseMatrix add(const SparseMatrix &other) const {
57+
if (rows != other.rows || cols != other.cols) {
4958
throw invalid_argument("Matrix dimensions must match for addition.");
5059
}
60+
5161
SparseMatrix result(rows, cols);
5262
int i = 0, j = 0;
53-
while(i < elements.size() && j < other.elements.size()) {
63+
64+
while (i < elements.size() && j < other.elements.size()) {
5465
int pos1 = elements[i].row * cols + elements[i].col;
5566
int pos2 = other.elements[j].row * cols + other.elements[j].col;
56-
if(pos1 < pos2) {
67+
68+
if (pos1 < pos2) {
5769
result.addElement(elements[i].row, elements[i].col, elements[i].value);
5870
i++;
59-
} else if(pos1 > pos2) {
71+
} else if (pos1 > pos2) {
6072
result.addElement(other.elements[j].row, other.elements[j].col, other.elements[j].value);
6173
j++;
6274
} else {
6375
int sum = elements[i].value + other.elements[j].value;
64-
result.addElement(elements[i].row, elements[i].col, sum);
76+
if (sum != 0) {
77+
result.addElement(elements[i].row, elements[i].col, sum);
78+
}
6579
i++; j++;
6680
}
6781
}
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++;
82+
83+
while (i < elements.size()) {
84+
result.addElement(elements[i].row, elements[i].col, elements[i].value);
85+
i++;
86+
}
87+
while (j < other.elements.size()) {
88+
result.addElement(other.elements[j].row, other.elements[j].col, other.elements[j].value);
89+
j++;
90+
}
91+
7092
return result;
7193
}
7294

73-
// Subtraction of two sparse matrices
74-
SparseMatrix subtract(SparseMatrix &other) {
95+
// ------------------------
96+
// Matrix Subtraction
97+
// ------------------------
98+
SparseMatrix subtract(const SparseMatrix &other) const {
7599
SparseMatrix negOther = other;
76-
for(auto &e : negOther.elements) e.value = -e.value;
100+
// Negate all values in the second matrix
101+
for (auto &e : negOther.elements) {
102+
e.value = -e.value;
103+
}
77104
return add(negOther);
78105
}
79106

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.");
107+
// ------------------------
108+
// Matrix Multiplication (Naive)
109+
// ------------------------
110+
SparseMatrix multiply(const SparseMatrix &other) const {
111+
if (cols != other.rows) {
112+
throw invalid_argument("Matrix dimensions are incompatible for multiplication.");
84113
}
114+
85115
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;
90116

91-
for(int i = 0; i < rows; i++) {
92-
for(int j = 0; j < other.cols; j++) {
117+
// Convert both matrices to full form for easier multiplication
118+
vector<vector<int>> full1(rows, vector<int>(cols, 0));
119+
vector<vector<int>> full2(other.rows, vector<int>(other.cols, 0));
120+
121+
for (const auto &e : elements)
122+
full1[e.row][e.col] = e.value;
123+
124+
for (const auto &e : other.elements)
125+
full2[e.row][e.col] = e.value;
126+
127+
// Multiply
128+
for (int i = 0; i < rows; i++) {
129+
for (int j = 0; j < other.cols; j++) {
93130
int sum = 0;
94-
for(int k = 0; k < cols; k++) {
131+
for (int k = 0; k < cols; k++) {
95132
sum += full1[i][k] * full2[k][j];
96133
}
97-
if(sum != 0) result.addElement(i,j,sum);
134+
if (sum != 0) {
135+
result.addElement(i, j, sum);
136+
}
98137
}
99138
}
139+
100140
return result;
101141
}
102142
};
103143

104-
// Example usage
144+
// ------------------------
145+
// Main Function - Example Usage
146+
// ------------------------
105147
int main() {
148+
// Define Matrix A
106149
SparseMatrix A(3, 3);
107150
A.addElement(0, 0, 1);
108151
A.addElement(0, 2, 2);
109152
A.addElement(1, 1, 3);
110153

154+
// Define Matrix B
111155
SparseMatrix B(3, 3);
112156
B.addElement(0, 0, 4);
113157
B.addElement(0, 2, 5);
114158
B.addElement(2, 2, 6);
115159

116-
cout << "Matrix A:\n"; A.print();
117-
cout << "\nMatrix B:\n"; B.print();
160+
cout << "Matrix A:\n";
161+
A.print();
162+
163+
cout << "\nMatrix B:\n";
164+
B.print();
118165

166+
// Addition
167+
cout << "\nA + B:\n";
119168
SparseMatrix C = A.add(B);
120-
cout << "\nA + B:\n"; C.print();
169+
C.print();
121170

171+
// Subtraction
172+
cout << "\nA - B:\n";
122173
SparseMatrix D = A.subtract(B);
123-
cout << "\nA - B:\n"; D.print();
174+
D.print();
124175

176+
// Multiplication
177+
cout << "\nA * B:\n";
125178
SparseMatrix E = A.multiply(B);
126-
cout << "\nA * B:\n"; E.print();
179+
E.print();
127180

128181
return 0;
129182
}

0 commit comments

Comments
 (0)