-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplication_Of_2_Sparse_Matrices.cpp
More file actions
178 lines (145 loc) · 5.29 KB
/
Multiplication_Of_2_Sparse_Matrices.cpp
File metadata and controls
178 lines (145 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
using namespace std;
// Define a struct to represent a sparse matrix element
struct SparseMatrix {
int row; // Row index of the element
int col; // Column index of the element
int value; // Value of the element
};
// Function to create a sparse matrix from a given dense matrix
SparseMatrix* createSparseMatrix(int** matrix, int numRows, int numCols) {
int numNonZero = 0; // Count of non-zero elements in the matrix
// Count the non-zero elements in the dense matrix
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numCols; ++j) {
if (matrix[i][j] != 0) {
numNonZero++;
}
}
}
// Create an array of SparseMatrix structs to store the non-zero elements
SparseMatrix* sparse = new SparseMatrix[numNonZero + 1];
// Store metadata in the first element (index 0) of the sparse matrix
sparse[0].row = numRows;
sparse[0].col = numCols;
sparse[0].value = numNonZero;
int count = 1; // Counter for iterating through the sparse matrix
// Populate the sparse matrix with non-zero elements
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numCols; ++j) {
if (matrix[i][j] != 0) {
sparse[count].row = i;
sparse[count].col = j;
sparse[count].value = matrix[i][j];
count++;
}
}
}
return sparse;
}
// Function to print a sparse matrix
void printSparseMatrix(SparseMatrix* sparse) {
int numRows = sparse[0].row;
int numCols = sparse[0].col;
int numNonZero = sparse[0].value;
cout << "Sparse Matrix:" << endl;
cout << "Row\tColumn\tValue" << endl;
// Iterate through the sparse matrix and print its elements
for (int i = 0; i <= numNonZero; ++i) {
cout << sparse[i].row << "\t" << sparse[i].col << "\t" << sparse[i].value << endl;
}
}
// Function to multiply two sparse matrices
void multiplySparseMatrices(SparseMatrix* A, SparseMatrix* B) {
int numRowsA = A[0].row;
int numColsA = A[0].col;
int numRowsB = B[0].row;
int numColsB = B[0].col;
// Check if matrix multiplication is possible
if (numColsA != numRowsB) {
cout << "Matrix multiplication not possible!" << endl;
return;
}
// Create a result matrix to store the multiplication result
int** result = new int*[numRowsA];
for (int i = 0; i < numRowsA; ++i) {
result[i] = new int[numColsB];
// Initialize result matrix elements to zero
for (int j = 0; j < numColsB; ++j) {
result[i][j] = 0;
// Iterate through sparse matrices A and B and perform multiplication
for (int k = 1; k <= A[0].value; ++k) {
if (A[k].row == i && B[k].col == j) {
result[i][j] += A[k].value * B[k].value;
}
}
}
}
// Print the resultant matrix
cout << "Resultant Matrix:" << endl;
for (int i = 0; i < numRowsA; ++i) {
for (int j = 0; j < numColsB; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}
// Clean up dynamically allocated memory
for (int i = 0; i < numRowsA; ++i) {
delete[] result[i];
}
delete[] result;
}
int main() {
int numRowsA, numColsA, numRowsB, numColsB;
// Input matrix dimensions
cout << "Enter the number of rows and columns for Matrix A: ";
cin >> numRowsA >> numColsA;
cout << "Enter the number of rows and columns for Matrix B: ";
cin >> numRowsB >> numColsB;
// Check if matrix multiplication is possible
if (numColsA != numRowsB) {
cout << "Matrix multiplication not possible!" << endl;
return 1;
}
// Allocate memory for dense matrices
int** matrixA = new int*[numRowsA];
int** matrixB = new int*[numRowsB];
// Input elements for Matrix A
cout << "Enter the elements of Matrix A:" << endl;
for (int i = 0; i < numRowsA; ++i) {
matrixA[i] = new int[numColsA];
for (int j = 0; j < numColsA; ++j) {
cin >> matrixA[i][j];
}
}
// Input elements for Matrix B
cout << "Enter the elements of Matrix B:" << endl;
for (int i = 0; i < numRowsB; ++i) {
matrixB[i] = new int[numColsB];
for (int j = 0; j < numColsB; ++j) {
cin >> matrixB[i][j];
}
}
// Create sparse matrices from dense matrices
SparseMatrix* sparseA = createSparseMatrix(matrixA, numRowsA, numColsA);
SparseMatrix* sparseB = createSparseMatrix(matrixB, numRowsB, numColsB);
// Print sparse matrices
cout << "Sparse Matrix A:" << endl;
printSparseMatrix(sparseA);
cout << "Sparse Matrix B:" << endl;
printSparseMatrix(sparseB);
// Multiply sparse matrices and print the result
multiplySparseMatrices(sparseA, sparseB);
// Clean up dynamically allocated memory
for (int i = 0; i < numRowsA; ++i) {
delete[] matrixA[i];
}
delete[] matrixA;
for (int i = 0; i < numRowsB; ++i) {
delete[] matrixB[i];
}
delete[] matrixB;
delete[] sparseA;
delete[] sparseB;
return 0;
}