Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions CPP/data_structures/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <vector>
using namespace std;

// Simplified version for demonstration

struct Element {
int row, col, value;
Element(int r, int c, int v) : row(r), col(c), value(v) {}
};

class SparseMatrix {
private:
vector<Element> elements;
int rows, cols;

public:
SparseMatrix(int r, int c) : rows(r), cols(c) {}

void addElement(int row, int col, int value) {
if (value != 0) {
elements.push_back(Element(row, col, value));
}
}

void display() const {
cout << "Sparse Matrix (" << rows << "x" << cols << "):\n";
cout << "Row\tCol\tValue\n";
for (const auto& elem : elements) {
cout << elem.row << "\t" << elem.col << "\t" << elem.value << endl;
}
cout << endl;
}

void displayFull() const {
cout << "Full Matrix:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int value = 0;
for (const auto& elem : elements) {
if (elem.row == i && elem.col == j) {
value = elem.value;
break;
}
}
cout << value << " ";
}
cout << endl;
}
cout << endl;
}
};

int main() {
cout << "=== SPARSE MATRIX EXAMPLE ===\n\n";

// Create first matrix: 3x3
// 1 0 2
// 0 0 0
// 3 0 4
SparseMatrix matrix1(3, 3);
matrix1.addElement(0, 0, 1);
matrix1.addElement(0, 2, 2);
matrix1.addElement(2, 0, 3);
matrix1.addElement(2, 2, 4);

cout << "Matrix 1:\n";
matrix1.display();
matrix1.displayFull();

// Create second matrix: 3x3
// 0 5 0
// 6 0 7
// 0 8 0
SparseMatrix matrix2(3, 3);
matrix2.addElement(0, 1, 5);
matrix2.addElement(1, 0, 6);
matrix2.addElement(1, 2, 7);
matrix2.addElement(2, 1, 8);

cout << "Matrix 2:\n";
matrix2.display();
matrix2.displayFull();

cout << "For complete operations (addition, multiplication, etc.),\n";
cout << "please run the main program: sparse_matrix_operations.cpp\n";

return 0;
}
139 changes: 139 additions & 0 deletions CPP/data_structures/sparse_matrix_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Sparse Matrix Operations in C++

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.

## Features

- **Addition**: Add two sparse matrices of the same dimensions
- **Subtraction**: Subtract one sparse matrix from another
- **Multiplication**: Matrix multiplication following standard rules
- **Element-wise Division**: Divide corresponding elements (not standard matrix division)
- **Transpose**: Get the transpose of a sparse matrix

## Data Structure

The program uses a triplet representation where each non-zero element is stored as:
- Row index
- Column index
- Value

## How to Compile and Run

```bash
g++ -o sparse_matrix_operations sparse_matrix_operations.cpp
./sparse_matrix_operations
```

## Usage Examples

### Example 1: Matrix Addition
```
Matrix A (3x3):
1 0 2
0 0 0
3 0 4

Matrix B (3x3):
0 5 0
6 0 7
0 8 0

Result A + B:
1 5 2
6 0 7
3 8 4
```

### Example 2: Matrix Multiplication
```
Matrix A (2x3):
1 0 2
0 3 0

Matrix B (3x2):
4 0
0 5
6 0

Result A × B:
16 0
0 15
```

### Input Format

When prompted, enter:
1. Number of rows
2. Number of columns
3. Number of non-zero elements
4. For each non-zero element: row_index column_index value

**Note**: Indices are 0-based (start from 0)

### Example Input Session
```
Enter number of rows: 3
Enter number of columns: 3
Enter number of non-zero elements: 4
Element 1: 0 0 1
Element 2: 0 2 2
Element 3: 2 0 3
Element 4: 2 2 4
```

## Operations Details

### Addition and Subtraction
- Matrices must have the same dimensions
- Corresponding elements are added/subtracted
- Result maintains sparse representation

### Multiplication
- First matrix columns must equal second matrix rows
- Standard matrix multiplication algorithm
- Efficiently handles sparse matrices

### Element-wise Division
- Matrices must have same dimensions
- Divides corresponding non-zero elements
- Warns about division by zero

### Transpose
- Swaps rows and columns
- Dimensions change from m×n to n×m

## Memory Efficiency

The sparse matrix representation is memory efficient:
- Only stores non-zero elements
- Space complexity: O(number of non-zero elements)
- Ideal for matrices with many zeros

## Time Complexity

- **Addition/Subtraction**: O(m + n) where m, n are non-zero elements in each matrix
- **Multiplication**: O(m × n) where m, n are non-zero elements in each matrix
- **Transpose**: O(k) where k is number of non-zero elements

## Error Handling

The program includes error handling for:
- Invalid matrix dimensions for operations
- Division by zero
- Invalid array indices
- Memory allocation issues

## Limitations

- Element-wise division is not standard matrix division
- Integer arithmetic only (can be extended to floating-point)
- No matrix inversion or other advanced operations

## Future Enhancements

Possible improvements:
- Support for floating-point numbers
- Matrix determinant calculation
- Matrix inversion for invertible matrices
- File I/O for large matrices
- Optimized algorithms for very large sparse matrices
Loading
Loading