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
/*++
Copyright (c) 2017 Arie Gurfinkel
Module Name:
min_cut.cpp
Abstract:
min cut solver
Author:
Bernhard Gleiss
Revision History:
--*/
#include "util/min_cut.h"
#include "util/trace.h"
min_cut::min_cut() {
// push back two empty vectors for source and sink
m_edges.push_back(edge_vector());
m_edges.push_back(edge_vector());
}
unsigned min_cut::new_node() {
m_edges.push_back(edge_vector());
return m_edges.size() - 1;
}
void min_cut::add_edge(unsigned int i, unsigned int j, unsigned capacity) {
m_edges.reserve(i + 1);
m_edges[i].push_back(edge(j, capacity));
TRACE(spacer_mincut, tout << "adding edge (" << i << "," << j << ")\n";);
}
void min_cut::compute_min_cut(unsigned_vector& cut_nodes) {
if (m_edges.size() == 2) {
return;
}
m_d.resize(m_edges.size());
m_pred.resize(m_edges.size());
// compute initial distances and number of nodes
compute_initial_distances();
unsigned i = 0;
while (m_d[0] < m_edges.size()) {
unsigned j = get_admissible_edge(i);
if (j < m_edges.size()) {
// advance(i)
m_pred[j] = i;
i = j;
// if i is the sink, augment path
if (i == 1) {
augment_path();
i = 0;
}
}
else {
// retreat
compute_distance(i);
if (i != 0) {
i = m_pred[i];
}
}
}
// split nodes into reachable and unreachable ones
bool_vector reachable(m_edges.size());
compute_reachable_nodes(reachable);
// find all edges between reachable and unreachable nodes and
// for each such edge, add corresponding lemma to unsat-core
compute_cut_and_add_lemmas(reachable, cut_nodes);
}
void min_cut::compute_initial_distances() {
unsigned_vector todo;
bool_vector visited(m_edges.size());
todo.push_back(0); // start at the source, since we do postorder traversel
while (!todo.empty()) {
unsigned current = todo.back();
// if we haven't already visited current
if (!visited[current]) {
bool exists_unvisited_parent = false;
// add unprocessed parents to stack for DFS. If there is at least
// one unprocessed parent, don't compute the result
// for current now, but wait until those unprocessed parents are processed
for (auto const& edge : m_edges[current]) {
unsigned parent = edge.node;
// if we haven't visited the current parent yet
if (!visited[parent]) {
// add it to the stack
todo.push_back(parent);
exists_unvisited_parent = true;
}
}
// if we already visited all parents, we can visit current too
if (!exists_unvisited_parent) {
visited[current] = true;
todo.pop_back();
compute_distance(current); // I.H. all parent distances are already computed
}
}
else {
todo.pop_back();
}
}
}
unsigned min_cut::get_admissible_edge(unsigned i) {
for (const auto& edge : m_edges[i]) {
if (edge.weight > 0 && m_d[i] == m_d[edge.node] + 1) {
return edge.node;
}
}
return m_edges.size(); // no element found
}
void min_cut::augment_path() {
// find bottleneck capacity
unsigned max = std::numeric_limits<unsigned int>::max();
unsigned k = 1;
while (k != 0) {
unsigned l = m_pred[k];
for (const auto& edge : m_edges[l]) {
if (edge.node == k) {
max = std::min(max, edge.weight);
}
}
k = l;
}
k = 1;
while (k != 0) {
unsigned l = m_pred[k];
// decrease capacity
for (auto& edge : m_edges[l]) {
if (edge.node == k) {
edge.weight -= max;
}
}
// increase reverse flow
bool already_exists = false;
for (auto& edge : m_edges[k]) {
if (edge.node == l) {
already_exists = true;
edge.weight += max;
}
}
if (!already_exists) {
m_edges[k].push_back(edge(1, max));
}
k = l;
}
}
void min_cut::compute_distance(unsigned i) {
if (i == 1) { // sink node
m_d[1] = 0;
}
else {
unsigned min = std::numeric_limits<unsigned int>::max();
// find edge (i,j) with positive residual capacity and smallest distance
for (const auto& edge : m_edges[i]) {
if (edge.weight > 0) {
min = std::min(min, m_d[edge.node] + 1);
}
}
m_d[i] = min;
}
}
void min_cut::compute_reachable_nodes(bool_vector& reachable) {
unsigned_vector todo;
todo.push_back(0);
while (!todo.empty()) {
unsigned current = todo.back();
todo.pop_back();
if (!reachable[current]) {
reachable[current] = true;
for (const auto& edge : m_edges[current]) {
if (edge.weight > 0) {
todo.push_back(edge.node);
}
}
}
}
}
void min_cut::compute_cut_and_add_lemmas(bool_vector& reachable, unsigned_vector& cut_nodes) {
unsigned_vector todo;
bool_vector visited(m_edges.size());
todo.push_back(0);
while (!todo.empty()) {
unsigned current = todo.back();
todo.pop_back();
if (!visited[current]) {
visited[current] = true;
for (const auto& edge : m_edges[current]) {
unsigned successor = edge.node;
if (reachable[successor]) {
todo.push_back(successor);
}
else {
cut_nodes.push_back(successor);
}
}
}
}
}