-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathford_fulkerson.rs
314 lines (293 loc) · 8.9 KB
/
ford_fulkerson.rs
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! The Ford-Fulkerson algorithm is a widely used algorithm to solve the maximum flow problem in a flow network.
//!
//! The maximum flow problem involves determining the maximum amount of flow that can be sent from a source vertex to a sink vertex
//! in a directed weighted graph, subject to capacity constraints on the edges.
use std::collections::VecDeque;
/// Enum representing the possible errors that can occur when running the Ford-Fulkerson algorithm.
#[derive(Debug, PartialEq)]
pub enum FordFulkersonError {
EmptyGraph,
ImproperGraph,
SourceOutOfBounds,
SinkOutOfBounds,
}
/// Performs a Breadth-First Search (BFS) on the residual graph to find an augmenting path
/// from the source vertex `source` to the sink vertex `sink`.
///
/// # Arguments
///
/// * `graph` - A reference to the residual graph represented as an adjacency matrix.
/// * `source` - The source vertex.
/// * `sink` - The sink vertex.
/// * `parent` - A mutable reference to the parent array used to store the augmenting path.
///
/// # Returns
///
/// Returns `true` if an augmenting path is found from `source` to `sink`, `false` otherwise.
fn bfs(graph: &[Vec<usize>], source: usize, sink: usize, parent: &mut [usize]) -> bool {
let mut visited = vec![false; graph.len()];
visited[source] = true;
parent[source] = usize::MAX;
let mut queue = VecDeque::new();
queue.push_back(source);
while let Some(current_vertex) = queue.pop_front() {
for (previous_vertex, &capacity) in graph[current_vertex].iter().enumerate() {
if !visited[previous_vertex] && capacity > 0 {
visited[previous_vertex] = true;
parent[previous_vertex] = current_vertex;
if previous_vertex == sink {
return true;
}
queue.push_back(previous_vertex);
}
}
}
false
}
/// Validates the input parameters for the Ford-Fulkerson algorithm.
///
/// This function checks if the provided graph, source vertex, and sink vertex
/// meet the requirements for the Ford-Fulkerson algorithm. It ensures the graph
/// is non-empty, square (each row has the same length as the number of rows), and
/// that the source and sink vertices are within the valid range of vertex indices.
///
/// # Arguments
///
/// * `graph` - A reference to the flow network represented as an adjacency matrix.
/// * `source` - The source vertex.
/// * `sink` - The sink vertex.
///
/// # Returns
///
/// Returns `Ok(())` if the input parameters are valid, otherwise returns an appropriate
/// `FordFulkersonError`.
fn validate_ford_fulkerson_input(
graph: &[Vec<usize>],
source: usize,
sink: usize,
) -> Result<(), FordFulkersonError> {
if graph.is_empty() {
return Err(FordFulkersonError::EmptyGraph);
}
if graph.iter().any(|row| row.len() != graph.len()) {
return Err(FordFulkersonError::ImproperGraph);
}
if source >= graph.len() {
return Err(FordFulkersonError::SourceOutOfBounds);
}
if sink >= graph.len() {
return Err(FordFulkersonError::SinkOutOfBounds);
}
Ok(())
}
/// Applies the Ford-Fulkerson algorithm to find the maximum flow in a flow network
/// represented by a weighted directed graph.
///
/// # Arguments
///
/// * `graph` - A mutable reference to the flow network represented as an adjacency matrix.
/// * `source` - The source vertex.
/// * `sink` - The sink vertex.
///
/// # Returns
///
/// Returns the maximum flow and the residual graph
pub fn ford_fulkerson(
graph: &[Vec<usize>],
source: usize,
sink: usize,
) -> Result<usize, FordFulkersonError> {
validate_ford_fulkerson_input(graph, source, sink)?;
let mut residual_graph = graph.to_owned();
let mut parent = vec![usize::MAX; graph.len()];
let mut max_flow = 0;
while bfs(&residual_graph, source, sink, &mut parent) {
let mut path_flow = usize::MAX;
let mut previous_vertex = sink;
while previous_vertex != source {
let current_vertex = parent[previous_vertex];
path_flow = path_flow.min(residual_graph[current_vertex][previous_vertex]);
previous_vertex = current_vertex;
}
previous_vertex = sink;
while previous_vertex != source {
let current_vertex = parent[previous_vertex];
residual_graph[current_vertex][previous_vertex] -= path_flow;
residual_graph[previous_vertex][current_vertex] += path_flow;
previous_vertex = current_vertex;
}
max_flow += path_flow;
}
Ok(max_flow)
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_max_flow {
($($name:ident: $tc:expr,)* ) => {
$(
#[test]
fn $name() {
let (graph, source, sink, expected_result) = $tc;
assert_eq!(ford_fulkerson(&graph, source, sink), expected_result);
}
)*
};
}
test_max_flow! {
test_empty_graph: (
vec![],
0,
0,
Err(FordFulkersonError::EmptyGraph),
),
test_source_out_of_bound: (
vec![
vec![0, 8, 0, 0, 3, 0],
vec![0, 0, 9, 0, 0, 0],
vec![0, 0, 0, 0, 7, 2],
vec![0, 0, 0, 0, 0, 5],
vec![0, 0, 7, 4, 0, 0],
vec![0, 0, 0, 0, 0, 0],
],
6,
5,
Err(FordFulkersonError::SourceOutOfBounds),
),
test_sink_out_of_bound: (
vec![
vec![0, 8, 0, 0, 3, 0],
vec![0, 0, 9, 0, 0, 0],
vec![0, 0, 0, 0, 7, 2],
vec![0, 0, 0, 0, 0, 5],
vec![0, 0, 7, 4, 0, 0],
vec![0, 0, 0, 0, 0, 0],
],
0,
6,
Err(FordFulkersonError::SinkOutOfBounds),
),
test_improper_graph: (
vec![
vec![0, 8],
vec![0],
],
0,
1,
Err(FordFulkersonError::ImproperGraph),
),
test_graph_with_small_flow: (
vec![
vec![0, 8, 0, 0, 3, 0],
vec![0, 0, 9, 0, 0, 0],
vec![0, 0, 0, 0, 7, 2],
vec![0, 0, 0, 0, 0, 5],
vec![0, 0, 7, 4, 0, 0],
vec![0, 0, 0, 0, 0, 0],
],
0,
5,
Ok(6),
),
test_graph_with_medium_flow: (
vec![
vec![0, 10, 0, 10, 0, 0],
vec![0, 0, 4, 2, 8, 0],
vec![0, 0, 0, 0, 0, 10],
vec![0, 0, 0, 0, 9, 0],
vec![0, 0, 6, 0, 0, 10],
vec![0, 0, 0, 0, 0, 0],
],
0,
5,
Ok(19),
),
test_graph_with_large_flow: (
vec![
vec![0, 12, 0, 13, 0, 0],
vec![0, 0, 10, 0, 0, 0],
vec![0, 0, 0, 13, 3, 15],
vec![0, 0, 7, 0, 15, 0],
vec![0, 0, 6, 0, 0, 17],
vec![0, 0, 0, 0, 0, 0],
],
0,
5,
Ok(23),
),
test_complex_graph: (
vec![
vec![0, 16, 13, 0, 0, 0],
vec![0, 0, 10, 12, 0, 0],
vec![0, 4, 0, 0, 14, 0],
vec![0, 0, 9, 0, 0, 20],
vec![0, 0, 0, 7, 0, 4],
vec![0, 0, 0, 0, 0, 0],
],
0,
5,
Ok(23),
),
test_disconnected_graph: (
vec![
vec![0, 0, 0, 0],
vec![0, 0, 0, 1],
vec![0, 0, 0, 1],
vec![0, 0, 0, 0],
],
0,
3,
Ok(0),
),
test_unconnected_sink: (
vec![
vec![0, 4, 0, 3, 0, 0],
vec![0, 0, 4, 0, 8, 0],
vec![0, 0, 0, 3, 0, 2],
vec![0, 0, 0, 0, 6, 0],
vec![0, 0, 6, 0, 0, 6],
vec![0, 0, 0, 0, 0, 0],
],
0,
5,
Ok(7),
),
test_no_edges: (
vec![
vec![0, 0, 0],
vec![0, 0, 0],
vec![0, 0, 0],
],
0,
2,
Ok(0),
),
test_single_vertex: (
vec![
vec![0],
],
0,
0,
Ok(0),
),
test_self_loop: (
vec![
vec![10, 0],
vec![0, 0],
],
0,
1,
Ok(0),
),
test_same_source_sink: (
vec![
vec![0, 10, 10],
vec![0, 0, 10],
vec![0, 0, 0],
],
0,
0,
Ok(0),
),
}
}