Skip to content

Commit 505a636

Browse files
authored
Add files via upload
1 parent c72e4ae commit 505a636

File tree

2 files changed

+259
-118
lines changed

2 files changed

+259
-118
lines changed

Sparse Matrix.cpp

Lines changed: 259 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,260 @@
1-
#include <iostream>
2-
3-
using namespace std;
4-
5-
struct Node {
6-
int row;
7-
int col;
8-
int data;
9-
};
10-
11-
struct Head {
12-
int row; // Total rows
13-
int col; // Total columns
14-
int numNodes;
15-
Node* nodes;
16-
};
17-
18-
bool isSparse(int** mat, int rows, int columns) {
19-
int zeroCounter = 0;
20-
21-
for (int i = 0; i < rows; i++) {
22-
for (int j = 0; j < columns; j++) {
23-
if (mat[i][j] == 0) {
24-
zeroCounter++;
25-
}
26-
}
27-
}
28-
29-
if (zeroCounter >= (rows * columns) / 2) {
30-
return true;
31-
}
32-
return false;
33-
}
34-
35-
Head toSparse(int** mat, int rows, int columns) {
36-
if (isSparse(mat, rows, columns)) {
37-
Head newHead;
38-
newHead.row = rows;
39-
newHead.col = columns;
40-
newHead.numNodes = 0;
41-
42-
int Counter = 0;
43-
for (int i = 0; i < rows; i++) {
44-
for (int j = 0; j < columns; j++) {
45-
if (mat[i][j] != 0) {
46-
Counter++;
47-
}
48-
}
49-
}
50-
51-
newHead.nodes = new Node[Counter];
52-
int tempIndex = 0;
53-
for (int i = 0; i < rows; i++) {
54-
for (int j = 0; j < columns; j++) {
55-
if (mat[i][j] != 0) {
56-
newHead.nodes[tempIndex].row = i;
57-
newHead.nodes[tempIndex].col = j;
58-
newHead.nodes[tempIndex].data = mat[i][j];
59-
newHead.numNodes++;
60-
tempIndex++;
61-
}
62-
}
63-
}
64-
return newHead;
65-
} else {
66-
Head newHead;
67-
newHead.row = rows;
68-
newHead.col = columns;
69-
newHead.numNodes = 0;
70-
newHead.nodes = NULL;
71-
return newHead; // Not Sparse
72-
}
73-
}
74-
75-
void display(int** mat, int rows, int columns) {
76-
for (int i = 0; i < rows; i++) {
77-
for (int j = 0; j < columns; j++) {
78-
cout << mat[i][j] << " ";
79-
}
80-
cout << endl;
81-
}
82-
}
83-
84-
void sparseDisplay(Head sparseMatrix) {
85-
cout << "Sparse Matrix:" << endl;
86-
cout << "\tRow\tColumn\tData" << endl;
87-
for (int i = 0; i < sparseMatrix.numNodes; i++) {
88-
cout << "\t" << sparseMatrix.nodes[i].row << "\t" << sparseMatrix.nodes[i].col << "\t" << sparseMatrix.nodes[i].data << endl;
89-
}
90-
}
91-
92-
int main() {
93-
int rows , columns ;
94-
cout << "Enter row & columns : " ;
95-
cin >> rows >> columns ;
96-
int** mat = new int*[rows];
97-
for (int i = 0; i < rows; i++) {
98-
mat[i] = new int[columns];
99-
for (int j = 0 ; j < columns ; j++){
100-
int tempNum ;
101-
cout << "Enter element of [" << i << "][" << j << "] : " ;
102-
cin >> tempNum ;
103-
mat[i][j] = tempNum ;
104-
}
105-
}
106-
107-
display(mat , rows , columns) ;3
108-
109-
Head sparseMatrix = toSparse(mat, rows, columns);
110-
111-
sparseDisplay(sparseMatrix);
112-
113-
return 0;
114-
}
115-
116-
/*
117-
Reza Asadi (Github : RezaGooner)
118-
1402/08/22 ~ 2023/11/13
1+
#include <iostream>
2+
#include <cassert>
3+
4+
using namespace std;
5+
6+
struct Node {
7+
int row;
8+
int col;
9+
int data;
10+
};
11+
12+
struct Head {
13+
int row; // Total rows
14+
int col; // Total columns
15+
int numNodes;
16+
Node* nodes; // nodes Array
17+
};
18+
19+
bool isSparse(int** mat, int rows, int columns) {
20+
int zeroCounter = 0;
21+
22+
for (int i = 0; i < rows; i++) {
23+
for (int j = 0; j < columns; j++) {
24+
if (mat[i][j] == 0) {
25+
zeroCounter++;
26+
}
27+
}
28+
}
29+
30+
if (zeroCounter >= (rows * columns) / 2) {
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
void display(int** mat, int rows, int columns) {
37+
for (int i = 0; i < rows; i++) {
38+
for (int j = 0; j < columns; j++) {
39+
cout << mat[i][j] << " ";
40+
}
41+
cout << endl;
42+
}
43+
}
44+
45+
void sparseDisplay(Head sparseMatrix) {
46+
cout << "Sparse Matrix:" << endl;
47+
cout << "\tRow\tColumn\tData" << endl;
48+
for (int i = 0; i < sparseMatrix.numNodes; i++) {
49+
cout << "\t" << sparseMatrix.nodes[i].row << "\t" << sparseMatrix.nodes[i].col << "\t" << sparseMatrix.nodes[i].data << endl;
50+
}
51+
}
52+
53+
Head toSparse(int** mat, int rows, int columns) {
54+
if (isSparse(mat, rows, columns)) {
55+
Head newHead;
56+
newHead.row = rows;
57+
newHead.col = columns;
58+
newHead.numNodes = 0;
59+
60+
int Counter = 0;
61+
for (int i = 0; i < rows; i++) {
62+
for (int j = 0; j < columns; j++) {
63+
if (mat[i][j] != 0) {
64+
Counter++;
65+
}
66+
}
67+
}
68+
69+
newHead.nodes = new Node[Counter];
70+
int tempIndex = 0;
71+
for (int i = 0; i < rows; i++) {
72+
for (int j = 0; j < columns; j++) {
73+
if (mat[i][j] != 0) {
74+
newHead.nodes[tempIndex].row = i;
75+
newHead.nodes[tempIndex].col = j;
76+
newHead.nodes[tempIndex].data = mat[i][j];
77+
newHead.numNodes++;
78+
tempIndex++;
79+
}
80+
}
81+
}
82+
return newHead;
83+
} else {
84+
Head newHead;
85+
newHead.row = rows;
86+
newHead.col = columns;
87+
newHead.numNodes = 0;
88+
newHead.nodes = NULL;
89+
return newHead; // Not Sparse
90+
}
91+
}
92+
93+
94+
95+
Head createSMatrix(){
96+
int rows , columns ;
97+
cout << "Enter row & columns : " ;
98+
cin >> rows >> columns ;
99+
int** mat = new int*[rows];
100+
for (int i = 0; i < rows; i++) {
101+
mat[i] = new int[columns];
102+
for (int j = 0 ; j < columns ; j++){
103+
int tempNum ;
104+
cout << "Enter element of [" << i << "][" << j << "] : " ;
105+
cin >> tempNum ;
106+
mat[i][j] = tempNum ;
107+
}
108+
}
109+
110+
display(mat , rows , columns) ;
111+
112+
Head sparseMatrix = toSparse(mat, rows, columns);
113+
114+
sparseDisplay(sparseMatrix);
115+
116+
return sparseMatrix ;
117+
}
118+
119+
Head adder(Head head1, Head head2) {
120+
assert(head1.row == head2.row && head1.col == head2.col);
121+
122+
Head newHead;
123+
newHead.row = head1.row;
124+
newHead.col = head1.col;
125+
newHead.numNodes = 0;
126+
newHead.nodes = new Node[head1.numNodes + head2.numNodes];
127+
128+
int index1 = 0, index2 = 0, newIndex = 0;
129+
while (index1 < head1.numNodes && index2 < head2.numNodes) {
130+
if (head1.nodes[index1].row < head2.nodes[index2].row
131+
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col < head2.nodes[index2].col)) {
132+
newHead.nodes[newIndex].row = head1.nodes[index1].row;
133+
newHead.nodes[newIndex].col = head1.nodes[index1].col;
134+
newHead.nodes[newIndex].data = head1.nodes[index1].data;
135+
index1++;
136+
} else if (head1.nodes[index1].row > head2.nodes[index2].row
137+
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col > head2.nodes[index2].col)) {
138+
newHead.nodes[newIndex].row = head2.nodes[index2].row;
139+
newHead.nodes[newIndex].col = head2.nodes[index2].col;
140+
newHead.nodes[newIndex].data = head2.nodes[index2].data;
141+
index2++;
142+
} else {
143+
newHead.nodes[newIndex].row = head1.nodes[index1].row;
144+
newHead.nodes[newIndex].col = head1.nodes[index1].col;
145+
newHead.nodes[newIndex].data = head1.nodes[index1].data + head2.nodes[index2].data;
146+
index1++;
147+
index2++;
148+
}
149+
newIndex++;
150+
newHead.numNodes++;
151+
}
152+
153+
// Copy remaining nodes from head1
154+
while (index1 < head1.numNodes) {
155+
newHead.nodes[newIndex].row = head1.nodes[index1].row;
156+
newHead.nodes[newIndex].col = head1.nodes[index1].col;
157+
newHead.nodes[newIndex].data = head1.nodes[index1].data;
158+
index1++;
159+
newIndex++;
160+
newHead.numNodes++;
161+
}
162+
163+
// Copy remaining nodes from head2
164+
while (index2 < head2.numNodes) {
165+
newHead.nodes[newIndex].row = head2.nodes[index2].row;
166+
newHead.nodes[newIndex].col = head2.nodes[index2].col;
167+
newHead.nodes[newIndex].data = head2.nodes[index2].data;
168+
index2++;
169+
newIndex++;
170+
newHead.numNodes++;
171+
}
172+
173+
if (newHead.numNodes >= ((newHead.row * newHead.col)/2) ){
174+
cout << "New matrix isn't sparse!" << endl ;
175+
}
176+
return newHead;
177+
}
178+
179+
Head subtractor(Head head1, Head head2) {
180+
assert(head1.row == head2.row && head1.col == head2.col);
181+
182+
Head newHead;
183+
newHead.row = head1.row;
184+
newHead.col = head1.col;
185+
newHead.numNodes = 0;
186+
newHead.nodes = new Node[head1.numNodes + head2.numNodes];
187+
188+
int index1 = 0, index2 = 0, newIndex = 0;
189+
while (index1 < head1.numNodes && index2 < head2.numNodes) {
190+
if (head1.nodes[index1].row < head2.nodes[index2].row
191+
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col < head2.nodes[index2].col)) {
192+
newHead.nodes[newIndex].row = head1.nodes[index1].row;
193+
newHead.nodes[newIndex].col = head1.nodes[index1].col;
194+
newHead.nodes[newIndex].data = head1.nodes[index1].data;
195+
index1++;
196+
} else if (head1.nodes[index1].row > head2.nodes[index2].row
197+
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col > head2.nodes[index2].col)) {
198+
newHead.nodes[newIndex].row = head2.nodes[index2].row;
199+
newHead.nodes[newIndex].col = head2.nodes[index2].col;
200+
newHead.nodes[newIndex].data = -head2.nodes[index2].data; // ????? ???????
201+
index2++;
202+
} else {
203+
newHead.nodes[newIndex].row = head1.nodes[index1].row;
204+
newHead.nodes[newIndex].col = head1.nodes[index1].col;
205+
newHead.nodes[newIndex].data = head1.nodes[index1].data - head2.nodes[index2].data; // ????? ???????
206+
index1++;
207+
index2++;
208+
}
209+
newIndex++;
210+
newHead.numNodes++;
211+
}
212+
213+
// ??? ???? ???? ??????? ?? head1
214+
while (index1 < head1.numNodes) {
215+
newHead.nodes[newIndex].row = head1.nodes[index1].row;
216+
newHead.nodes[newIndex].col = head1.nodes[index1].col;
217+
newHead.nodes[newIndex].data = head1.nodes[index1].data;
218+
index1++;
219+
newIndex++;
220+
newHead.numNodes++;
221+
}
222+
223+
// ??? ???? ???? ??????? ?? head2
224+
while (index2 < head2.numNodes) {
225+
newHead.nodes[newIndex].row = head2.nodes[index2].row;
226+
newHead.nodes[newIndex].col = head2.nodes[index2].col;
227+
newHead.nodes[newIndex].data = -head2.nodes[index2].data; // ????? ???????
228+
index2++;
229+
newIndex++;
230+
newHead.numNodes++;
231+
}
232+
233+
if (newHead.numNodes >= ((newHead.row * newHead.col)/2) ){
234+
cout << "New matrix isn't sparse!" << endl ;
235+
}
236+
return newHead;
237+
}
238+
239+
void scalarProduct(Head* head, int num) {
240+
for (int i = 0; i < head->numNodes; i++) {
241+
head->nodes[i].data *= num;
242+
}
243+
}
244+
245+
int main() {
246+
//Head mat1 = createSMatrix();
247+
//Head mat2 = createSMatrix();
248+
249+
//scalarProduct(&mat1, 2);
250+
//sparseDisplay(adder(mat1 , mat2));
251+
//sparseDisplay(subtractor(mat1 , mat2));
252+
253+
return 0;
254+
}
255+
256+
/*
257+
Reza Asadi (Github : RezaGooner)
258+
1402/08/22 ~ 2023/11/13
259+
Edited : 08/24 ~ 11/15
119260
*/

Sparse Matrix.exe

4.32 KB
Binary file not shown.

0 commit comments

Comments
 (0)