Подскажите где ошибка, видимо функция умножения матриц неправильно работает (скрин приложен). Что я делаю не так?
Ответ умножения разумеется должен быть такой же как и матрица А
C++ |
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
| #include <iostream>
#include <algorithm>
#include <windows.h>
using namespace std;
class Graph {
private:
bool** adjMatrix;
bool** Pmatrix;
int numVertices;
int* p; // перестановка
public:
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new bool* [numVertices];
Pmatrix = new bool* [numVertices];
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];
Pmatrix[i] = new bool[numVertices];
for (int j = 0; j < numVertices; j++) {
adjMatrix[i][j] = false;
Pmatrix[i][j] = false;
}
}
p = new int[numVertices];
for (int i = 0; i < numVertices; i++)
p[i] = i;
}
void addEdge(int i, int j) {
adjMatrix[i][j] = true;
adjMatrix[j][i] = true;
}
int* getP() {
return p;
}
bool** getAdjMatrix() {
return adjMatrix;
}
int* retPer() {
next_permutation(p, p + numVertices);
cout << "Перестановка" << endl;
for (int i = 0; i < numVertices; i++)
cout << p[i] << " ";
cout << endl;
return p;
}
bool** retPmatrix(int* p) {
for (int i = 0; i < numVertices; i++)
for (int j = 0; j < numVertices; j++)
Pmatrix[i][j] = false;
for (int i = 0; i < numVertices; i++)
Pmatrix[p[i]][i] = true;
return Pmatrix;
}
bool** traspPmatrix(bool** Pmatrix) {
bool** tPmatrix = new bool* [numVertices];
for (int i = 0; i < numVertices; i++) {
tPmatrix[i] = new bool[numVertices];
for (int j = 0; j < numVertices; j++)
tPmatrix[i][j] = Pmatrix[j][i];
}
return tPmatrix;
}
bool** mulMatrix(bool** A, bool** B) {
bool** C = new bool* [numVertices];
int i, j, k;
for (i = 0; i < numVertices; i++) {
C[i] = new bool[numVertices];
for (j = 0; j < numVertices; j++)
for (k = 0; k < numVertices; k++)
C[i][j] += A[i][k] * B[k][j];
}
return C;
}
void printMatrix(bool** Matrix) {
for (int i = 0; i < numVertices; i++) {
cout << i << " : ";
for (int j = 0; j < numVertices; j++)
cout << Matrix[i][j] << " ";
cout << endl;
}
cout << endl;
}
~Graph() {
for (int i = 0; i < numVertices; i++)
delete[] adjMatrix[i];
delete[] adjMatrix;
}
};
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
Graph g1(4);
//GRAPH1////////
g1.addEdge(0, 1);
g1.addEdge(0, 2);
g1.addEdge(1, 2);
g1.addEdge(2, 3);
cout << "Матрица смежности графа А" << endl;
g1.printMatrix(g1.getAdjMatrix());
cout << "Матрица перестановки P" << endl;
g1.printMatrix(g1.retPmatrix(g1.getP()));
cout << "Умножение PA" << endl;
g1.printMatrix(g1.mulMatrix(g1.retPmatrix(g1.getP()), g1.getAdjMatrix()));
} |
|