Debian Codegraph @

lib/common/ns.c

raw
1 /**
2 * @file
3 * @brief Network Simplex algorithm for ranking nodes of a DAG, @ref rank, @ref rank2
4 * @ingroup common_render
5 */
6
7 /*************************************************************************
8 * Copyright (c) 2011 AT&T Intellectual Property
9 * All rights reserved. This program and the accompanying materials
10 * are made available under the terms of the Eclipse Public License v1.0
11 * which accompanies this distribution, and is available at
12 * https://www.eclipse.org/legal/epl-v10.html
13 *
14 * Contributors: Details at https://graphviz.org
15 *************************************************************************/
16
17 #include "config.h"
18
19 #include <assert.h>
20 #include <common/render.h>
21 #include <limits.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <util/alloc.h>
27 #include <util/exit.h>
28 #include <util/gv_math.h>
29 #include <util/list.h>
30 #include <util/overflow.h>
31 #include <util/prisize_t.h>
32 #include <util/streq.h>
33
34 static void dfs_cutval(node_t * v, edge_t * par);
35 static int dfs_range_init(node_t *v);
36 static int dfs_range(node_t * v, edge_t * par, int low);
37 static int x_val(edge_t * e, node_t * v, int dir);
38 #ifdef DEBUG
39 static void check_cycles(graph_t * g);
40 #endif
41
42 #define LENGTH(e) (ND_rank(aghead(e)) - ND_rank(agtail(e)))
43 #define SLACK(e) (LENGTH(e) - ED_minlen(e))
44 #define SEQ(a,b,c) ((a) <= (b) && (b) <= (c))
45 #define TREE_EDGE(e) (ED_tree_index(e) >= 0)
46
47 typedef struct {
48 graph_t *G;
49 LIST(edge_t *) Tree_edge;
50 size_t S_i; /* search index for enter_edge */
51 size_t N_edges, N_nodes;
52 int Search_size;
53 } network_simplex_ctx_t;
54
55 enum { SEARCHSIZE = 30 };
56
57 static int add_tree_edge(network_simplex_ctx_t *ctx, edge_t * e)
58 {
59 if (TREE_EDGE(e)) {
60 agerrorf("add_tree_edge: missing tree edge\n");
61 return -1;
62 }
63 assert(LIST_SIZE(&ctx->Tree_edge) <= INT_MAX);
64 ED_tree_index(e) = (int)LIST_SIZE(&ctx->Tree_edge);
65 LIST_APPEND(&ctx->Tree_edge, e);
66 node_t *n = agtail(e);
67 ND_mark(n) = true;
68 ND_tree_out(n).list[ND_tree_out(n).size++] = e;
69 ND_tree_out(n).list[ND_tree_out(n).size] = NULL;
70 if (ND_out(n).list[ND_tree_out(n).size - 1] == 0) {
71 agerrorf("add_tree_edge: empty outedge list\n");
72 return -1;
73 }
74 n = aghead(e);
75 ND_mark(n) = true;
76 ND_tree_in(n).list[ND_tree_in(n).size++] = e;
77 ND_tree_in(n).list[ND_tree_in(n).size] = NULL;
78 if (ND_in(n).list[ND_tree_in(n).size - 1] == 0) {
79 agerrorf("add_tree_edge: empty inedge list\n");
80 return -1;
81 }
82 return 0;
83 }
84
85 /**
86 * Invalidate DFS attributes by walking up the tree from to_node till lca
87 * (inclusively). Called when updating tree to improve pruning in dfs_range().
88 * Assigns ND_low(n) = -1 for the affected nodes.
89 */
90 static void invalidate_path(node_t *lca, node_t *to_node) {
91 while (true) {
92 if (ND_low(to_node) == -1)
93 break;
94
95 ND_low(to_node) = -1;
96
97 edge_t *e = ND_par(to_node);
98 if (e == NULL)
99 break;
100
101 if (ND_lim(to_node) >= ND_lim(lca)) {
102 if (to_node != lca)
103 agerrorf("invalidate_path: skipped over LCA\n");
104 break;
105 }
106
107 if (ND_lim(agtail(e)) > ND_lim(aghead(e)))
108 to_node = agtail(e);
109 else
110 to_node = aghead(e);
111 }
112 }
113
114 static void exchange_tree_edges(network_simplex_ctx_t *ctx, edge_t * e, edge_t * f)
115 {
116 ED_tree_index(f) = ED_tree_index(e);
117 assert(ED_tree_index(e) >= 0);
118 LIST_SET(&ctx->Tree_edge, (size_t)ED_tree_index(e), f);
119 ED_tree_index(e) = -1;
120
121 node_t *n = agtail(e);
122 size_t i = --ND_tree_out(n).size;
123 size_t j;
124 for (j = 0; j <= i; j++)
125 if (ND_tree_out(n).list[j] == e)
126 break;
127 ND_tree_out(n).list[j] = ND_tree_out(n).list[i];
128 ND_tree_out(n).list[i] = NULL;
129 n = aghead(e);
130 i = --ND_tree_in(n).size;
131 for (j = 0; j <= i; j++)
132 if (ND_tree_in(n).list[j] == e)
133 break;
134 ND_tree_in(n).list[j] = ND_tree_in(n).list[i];
135 ND_tree_in(n).list[i] = NULL;
136
137 n = agtail(f);
138 ND_tree_out(n).list[ND_tree_out(n).size++] = f;
139 ND_tree_out(n).list[ND_tree_out(n).size] = NULL;
140 n = aghead(f);
141 ND_tree_in(n).list[ND_tree_in(n).size++] = f;
142 ND_tree_in(n).list[ND_tree_in(n).size] = NULL;
143 }
144
145 static
146 void init_rank(network_simplex_ctx_t *ctx)
147 {
148 edge_t *e;
149
150 LIST(node_t *) Q = {0};
151 LIST_RESERVE(&Q, ctx->N_nodes);
152 size_t ctr = 0;
153
154 for (node_t *v = GD_nlist(ctx->G); v; v = ND_next(v)) {
155 if (ND_priority(v) == 0)
156 LIST_PUSH_BACK(&Q, v);
157 }
158
159 while (!LIST_IS_EMPTY(&Q)) {
160 node_t *const v = LIST_POP_FRONT(&Q);
161 ND_rank(v) = 0;
162 ctr++;
163 for (int i = 0; (e = ND_in(v).list[i]); i++)
164 ND_rank(v) = MAX(ND_rank(v), ND_rank(agtail(e)) + ED_minlen(e));
165 for (int i = 0; (e = ND_out(v).list[i]); i++) {
166 if (--ND_priority(aghead(e)) <= 0)
167 LIST_PUSH_BACK(&Q, aghead(e));
168 }
169 }
170 if (ctr != ctx->N_nodes) {
171 agerrorf("trouble in init_rank\n");
172 for (node_t *v = GD_nlist(ctx->G); v; v = ND_next(v))
173 if (ND_priority(v))
174 agerr(AGPREV, "\t%s %d\n", agnameof(v), ND_priority(v));
175 }
176 LIST_FREE(&Q);
177 }
178
179 static edge_t *leave_edge(network_simplex_ctx_t *ctx)
180 {
181 edge_t *f, *rv = NULL;
182 int cnt = 0;
183
184 size_t j = ctx->S_i;
185 while (ctx->S_i < LIST_SIZE(&ctx->Tree_edge)) {
186 if (ED_cutvalue(f = LIST_GET(&ctx->Tree_edge, ctx->S_i)) < 0) {
187 if (rv) {
188 if (ED_cutvalue(rv) > ED_cutvalue(f))
189 rv = f;
190 } else
191 rv = LIST_GET(&ctx->Tree_edge, ctx->S_i);
192 if (++cnt >= ctx->Search_size)
193 return rv;
194 }
195 ctx->S_i++;
196 }
197 if (j > 0) {
198 ctx->S_i = 0;
199 while (ctx->S_i < j) {
200 if (ED_cutvalue(f = LIST_GET(&ctx->Tree_edge, ctx->S_i)) < 0) {
201 if (rv) {
202 if (ED_cutvalue(rv) > ED_cutvalue(f))
203 rv = f;
204 } else
205 rv = LIST_GET(&ctx->Tree_edge, ctx->S_i);
206 if (++cnt >= ctx->Search_size)
207 return rv;
208 }
209 ctx->S_i++;
210 }
211 }
212 return rv;
213 }
214
215 static edge_t *dfs_enter_outedge(node_t *v, int Low, int Lim) {
216 edge_t *e;
217 edge_t *Enter = NULL;
218 int Slack = INT_MAX;
219
220 LIST(node_t *) todo = {0};
221 LIST_APPEND(&todo, v);
222
223 while (!LIST_IS_EMPTY(&todo)) {
224 v = LIST_POP_BACK(&todo);
225
226 for (int i = 0; (e = ND_out(v).list[i]); i++) {
227 if (!TREE_EDGE(e)) {
228 if (!SEQ(Low, ND_lim(aghead(e)), Lim)) {
229 const int slack = SLACK(e);
230 if (slack < Slack || Enter == NULL) {
231 Enter = e;
232 Slack = slack;
233 }
234 }
235 } else if (ND_lim(aghead(e)) < ND_lim(v))
236 LIST_APPEND(&todo, aghead(e));
237 }
238 for (int i = 0; (e = ND_tree_in(v).list[i]) && Slack > 0; i++)
239 if (ND_lim(agtail(e)) < ND_lim(v))
240 LIST_APPEND(&todo, agtail(e));
241
242 }
243 LIST_FREE(&todo);
244
245 return Enter;
246 }
247
248 static edge_t *dfs_enter_inedge(node_t *v, int Low, int Lim) {
249 edge_t *e;
250
251 edge_t *Enter = NULL;
252 int Slack = INT_MAX;
253
254 LIST(node_t *) todo = {0};
255 LIST_APPEND(&todo, v);
256
257 while (!LIST_IS_EMPTY(&todo)) {
258 v = LIST_POP_BACK(&todo);
259
260 for (int i = 0; (e = ND_in(v).list[i]); i++) {
261 if (!TREE_EDGE(e)) {
262 if (!SEQ(Low, ND_lim(agtail(e)), Lim)) {
263 const int slack = SLACK(e);
264 if (slack < Slack || Enter == NULL) {
265 Enter = e;
266 Slack = slack;
267 }
268 }
269 } else if (ND_lim(agtail(e)) < ND_lim(v))
270 LIST_APPEND(&todo, agtail(e));
271 }
272 for (int i = 0; (e = ND_tree_out(v).list[i]) && Slack > 0; i++)
273 if (ND_lim(aghead(e)) < ND_lim(v))
274 LIST_APPEND(&todo, aghead(e));
275
276 }
277 LIST_FREE(&todo);
278
279 return Enter;
280 }
281
282 static edge_t *enter_edge(edge_t *e) {
283 node_t *v;
284 bool outsearch;
285
286 /* v is the down node */
287 if (ND_lim(agtail(e)) < ND_lim(aghead(e))) {
288 v = agtail(e);
289 outsearch = false;
290 } else {
291 v = aghead(e);
292 outsearch = true;
293 }
294 if (outsearch)
295 return dfs_enter_outedge(v, ND_low(v), ND_lim(v));
296 return dfs_enter_inedge(v, ND_low(v), ND_lim(v));
297 }
298
299 static void init_cutvalues(network_simplex_ctx_t *ctx)
300 {
301 dfs_range_init(GD_nlist(ctx->G));
302 dfs_cutval(GD_nlist(ctx->G), NULL);
303 }
304
305 /* functions for initial tight tree construction */
306 // borrow field from network simplex - overwritten in init_cutvalues() forgive me
307 #define ND_subtree(n) (subtree_t*)ND_par(n)
308 #define ND_subtree_set(n,value) (ND_par(n) = (edge_t*)value)
309
310 typedef struct subtree_s {
311 node_t *rep; /* some node in the tree */
312 int size; /* total tight tree size */
313 size_t heap_index; ///< required to find non-min elts when merged
314 struct subtree_s *par; /* union find */
315 } subtree_t;
316
317 /// is this subtree stored in an STheap?
318 static bool on_heap(const subtree_t *tree) {
319 return tree->heap_index != SIZE_MAX;
320 }
321
322 /// state for use in `tight_subtree_search`
323 typedef struct {
324 Agnode_t *v;
325 int in_i; ///< iteration counter through `ND_in(v).list`
326 int out_i; ///< iteration counter through `ND_out(v).list`
327 int rv; ///< result value
328 } tst_t;
329
330 /* find initial tight subtrees */
331 static int tight_subtree_search(network_simplex_ctx_t *ctx, Agnode_t *v, subtree_t *st)
332 {
333 Agedge_t *e;
334
335 int rv = 1;
336 ND_subtree_set(v,st);
337
338 LIST(tst_t) todo = {0};
339 LIST_PUSH_BACK(&todo, ((tst_t){.v = v, .rv = 1}));
340
341 while (!LIST_IS_EMPTY(&todo)) {
342 bool updated = false;
343 tst_t *top = LIST_BACK(&todo);
344
345 for (; (e = ND_in(top->v).list[top->in_i]); top->in_i++) {
346 if (TREE_EDGE(e)) continue;
347 if (ND_subtree(agtail(e)) == 0 && SLACK(e) == 0) {
348 if (add_tree_edge(ctx, e) != 0) {
349 LIST_DROP_BACK(&todo);
350 if (LIST_IS_EMPTY(&todo)) {
351 rv = -1;
352 } else {
353 --LIST_BACK(&todo)->rv;
354 }
355 } else {
356 ND_subtree_set(agtail(e), st);
357 const tst_t next = {.v = agtail(e), .rv = 1};
358 LIST_PUSH_BACK(&todo, next);
359 }
360 updated = true;
361 break;
362 }
363 }
364 if (updated) {
365 continue;
366 }
367
368 for (; (e = ND_out(top->v).list[top->out_i]); top->out_i++) {
369 if (TREE_EDGE(e)) continue;
370 if (ND_subtree(aghead(e)) == 0 && SLACK(e) == 0) {
371 if (add_tree_edge(ctx, e) != 0) {
372 LIST_DROP_BACK(&todo);
373 if (LIST_IS_EMPTY(&todo)) {
374 rv = -1;
375 } else {
376 --LIST_BACK(&todo)->rv;
377 }
378 } else {
379 ND_subtree_set(aghead(e), st);
380 const tst_t next = {.v = aghead(e), .rv = 1};
381 LIST_PUSH_BACK(&todo, next);
382 }
383 updated = true;
384 break;
385 }
386 }
387 if (updated) {
388 continue;
389 }
390
391 const tst_t last = LIST_POP_BACK(&todo);
392 if (LIST_IS_EMPTY(&todo)) {
393 rv = last.rv;
394 } else {
395 LIST_BACK(&todo)->rv += last.rv;
396 }
397 }
398
399 LIST_FREE(&todo);
400
401 return rv;
402 }
403
404 static subtree_t *find_tight_subtree(network_simplex_ctx_t *ctx, Agnode_t *v)
405 {
406 subtree_t *rv = gv_alloc(sizeof(subtree_t));
407 rv->rep = v;
408 rv->size = tight_subtree_search(ctx,v,rv);
409 if (rv->size < 0) {
410 free(rv);
411 return NULL;
412 }
413 rv->par = rv;
414 return rv;
415 }
416
417 typedef struct STheap_s {
418 subtree_t **elt;
419 size_t size;
420 } STheap_t;
421
422 static subtree_t *STsetFind(Agnode_t *n0)
423 {
424 subtree_t *s0 = ND_subtree(n0);
425 while (s0->par && s0->par != s0) {
426 if (s0->par->par) {s0->par = s0->par->par;} /* path compression for the code weary */
427 s0 = s0->par;
428 }
429 return s0;
430 }
431
432 static subtree_t *STsetUnion(subtree_t *s0, subtree_t *s1)
433 {
434 subtree_t *r0, *r1, *r;
435
436 for (r0 = s0; r0->par && r0->par != r0; r0 = r0->par);
437 for (r1 = s1; r1->par && r1->par != r1; r1 = r1->par);
438 if (r0 == r1) return r0; /* safety code but shouldn't happen */
439 assert(on_heap(r0) || on_heap(r1));
440 if (!on_heap(r1)) r = r0;
441 else if (!on_heap(r0)) r = r1;
442 else if (r1->size < r0->size) r = r0;
443 else r = r1;
444
445 r0->par = r1->par = r;
446 r->size = r0->size + r1->size;
447 assert(on_heap(r));
448 return r;
449 }
450
451 /* find tightest edge to another tree incident on the given tree */
452 static Agedge_t *inter_tree_edge_search(Agnode_t *v, Agnode_t *from, Agedge_t *best)
453 {
454 Agedge_t *e;
455 subtree_t *ts = STsetFind(v);
456 if (best && SLACK(best) == 0) return best;
457 for (int i = 0; (e = ND_out(v).list[i]); i++) {
458 if (TREE_EDGE(e)) {
459 if (aghead(e) == from) continue; // do not search back in tree
460 best = inter_tree_edge_search(aghead(e),v,best); // search forward in tree
461 }
462 else {
463 if (STsetFind(aghead(e)) != ts) { // encountered candidate edge
464 if (best == 0 || SLACK(e) < SLACK(best)) best = e;
465 }
466 /* else ignore non-tree edge between nodes in the same tree */
467 }
468 }
469 /* the following code must mirror the above, but for in-edges */
470 for (int i = 0; (e = ND_in(v).list[i]); i++) {
471 if (TREE_EDGE(e)) {
472 if (agtail(e) == from) continue;
473 best = inter_tree_edge_search(agtail(e),v,best);
474 }
475 else {
476 if (STsetFind(agtail(e)) != ts) {
477 if (best == 0 || SLACK(e) < SLACK(best)) best = e;
478 }
479 }
480 }
481 return best;
482 }
483
484 static Agedge_t *inter_tree_edge(subtree_t *tree)
485 {
486 return inter_tree_edge_search(tree->rep, NULL, NULL);
487 }
488
489 static size_t STheapsize(const STheap_t *heap) { return heap->size; }
490
491 static void STheapify(STheap_t *heap, size_t i) {
492 subtree_t **elt = heap->elt;
493 do {
494 const size_t left = 2 * (i + 1) - 1;
495 const size_t right = 2 * (i + 1);
496 size_t smallest = i;
497 if (left < heap->size && elt[left]->size < elt[smallest]->size) smallest = left;
498 if (right < heap->size && elt[right]->size < elt[smallest]->size) smallest = right;
499 if (smallest != i) {
500 SWAP(&elt[i], &elt[smallest]);
501 elt[i]->heap_index = i;
502 elt[smallest]->heap_index = smallest;
503 i = smallest;
504 }
505 else break;
506 } while (i < heap->size);
507 }
508
509 static STheap_t *STbuildheap(subtree_t **elt, size_t size) {
510 STheap_t *heap = gv_alloc(sizeof(STheap_t));
511 heap->elt = elt;
512 heap->size = size;
513 for (size_t i = 0; i < heap->size; i++) heap->elt[i]->heap_index = i;
514 for (size_t i = heap->size / 2; i != SIZE_MAX; i--)
515 STheapify(heap,i);
516 return heap;
517 }
518
519 static
520 subtree_t *STextractmin(STheap_t *heap)
521 {
522 subtree_t *rv = heap->elt[0];
523 rv->heap_index = SIZE_MAX;
524 // mark this as not participating in the heap anymore
525 heap->elt[0] = heap->elt[heap->size - 1];
526 heap->elt[0]->heap_index = 0;
527 heap->elt[heap->size -1] = rv; /* needed to free storage later */
528 heap->size--;
529 STheapify(heap,0);
530 return rv;
531 }
532
533 static
534 void tree_adjust(Agnode_t *v, Agnode_t *from, int delta)
535 {
536 Agedge_t *e;
537 ND_rank(v) += delta;
538 for (int i = 0; (e = ND_tree_in(v).list[i]); i++) {
539 Agnode_t *const w = agtail(e);
540 if (w != from)
541 tree_adjust(w, v, delta);
542 }
543 for (int i = 0; (e = ND_tree_out(v).list[i]); i++) {
544 Agnode_t *const w = aghead(e);
545 if (w != from)
546 tree_adjust(w, v, delta);
547 }
548 }
549
550 static
551 subtree_t *merge_trees(network_simplex_ctx_t *ctx, Agedge_t *e) /* entering tree edge */
552 {
553 assert(!TREE_EDGE(e));
554
555 subtree_t *const t0 = STsetFind(agtail(e));
556 subtree_t *const t1 = STsetFind(aghead(e));
557
558 if (!on_heap(t0)) { // move t0
559 const int delta = SLACK(e);
560 if (delta != 0)
561 tree_adjust(t0->rep,NULL,delta);
562 }
563 else { // move t1
564 const int delta = -SLACK(e);
565 if (delta != 0)
566 tree_adjust(t1->rep,NULL,delta);
567 }
568 if (add_tree_edge(ctx, e) != 0) {
569 return NULL;
570 }
571 return STsetUnion(t0, t1);
572 }
573
574 /* Construct initial tight tree. Graph must be connected, feasible.
575 * Adjust ND_rank(v) as needed. add_tree_edge() on tight tree edges.
576 * trees are basically lists of nodes stored in `LIST(node_t *)`s.
577 * Return 1 if input graph is not connected; 0 on success.
578 */
579 static
580 int feasible_tree(network_simplex_ctx_t *ctx)
581 {
582 Agedge_t *ee;
583 size_t subtree_count = 0;
584 STheap_t *heap = NULL;
585 int error = 0;
586
587 /* initialization */
588 for (Agnode_t *n = GD_nlist(ctx->G); n != NULL; n = ND_next(n)) {
589 ND_subtree_set(n,0);
590 }
591
592 subtree_t **tree = gv_calloc(ctx->N_nodes, sizeof(subtree_t *));
593 /* given init_rank, find all tight subtrees */
594 for (Agnode_t *n = GD_nlist(ctx->G); n != NULL; n = ND_next(n)) {
595 if (ND_subtree(n) == 0) {
596 tree[subtree_count] = find_tight_subtree(ctx, n);
597 if (tree[subtree_count] == NULL) {
598 error = 2;
599 goto end;
600 }
601 subtree_count++;
602 }
603 }
604
605 /* incrementally merge subtrees */
606 heap = STbuildheap(tree,subtree_count);
607 while (STheapsize(heap) > 1) {
608 subtree_t *tree0 = STextractmin(heap);
609 if (!(ee = inter_tree_edge(tree0))) {
610 error = 1;
611 break;
612 }
613 subtree_t *tree1 = merge_trees(ctx, ee);
614 if (tree1 == NULL) {
615 error = 2;
616 break;
617 }
618 STheapify(heap,tree1->heap_index);
619 }
620
621 end:
622 free(heap);
623 for (size_t i = 0; i < subtree_count; i++) free(tree[i]);
624 free(tree);
625 if (error) return error;
626 assert(LIST_SIZE(&ctx->Tree_edge) == ctx->N_nodes - 1);
627 init_cutvalues(ctx);
628 return 0;
629 }
630
631 /* walk up from v to LCA(v,w), setting new cutvalues. */
632 static Agnode_t *treeupdate(Agnode_t *v, Agnode_t *w, int cutvalue, bool dir) {
633 while (!SEQ(ND_low(v), ND_lim(w), ND_lim(v))) {
634 edge_t *const e = ND_par(v);
635 const bool d = v == agtail(e) ? dir : !dir;
636 if (d)
637 ED_cutvalue(e) += cutvalue;
638 else
639 ED_cutvalue(e) -= cutvalue;
640 if (ND_lim(agtail(e)) > ND_lim(aghead(e)))
641 v = agtail(e);
642 else
643 v = aghead(e);
644 }
645 return v;
646 }
647
648 static void rerank(Agnode_t * v, int delta)
649 {
650 edge_t *e;
651
652 ND_rank(v) -= delta;
653 for (int i = 0; (e = ND_tree_out(v).list[i]); i++)
654 if (e != ND_par(v))
655 rerank(aghead(e), delta);
656 for (int i = 0; (e = ND_tree_in(v).list[i]); i++)
657 if (e != ND_par(v))
658 rerank(agtail(e), delta);
659 }
660
661 /* e is the tree edge that is leaving and f is the nontree edge that
662 * is entering. compute new cut values, ranks, and exchange e and f.
663 */
664 static int
665 update(network_simplex_ctx_t *ctx, edge_t * e, edge_t * f)
666 {
667 const int delta = SLACK(f);
668 /* "for (v = in nodes in tail side of e) do ND_rank(v) -= delta;" */
669 if (delta > 0) {
670 size_t s = ND_tree_in(agtail(e)).size + ND_tree_out(agtail(e)).size;
671 if (s == 1)
672 rerank(agtail(e), delta);
673 else {
674 s = ND_tree_in(aghead(e)).size + ND_tree_out(aghead(e)).size;
675 if (s == 1)
676 rerank(aghead(e), -delta);
677 else {
678 if (ND_lim(agtail(e)) < ND_lim(aghead(e)))
679 rerank(agtail(e), delta);
680 else
681 rerank(aghead(e), -delta);
682 }
683 }
684 }
685
686 const int cutvalue = ED_cutvalue(e);
687 Agnode_t *const lca = treeupdate(agtail(f), aghead(f), cutvalue, true);
688 if (treeupdate(aghead(f), agtail(f), cutvalue, false) != lca) {
689 agerrorf("update: mismatched lca in treeupdates\n");
690 return 2;
691 }
692
693 // invalidate paths from LCA till affected nodes:
694 int lca_low = ND_low(lca);
695 invalidate_path(lca, aghead(f));
696 invalidate_path(lca, agtail(f));
697
698 ED_cutvalue(f) = -cutvalue;
699 ED_cutvalue(e) = 0;
700 exchange_tree_edges(ctx, e, f);
701 dfs_range(lca, ND_par(lca), lca_low);
702 return 0;
703 }
704
705 static int scan_and_normalize(network_simplex_ctx_t *ctx) {
706 int Minrank = INT_MAX;
707 int Maxrank = INT_MIN;
708 for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n)) {
709 if (ND_node_type(n) == NORMAL) {
710 Minrank = MIN(Minrank, ND_rank(n));
711 Maxrank = MAX(Maxrank, ND_rank(n));
712 }
713 }
714 for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n))
715 ND_rank(n) -= Minrank;
716 Maxrank -= Minrank;
717 return Maxrank;
718 }
719
720 static void reset_lists(network_simplex_ctx_t *ctx) {
721 LIST_FREE(&ctx->Tree_edge);
722 }
723
724 static void
725 freeTreeList (network_simplex_ctx_t *ctx, graph_t* g)
726 {
727 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
728 free_list(ND_tree_in(n));
729 free_list(ND_tree_out(n));
730 ND_mark(n) = false;
731 }
732 reset_lists(ctx);
733 }
734
735 static void LR_balance(network_simplex_ctx_t *ctx)
736 {
737 for (size_t i = 0; i < LIST_SIZE(&ctx->Tree_edge); i++) {
738 edge_t *const e = LIST_GET(&ctx->Tree_edge, i);
739 if (ED_cutvalue(e) == 0) {
740 edge_t *const f = enter_edge(e);
741 if (f == NULL)
742 continue;
743 const int delta = SLACK(f);
744 if (delta <= 1)
745 continue;
746 if (ND_lim(agtail(e)) < ND_lim(aghead(e)))
747 rerank(agtail(e), delta / 2);
748 else
749 rerank(aghead(e), -delta / 2);
750 }
751 }
752 freeTreeList(ctx, ctx->G);
753 }
754
755 static int decreasingrankcmpf(const void *x, const void *y) {
756 node_t *const *n0 = x;
757 node_t *const *n1 = y;
758 if (ND_rank(*n1) < ND_rank(*n0)) {
759 return -1;
760 }
761 if (ND_rank(*n1) > ND_rank(*n0)) {
762 return 1;
763 }
764 return 0;
765 }
766
767 static int increasingrankcmpf(const void *x, const void *y) {
768 return -decreasingrankcmpf(x, y);
769 }
770
771 static void TB_balance(network_simplex_ctx_t *ctx)
772 {
773 edge_t *e;
774 int adj = 0;
775 char *s;
776
777 const int Maxrank = scan_and_normalize(ctx);
778
779 /* find nodes that are not tight and move to less populated ranks */
780 assert(Maxrank >= 0);
781 int *nrank = gv_calloc((size_t)Maxrank + 1, sizeof(int));
782 if ( (s = agget(ctx->G,"TBbalance")) ) {
783 if (streq(s,"min")) adj = 1;
784 else if (streq(s,"max")) adj = 2;
785 if (adj) for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n))
786 if (ND_node_type(n) == NORMAL) {
787 if (ND_in(n).size == 0 && adj == 1) {
788 ND_rank(n) = 0;
789 }
790 if (ND_out(n).size == 0 && adj == 2) {
791 ND_rank(n) = Maxrank;
792 }
793 }
794 }
795 LIST(node_t *) Tree_node = {0};
796 LIST_RESERVE(&Tree_node, ctx->N_nodes);
797 for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n)) {
798 LIST_APPEND(&Tree_node, n);
799 }
800 LIST_SORT(&Tree_node, adj > 1 ? decreasingrankcmpf: increasingrankcmpf);
801 for (size_t i = 0; i < LIST_SIZE(&Tree_node); i++) {
802 node_t *const n = LIST_GET(&Tree_node, i);
803 if (ND_node_type(n) == NORMAL)
804 nrank[ND_rank(n)]++;
805 }
806 for (size_t ii = 0; ii < LIST_SIZE(&Tree_node); ii++) {
807 node_t *const n = LIST_GET(&Tree_node, ii);
808 if (ND_node_type(n) != NORMAL)
809 continue;
810 int inweight = 0;
811 int outweight = 0;
812 int low = 0;
813 int high = Maxrank;
814 for (size_t i = 0; (e = ND_in(n).list[i]); i++) {
815 inweight += ED_weight(e);
816 low = MAX(low, ND_rank(agtail(e)) + ED_minlen(e));
817 }
818 for (size_t i = 0; (e = ND_out(n).list[i]); i++) {
819 outweight += ED_weight(e);
820 high = MIN(high, ND_rank(aghead(e)) - ED_minlen(e));
821 }
822 if (low < 0)
823 low = 0; /* vnodes can have ranks < 0 */
824 if (adj) {
825 if (inweight == outweight)
826 ND_rank(n) = adj == 1 ? low : high;
827 }
828 else {
829 if (inweight == outweight) {
830 int choice = low;
831 for (int i = low + 1; i <= high; i++)
832 if (nrank[i] < nrank[choice])
833 choice = i;
834 nrank[ND_rank(n)]--;
835 nrank[choice]++;
836 ND_rank(n) = choice;
837 }
838 }
839 free_list(ND_tree_in(n));
840 free_list(ND_tree_out(n));
841 ND_mark(n) = false;
842 }
843 LIST_FREE(&Tree_node);
844 free(nrank);
845 }
846
847 static bool init_graph(network_simplex_ctx_t *ctx, graph_t *g) {
848 edge_t *e;
849
850 *ctx = (network_simplex_ctx_t){.G = g};
851 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
852 ND_mark(n) = false;
853 ctx->N_nodes++;
854 for (size_t i = 0; ND_out(n).list[i]; i++)
855 ctx->N_edges++;
856 }
857
858 LIST_RESERVE(&ctx->Tree_edge, ctx->N_nodes);
859
860 bool feasible = true;
861 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
862 ND_priority(n) = 0;
863 size_t i;
864 for (i = 0; (e = ND_in(n).list[i]); i++) {
865 ND_priority(n)++;
866 ED_cutvalue(e) = 0;
867 ED_tree_index(e) = -1;
868 if (ND_rank(aghead(e)) - ND_rank(agtail(e)) < ED_minlen(e))
869 feasible = false;
870 }
871 ND_tree_in(n).list = gv_calloc(i + 1, sizeof(edge_t *));
872 ND_tree_in(n).size = 0;
873 for (i = 0; ND_out(n).list[i]; i++);
874 ND_tree_out(n).list = gv_calloc(i + 1, sizeof(edge_t *));
875 ND_tree_out(n).size = 0;
876 }
877 return feasible;
878 }
879
880 /* graphSize:
881 * Compute no. of nodes and edges in the graph
882 */
883 static void graphSize(graph_t *g, size_t *nn, size_t *ne) {
884 size_t nnodes = 0;
885 size_t nedges = 0;
886 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
887 nnodes++;
888 for (size_t i = 0; ND_out(n).list[i]; i++) {
889 nedges++;
890 }
891 }
892 *nn = nnodes;
893 *ne = nedges;
894 }
895
896 /* rank:
897 * Apply network simplex to rank the nodes in a graph.
898 * Uses ED_minlen as the internode constraint: if a->b with minlen=ml,
899 * rank b - rank a >= ml.
900 * Assumes the graph has the following additional structure:
901 * A list of all nodes, starting at GD_nlist, and linked using ND_next.
902 * Out and in edges lists stored in ND_out and ND_in, even if the node
903 * doesn't have any out or in edges.
904 * The node rank values are stored in ND_rank.
905 * Returns 0 if successful; returns 1 if the graph was not connected;
906 * returns 2 if something seriously wrong;
907 */
908 int rank2(graph_t * g, int balance, int maxiter, int search_size)
909 {
910 int iter = 0;
911 char ns[] = "network simplex: ";
912 edge_t *e;
913 network_simplex_ctx_t ctx = {0};
914
915 #ifdef DEBUG
916 check_cycles(g);
917 #endif
918 if (Verbose) {
919 size_t nn, ne;
920 graphSize (g, &nn, &ne);
921 fprintf(stderr, "%s %" PRISIZE_T " nodes %" PRISIZE_T
922 " edges maxiter=%d balance=%d\n", ns, nn, ne, maxiter, balance);
923 start_timer();
924 }
925 bool feasible = init_graph(&ctx, g);
926 if (!feasible)
927 init_rank(&ctx);
928
929 if (search_size >= 0)
930 ctx.Search_size = search_size;
931 else
932 ctx.Search_size = SEARCHSIZE;
933
934 {
935 const int err = feasible_tree(&ctx);
936 if (err != 0) {
937 freeTreeList(&ctx, g);
938 return err;
939 }
940 }
941 if (maxiter <= 0) {
942 freeTreeList(&ctx, g);
943 return 0;
944 }
945
946 while ((e = leave_edge(&ctx))) {
947 edge_t *const f = enter_edge(e);
948 const int err = update(&ctx, e, f);
949 if (err != 0) {
950 freeTreeList(&ctx, g);
951 return err;
952 }
953 iter++;
954 if (Verbose && iter % 100 == 0) {
955 if (iter % 1000 == 100)
956 fputs(ns, stderr);
957 fprintf(stderr, "%d ", iter);
958 if (iter % 1000 == 0)
959 fputc('\n', stderr);
960 }
961 if (iter >= maxiter)
962 break;
963 }
964 switch (balance) {
965 case 1:
966 TB_balance(&ctx);
967 reset_lists(&ctx);
968 break;
969 case 2:
970 LR_balance(&ctx);
971 break;
972 default:
973 (void)scan_and_normalize(&ctx);
974 freeTreeList (&ctx, ctx.G);
975 break;
976 }
977 if (Verbose) {
978 if (iter >= 100)
979 fputc('\n', stderr);
980 fprintf(stderr, "%s%" PRISIZE_T " nodes %" PRISIZE_T " edges %d iter %.2f sec\n",
981 ns, ctx.N_nodes, ctx.N_edges, iter, elapsed_sec());
982 }
983 return 0;
984 }
985
986 int rank(graph_t * g, int balance, int maxiter)
987 {
988 char *s;
989 int search_size;
990
991 if ((s = agget(g, "searchsize")))
992 search_size = atoi(s);
993 else
994 search_size = SEARCHSIZE;
995
996 return rank2 (g, balance, maxiter, search_size);
997 }
998
999 /* set cut value of f, assuming values of edges on one side were already set */
1000 static void x_cutval(edge_t * f)
1001 {
1002 node_t *v;
1003 edge_t *e;
1004 int dir;
1005
1006 /* set v to the node on the side of the edge already searched */
1007 if (ND_par(agtail(f)) == f) {
1008 v = agtail(f);
1009 dir = 1;
1010 } else {
1011 v = aghead(f);
1012 dir = -1;
1013 }
1014
1015 int sum = 0;
1016 for (int i = 0; (e = ND_out(v).list[i]); i++)
1017 if (sadd_overflow(sum, x_val(e, v, dir), &sum)) {
1018 agerrorf("overflow when computing edge weight sum\n");
1019 graphviz_exit(EXIT_FAILURE);
1020 }
1021 for (int i = 0; (e = ND_in(v).list[i]); i++)
1022 if (sadd_overflow(sum, x_val(e, v, dir), &sum)) {
1023 agerrorf("overflow when computing edge weight sum\n");
1024 graphviz_exit(EXIT_FAILURE);
1025 }
1026 ED_cutvalue(f) = sum;
1027 }
1028
1029 static int x_val(edge_t * e, node_t * v, int dir)
1030 {
1031 node_t *other;
1032 int d, rv, f;
1033
1034 if (agtail(e) == v)
1035 other = aghead(e);
1036 else
1037 other = agtail(e);
1038 if (!(SEQ(ND_low(v), ND_lim(other), ND_lim(v)))) {
1039 f = 1;
1040 rv = ED_weight(e);
1041 } else {
1042 f = 0;
1043 if (TREE_EDGE(e))
1044 rv = ED_cutvalue(e);
1045 else
1046 rv = 0;
1047 rv -= ED_weight(e);
1048 }
1049 if (dir > 0) {
1050 if (aghead(e) == v)
1051 d = 1;
1052 else
1053 d = -1;
1054 } else {
1055 if (agtail(e) == v)
1056 d = 1;
1057 else
1058 d = -1;
1059 }
1060 if (f)
1061 d = -d;
1062 if (d < 0)
1063 rv = -rv;
1064 return rv;
1065 }
1066
1067 static void dfs_cutval(node_t * v, edge_t * par)
1068 {
1069 edge_t *e;
1070
1071 for (int i = 0; (e = ND_tree_out(v).list[i]); i++)
1072 if (e != par)
1073 dfs_cutval(aghead(e), e);
1074 for (int i = 0; (e = ND_tree_in(v).list[i]); i++)
1075 if (e != par)
1076 dfs_cutval(agtail(e), e);
1077 if (par)
1078 x_cutval(par);
1079 }
1080
1081 /// local state used by `dfs_range*`
1082 typedef struct {
1083 node_t *v;
1084 edge_t *par;
1085 int lim;
1086 int tree_out_i;
1087 int tree_in_i;
1088 } dfs_state_t;
1089
1090 /*
1091 * Initializes DFS range attributes (par, low, lim) over tree nodes such that:
1092 * ND_par(n) - parent tree edge
1093 * ND_low(n) - min DFS index for nodes in sub-tree (>= 1)
1094 * ND_lim(n) - max DFS index for nodes in sub-tree
1095 */
1096 static int dfs_range_init(node_t *v) {
1097 int lim = 0;
1098
1099 LIST(dfs_state_t) todo = {0};
1100
1101 ND_par(v) = NULL;
1102 ND_low(v) = 1;
1103 const dfs_state_t root = {.v = v, .par = NULL, .lim = 1};
1104 LIST_PUSH_BACK(&todo, root);
1105
1106 while (!LIST_IS_EMPTY(&todo)) {
1107 bool pushed_new = false;
1108 dfs_state_t *const s = LIST_BACK(&todo);
1109
1110 while (ND_tree_out(s->v).list[s->tree_out_i]) {
1111 edge_t *const e = ND_tree_out(s->v).list[s->tree_out_i];
1112 ++s->tree_out_i;
1113 if (e != s->par) {
1114 node_t *const n = aghead(e);
1115 ND_par(n) = e;
1116 ND_low(n) = s->lim;
1117 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1118 LIST_PUSH_BACK(&todo, next);
1119 pushed_new = true;
1120 break;
1121 }
1122 }
1123 if (pushed_new) {
1124 continue;
1125 }
1126
1127 while (ND_tree_in(s->v).list[s->tree_in_i]) {
1128 edge_t *const e = ND_tree_in(s->v).list[s->tree_in_i];
1129 ++s->tree_in_i;
1130 if (e != s->par) {
1131 node_t *const n = agtail(e);
1132 ND_par(n) = e;
1133 ND_low(n) = s->lim;
1134 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1135 LIST_PUSH_BACK(&todo, next);
1136 pushed_new = true;
1137 break;
1138 }
1139 }
1140 if (pushed_new) {
1141 continue;
1142 }
1143
1144 ND_lim(s->v) = s->lim;
1145
1146 lim = s->lim;
1147 LIST_DROP_BACK(&todo);
1148
1149 if (!LIST_IS_EMPTY(&todo)) {
1150 LIST_BACK(&todo)->lim = lim + 1;
1151 }
1152 }
1153
1154 LIST_FREE(&todo);
1155
1156 return lim + 1;
1157 }
1158
1159 /*
1160 * Incrementally updates DFS range attributes
1161 */
1162 static int dfs_range(node_t * v, edge_t * par, int low)
1163 {
1164 int lim = 0;
1165
1166 if (ND_par(v) == par && ND_low(v) == low) {
1167 return ND_lim(v) + 1;
1168 }
1169
1170 LIST(dfs_state_t) todo = {0};
1171
1172 ND_par(v) = par;
1173 ND_low(v) = low;
1174 const dfs_state_t root = {.v = v, .par = par, .lim = low};
1175 LIST_PUSH_BACK(&todo, root);
1176
1177 while (!LIST_IS_EMPTY(&todo)) {
1178 bool processed_child = false;
1179 dfs_state_t *const s = LIST_BACK(&todo);
1180
1181 while (ND_tree_out(s->v).list[s->tree_out_i]) {
1182 edge_t *const e = ND_tree_out(s->v).list[s->tree_out_i];
1183 ++s->tree_out_i;
1184 if (e != s->par) {
1185 node_t *const n = aghead(e);
1186 if (ND_par(n) == e && ND_low(n) == s->lim) {
1187 s->lim = ND_lim(n) + 1;
1188 } else {
1189 ND_par(n) = e;
1190 ND_low(n) = s->lim;
1191 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1192 LIST_PUSH_BACK(&todo, next);
1193 }
1194 processed_child = true;
1195 break;
1196 }
1197 }
1198 if (processed_child) {
1199 continue;
1200 }
1201
1202 while (ND_tree_in(s->v).list[s->tree_in_i]) {
1203 edge_t *const e = ND_tree_in(s->v).list[s->tree_in_i];
1204 ++s->tree_in_i;
1205 if (e != s->par) {
1206 node_t *const n = agtail(e);
1207 if (ND_par(n) == e && ND_low(n) == s->lim) {
1208 s->lim = ND_lim(n) + 1;
1209 } else {
1210 ND_par(n) = e;
1211 ND_low(n) = s->lim;
1212 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1213 LIST_PUSH_BACK(&todo, next);
1214 }
1215 processed_child = true;
1216 break;
1217 }
1218 }
1219 if (processed_child) {
1220 continue;
1221 }
1222
1223 ND_lim(s->v) = s->lim;
1224
1225 lim = s->lim;
1226 LIST_DROP_BACK(&todo);
1227
1228 if (!LIST_IS_EMPTY(&todo)) {
1229 LIST_BACK(&todo)->lim = lim + 1;
1230 }
1231 }
1232
1233 LIST_FREE(&todo);
1234
1235 return lim + 1;
1236 }
1237
1238 #ifdef DEBUG
1239 void tchk(network_simplex_ctx_t *ctx)
1240 {
1241 edge_t *e;
1242
1243 size_t n_cnt = 0;
1244 size_t e_cnt = 0;
1245 for (node_t *n = agfstnode(ctx->G); n; n = agnxtnode(ctx->G, n)) {
1246 n_cnt++;
1247 for (int i = 0; (e = ND_tree_out(n).list[i]); i++) {
1248 e_cnt++;
1249 if (SLACK(e) > 0)
1250 fprintf(stderr, "not a tight tree %p", e);
1251 }
1252 }
1253 if (e_cnt != LIST_SIZE(&ctx->Tree_edge))
1254 fprintf(stderr, "something missing\n");
1255 }
1256
1257 static void dump_node(FILE *sink, node_t *n) {
1258 if (ND_node_type(n)) {
1259 fprintf(sink, "%p", n);
1260 }
1261 else
1262 fputs(agnameof(n), sink);
1263 }
1264
1265 static void dump_graph (graph_t* g)
1266 {
1267 edge_t *e;
1268 FILE *const fp = fopen ("ns.gv", "w");
1269 fprintf (fp, "digraph \"%s\" {\n", agnameof(g));
1270 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
1271 fputs(" \"", fp);
1272 dump_node(fp, n);
1273 fputs("\"\n", fp);
1274 }
1275 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
1276 for (int i = 0; (e = ND_out(n).list[i]); i++) {
1277 fputs(" \"", fp);
1278 dump_node(fp, n);
1279 fputs("\"", fp);
1280 node_t *const w = aghead(e);
1281 fputs(" -> \"", fp);
1282 dump_node(fp, w);
1283 fputs("\"\n", fp);
1284 }
1285 }
1286
1287 fprintf (fp, "}\n");
1288 fclose (fp);
1289 }
1290
1291 static node_t *checkdfs(graph_t* g, node_t * n)
1292 {
1293 edge_t *e;
1294
1295 if (ND_mark(n))
1296 return NULL;
1297 ND_mark(n) = true;
1298 ND_onstack(n) = true;
1299 for (int i = 0; (e = ND_out(n).list[i]); i++) {
1300 node_t *const w = aghead(e);
1301 if (ND_onstack(w)) {
1302 dump_graph (g);
1303 fprintf(stderr, "cycle: last edge %p %s(%p) %s(%p)\n", e, agnameof(n), n,
1304 agnameof(w), w);
1305 return w;
1306 }
1307 else {
1308 if (!ND_mark(w)) {
1309 node_t *const x = checkdfs(g, w);
1310 if (x) {
1311 fprintf(stderr,"unwind %p %s(%p)\n", e, agnameof(n), n);
1312 if (x != n) return x;
1313 fprintf(stderr,"unwound to root\n");
1314 fflush(stderr);
1315 abort();
1316 return NULL;
1317 }
1318 }
1319 }
1320 }
1321 ND_onstack(n) = false;
1322 return NULL;
1323 }
1324
1325 void check_cycles(graph_t * g)
1326 {
1327 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
1328 ND_mark(n) = false;
1329 ND_onstack(n) = false;
1330 }
1331 for (node_t *n = GD_nlist(g); n; n = ND_next(n))
1332 checkdfs(g, n);
1333 }
1334 #endif /* DEBUG */