|
| 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