-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax-flow-mat.cpp
More file actions
156 lines (127 loc) · 3.88 KB
/
max-flow-mat.cpp
File metadata and controls
156 lines (127 loc) · 3.88 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
// Max Flow (Ford-Fulkerson with BFS (Edmonds-Karp))
// TODO: 가중치 0인 간선을 아예 제거해버려야 더 빠를듯..
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include "graph-mat.hpp"
#define REP(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define endl '\n'
#define MAX_SIZE 4
using namespace std;
void maxFlow(Graph *graph, int s, int t);
bool findPath(Graph *graph, int s, int t, vector<int> &path);
void maxFlow(Graph *graph, int s, int t) {
if (s == t)
cout << "source is equal to sink" << endl;
else if (t >= graph->V)
cout << "sink too large" << endl;
int maxflow = 0;
while (1) {
vector<int> path(graph->getNodeSize(), -1);
if (!findPath(graph, s, t, path)) {
cout << "max flow found: " << maxflow << endl;
break;
} else {
int len = 0;
REP(i, 0, graph->getNodeSize()) {
if (path[i] == -1) break;
//cout << path[i] << " ";
len++;
}
//cout << endl;
int min = INF;
REP(i, 0, len - 1) {
if (min > graph->adj[path[i]][path[i + 1]]) {
min = graph->adj[path[i]][path[i + 1]];
}
}
maxflow += min;
cout << "incremented flow: " << min << ", current flow: " << maxflow << endl;
//graph->print();
REP(i, 0, len - 1) {
graph->adj[path[i]][path[i + 1]] -= min;
}
//graph->print();
for (int i = len-1; i >= 1; i--) {
graph->addEdge(path[i], path[i-1], min);
}
//graph->print();
}
}
return;
}
bool findPath(Graph *graph, int s, int t, vector<int> &path) {
vector<int> visited(graph->getNodeSize());
queue<int> q;
vector<int> p(graph->getNodeSize(), -1);
stack<int> path_stack;
visited[s] = 1;
q.push(s);
bool valid = false;
while (!q.empty()) {
int v = q.front();
q.pop();
if (v == t) {
valid = true;
break;
}
REP(i, 0, graph->getNodeSize()) {
if (i == v) continue;
if (!visited[i] && graph->adj[v][i] > 0 && graph->adj[v][i] < INF) {
q.push(i);
visited[i] = 1;
p[i] = v;
}
}
}
if (valid) {
int tmp = t;
while (tmp != s) {
path_stack.push(tmp);
tmp = p[tmp];
}
path_stack.push(s);
int idx = 0;
while (!path_stack.empty()) {
path[idx++] = path_stack.top();
path_stack.pop();
}
return true;
}
return false;
}
int main() {
Graph *graph = new Graph(MAX_SIZE);
// book example
// int weight[MAX_SIZE][MAX_SIZE] = {
// {0, 5, INF, 4, INF, INF},
// {INF, 0, 6, INF, INF, INF},
// {INF, INF, 0, INF, 8, 5},
// {INF, 3, INF, 0, 1, INF},
// {INF, INF, INF, INF, 0, 2},
// {INF, INF, INF, INF, INF, 0}};
// ratsgo's example
int weight[MAX_SIZE][MAX_SIZE] = {
{0, 2, 5, INF},
{INF, 0, 1, 4},
{INF, INF, 0, 3},
{INF, INF, INF, 0}};
// gazelle's example
// int weight[MAX_SIZE][MAX_SIZE] = {
// {0, 7, INF, 5, INF, INF},
// {INF, 0, 3, 5, INF, INF},
// {INF, INF, 0, 2, INF, 6},
// {INF, INF, INF, 0, 9, INF},
// {INF, INF, 2, INF, 0, 4},
// {INF, INF, INF, INF, INF, 0}};
REP(i, 0, MAX_SIZE) {
REP(j, 0, MAX_SIZE) {
graph->addEdge(i, j, weight[i][j]);
}
}
maxFlow(graph, 0, 3);
delete graph;
return 0;
}