Skip to content

Commit 3c4c397

Browse files
Merge pull request #202 from AnirudhPhophalia/sparsematrix_cpp_anirudhphophalia
Add sparse matrix operations implementation and documentation
2 parents 90611e9 + dda4bf3 commit 3c4c397

File tree

3 files changed

+628
-0
lines changed

3 files changed

+628
-0
lines changed

CPP/data_structures/example.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
// Simplified version for demonstration
6+
struct Element {
7+
int row, col, value;
8+
Element(int r, int c, int v) : row(r), col(c), value(v) {}
9+
};
10+
11+
class SparseMatrix {
12+
private:
13+
vector<Element> elements;
14+
int rows, cols;
15+
16+
public:
17+
SparseMatrix(int r, int c) : rows(r), cols(c) {}
18+
19+
void addElement(int row, int col, int value) {
20+
if (value != 0) {
21+
elements.push_back(Element(row, col, value));
22+
}
23+
}
24+
25+
void display() const {
26+
cout << "Sparse Matrix (" << rows << "x" << cols << "):\n";
27+
cout << "Row\tCol\tValue\n";
28+
for (const auto& elem : elements) {
29+
cout << elem.row << "\t" << elem.col << "\t" << elem.value << endl;
30+
}
31+
cout << endl;
32+
}
33+
34+
void displayFull() const {
35+
cout << "Full Matrix:\n";
36+
for (int i = 0; i < rows; i++) {
37+
for (int j = 0; j < cols; j++) {
38+
int value = 0;
39+
for (const auto& elem : elements) {
40+
if (elem.row == i && elem.col == j) {
41+
value = elem.value;
42+
break;
43+
}
44+
}
45+
cout << value << " ";
46+
}
47+
cout << endl;
48+
}
49+
cout << endl;
50+
}
51+
};
52+
53+
int main() {
54+
cout << "=== SPARSE MATRIX EXAMPLE ===\n\n";
55+
56+
// Create first matrix: 3x3
57+
// 1 0 2
58+
// 0 0 0
59+
// 3 0 4
60+
SparseMatrix matrix1(3, 3);
61+
matrix1.addElement(0, 0, 1);
62+
matrix1.addElement(0, 2, 2);
63+
matrix1.addElement(2, 0, 3);
64+
matrix1.addElement(2, 2, 4);
65+
66+
cout << "Matrix 1:\n";
67+
matrix1.display();
68+
matrix1.displayFull();
69+
70+
// Create second matrix: 3x3
71+
// 0 5 0
72+
// 6 0 7
73+
// 0 8 0
74+
SparseMatrix matrix2(3, 3);
75+
matrix2.addElement(0, 1, 5);
76+
matrix2.addElement(1, 0, 6);
77+
matrix2.addElement(1, 2, 7);
78+
matrix2.addElement(2, 1, 8);
79+
80+
cout << "Matrix 2:\n";
81+
matrix2.display();
82+
matrix2.displayFull();
83+
84+
cout << "For complete operations (addition, multiplication, etc.),\n";
85+
cout << "please run the main program: sparse_matrix_operations.cpp\n";
86+
87+
return 0;
88+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Sparse Matrix Operations in C++
2+
3+
This program implements various operations on sparse matrices using C++. A sparse matrix is a matrix that contains mostly zero elements, making it memory-efficient to store only the non-zero elements.
4+
5+
## Features
6+
7+
- **Addition**: Add two sparse matrices of the same dimensions
8+
- **Subtraction**: Subtract one sparse matrix from another
9+
- **Multiplication**: Matrix multiplication following standard rules
10+
- **Element-wise Division**: Divide corresponding elements (not standard matrix division)
11+
- **Transpose**: Get the transpose of a sparse matrix
12+
13+
## Data Structure
14+
15+
The program uses a triplet representation where each non-zero element is stored as:
16+
- Row index
17+
- Column index
18+
- Value
19+
20+
## How to Compile and Run
21+
22+
```bash
23+
g++ -o sparse_matrix_operations sparse_matrix_operations.cpp
24+
./sparse_matrix_operations
25+
```
26+
27+
## Usage Examples
28+
29+
### Example 1: Matrix Addition
30+
```
31+
Matrix A (3x3):
32+
1 0 2
33+
0 0 0
34+
3 0 4
35+
36+
Matrix B (3x3):
37+
0 5 0
38+
6 0 7
39+
0 8 0
40+
41+
Result A + B:
42+
1 5 2
43+
6 0 7
44+
3 8 4
45+
```
46+
47+
### Example 2: Matrix Multiplication
48+
```
49+
Matrix A (2x3):
50+
1 0 2
51+
0 3 0
52+
53+
Matrix B (3x2):
54+
4 0
55+
0 5
56+
6 0
57+
58+
Result A × B:
59+
16 0
60+
0 15
61+
```
62+
63+
### Input Format
64+
65+
When prompted, enter:
66+
1. Number of rows
67+
2. Number of columns
68+
3. Number of non-zero elements
69+
4. For each non-zero element: row_index column_index value
70+
71+
**Note**: Indices are 0-based (start from 0)
72+
73+
### Example Input Session
74+
```
75+
Enter number of rows: 3
76+
Enter number of columns: 3
77+
Enter number of non-zero elements: 4
78+
Element 1: 0 0 1
79+
Element 2: 0 2 2
80+
Element 3: 2 0 3
81+
Element 4: 2 2 4
82+
```
83+
84+
## Operations Details
85+
86+
### Addition and Subtraction
87+
- Matrices must have the same dimensions
88+
- Corresponding elements are added/subtracted
89+
- Result maintains sparse representation
90+
91+
### Multiplication
92+
- First matrix columns must equal second matrix rows
93+
- Standard matrix multiplication algorithm
94+
- Efficiently handles sparse matrices
95+
96+
### Element-wise Division
97+
- Matrices must have same dimensions
98+
- Divides corresponding non-zero elements
99+
- Warns about division by zero
100+
101+
### Transpose
102+
- Swaps rows and columns
103+
- Dimensions change from m×n to n×m
104+
105+
## Memory Efficiency
106+
107+
The sparse matrix representation is memory efficient:
108+
- Only stores non-zero elements
109+
- Space complexity: O(number of non-zero elements)
110+
- Ideal for matrices with many zeros
111+
112+
## Time Complexity
113+
114+
- **Addition/Subtraction**: O(m + n) where m, n are non-zero elements in each matrix
115+
- **Multiplication**: O(m × n) where m, n are non-zero elements in each matrix
116+
- **Transpose**: O(k) where k is number of non-zero elements
117+
118+
## Error Handling
119+
120+
The program includes error handling for:
121+
- Invalid matrix dimensions for operations
122+
- Division by zero
123+
- Invalid array indices
124+
- Memory allocation issues
125+
126+
## Limitations
127+
128+
- Element-wise division is not standard matrix division
129+
- Integer arithmetic only (can be extended to floating-point)
130+
- No matrix inversion or other advanced operations
131+
132+
## Future Enhancements
133+
134+
Possible improvements:
135+
- Support for floating-point numbers
136+
- Matrix determinant calculation
137+
- Matrix inversion for invertible matrices
138+
- File I/O for large matrices
139+
- Optimized algorithms for very large sparse matrices

0 commit comments

Comments
 (0)