Skip to content

Commit 3b5fe63

Browse files
committed
askrene algorithm add helper for flow conservation
Changelog-None: askrene algorithm add helper for flow conservation Signed-off-by: Lagrang3 <[email protected]>
1 parent d9ac65e commit 3b5fe63

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

plugins/askrene/algorithm.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,35 @@ static s64 get_augmenting_flow(const struct graph *graph,
212212
return flow;
213213
}
214214

215+
216+
/* Helper.
217+
* Sends an amount of flow through an arc, changing the flow balance of the
218+
* nodes connected by the arc and the [residual] capacity of the arc and its
219+
* dual. */
220+
static inline void sendflow(const struct graph *graph, const struct arc arc,
221+
const s64 flow, s64 *arc_capacity,
222+
s64 *node_balance)
223+
{
224+
const struct arc dual = arc_dual(graph, arc);
225+
226+
arc_capacity[arc.idx] -= flow;
227+
arc_capacity[dual.idx] += flow;
228+
229+
if (node_balance) {
230+
const struct node src = arc_tail(graph, arc),
231+
dst = arc_tail(graph, dual);
232+
233+
node_balance[src.idx] -= flow;
234+
node_balance[dst.idx] += flow;
235+
}
236+
}
237+
215238
/* Augment a `flow` amount along the path defined by `prev`.*/
216239
static void augment_flow(const struct graph *graph,
217240
const struct node source,
218241
const struct node target,
219242
const struct arc *prev,
243+
s64 *excess,
220244
s64 *capacity,
221245
s64 flow)
222246
{
@@ -232,15 +256,8 @@ static void augment_flow(const struct graph *graph,
232256
while (cur.idx != source.idx) {
233257
assert(cur.idx < max_num_nodes);
234258
const struct arc arc = prev[cur.idx];
235-
const struct arc dual = arc_dual(graph, arc);
236-
237-
assert(arc.idx < max_num_arcs);
238-
assert(dual.idx < max_num_arcs);
239-
240-
capacity[arc.idx] -= flow;
241-
capacity[dual.idx] += flow;
242259

243-
assert(capacity[arc.idx] >= 0);
260+
sendflow(graph, arc, flow, capacity, excess);
244261

245262
/* we are traversing in the opposite direction to the flow,
246263
* hence the next node is at the tail of the arc. */
@@ -249,7 +266,7 @@ static void augment_flow(const struct graph *graph,
249266
/* We may never have a path exceeds the number of nodes, it this
250267
* happens it means we have an infinite loop. */
251268
path_length++;
252-
if(path_length >= max_num_nodes)
269+
if (path_length >= max_num_nodes)
253270
break;
254271
}
255272
assert(path_length < max_num_nodes);
@@ -297,7 +314,8 @@ bool simple_feasibleflow(const tal_t *ctx,
297314
delta = MIN(amount, delta);
298315
assert(delta > 0 && delta <= amount);
299316

300-
augment_flow(graph, source, destination, prev, capacity, delta);
317+
augment_flow(graph, source, destination, prev, NULL, capacity,
318+
delta);
301319
amount -= delta;
302320
}
303321
finish:
@@ -368,7 +386,8 @@ bool simple_mcf(const tal_t *ctx, const struct graph *graph,
368386
delta = MIN(remaining_amount, delta);
369387
assert(delta > 0 && delta <= remaining_amount);
370388

371-
augment_flow(graph, source, destination, prev, capacity, delta);
389+
augment_flow(graph, source, destination, prev, NULL, capacity,
390+
delta);
372391
remaining_amount -= delta;
373392

374393
/* update potentials */

0 commit comments

Comments
 (0)