Repository navigation
Expand file tree
/
Copy pathmaxflow.cpp
More file actions
160 lines (135 loc) · 4.57 KB
/
Copy pathmaxflow.cpp
File metadata and controls
160 lines (135 loc) · 4.57 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
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <vector>
#include <queue>
#include <limits>
#include <algorithm>
namespace py = pybind11;
class MaxFlow {
private:
struct Edge {
int to, rev;
double cap;
};
std::vector<std::vector<Edge>> graph;
std::vector<int> level; // 层次图
std::vector<int> iter; // 当前弧优化
// BFS 构建层次图
bool bfs(int s, int t) {
level.assign(graph.size(), -1);
level[s] = 0;
std::queue<int> q;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto& e : graph[v]) {
if (level[e.to] < 0 && e.cap > 1e-9) {
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
}
return level[t] >= 0;
}
// 迭代式 DFS
double dfs(int v, int t, double f) {
if (v == t) return f;
for (int& i = iter[v]; i < graph[v].size(); i++) {
Edge& e = graph[v][i];
if (level[v] + 1 == level[e.to] && e.cap > 1e-9) {
double d = dfs(e.to, t, std::min(f, e.cap));
if (d > 1e-9) {
e.cap -= d;
graph[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
public:
MaxFlow(int n) : graph(n), level(n), iter(n) {
printf("MaxFlow initialized with %d nodes\n", n);
fflush(stdout);
// 只为像素节点预分配
for (int i = 0; i < n - 2; i++) {
graph[i].reserve(6);
}
}
void add_edge(int from, int to, double cap, double revcap) {
int from_idx = graph[from].size();
int to_idx = graph[to].size();
graph[from].push_back({to, to_idx, cap});
graph[to].push_back({from, from_idx, revcap});
}
double max_flow(int s, int t) {
printf("Starting max_flow\n");
fflush(stdout);
double flow = 0;
int iterations = 0;
while (bfs(s, t)) {
iter.assign(graph.size(), 0);
double f;
while ((f = dfs(s, t, std::numeric_limits<double>::max())) > 1e-9) {
flow += f;
iterations++;
if (iterations % 1000 == 0) {
printf("Iteration %d, flow: %.2f\n", iterations, flow);
fflush(stdout);
}
if (iterations > 1000000) {
printf("WARNING: Too many iterations\n");
fflush(stdout);
break;
}
}
if(iterations > 1000000) break;
}
printf("Max flow completed: %.2f in %d iterations\n", flow, iterations);
fflush(stdout);
return flow;
}
py::array_t<int> get_cut(int height, int width, int s, bool extract_full=true) {
printf("Computing cut\n");
fflush(stdout);
auto result = py::array_t<int>(height * width);
auto buf = result.request();
int* ptr = (int*)buf.ptr;
std::vector<bool> visited(graph.size(), false);
std::queue<int> q;
q.push(s);
visited[s] = true;
std::vector<bool> bound(graph.size(), false);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto& e : graph[v]) {
if (!visited[e.to] && e.cap > 1e-9) {
visited[e.to] = true;
q.push(e.to);
}
if (!visited[e.to] && e.cap<=1e-9 && e.to!=s && e.to!=s+1) {
bound[v] = true;
}
}
}
for (int i = 0; i < height * width; i++) {
if(extract_full)
ptr[i] = visited[i] ? 0 : 255;
else ptr[i] = bound[i] ? 255: 0;
}
result.resize({height, width});
printf("Cut completed\n");
fflush(stdout);
return result;
}
};
PYBIND11_MODULE(maxflow_cpp, m) {
py::class_<MaxFlow>(m, "MaxFlow")
.def(py::init<int>())
.def("add_edge", &MaxFlow::add_edge)
.def("max_flow", &MaxFlow::max_flow)
.def("get_cut", &MaxFlow::get_cut);
}