dwave-preprocessing 0.6.7__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dwave/__init__.py +16 -0
- dwave/preprocessing/__init__.py +42 -0
- dwave/preprocessing/composites/__init__.py +19 -0
- dwave/preprocessing/composites/clip.py +114 -0
- dwave/preprocessing/composites/connected_components.py +113 -0
- dwave/preprocessing/composites/fix_variables.py +153 -0
- dwave/preprocessing/composites/scale.py +144 -0
- dwave/preprocessing/composites/spin_reversal_transform.py +221 -0
- dwave/preprocessing/cyfix_variables.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/cyfix_variables.cpp +29498 -0
- dwave/preprocessing/cyfix_variables.pyx +58 -0
- dwave/preprocessing/include/dwave/exceptions.hpp +28 -0
- dwave/preprocessing/include/dwave/flags.hpp +63 -0
- dwave/preprocessing/include/dwave/presolve.hpp +91 -0
- dwave/preprocessing/include/dwave-preprocessing/fix_variables.hpp +119 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_data_structures.hpp +152 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_graph_algorithms.hpp +476 -0
- dwave/preprocessing/include/dwave-preprocessing/implication_network.hpp +690 -0
- dwave/preprocessing/include/dwave-preprocessing/mapping_policy.hpp +124 -0
- dwave/preprocessing/include/dwave-preprocessing/posiform_info.hpp +384 -0
- dwave/preprocessing/include/dwave-preprocessing/push_relabel.hpp +775 -0
- dwave/preprocessing/libcpp.pxd +59 -0
- dwave/preprocessing/lower_bounds.py +94 -0
- dwave/preprocessing/presolve/__init__.pxd +13 -0
- dwave/preprocessing/presolve/__init__.py +16 -0
- dwave/preprocessing/presolve/cypresolve.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/presolve/cypresolve.cpp +39255 -0
- dwave/preprocessing/presolve/cypresolve.pxd +45 -0
- dwave/preprocessing/presolve/cypresolve.pyi +50 -0
- dwave/preprocessing/presolve/cypresolve.pyx +340 -0
- dwave/preprocessing/presolve/exceptions.py +21 -0
- dwave/preprocessing/presolve/pypresolve.py +173 -0
- dwave/preprocessing/src/exceptions.cpp +21 -0
- dwave/preprocessing/src/presolve.cpp +105 -0
- dwave/preprocessing/src/presolveimpl.hpp +1060 -0
- dwave_preprocessing-0.6.7.dist-info/DELVEWHEEL +2 -0
- dwave_preprocessing-0.6.7.dist-info/LICENSE +203 -0
- dwave_preprocessing-0.6.7.dist-info/METADATA +114 -0
- dwave_preprocessing-0.6.7.dist-info/RECORD +42 -0
- dwave_preprocessing-0.6.7.dist-info/WHEEL +5 -0
- dwave_preprocessing-0.6.7.dist-info/top_level.txt +1 -0
- dwave_preprocessing.libs/msvcp140-c5f1b59771da57851103ca2011763553.dll +0 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
// Copyright 2021 D-Wave Systems Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
#ifndef HELPER_GRAPH_ALGORITHM_HPP_INCLUDED
|
|
16
|
+
#define HELPER_GRAPH_ALGORITHM_HPP_INCLUDED
|
|
17
|
+
|
|
18
|
+
#include <algorithm>
|
|
19
|
+
#include <atomic>
|
|
20
|
+
#include <iostream>
|
|
21
|
+
#include <vector>
|
|
22
|
+
|
|
23
|
+
#include "helper_data_structures.hpp"
|
|
24
|
+
|
|
25
|
+
// Perform breadth first search from a certain vertex, a depth equal to the
|
|
26
|
+
// number of vertices means that vertex could not be reached from the
|
|
27
|
+
// start_vertex, since the maximum depth can be equal to number of vertices -1.
|
|
28
|
+
int breadthFirstSearch(std::vector<std::vector<int>> &adjacency_list,
|
|
29
|
+
int start_vertex, std::vector<int> &depth_values,
|
|
30
|
+
bool print_result = false) {
|
|
31
|
+
int num_vertices = adjacency_list.size();
|
|
32
|
+
int UNVISITED = num_vertices;
|
|
33
|
+
vector_based_queue<int> vertex_queue(num_vertices);
|
|
34
|
+
depth_values.resize(num_vertices);
|
|
35
|
+
std::fill(depth_values.begin(), depth_values.end(), UNVISITED);
|
|
36
|
+
|
|
37
|
+
depth_values[start_vertex] = 0;
|
|
38
|
+
vertex_queue.push(start_vertex);
|
|
39
|
+
|
|
40
|
+
while (!vertex_queue.empty()) {
|
|
41
|
+
int parent_vertex = vertex_queue.pop();
|
|
42
|
+
int current_depth = depth_values[parent_vertex] + 1;
|
|
43
|
+
auto eit = adjacency_list[parent_vertex].begin();
|
|
44
|
+
auto eit_end = adjacency_list[parent_vertex].end();
|
|
45
|
+
for (; eit != eit_end; eit++) {
|
|
46
|
+
int to_vertex = *eit;
|
|
47
|
+
if (depth_values[to_vertex] == UNVISITED) {
|
|
48
|
+
depth_values[to_vertex] = current_depth;
|
|
49
|
+
vertex_queue.push(to_vertex);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return UNVISITED;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Perform breadth first search on a residual graph from a certain vertex, a
|
|
57
|
+
// depth equal to the number of vertices means that vertex could not be reached
|
|
58
|
+
// from the start_vertex, since the maximum depth can be equal to number of
|
|
59
|
+
// vertices -1.
|
|
60
|
+
// @param depth_values : reference to vector that should be filled with depth
|
|
61
|
+
// values found by the search.
|
|
62
|
+
// @param reverse : if set to true, the search does reverse breadth first
|
|
63
|
+
// search, that is it tries to find if a certain vertex can reach the
|
|
64
|
+
// start_vertex or not, In case of a flow graph the search should consider if
|
|
65
|
+
// there is residual capacity in the reverse edge of the edge that connects the
|
|
66
|
+
// parent to the child.
|
|
67
|
+
// @returns the unreachable depth.
|
|
68
|
+
template <class EdgeType>
|
|
69
|
+
int breadthFirstSearchResidual(
|
|
70
|
+
std::vector<std::vector<EdgeType>> &adjacency_list, int start_vertex,
|
|
71
|
+
std::vector<int> &depth_values, bool reverse = false,
|
|
72
|
+
bool print_result = false) {
|
|
73
|
+
// using capacity_t = typename EdgeType::capacity_type;
|
|
74
|
+
int num_vertices = adjacency_list.size();
|
|
75
|
+
int UNVISITED = num_vertices;
|
|
76
|
+
vector_based_queue<int> vertex_queue(num_vertices);
|
|
77
|
+
depth_values.resize(num_vertices);
|
|
78
|
+
std::fill(depth_values.begin(), depth_values.end(), UNVISITED);
|
|
79
|
+
|
|
80
|
+
depth_values[start_vertex] = 0;
|
|
81
|
+
vertex_queue.push(start_vertex);
|
|
82
|
+
|
|
83
|
+
// The check for whether the search should be reverse or not could be done
|
|
84
|
+
// inside the innermost loop, but that would be detrimental to performance.
|
|
85
|
+
if (reverse) {
|
|
86
|
+
while (!vertex_queue.empty()) {
|
|
87
|
+
int parent_vertex = vertex_queue.pop();
|
|
88
|
+
int current_depth = depth_values[parent_vertex] + 1;
|
|
89
|
+
auto eit = adjacency_list[parent_vertex].begin();
|
|
90
|
+
auto eit_end = adjacency_list[parent_vertex].end();
|
|
91
|
+
for (; eit != eit_end; eit++) {
|
|
92
|
+
int to_vertex = eit->to_vertex;
|
|
93
|
+
if (eit->getReverseEdgeResidual() &&
|
|
94
|
+
depth_values[to_vertex] == UNVISITED) {
|
|
95
|
+
depth_values[to_vertex] = current_depth;
|
|
96
|
+
vertex_queue.push(to_vertex);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
while (!vertex_queue.empty()) {
|
|
102
|
+
int parent_vertex = vertex_queue.pop();
|
|
103
|
+
int current_depth = depth_values[parent_vertex] + 1;
|
|
104
|
+
auto eit = adjacency_list[parent_vertex].begin();
|
|
105
|
+
auto eit_end = adjacency_list[parent_vertex].end();
|
|
106
|
+
for (; eit != eit_end; eit++) {
|
|
107
|
+
int to_vertex = eit->to_vertex;
|
|
108
|
+
if (eit->residual && depth_values[to_vertex] == UNVISITED) {
|
|
109
|
+
depth_values[to_vertex] = current_depth;
|
|
110
|
+
vertex_queue.push(to_vertex);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (print_result) {
|
|
117
|
+
std::vector<int> level_sizes;
|
|
118
|
+
std::vector<std::vector<int>> levels;
|
|
119
|
+
levels.resize(num_vertices + 1);
|
|
120
|
+
level_sizes.resize(num_vertices + 1, 0);
|
|
121
|
+
for (std::size_t i = 0; i < depth_values.size(); i++) {
|
|
122
|
+
level_sizes[depth_values[i]]++;
|
|
123
|
+
}
|
|
124
|
+
for (std::size_t i = 0; i < level_sizes.size(); i++) {
|
|
125
|
+
levels[i].reserve(level_sizes[i]);
|
|
126
|
+
}
|
|
127
|
+
for (std::size_t i = 0; i < depth_values.size(); i++) {
|
|
128
|
+
levels[depth_values[i]].push_back(i);
|
|
129
|
+
}
|
|
130
|
+
std::cout << std::endl;
|
|
131
|
+
std::cout << "Printing " << (reverse ? "reverse " : "")
|
|
132
|
+
<< "breadth first search result starting from vertex : "
|
|
133
|
+
<< start_vertex << std::endl;
|
|
134
|
+
std::cout << std::endl;
|
|
135
|
+
for (std::size_t i = 0; i < levels.size(); i++) {
|
|
136
|
+
if (!levels[i].size()) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
std::cout << "Level " << i << " has " << levels[i].size()
|
|
140
|
+
<< " vertices : " << std::endl;
|
|
141
|
+
for (std::size_t j = 0; j < levels[i].size(); j++) {
|
|
142
|
+
std::cout << levels[i][j] << " ";
|
|
143
|
+
}
|
|
144
|
+
std::cout << std::endl;
|
|
145
|
+
}
|
|
146
|
+
std::cout << std::endl;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return UNVISITED;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Check if the flow value in a given graph represented as an adjacency list is
|
|
153
|
+
// valid or not.
|
|
154
|
+
// @returns std::pair<the value of flow, if flow is valid or not>
|
|
155
|
+
template <class EdgeType>
|
|
156
|
+
std::pair<typename EdgeType::capacity_type, bool>
|
|
157
|
+
isFlowValid(std::vector<std::vector<EdgeType>> &adjacency_list, int source,
|
|
158
|
+
int sink) {
|
|
159
|
+
using capacity_t = typename EdgeType::capacity_type;
|
|
160
|
+
bool valid_flow = true;
|
|
161
|
+
int num_vertices = adjacency_list.size();
|
|
162
|
+
std::vector<capacity_t> excess(num_vertices, 0);
|
|
163
|
+
|
|
164
|
+
double overflow_detector = 0;
|
|
165
|
+
auto eit = adjacency_list[source].begin();
|
|
166
|
+
auto eit_end = adjacency_list[source].end();
|
|
167
|
+
for (; eit != eit_end; eit++) {
|
|
168
|
+
overflow_detector += eit->getCapacity();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (overflow_detector >
|
|
172
|
+
static_cast<double>(std::numeric_limits<capacity_t>::max())) {
|
|
173
|
+
std::cout << "WARNING : Overflow is possible in the flow network. Sum of "
|
|
174
|
+
"capacity of out edges from source exceed numeric limit of "
|
|
175
|
+
"the type used for capacity."
|
|
176
|
+
<< std::endl;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Since we are validating our algorithms, we will not retrieve the
|
|
180
|
+
// value of residual/capacity of a reverse edge from its counterpart
|
|
181
|
+
// which we generally do for performance reasons. Here we will actually
|
|
182
|
+
// access the data and verify if the flow constraints hold or not.
|
|
183
|
+
for (int from_vertex = 0; from_vertex < num_vertices; from_vertex++) {
|
|
184
|
+
auto eit = adjacency_list[from_vertex].begin();
|
|
185
|
+
auto eit_end = adjacency_list[from_vertex].end();
|
|
186
|
+
for (; eit != eit_end; eit++) {
|
|
187
|
+
int to_vertex = eit->to_vertex;
|
|
188
|
+
auto reverse_eit =
|
|
189
|
+
adjacency_list[to_vertex].begin() + eit->reverse_edge_index;
|
|
190
|
+
capacity_t edge_capacity = eit->getCapacity();
|
|
191
|
+
capacity_t edge_residual = eit->residual;
|
|
192
|
+
capacity_t reverse_edge_capacity = reverse_eit->getCapacity();
|
|
193
|
+
capacity_t reverse_edge_residual = reverse_eit->residual;
|
|
194
|
+
bool valid_edge =
|
|
195
|
+
(eit->getReverseEdgeCapacity() == reverse_edge_capacity) &&
|
|
196
|
+
(eit->getReverseEdgeResidual() == reverse_edge_residual) &&
|
|
197
|
+
(edge_capacity >= 0) && (edge_residual >= 0);
|
|
198
|
+
if (edge_capacity > 0) {
|
|
199
|
+
// Residual edge having capacity 0 is a valid assumption for posiforms,
|
|
200
|
+
// since no term with two variables appear multiple times with different
|
|
201
|
+
// ordering of the variables. This assumption can be maintained with
|
|
202
|
+
// other graphs too.
|
|
203
|
+
valid_edge = valid_edge && (reverse_edge_capacity == 0) &&
|
|
204
|
+
(edge_residual <= edge_capacity) &&
|
|
205
|
+
((edge_residual + reverse_edge_residual) == edge_capacity);
|
|
206
|
+
|
|
207
|
+
capacity_t flow = (edge_capacity - edge_residual);
|
|
208
|
+
excess[from_vertex] -= flow;
|
|
209
|
+
excess[to_vertex] += flow;
|
|
210
|
+
}
|
|
211
|
+
if (!valid_edge) {
|
|
212
|
+
std::cout << "Invalid Flow due to following edge pair :" << std::endl;
|
|
213
|
+
eit->print();
|
|
214
|
+
reverse_eit->print();
|
|
215
|
+
}
|
|
216
|
+
valid_flow = valid_flow && valid_edge;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
221
|
+
if ((vertex == source) || (vertex == sink)) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (excess[vertex]) {
|
|
225
|
+
std::cout << "Excess flow of " << excess[vertex]
|
|
226
|
+
<< " in vertex : " << vertex << std::endl;
|
|
227
|
+
valid_flow = false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (excess[sink] != -excess[source]) {
|
|
232
|
+
std::cout << "Flow out of source : " << -excess[source]
|
|
233
|
+
<< " is not equal to flow into sink : " << excess[sink]
|
|
234
|
+
<< std::endl;
|
|
235
|
+
std::cout << "Difference is : "
|
|
236
|
+
<< std::llabs(std::llabs(excess[source]) -
|
|
237
|
+
std::llabs(excess[sink]))
|
|
238
|
+
<< std::endl;
|
|
239
|
+
valid_flow = false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return {excess[sink], valid_flow};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Check if the flow value in a given graph represented as an adjacency list is
|
|
246
|
+
// a valid max-flow or not and also return the flow value.
|
|
247
|
+
template <class EdgeType>
|
|
248
|
+
std::pair<typename EdgeType::capacity_type, bool>
|
|
249
|
+
isMaximumFlow(std::vector<std::vector<EdgeType>> &adjacency_list, int source,
|
|
250
|
+
int sink) {
|
|
251
|
+
|
|
252
|
+
// If the flow follows the constraints of network flow.
|
|
253
|
+
auto validity_result = isFlowValid(adjacency_list, source, sink);
|
|
254
|
+
|
|
255
|
+
// If the flow is a maximum flow, the source will be unreachable from the sink
|
|
256
|
+
// through a reverse breadth first search, meaning the source cannot reach the
|
|
257
|
+
// sink through any augmenting path.
|
|
258
|
+
std::vector<int> depth_values;
|
|
259
|
+
int UNVISITED = breadthFirstSearchResidual(adjacency_list, sink, depth_values,
|
|
260
|
+
true, false);
|
|
261
|
+
return {validity_result.first,
|
|
262
|
+
(validity_result.second && (depth_values[source] == UNVISITED))};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Tarzan's strongly connected component algoirhtm using iterative depth first
|
|
266
|
+
// search. Note there will be no explicit stack used for this depth first
|
|
267
|
+
// search, but instead the iterators pointing to the edges to be traversed for
|
|
268
|
+
// each vertex will be updated and an array containing parents of vertices will
|
|
269
|
+
// help simulate the stack. When we want to pop the stack we can basically look
|
|
270
|
+
// at the parent of the current vertex being processed and when we want to push
|
|
271
|
+
// a vertex into the stack, we can save it as the parent of the child vertex and
|
|
272
|
+
// restart the loop with the child vertex as the current vertex.
|
|
273
|
+
int stronglyConnectedComponents(std::vector<std::vector<int>> &adjacency_list,
|
|
274
|
+
std::vector<int> &components) {
|
|
275
|
+
int num_vertices = adjacency_list.size();
|
|
276
|
+
using edge_iterator = typename std::vector<int>::iterator;
|
|
277
|
+
components.resize(num_vertices);
|
|
278
|
+
vector_based_stack<int> component_stack(num_vertices);
|
|
279
|
+
std::vector<bool> in_component_stack(num_vertices, false);
|
|
280
|
+
int UNVISITED = num_vertices;
|
|
281
|
+
std::vector<int> low_link_id(num_vertices, UNVISITED);
|
|
282
|
+
std::vector<int> vertex_visit_id(num_vertices, UNVISITED);
|
|
283
|
+
std::vector<int> parent(num_vertices);
|
|
284
|
+
int visit_id = 0;
|
|
285
|
+
int num_strong_components = 0;
|
|
286
|
+
std::vector<std::pair<edge_iterator, edge_iterator>> pending_out_edges(
|
|
287
|
+
num_vertices);
|
|
288
|
+
|
|
289
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
290
|
+
pending_out_edges[vertex] = {adjacency_list[vertex].begin(),
|
|
291
|
+
adjacency_list[vertex].end()};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Iterative DFS.
|
|
295
|
+
for (int current_vertex = 0; current_vertex < num_vertices;
|
|
296
|
+
current_vertex++) {
|
|
297
|
+
if (vertex_visit_id[current_vertex] == UNVISITED) {
|
|
298
|
+
|
|
299
|
+
// The root of a DFS tree.
|
|
300
|
+
int root_vertex = current_vertex;
|
|
301
|
+
vertex_visit_id[current_vertex] = visit_id;
|
|
302
|
+
low_link_id[current_vertex] = visit_id;
|
|
303
|
+
visit_id++;
|
|
304
|
+
component_stack.push(current_vertex);
|
|
305
|
+
in_component_stack[current_vertex] = true;
|
|
306
|
+
|
|
307
|
+
while (true) {
|
|
308
|
+
|
|
309
|
+
// We need to increment the actual iterator in the array. Think
|
|
310
|
+
// of recursive DFS, the counter of a for loop belonging to a particular
|
|
311
|
+
// stack frame is saved in the stack frame while the function calls
|
|
312
|
+
// itself recursively so when it comes back it finds the counter with
|
|
313
|
+
// the proper value.
|
|
314
|
+
for (; pending_out_edges[current_vertex].first !=
|
|
315
|
+
pending_out_edges[current_vertex].second;
|
|
316
|
+
pending_out_edges[current_vertex].first++) {
|
|
317
|
+
int child_vertex = *(pending_out_edges[current_vertex].first);
|
|
318
|
+
if (vertex_visit_id[child_vertex] == UNVISITED) {
|
|
319
|
+
vertex_visit_id[child_vertex] = visit_id;
|
|
320
|
+
low_link_id[child_vertex] = visit_id;
|
|
321
|
+
visit_id++;
|
|
322
|
+
component_stack.push(child_vertex);
|
|
323
|
+
in_component_stack[child_vertex] = true;
|
|
324
|
+
|
|
325
|
+
// Equivalent to calling DFS, i.e pushing into the stack. The
|
|
326
|
+
// while loop above will start the for loop all over again, but
|
|
327
|
+
// this time the current_vertex will be the child_vertex, and we
|
|
328
|
+
// will be able to trace back as the parent-child relationship is
|
|
329
|
+
// saved in the parent array. Note breaking the loop does not
|
|
330
|
+
// allow the iterator of edges to be incremented for the parent,
|
|
331
|
+
// just like in a recursive DFS, the for loop does not get
|
|
332
|
+
// incremented till the recursive call to DFS finishes.
|
|
333
|
+
parent[child_vertex] = current_vertex;
|
|
334
|
+
current_vertex = child_vertex;
|
|
335
|
+
break;
|
|
336
|
+
} else if (in_component_stack[child_vertex]) {
|
|
337
|
+
low_link_id[current_vertex] = std::min(low_link_id[current_vertex],
|
|
338
|
+
low_link_id[child_vertex]);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Finished exploring current vertex, edges.
|
|
343
|
+
if (pending_out_edges[current_vertex].first ==
|
|
344
|
+
pending_out_edges[current_vertex].second) {
|
|
345
|
+
if (low_link_id[current_vertex] == vertex_visit_id[current_vertex]) {
|
|
346
|
+
int popped_vertex = -1;
|
|
347
|
+
do {
|
|
348
|
+
popped_vertex = component_stack.pop();
|
|
349
|
+
in_component_stack[popped_vertex] = false;
|
|
350
|
+
components[popped_vertex] = num_strong_components;
|
|
351
|
+
} while (popped_vertex != current_vertex);
|
|
352
|
+
num_strong_components++;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// If it is a root, then we break the while loop and the for loop
|
|
356
|
+
// above will automatically select the next vertex. It is similar to
|
|
357
|
+
// the situation that in DFS a wrapper function calls the first
|
|
358
|
+
// instance of DFS on different roots, and before the call there is no
|
|
359
|
+
// stack frame for DFS function. If it is not the root we need
|
|
360
|
+
// to simulate the popping of the stack, this we do by setting the
|
|
361
|
+
// current_vertex to be the parent of the child_vertex, we should
|
|
362
|
+
// also increment the edge iterator of the current vertex since in DFS
|
|
363
|
+
// we increment the iterator when the call to child DFS returns. The
|
|
364
|
+
// for loop will start again for the parent. Note: the current_vertex
|
|
365
|
+
// is changing within the loop so the edge iterator for the original
|
|
366
|
+
// current_vertex does not get incremented until and unless edges
|
|
367
|
+
// which cannot be traversed are encountered.
|
|
368
|
+
if (current_vertex != root_vertex) {
|
|
369
|
+
int completed_vertex = current_vertex;
|
|
370
|
+
current_vertex = parent[current_vertex];
|
|
371
|
+
|
|
372
|
+
// This is the DFS callback, here the recursive function has
|
|
373
|
+
// returned and we can perform the comparison with the low link id
|
|
374
|
+
// of the child node.
|
|
375
|
+
low_link_id[current_vertex] = std::min(
|
|
376
|
+
low_link_id[current_vertex], low_link_id[completed_vertex]);
|
|
377
|
+
pending_out_edges[current_vertex].first++;
|
|
378
|
+
} else {
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return num_strong_components;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Transpose an adjacency list.
|
|
389
|
+
void getTransposedAdjacencyList(std::vector<std::vector<int>> original,
|
|
390
|
+
std::vector<std::vector<int>> &transposed) {
|
|
391
|
+
int num_vertices = original.size();
|
|
392
|
+
std::atomic<int> *transposed_sizes =
|
|
393
|
+
(std::atomic<int> *)malloc(num_vertices * sizeof(std::atomic<int>));
|
|
394
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
395
|
+
transposed_sizes[vertex] = 0;
|
|
396
|
+
}
|
|
397
|
+
transposed.resize(num_vertices);
|
|
398
|
+
|
|
399
|
+
#pragma omp parallel for
|
|
400
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
401
|
+
auto eit = original[vertex].begin();
|
|
402
|
+
auto eit_end = original[vertex].end();
|
|
403
|
+
for (; eit != eit_end; eit++) {
|
|
404
|
+
transposed_sizes[*eit]++;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
409
|
+
transposed[vertex].reserve(transposed_sizes[vertex]);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
for (int vertex = 0; vertex < num_vertices; vertex++) {
|
|
413
|
+
auto eit = original[vertex].begin();
|
|
414
|
+
auto eit_end = original[vertex].end();
|
|
415
|
+
for (; eit != eit_end; eit++) {
|
|
416
|
+
transposed[*eit].push_back(vertex);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
free(transposed_sizes);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// We create the graph of strongly connected graphs, while processing each
|
|
424
|
+
// strongly connected component in parallel. Given the sizes of different
|
|
425
|
+
// components may vary widely, parallelism applied in this fashion is not the
|
|
426
|
+
// most efficient but if we try to parallelize over all the vertices of the
|
|
427
|
+
// residual graph where all threads would write into the adjacency matrix of
|
|
428
|
+
// strongly connected components whether the edges of a vertex contribute an
|
|
429
|
+
// entry to that matrix or not, it would require us to allocate that adjacency
|
|
430
|
+
// matrix which may be very large if the number of strongly connected components
|
|
431
|
+
// is nearly equal to the number of vertices and we would risk memory allocation
|
|
432
|
+
// failure.
|
|
433
|
+
void createGraphOfStronglyConnectedComponents(
|
|
434
|
+
std::vector<int> &vertex_to_component_map,
|
|
435
|
+
std::vector<std::vector<int>> &components,
|
|
436
|
+
std::vector<std::vector<int>> &adjacency_list_residual,
|
|
437
|
+
std::vector<std::vector<int>> &adjacency_list_components) {
|
|
438
|
+
int num_components = components.size();
|
|
439
|
+
adjacency_list_components.resize(num_components);
|
|
440
|
+
#pragma omp parallel
|
|
441
|
+
{
|
|
442
|
+
std::vector<int> temp_buffer(num_components);
|
|
443
|
+
// Better not use vector of booleans in parallel regions.
|
|
444
|
+
std::vector<int> found_edge_to_component(num_components, false);
|
|
445
|
+
#pragma omp for
|
|
446
|
+
for (int component = 0; component < num_components; component++) {
|
|
447
|
+
int num_out_edges = 0;
|
|
448
|
+
auto vit = components[component].begin();
|
|
449
|
+
auto vit_end = components[component].end();
|
|
450
|
+
for (; vit != vit_end; vit++) {
|
|
451
|
+
int vertex = *vit;
|
|
452
|
+
auto eit = adjacency_list_residual[vertex].begin();
|
|
453
|
+
auto eit_end = adjacency_list_residual[vertex].end();
|
|
454
|
+
for (; eit != eit_end; eit++) {
|
|
455
|
+
int to_component = vertex_to_component_map[*eit];
|
|
456
|
+
if ((to_component != component) &&
|
|
457
|
+
(!found_edge_to_component[to_component])) {
|
|
458
|
+
found_edge_to_component[to_component] = true;
|
|
459
|
+
temp_buffer[num_out_edges++] = to_component;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
adjacency_list_components[component].assign(
|
|
465
|
+
temp_buffer.begin(), temp_buffer.begin() + num_out_edges);
|
|
466
|
+
|
|
467
|
+
// We have to reinitialize the hash table, instead of using std::clear
|
|
468
|
+
// this is faster as we clear only the set elements.
|
|
469
|
+
for (int i = 0; i < num_out_edges; i++) {
|
|
470
|
+
found_edge_to_component[temp_buffer[i]] = false;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#endif // HELPER_GRAPH_ALGORITHM_HPP_INCLUDED
|