2
2
Network simplex algorithm, inspired by networkx library implementation (https://networkx.org/documentation/stable/_modules/networkx/algorithms/flow/networksimplex.html)
3
3
*/
4
4
use std:: collections:: HashMap ;
5
+ use std:: fmt:: Debug ;
5
6
use std:: hash:: Hash ;
6
7
use std:: iter:: repeat;
7
8
use std:: usize;
8
9
9
- #[ derive( Clone ) ]
10
+ #[ derive( Clone , Debug ) ]
10
11
struct Node {
11
12
supply : f64 ,
12
13
}
13
14
14
- #[ derive( Clone ) ]
15
+ #[ derive( Clone , Debug ) ]
15
16
struct Edge {
16
17
start : usize ,
17
18
end : usize ,
18
19
capacity : f64 ,
19
20
cost : f64 ,
20
21
}
21
22
22
- #[ derive( Clone ) ]
23
- pub struct Graph < T : Eq + Hash > {
23
+ #[ derive( Clone , Debug ) ]
24
+ pub struct Graph < T : Eq + Hash + Debug > {
24
25
nodes : Vec < Node > ,
25
26
node_label_to_index : HashMap < T , usize > ,
26
27
edges : Vec < Edge > ,
27
28
}
28
29
29
- impl < T : Eq + Hash > Graph < T > {
30
+ impl < T : Eq + Hash + Debug > Graph < T > {
30
31
pub fn new ( ) -> Graph < T > {
31
32
Graph {
32
33
nodes : vec ! [ ] ,
@@ -69,7 +70,8 @@ impl<T: Eq + Hash> Graph<T> {
69
70
}
70
71
}
71
72
72
- struct Solution < ' a , T : Eq + Hash > {
73
+ #[ derive( Debug ) ]
74
+ struct Solution < ' a , T : Eq + Hash + Debug > {
73
75
graph : & ' a Graph < T > ,
74
76
edge_count : usize ,
75
77
potentials : Vec < f64 > ,
@@ -118,7 +120,7 @@ impl Iterator for SubtreeIterator<'_> {
118
120
}
119
121
}
120
122
121
- impl < T : Eq + Hash > Solution < ' _ , T > {
123
+ impl < T : Eq + Hash + Debug > Solution < ' _ , T > {
122
124
fn new ( graph : & mut Graph < T > , eps : f64 ) -> Solution < T > {
123
125
let n = graph. nodes . len ( ) ;
124
126
let m = graph. edges . len ( ) ;
@@ -129,27 +131,28 @@ impl<T: Eq + Hash> Solution<'_, T> {
129
131
let faux_inf = 3.0 * supplies. iter ( ) . chain ( vec ! [ cost_sum, capacity_sum] . iter ( ) ) . max_by ( |a, b| a. partial_cmp ( b) . unwrap ( ) ) . unwrap ( ) ;
130
132
// Add artificial root and edges
131
133
for ( i, u) in graph. nodes . iter ( ) . enumerate ( ) {
132
- if u. supply < eps {
133
- graph . edges . push ( Edge {
134
+ let edge = if u. supply < - eps {
135
+ Edge {
134
136
start : n,
135
137
end : i,
136
138
capacity : faux_inf,
137
139
cost : faux_inf,
138
- } )
140
+ }
139
141
} else {
140
- graph . edges . push ( Edge {
142
+ Edge {
141
143
start : i,
142
144
end : n,
143
145
capacity : faux_inf,
144
146
cost : faux_inf,
145
- } )
146
- }
147
+ }
148
+ } ;
149
+ graph. edges . push ( edge) ;
147
150
}
148
151
let bfs = Solution {
149
152
graph,
150
153
edge_count : m,
151
154
potentials : graph. nodes . iter ( ) . map ( |node| {
152
- if node. supply >= -eps {
155
+ if node. supply >= 0.0 {
153
156
faux_inf
154
157
} else {
155
158
-faux_inf
@@ -432,7 +435,7 @@ fn argmin<S: Copy>(edges: Vec<S>, func: impl Fn(S) -> f64) -> Option<S> {
432
435
argmin
433
436
}
434
437
435
- pub fn network_simplex < T : Eq + Hash + Clone > ( graph : Graph < T > , eps : f64 ) -> Vec < f64 > {
438
+ pub fn network_simplex < T : Eq + Hash + Clone + Debug > ( graph : Graph < T > , eps : f64 ) -> Vec < f64 > {
436
439
let mut graph = graph. clone ( ) ;
437
440
let mut solution = Solution :: new ( & mut graph, eps) ;
438
441
0 commit comments