-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathLimitation.cpp
More file actions
289 lines (251 loc) · 10.1 KB
/
PathLimitation.cpp
File metadata and controls
289 lines (251 loc) · 10.1 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "PathLimitation.h"
#include <queue>
#include <unordered_set>
int CheckPath(vector<int> path, map<pair<int, int>, string> transistors) {
map<string, int> VarMap;
for (int i = 0; i < path.size() - 1; i++) {
pair<int, int> edge = (path[i] > path[i + 1]) ? make_pair(path[i + 1], path[i]) : make_pair(path[i], path[i + 1]);
if (transistors.count(edge)) {
string transistor = transistors[edge];
if (transistor[0] == '-') {
transistor.erase(transistor.begin());
VarMap[transistor] |= 2;
}
else
VarMap[transistor] |= 1;
}
}
for (auto item : VarMap) {
if (item.second == 3)
return false;
}
return true;
}
int CheckPath(vector<int> path, map<int, string> transistors) {
map<string, int> VarMap;
for (int i = 0; i < path.size(); i++) {
if (transistors.count(path[i])) {
string transistor = transistors[path[i]];
if (transistor[0] == '!') {
transistor.erase(transistor.begin());
VarMap[transistor] |= 2;
}
else
VarMap[transistor] |= 1;
}
}
for (auto item : VarMap) {
if (item.second == 3)
return false;
}
return true;
}
vector<pair<pair<int, int>, string>> bfs(vector<vector<int>>& graph, int start_node, map<int, string> transistors)
{
queue<int> q;
unordered_set<int> visited;
vector<int> BfsNodes;
vector<pair<pair<int, int>, string>> transistor_pairs;
q.push(start_node);
visited.insert(start_node);
while (!q.empty()) {
int node = q.front();
q.pop();
BfsNodes.push_back(node);
// cout << node << " "; // Process the node (in this case, we print it)
for (int neighbor : graph[node]) {
if (visited.find(neighbor) == visited.end()) {
q.push(neighbor);
visited.insert(neighbor);
}
}
}
for (int i = 0; i < BfsNodes.size(); i++) {
if (transistors.count(BfsNodes[i]))
transistor_pairs.push_back(make_pair(make_pair(BfsNodes[i - 1], BfsNodes[i + 1]), transistors[BfsNodes[i]]));
}
return transistor_pairs;
}
int CountPathTransistors(vector<int> path, map<pair<int, int>, string> transistors) {
int counter = 0;
for (int i = 0; i < path.size() - 1; i++) {
pair<int, int> edge = (path[i] > path[i + 1]) ? make_pair(path[i + 1], path[i]) : make_pair(path[i], path[i + 1]);
counter += transistors.count(edge);
}
return counter;
}
vector<int> GetPathInternalEdges(vector<int> path, map<pair<int, int>, int> AllEdges) {
vector<int> InternalEdges;
for (int i = 0; i < path.size() - 1; i++) {
pair<int, int> edge = (path[i] > path[i + 1]) ? make_pair(path[i + 1], path[i]) : make_pair(path[i], path[i + 1]);
if (AllEdges.count(edge)) {
InternalEdges.push_back(AllEdges[edge]);
}
}
return InternalEdges;
}
vector<int> GetPathTransistorEdges(vector<int> path, map<pair<int, int>, int> Transistor_cnfvars) {
vector<int> TransistorEdges;
for (int i = 0; i < path.size() - 1; i++) {
pair<int, int> edge = (path[i] > path[i + 1]) ? make_pair(path[i + 1], path[i]) : make_pair(path[i], path[i + 1]);
if (Transistor_cnfvars.count(edge)) {
TransistorEdges.push_back(Transistor_cnfvars[edge]);
}
}
return TransistorEdges;
}
// Recursive function to find all simple paths in the graph
void findAllPaths(vector<vector<int>>& graph, vector<bool>& visited, int start, int end, vector<int>& path, vector<vector<int>>& paths,
map<pair<int, int>, string> transistors) {
visited[start] = true;
path.push_back(start);
// If the end node is reached, add the current path to the result
if (start == end) {
if (CheckPath(path, transistors))
paths.push_back(path);
}
else {
// Recur for all adjacent vertices
for (auto i : graph[start]) {
if (!visited[i]) {
findAllPaths(graph, visited, i, end, path, paths, transistors);
}
}
}
// Backtrack and remove the current node from the path
visited[start] = false;
path.pop_back();
}
void findAllPaths(vector<vector<int>>& graph, vector<bool>& visited, int start, int end, vector<int>& path, vector<vector<string>>& paths,
map<int, string> transistors) {
visited[start] = true;
path.push_back(start);
// If the end node is reached, add the current path to the result
if (start == end) {
if (CheckPath(path, transistors)) {
vector<string> spath;
for (auto node : path)
if (transistors.count(node))
spath.push_back(transistors[node]);
paths.push_back(spath);
}
}
else {
// Recur for all adjacent vertices
for (auto i : graph[start]) {
if (!visited[i]) {
findAllPaths(graph, visited, i, end, path, paths, transistors);
}
}
}
// Backtrack and remove the current node from the path
visited[start] = false;
path.pop_back();
}
void findAllPathIDs(vector<vector<int>>& graph, vector<bool>& visited, int start, int end, vector<int>& path, vector<vector<int>>& paths,
map<int, string> transistors) {
visited[start] = true;
path.push_back(start);
// If the end node is reached, add the current path to the result
if (start == end) {
if (CheckPath(path, transistors)) {
vector<int> TransistorPathID;
for (auto node : path)
if (transistors.count(node))
TransistorPathID.push_back(node);
paths.push_back(TransistorPathID);
}
}
else {
// Recur for all adjacent vertices
for (auto i : graph[start]) {
if (!visited[i]) {
findAllPathIDs(graph, visited, i, end, path, paths, transistors);
}
}
}
// Backtrack and remove the current node from the path
visited[start] = false;
path.pop_back();
}
// Function to find all simple paths between two nodes in a graph
vector<vector<int>> getSimplePaths(vector<vector<int>>& graph, int start, int end, map<pair<int, int>, string> transistors) {
vector<bool> visited(graph.size(), false);
vector<int> path;
vector<vector<int>> paths;
// Find all paths using DFS
findAllPaths(graph, visited, start, end, path, paths, transistors);
return paths;
}
vector<vector<string>> getSimpleTransistorPaths(vector<vector<int>>& graph, int start, int end, map<int, string>& transistors) {
vector<bool> visited(graph.size(), false);
vector<int> path;
vector<vector<string>> paths;
// Find all paths using DFS
findAllPaths(graph, visited, start, end, path, paths, transistors);
return paths;
}
vector<vector<int>> getSimpleTransistorIDPaths(vector<vector<int>>& graph, int start, int end, map<int, string>& transistors) {
vector<bool> visited(graph.size(), false);
vector<int> path;
vector<vector<int>> paths;
// Find all paths using DFS
findAllPathIDs(graph, visited, start, end, path, paths, transistors);
return paths;
}
void getLongerKPaths(vector<vector<int>>& paths, vector<vector<int>>& Kpaths, int K, map<pair<int, int>, string>& transistors) {
for (auto path : paths)
if (CountPathTransistors(path, transistors) > K)
Kpaths.push_back(path);
}
vector<vector<int>> IncrementalAddLimitedPathConstraints(map<pair<int, int>, int> AllEdges, map<pair<int, int>, string> transistors, map<pair<int, int>, int>& transistors_cnfvar, int nNodes, vector<int> Ks, vector<int> OutClusters) {
vector<vector<int>> LimitedPathClauses;
vector<vector<int>> graph(nNodes);
for (auto Edge : AllEdges) {
graph[Edge.first.first].push_back(Edge.first.second);
graph[Edge.first.second].push_back(Edge.first.first);
}
/*for (int i = 0; i < AllEdges.size(); i++) {
graph[AllEdges[i].first].push_back(AllEdges[i].second);
graph[AllEdges[i].second].push_back(AllEdges[i].first);
}*/
for (auto item : transistors) {
graph[item.first.first].push_back(item.first.second);
graph[item.first.second].push_back(item.first.first);
}
// Find all simple paths between two nodes
int i = 0;
vector<int> transistorClause;
for (auto item : transistors_cnfvar)
transistorClause.push_back(-item.second);
for (auto id : OutClusters) {
vector<vector<int>> paths = getSimplePaths(graph, 0, id, transistors);
for (auto path : paths) {
if (CountPathTransistors(path, transistors) > Ks[i]) {
vector<int> InternalEdges = GetPathInternalEdges(path, AllEdges);
// vector<int> TransistorEdges = GetPathTransistorEdges(path, transistors_cnfvar);
vector<int> clause;
/*for (auto eid : TransistorEdges)
LimitedPathClauses.push_back({ eid });*/
for (auto cnfvar : transistorClause)
clause.push_back(cnfvar);
for (auto eid : InternalEdges)
clause.push_back(-eid);
for (auto literal : clause)
cout << literal << " ";
cout << "(";
for (int j = 0; j < path.size() - 1; j++) {
pair<int, int> edge = (path[j] > path[j + 1]) ? make_pair(path[j + 1], path[j]) : make_pair(path[j], path[j + 1]);
if (transistors.count(edge)) {
cout << transistors[edge] << " ";
}
}
cout << ")";
cout << endl;
LimitedPathClauses.push_back(clause);
}
}
i++;
}
return LimitedPathClauses;
}