-
Notifications
You must be signed in to change notification settings - Fork 0
/
vf2.hpp
217 lines (174 loc) · 5.14 KB
/
vf2.hpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "graph.hpp"
#include <algorithm>
#include <map>
#include <utility>
#include <vector>
using namespace std;
// 参数T代表图的结点类型
template <typename T>
class Vf2 {
public:
typedef pair<T, T> Mapping;
typedef vector<Mapping> MappingSet;
typedef vector<T> PointSet;
typedef Graph<T> Vf2Graph;
Vf2(const Vf2Graph& g1, const Vf2Graph& g2)
: g1(g1)
, g2(g2) {
}
// 返回值代表不重复的解的个数
int vf2(const MappingSet& state) {
if (isCoversG2(state)) {
if (isResult(state)) {
results.push_back(state);
return 1;
}
return 0;
}
MappingSet ps = buildPs(state);
int ret = 0;
for (auto pr : ps) {
ret += vf2(add(state, pr.first, pr.second));
}
// restore data structure
return ret;
}
vector<MappingSet> getResults() {
return results;
}
static vector<MappingSet> runV2f(const Vf2Graph& g1, const Vf2Graph& g2) {
Vf2 algorObj(g1, g2);
algorObj.vf2(MappingSet {});
return algorObj.getResults();
}
private:
Vf2Graph g1, g2;
vector<MappingSet> results;
PointSet getFirstOfMappingSet(const MappingSet& state) {
PointSet ret;
for (auto p : state) {
ret.push_back(p.first);
}
return ret;
}
PointSet getSecondOfMappingSet(const MappingSet& state) {
PointSet ret;
for (auto p : state) {
ret.push_back(p.second);
}
return ret;
}
vector<T> diff(const vector<T>& arr1, const vector<T>& arr2) {
vector<T> ret;
for (T it : arr1) {
if (find(arr2.begin(), arr2.end(), it) == arr2.end()) {
ret.push_back(it);
}
}
return ret;
}
PointSet getTerminalPoint(Vf2Graph& g, const PointSet& state) {
PointSet ret;
for (T pt : g.V) {
if (find(state.begin(), state.end(), pt) != state.end()) {
continue;
}
for (T pt1 : state) {
if (g.isVertexConneted(pt, pt1) > 0) {
ret.push_back(pt);
}
}
}
return ret;
}
// M是否包含整个G2
bool isCoversG2(const MappingSet& state) {
map<T, bool> core;
for (auto pr : state) {
core[pr.second] = true;
}
for (T pt : g2.V) {
if (!core[pt]) {
return false;
}
}
return true;
}
// 生成Ps集合
MappingSet buildPs(const MappingSet& state) {
MappingSet ps;
PointSet M1 = getFirstOfMappingSet(state);
PointSet M2 = getSecondOfMappingSet(state);
PointSet T1 = getTerminalPoint(g1, M1);
PointSet T2 = getTerminalPoint(g2, M2);
if (T1.size() > 0 && T2.size() > 0) {
T minValue = *min_element(T2.begin(), T2.end());
for (T pt : T1) {
if (F(state, pt, minValue, M1, M2, T1, T2)) { // 这里调用F 函数过滤,防止了重复构造 M1 M2 T1 T2
ps.push_back({pt, minValue});
}
}
return ps;
}
PointSet diff1 = diff(g1.V, M1), diff2 = diff(g2.V, M2);
T minValue = *min_element(diff2.begin(), diff2.end());
for (T pt : diff1) {
if (F(state, pt, minValue, M1, M2, T1, T2)) {
ps.push_back({pt, minValue});
}
}
return ps;
}
// 状态函数
bool F(const MappingSet& state, T n, T m,
PointSet M1,
PointSet M2,
PointSet T1,
PointSet T2) {
auto g1TLen = T1.size();
auto g2TLen = T2.size();
PointSet diff1 = diff(g1.V, M1), diff2 = diff(g2.V, M2);
diff1 = diff(diff1, T1);
diff2 = diff(diff2, T2);
return g1TLen >= g2TLen && diff1.size() >= diff2.size();
}
MappingSet add(MappingSet M, T n, T m) {
M.push_back({n, m});
return M;
}
bool MappingsEq(const MappingSet& mp1, const MappingSet& mp2) {
if (mp1.size() != mp2.size()) {
return false;
}
for (size_t i = 0; i < mp1.size(); i++) {
if (mp1[i].first != mp2[i].first) {
return false;
}
if (mp1[i].second != mp2[i].second) {
return false;
}
}
return true;
}
bool inResult(const MappingSet& state) {
for (MappingSet res : results) {
for (int i = 0; i < state.size(); i++) {
if (MappingsEq(res, state)) {
return true;
}
}
}
return false;
}
bool isResult(const MappingSet& state) {
for (Mapping p1 : state) {
for (Mapping p2 : state) {
if (g1.isVertexConneted(p1.first, p2.first) != g2.isVertexConneted(p1.second, p2.second)) {
return false;
}
}
}
// 因为是无向图算法可能产生重复解
return !inResult(state);
}
};