|
1 | 1 | #include <bits/stdc++.h> |
2 | 2 | using namespace std; |
3 | 3 |
|
| 4 | +/* |
| 5 | +========================================== |
| 6 | + Sparse Matrix - Basic Operations |
| 7 | + Operations: Addition, Subtraction, Multiplication |
| 8 | + Representation: Triplet (row, col, value) |
| 9 | +========================================== |
| 10 | +*/ |
| 11 | + |
4 | 12 | // Structure to store a non-zero element of a sparse matrix |
5 | 13 | struct Element { |
6 | 14 | int row; |
7 | 15 | int col; |
8 | 16 | int value; |
9 | 17 | }; |
10 | 18 |
|
11 | | -// Class to represent a sparse matrix |
| 19 | +// Class to represent a Sparse Matrix |
12 | 20 | class SparseMatrix { |
13 | 21 | 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 |
16 | 24 |
|
17 | 25 | public: |
| 26 | + // Constructor to initialize the matrix with given rows and columns |
18 | 27 | SparseMatrix(int r, int c) { |
19 | 28 | rows = r; |
20 | 29 | cols = c; |
21 | | - nonZeroCount = 0; |
22 | 30 | } |
23 | 31 |
|
24 | | - // Add a non-zero element |
| 32 | + // Add a non-zero element to the sparse matrix |
25 | 33 | void addElement(int r, int c, int val) { |
26 | | - if(val != 0) { |
| 34 | + if (val != 0) { |
27 | 35 | elements.push_back({r, c, val}); |
28 | | - nonZeroCount++; |
29 | 36 | } |
30 | 37 | } |
31 | 38 |
|
32 | | - // Print sparse matrix |
33 | | - void print() { |
| 39 | + // Print the full matrix (converts sparse → dense for display) |
| 40 | + void print() const { |
34 | 41 | vector<vector<int>> full(rows, vector<int>(cols, 0)); |
35 | | - for(auto &e : elements) { |
| 42 | + for (const auto &e : elements) { |
36 | 43 | full[e.row][e.col] = e.value; |
37 | 44 | } |
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++) { |
40 | 47 | cout << full[i][j] << " "; |
41 | 48 | } |
42 | 49 | cout << endl; |
43 | 50 | } |
44 | 51 | } |
45 | 52 |
|
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) { |
49 | 58 | throw invalid_argument("Matrix dimensions must match for addition."); |
50 | 59 | } |
| 60 | + |
51 | 61 | SparseMatrix result(rows, cols); |
52 | 62 | int i = 0, j = 0; |
53 | | - while(i < elements.size() && j < other.elements.size()) { |
| 63 | + |
| 64 | + while (i < elements.size() && j < other.elements.size()) { |
54 | 65 | int pos1 = elements[i].row * cols + elements[i].col; |
55 | 66 | int pos2 = other.elements[j].row * cols + other.elements[j].col; |
56 | | - if(pos1 < pos2) { |
| 67 | + |
| 68 | + if (pos1 < pos2) { |
57 | 69 | result.addElement(elements[i].row, elements[i].col, elements[i].value); |
58 | 70 | i++; |
59 | | - } else if(pos1 > pos2) { |
| 71 | + } else if (pos1 > pos2) { |
60 | 72 | result.addElement(other.elements[j].row, other.elements[j].col, other.elements[j].value); |
61 | 73 | j++; |
62 | 74 | } else { |
63 | 75 | 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 | + } |
65 | 79 | i++; j++; |
66 | 80 | } |
67 | 81 | } |
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 | + |
70 | 92 | return result; |
71 | 93 | } |
72 | 94 |
|
73 | | - // Subtraction of two sparse matrices |
74 | | - SparseMatrix subtract(SparseMatrix &other) { |
| 95 | + // ------------------------ |
| 96 | + // Matrix Subtraction |
| 97 | + // ------------------------ |
| 98 | + SparseMatrix subtract(const SparseMatrix &other) const { |
75 | 99 | 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 | + } |
77 | 104 | return add(negOther); |
78 | 105 | } |
79 | 106 |
|
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."); |
84 | 113 | } |
| 114 | + |
85 | 115 | 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 | 116 |
|
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++) { |
93 | 130 | int sum = 0; |
94 | | - for(int k = 0; k < cols; k++) { |
| 131 | + for (int k = 0; k < cols; k++) { |
95 | 132 | sum += full1[i][k] * full2[k][j]; |
96 | 133 | } |
97 | | - if(sum != 0) result.addElement(i,j,sum); |
| 134 | + if (sum != 0) { |
| 135 | + result.addElement(i, j, sum); |
| 136 | + } |
98 | 137 | } |
99 | 138 | } |
| 139 | + |
100 | 140 | return result; |
101 | 141 | } |
102 | 142 | }; |
103 | 143 |
|
104 | | -// Example usage |
| 144 | +// ------------------------ |
| 145 | +// Main Function - Example Usage |
| 146 | +// ------------------------ |
105 | 147 | int main() { |
| 148 | + // Define Matrix A |
106 | 149 | SparseMatrix A(3, 3); |
107 | 150 | A.addElement(0, 0, 1); |
108 | 151 | A.addElement(0, 2, 2); |
109 | 152 | A.addElement(1, 1, 3); |
110 | 153 |
|
| 154 | + // Define Matrix B |
111 | 155 | SparseMatrix B(3, 3); |
112 | 156 | B.addElement(0, 0, 4); |
113 | 157 | B.addElement(0, 2, 5); |
114 | 158 | B.addElement(2, 2, 6); |
115 | 159 |
|
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(); |
118 | 165 |
|
| 166 | + // Addition |
| 167 | + cout << "\nA + B:\n"; |
119 | 168 | SparseMatrix C = A.add(B); |
120 | | - cout << "\nA + B:\n"; C.print(); |
| 169 | + C.print(); |
121 | 170 |
|
| 171 | + // Subtraction |
| 172 | + cout << "\nA - B:\n"; |
122 | 173 | SparseMatrix D = A.subtract(B); |
123 | | - cout << "\nA - B:\n"; D.print(); |
| 174 | + D.print(); |
124 | 175 |
|
| 176 | + // Multiplication |
| 177 | + cout << "\nA * B:\n"; |
125 | 178 | SparseMatrix E = A.multiply(B); |
126 | | - cout << "\nA * B:\n"; E.print(); |
| 179 | + E.print(); |
127 | 180 |
|
128 | 181 | return 0; |
129 | 182 | } |
0 commit comments