Graphviz 14.1.4~dev.20260305.2341
Loading...
Searching...
No Matches
ns.c
Go to the documentation of this file.
1
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
34static void dfs_cutval(node_t * v, edge_t * par);
35static int dfs_range_init(node_t *v);
36static int dfs_range(node_t * v, edge_t * par, int low);
37static int x_val(edge_t * e, node_t * v, int dir);
38#ifdef DEBUG
39static 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
47typedef struct {
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;
54
55enum { SEARCHSIZE = 30 };
56
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
90static 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
115{
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
145static
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
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
215static 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
248static 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
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
300{
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
310typedef struct subtree_s {
311 node_t *rep; /* some node in the tree */
312 int size; /* total tight tree size */
313 size_t heap_index;
314 struct subtree_s *par; /* union find */
316
318static bool on_heap(const subtree_t *tree) {
319 return tree->heap_index != SIZE_MAX;
320}
321
323typedef struct {
325 int in_i;
326 int out_i;
327 int rv;
328} tst_t;
329
330/* find initial tight subtrees */
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 ++top->in_i;
357 ND_subtree_set(agtail(e), st);
358 const tst_t next = {.v = agtail(e), .rv = 1};
359 LIST_PUSH_BACK(&todo, next);
360 }
361 updated = true;
362 break;
363 }
364 }
365 if (updated) {
366 continue;
367 }
368
369 for (; (e = ND_out(top->v).list[top->out_i]); top->out_i++) {
370 if (TREE_EDGE(e)) continue;
371 if (ND_subtree(aghead(e)) == 0 && SLACK(e) == 0) {
372 if (add_tree_edge(ctx, e) != 0) {
373 LIST_DROP_BACK(&todo);
374 if (LIST_IS_EMPTY(&todo)) {
375 rv = -1;
376 } else {
377 --LIST_BACK(&todo)->rv;
378 }
379 } else {
380 ++top->out_i;
381 ND_subtree_set(aghead(e), st);
382 const tst_t next = {.v = aghead(e), .rv = 1};
383 LIST_PUSH_BACK(&todo, next);
384 }
385 updated = true;
386 break;
387 }
388 }
389 if (updated) {
390 continue;
391 }
392
393 const tst_t last = LIST_POP_BACK(&todo);
394 if (LIST_IS_EMPTY(&todo)) {
395 rv = last.rv;
396 } else {
397 LIST_BACK(&todo)->rv += last.rv;
398 }
399 }
400
401 LIST_FREE(&todo);
402
403 return rv;
404}
405
407{
408 subtree_t *rv = gv_alloc(sizeof(subtree_t));
409 rv->rep = v;
410 rv->size = tight_subtree_search(ctx,v,rv);
411 if (rv->size < 0) {
412 free(rv);
413 return NULL;
414 }
415 rv->par = rv;
416 return rv;
417}
418
419typedef struct STheap_s {
421 size_t size;
423
425{
426 subtree_t *s0 = ND_subtree(n0);
427 while (s0->par && s0->par != s0) {
428 if (s0->par->par) {s0->par = s0->par->par;} /* path compression for the code weary */
429 s0 = s0->par;
430 }
431 return s0;
432}
433
435{
436 subtree_t *r0, *r1, *r;
437
438 for (r0 = s0; r0->par && r0->par != r0; r0 = r0->par);
439 for (r1 = s1; r1->par && r1->par != r1; r1 = r1->par);
440 if (r0 == r1) return r0; /* safety code but shouldn't happen */
441 assert(on_heap(r0) || on_heap(r1));
442 if (!on_heap(r1)) r = r0;
443 else if (!on_heap(r0)) r = r1;
444 else if (r1->size < r0->size) r = r0;
445 else r = r1;
446
447 r0->par = r1->par = r;
448 r->size = r0->size + r1->size;
449 assert(on_heap(r));
450 return r;
451}
452
453/* find tightest edge to another tree incident on the given tree */
455
456 // per-node state
457 typedef struct {
458 Agnode_t *v;
459 subtree_t *ts;
460 Agnode_t *from;
461 int out_i;
462 int in_i;
463 } state_t;
464
465 LIST(state_t) todo = {0};
466 LIST_PUSH_BACK(&todo, ((state_t){.v = v, .ts = STsetFind(v)}));
467
468 Agedge_t *best = NULL;
469
470 while (!LIST_IS_EMPTY(&todo)) {
471 state_t *const s = LIST_BACK(&todo);
472 if (s->out_i == 0 && s->in_i == 0 && best != NULL && SLACK(best) == 0) {
473 LIST_DROP_BACK(&todo);
474 continue;
475 }
476
477 bool updated = false;
478 Agedge_t *e;
479 for (; (e = ND_out(s->v).list[s->out_i]) != NULL; ++s->out_i) {
480 if (TREE_EDGE(e)) {
481 if (aghead(e) == s->from) continue; // do not search back in tree
482 ++s->out_i;
483 LIST_PUSH_BACK(&todo, ((state_t){.v = aghead(e),
484 .ts = STsetFind(aghead(e)),
485 .from = s->v}));
486 // search forward in tree
487 updated = true;
488 break;
489 } else {
490 if (STsetFind(aghead(e)) != s->ts) { // encountered candidate edge
491 if (best == NULL || SLACK(e) < SLACK(best)) best = e;
492 }
493 // else ignore non-tree edge between nodes in the same tree
494 }
495 }
496 if (updated) {
497 continue;
498 }
499
500 // the following code must mirror the above, but for in-edges
501 for (; (e = ND_in(s->v).list[s->in_i]); ++s->in_i) {
502 if (TREE_EDGE(e)) {
503 if (agtail(e) == s->from) continue;
504 ++s->in_i;
505 LIST_PUSH_BACK(&todo, ((state_t){.v = agtail(e),
506 .ts = STsetFind(agtail(e)),
507 .from = s->v}));
508 updated = true;
509 break;
510 } else {
511 if (STsetFind(agtail(e)) != s->ts) {
512 if (best == NULL || SLACK(e) < SLACK(best)) best = e;
513 }
514 }
515 }
516 if (updated) {
517 continue;
518 }
519
520 LIST_DROP_BACK(&todo);
521 }
522
523 LIST_FREE(&todo);
524 return best;
525}
526
528{
529 return inter_tree_edge_search(tree->rep);
530}
531
532static size_t STheapsize(const STheap_t *heap) { return heap->size; }
533
534static void STheapify(STheap_t *heap, size_t i) {
535 subtree_t **elt = heap->elt;
536 do {
537 const size_t left = 2 * (i + 1) - 1;
538 const size_t right = 2 * (i + 1);
539 size_t smallest = i;
540 if (left < heap->size && elt[left]->size < elt[smallest]->size) smallest = left;
541 if (right < heap->size && elt[right]->size < elt[smallest]->size) smallest = right;
542 if (smallest != i) {
543 SWAP(&elt[i], &elt[smallest]);
544 elt[i]->heap_index = i;
545 elt[smallest]->heap_index = smallest;
546 i = smallest;
547 }
548 else break;
549 } while (i < heap->size);
550}
551
552static STheap_t *STbuildheap(subtree_t **elt, size_t size) {
553 STheap_t *heap = gv_alloc(sizeof(STheap_t));
554 heap->elt = elt;
555 heap->size = size;
556 for (size_t i = 0; i < heap->size; i++) heap->elt[i]->heap_index = i;
557 for (size_t i = heap->size / 2; i != SIZE_MAX; i--)
558 STheapify(heap,i);
559 return heap;
560}
561
562static
564{
565 subtree_t *rv = heap->elt[0];
566 rv->heap_index = SIZE_MAX;
567 // mark this as not participating in the heap anymore
568 heap->elt[0] = heap->elt[heap->size - 1];
569 heap->elt[0]->heap_index = 0;
570 heap->elt[heap->size -1] = rv; /* needed to free storage later */
571 heap->size--;
572 STheapify(heap,0);
573 return rv;
574}
575
576static
577void tree_adjust(Agnode_t *v, Agnode_t *from, int delta)
578{
579 Agedge_t *e;
580 ND_rank(v) += delta;
581 for (int i = 0; (e = ND_tree_in(v).list[i]); i++) {
582 Agnode_t *const w = agtail(e);
583 if (w != from)
584 tree_adjust(w, v, delta);
585 }
586 for (int i = 0; (e = ND_tree_out(v).list[i]); i++) {
587 Agnode_t *const w = aghead(e);
588 if (w != from)
589 tree_adjust(w, v, delta);
590 }
591}
592
593static
594subtree_t *merge_trees(network_simplex_ctx_t *ctx, Agedge_t *e) /* entering tree edge */
595{
596 assert(!TREE_EDGE(e));
597
598 subtree_t *const t0 = STsetFind(agtail(e));
599 subtree_t *const t1 = STsetFind(aghead(e));
600
601 if (!on_heap(t0)) { // move t0
602 const int delta = SLACK(e);
603 if (delta != 0)
605 }
606 else { // move t1
607 const int delta = -SLACK(e);
608 if (delta != 0)
610 }
611 if (add_tree_edge(ctx, e) != 0) {
612 return NULL;
613 }
614 return STsetUnion(t0, t1);
615}
616
617/* Construct initial tight tree. Graph must be connected, feasible.
618 * Adjust ND_rank(v) as needed. add_tree_edge() on tight tree edges.
619 * trees are basically lists of nodes stored in `LIST(node_t *)`s.
620 * Return 1 if input graph is not connected; 0 on success.
621 */
622static
624{
625 Agedge_t *ee;
626 size_t subtree_count = 0;
627 STheap_t *heap = NULL;
628 int error = 0;
629
630 /* initialization */
631 for (Agnode_t *n = GD_nlist(ctx->G); n != NULL; n = ND_next(n)) {
632 ND_subtree_set(n,0);
633 }
634
635 subtree_t **tree = gv_calloc(ctx->N_nodes, sizeof(subtree_t *));
636 /* given init_rank, find all tight subtrees */
637 for (Agnode_t *n = GD_nlist(ctx->G); n != NULL; n = ND_next(n)) {
638 if (ND_subtree(n) == 0) {
639 tree[subtree_count] = find_tight_subtree(ctx, n);
640 if (tree[subtree_count] == NULL) {
641 error = 2;
642 goto end;
643 }
644 subtree_count++;
645 }
646 }
647
648 /* incrementally merge subtrees */
649 heap = STbuildheap(tree,subtree_count);
650 while (STheapsize(heap) > 1) {
651 subtree_t *tree0 = STextractmin(heap);
652 if (!(ee = inter_tree_edge(tree0))) {
653 error = 1;
654 break;
655 }
656 subtree_t *tree1 = merge_trees(ctx, ee);
657 if (tree1 == NULL) {
658 error = 2;
659 break;
660 }
661 STheapify(heap,tree1->heap_index);
662 }
663
664end:
665 free(heap);
666 for (size_t i = 0; i < subtree_count; i++) free(tree[i]);
667 free(tree);
668 if (error) return error;
669 assert(LIST_SIZE(&ctx->Tree_edge) == ctx->N_nodes - 1);
670 init_cutvalues(ctx);
671 return 0;
672}
673
674/* walk up from v to LCA(v,w), setting new cutvalues. */
675static Agnode_t *treeupdate(Agnode_t *v, Agnode_t *w, int cutvalue, bool dir) {
676 while (!SEQ(ND_low(v), ND_lim(w), ND_lim(v))) {
677 edge_t *const e = ND_par(v);
678 const bool d = v == agtail(e) ? dir : !dir;
679 if (d)
680 ED_cutvalue(e) += cutvalue;
681 else
682 ED_cutvalue(e) -= cutvalue;
683 if (ND_lim(agtail(e)) > ND_lim(aghead(e)))
684 v = agtail(e);
685 else
686 v = aghead(e);
687 }
688 return v;
689}
690
691static void rerank(Agnode_t * v, int delta)
692{
693 edge_t *e;
694
695 ND_rank(v) -= delta;
696 for (int i = 0; (e = ND_tree_out(v).list[i]); i++)
697 if (e != ND_par(v))
698 rerank(aghead(e), delta);
699 for (int i = 0; (e = ND_tree_in(v).list[i]); i++)
700 if (e != ND_par(v))
701 rerank(agtail(e), delta);
702}
703
704/* e is the tree edge that is leaving and f is the nontree edge that
705 * is entering. compute new cut values, ranks, and exchange e and f.
706 */
707static int
709{
710 const int delta = SLACK(f);
711 /* "for (v = in nodes in tail side of e) do ND_rank(v) -= delta;" */
712 if (delta > 0) {
713 size_t s = ND_tree_in(agtail(e)).size + ND_tree_out(agtail(e)).size;
714 if (s == 1)
715 rerank(agtail(e), delta);
716 else {
717 s = ND_tree_in(aghead(e)).size + ND_tree_out(aghead(e)).size;
718 if (s == 1)
719 rerank(aghead(e), -delta);
720 else {
721 if (ND_lim(agtail(e)) < ND_lim(aghead(e)))
722 rerank(agtail(e), delta);
723 else
724 rerank(aghead(e), -delta);
725 }
726 }
727 }
728
729 const int cutvalue = ED_cutvalue(e);
730 Agnode_t *const lca = treeupdate(agtail(f), aghead(f), cutvalue, true);
731 if (treeupdate(aghead(f), agtail(f), cutvalue, false) != lca) {
732 agerrorf("update: mismatched lca in treeupdates\n");
733 return 2;
734 }
735
736 // invalidate paths from LCA till affected nodes:
737 int lca_low = ND_low(lca);
738 invalidate_path(lca, aghead(f));
739 invalidate_path(lca, agtail(f));
740
741 ED_cutvalue(f) = -cutvalue;
742 ED_cutvalue(e) = 0;
743 exchange_tree_edges(ctx, e, f);
744 dfs_range(lca, ND_par(lca), lca_low);
745 return 0;
746}
747
749 int Minrank = INT_MAX;
750 int Maxrank = INT_MIN;
751 for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n)) {
752 if (ND_node_type(n) == NORMAL) {
753 Minrank = MIN(Minrank, ND_rank(n));
754 Maxrank = MAX(Maxrank, ND_rank(n));
755 }
756 }
757 for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n))
758 ND_rank(n) -= Minrank;
759 Maxrank -= Minrank;
760 return Maxrank;
761}
762
764 LIST_FREE(&ctx->Tree_edge);
765}
766
767static void
769{
770 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
773 ND_mark(n) = false;
774 }
775 reset_lists(ctx);
776}
777
779{
780 for (size_t i = 0; i < LIST_SIZE(&ctx->Tree_edge); i++) {
781 edge_t *const e = LIST_GET(&ctx->Tree_edge, i);
782 if (ED_cutvalue(e) == 0) {
783 edge_t *const f = enter_edge(e);
784 if (f == NULL)
785 continue;
786 const int delta = SLACK(f);
787 if (delta <= 1)
788 continue;
789 if (ND_lim(agtail(e)) < ND_lim(aghead(e)))
790 rerank(agtail(e), delta / 2);
791 else
792 rerank(aghead(e), -delta / 2);
793 }
794 }
795 freeTreeList(ctx, ctx->G);
796}
797
798static int decreasingrankcmpf(const void *x, const void *y) {
799 node_t *const *n0 = x;
800 node_t *const *n1 = y;
801 if (ND_rank(*n1) < ND_rank(*n0)) {
802 return -1;
803 }
804 if (ND_rank(*n1) > ND_rank(*n0)) {
805 return 1;
806 }
807 return 0;
808}
809
810static int increasingrankcmpf(const void *x, const void *y) {
811 return -decreasingrankcmpf(x, y);
812}
813
815{
816 edge_t *e;
817 int adj = 0;
818 char *s;
819
820 const int Maxrank = scan_and_normalize(ctx);
821
822 /* find nodes that are not tight and move to less populated ranks */
823 assert(Maxrank >= 0);
824 int *nrank = gv_calloc((size_t)Maxrank + 1, sizeof(int));
825 if ( (s = agget(ctx->G,"TBbalance")) ) {
826 if (streq(s,"min")) adj = 1;
827 else if (streq(s,"max")) adj = 2;
828 if (adj) for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n))
829 if (ND_node_type(n) == NORMAL) {
830 if (ND_in(n).size == 0 && adj == 1) {
831 ND_rank(n) = 0;
832 }
833 if (ND_out(n).size == 0 && adj == 2) {
834 ND_rank(n) = Maxrank;
835 }
836 }
837 }
838 LIST(node_t *) Tree_node = {0};
839 LIST_RESERVE(&Tree_node, ctx->N_nodes);
840 for (node_t *n = GD_nlist(ctx->G); n; n = ND_next(n)) {
841 LIST_APPEND(&Tree_node, n);
842 }
843 LIST_SORT(&Tree_node, adj > 1 ? decreasingrankcmpf: increasingrankcmpf);
844 for (size_t i = 0; i < LIST_SIZE(&Tree_node); i++) {
845 node_t *const n = LIST_GET(&Tree_node, i);
846 if (ND_node_type(n) == NORMAL)
847 nrank[ND_rank(n)]++;
848 }
849 for (size_t ii = 0; ii < LIST_SIZE(&Tree_node); ii++) {
850 node_t *const n = LIST_GET(&Tree_node, ii);
851 if (ND_node_type(n) != NORMAL)
852 continue;
853 int inweight = 0;
854 int outweight = 0;
855 int low = 0;
856 int high = Maxrank;
857 for (size_t i = 0; (e = ND_in(n).list[i]); i++) {
858 inweight += ED_weight(e);
859 low = MAX(low, ND_rank(agtail(e)) + ED_minlen(e));
860 }
861 for (size_t i = 0; (e = ND_out(n).list[i]); i++) {
862 outweight += ED_weight(e);
863 high = MIN(high, ND_rank(aghead(e)) - ED_minlen(e));
864 }
865 if (low < 0)
866 low = 0; /* vnodes can have ranks < 0 */
867 if (adj) {
868 if (inweight == outweight)
869 ND_rank(n) = adj == 1 ? low : high;
870 }
871 else {
872 if (inweight == outweight) {
873 int choice = low;
874 for (int i = low + 1; i <= high; i++)
875 if (nrank[i] < nrank[choice])
876 choice = i;
877 nrank[ND_rank(n)]--;
878 nrank[choice]++;
879 ND_rank(n) = choice;
880 }
881 }
884 ND_mark(n) = false;
885 }
886 LIST_FREE(&Tree_node);
887 free(nrank);
888}
889
891 edge_t *e;
892
893 *ctx = (network_simplex_ctx_t){.G = g};
894 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
895 ND_mark(n) = false;
896 ctx->N_nodes++;
897 for (size_t i = 0; ND_out(n).list[i]; i++)
898 ctx->N_edges++;
899 }
900
901 LIST_RESERVE(&ctx->Tree_edge, ctx->N_nodes);
902
903 bool feasible = true;
904 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
905 ND_priority(n) = 0;
906 size_t i;
907 for (i = 0; (e = ND_in(n).list[i]); i++) {
908 ND_priority(n)++;
909 ED_cutvalue(e) = 0;
910 ED_tree_index(e) = -1;
911 if (ND_rank(aghead(e)) - ND_rank(agtail(e)) < ED_minlen(e))
912 feasible = false;
913 }
914 ND_tree_in(n).list = gv_calloc(i + 1, sizeof(edge_t *));
915 ND_tree_in(n).size = 0;
916 for (i = 0; ND_out(n).list[i]; i++);
917 ND_tree_out(n).list = gv_calloc(i + 1, sizeof(edge_t *));
918 ND_tree_out(n).size = 0;
919 }
920 return feasible;
921}
922
923/* graphSize:
924 * Compute no. of nodes and edges in the graph
925 */
926static void graphSize(graph_t *g, size_t *nn, size_t *ne) {
927 size_t nnodes = 0;
928 size_t nedges = 0;
929 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
930 nnodes++;
931 for (size_t i = 0; ND_out(n).list[i]; i++) {
932 nedges++;
933 }
934 }
935 *nn = nnodes;
936 *ne = nedges;
937}
938
939/* rank:
940 * Apply network simplex to rank the nodes in a graph.
941 * Uses ED_minlen as the internode constraint: if a->b with minlen=ml,
942 * rank b - rank a >= ml.
943 * Assumes the graph has the following additional structure:
944 * A list of all nodes, starting at GD_nlist, and linked using ND_next.
945 * Out and in edges lists stored in ND_out and ND_in, even if the node
946 * doesn't have any out or in edges.
947 * The node rank values are stored in ND_rank.
948 * Returns 0 if successful; returns 1 if the graph was not connected;
949 * returns 2 if something seriously wrong;
950 */
951int rank2(graph_t * g, int balance, int maxiter, int search_size)
952{
953 int iter = 0;
954 char ns[] = "network simplex: ";
955 edge_t *e;
956 network_simplex_ctx_t ctx = {0};
957
958#ifdef DEBUG
959 check_cycles(g);
960#endif
961 if (Verbose) {
962 size_t nn, ne;
963 graphSize (g, &nn, &ne);
964 fprintf(stderr, "%s %" PRISIZE_T " nodes %" PRISIZE_T
965 " edges maxiter=%d balance=%d\n", ns, nn, ne, maxiter, balance);
966 start_timer();
967 }
968 bool feasible = init_graph(&ctx, g);
969 if (!feasible)
970 init_rank(&ctx);
971
972 if (search_size >= 0)
973 ctx.Search_size = search_size;
974 else
975 ctx.Search_size = SEARCHSIZE;
976
977 {
978 const int err = feasible_tree(&ctx);
979 if (err != 0) {
980 freeTreeList(&ctx, g);
981 return err;
982 }
983 }
984 if (maxiter <= 0) {
985 freeTreeList(&ctx, g);
986 return 0;
987 }
988
989 while ((e = leave_edge(&ctx))) {
990 edge_t *const f = enter_edge(e);
991 const int err = update(&ctx, e, f);
992 if (err != 0) {
993 freeTreeList(&ctx, g);
994 return err;
995 }
996 iter++;
997 if (Verbose && iter % 100 == 0) {
998 if (iter % 1000 == 100)
999 fputs(ns, stderr);
1000 fprintf(stderr, "%d ", iter);
1001 if (iter % 1000 == 0)
1002 fputc('\n', stderr);
1003 }
1004 if (iter >= maxiter)
1005 break;
1006 }
1007 switch (balance) {
1008 case 1:
1009 TB_balance(&ctx);
1010 reset_lists(&ctx);
1011 break;
1012 case 2:
1013 LR_balance(&ctx);
1014 break;
1015 default:
1016 (void)scan_and_normalize(&ctx);
1017 freeTreeList (&ctx, ctx.G);
1018 break;
1019 }
1020 if (Verbose) {
1021 if (iter >= 100)
1022 fputc('\n', stderr);
1023 fprintf(stderr, "%s%" PRISIZE_T " nodes %" PRISIZE_T " edges %d iter %.2f sec\n",
1024 ns, ctx.N_nodes, ctx.N_edges, iter, elapsed_sec());
1025 }
1026 return 0;
1027}
1028
1029int rank(graph_t * g, int balance, int maxiter)
1030{
1031 char *s;
1032 int search_size;
1033
1034 if ((s = agget(g, "searchsize")))
1035 search_size = atoi(s);
1036 else
1037 search_size = SEARCHSIZE;
1038
1039 return rank2 (g, balance, maxiter, search_size);
1040}
1041
1042/* set cut value of f, assuming values of edges on one side were already set */
1043static void x_cutval(edge_t * f)
1044{
1045 node_t *v;
1046 edge_t *e;
1047 int dir;
1048
1049 /* set v to the node on the side of the edge already searched */
1050 if (ND_par(agtail(f)) == f) {
1051 v = agtail(f);
1052 dir = 1;
1053 } else {
1054 v = aghead(f);
1055 dir = -1;
1056 }
1057
1058 int sum = 0;
1059 for (int i = 0; (e = ND_out(v).list[i]); i++)
1060 if (sadd_overflow(sum, x_val(e, v, dir), &sum)) {
1061 agerrorf("overflow when computing edge weight sum\n");
1062 graphviz_exit(EXIT_FAILURE);
1063 }
1064 for (int i = 0; (e = ND_in(v).list[i]); i++)
1065 if (sadd_overflow(sum, x_val(e, v, dir), &sum)) {
1066 agerrorf("overflow when computing edge weight sum\n");
1067 graphviz_exit(EXIT_FAILURE);
1068 }
1069 ED_cutvalue(f) = sum;
1070}
1071
1072static int x_val(edge_t * e, node_t * v, int dir)
1073{
1074 node_t *other;
1075 int d, rv, f;
1076
1077 if (agtail(e) == v)
1078 other = aghead(e);
1079 else
1080 other = agtail(e);
1081 if (!(SEQ(ND_low(v), ND_lim(other), ND_lim(v)))) {
1082 f = 1;
1083 rv = ED_weight(e);
1084 } else {
1085 f = 0;
1086 if (TREE_EDGE(e))
1087 rv = ED_cutvalue(e);
1088 else
1089 rv = 0;
1090 rv -= ED_weight(e);
1091 }
1092 if (dir > 0) {
1093 if (aghead(e) == v)
1094 d = 1;
1095 else
1096 d = -1;
1097 } else {
1098 if (agtail(e) == v)
1099 d = 1;
1100 else
1101 d = -1;
1102 }
1103 if (f)
1104 d = -d;
1105 if (d < 0)
1106 rv = -rv;
1107 return rv;
1108}
1109
1110static void dfs_cutval(node_t * v, edge_t * par)
1111{
1112 // per-node state
1113 typedef struct {
1114 node_t *v;
1115 edge_t *par;
1116 int out_i;
1117 int in_i;
1118 } state_t;
1119
1120 LIST(state_t) todo = {0};
1121 LIST_PUSH_BACK(&todo, ((state_t){.v = v, .par = par}));
1122
1123 while (!LIST_IS_EMPTY(&todo)) {
1124 state_t *const top = LIST_BACK(&todo);
1125
1126 bool updated = false;
1127 edge_t *e;
1128 for (; (e = ND_tree_out(top->v).list[top->out_i]); ++top->out_i) {
1129 if (e != top->par) {
1130 ++top->out_i;
1131 LIST_PUSH_BACK(&todo, ((state_t){.v = aghead(e), .par = e}));
1132 updated = true;
1133 break;
1134 }
1135 }
1136 if (updated) {
1137 continue;
1138 }
1139
1140 for (; (e = ND_tree_in(top->v).list[top->in_i]); ++top->in_i) {
1141 if (e != top->par) {
1142 ++top->in_i;
1143 LIST_PUSH_BACK(&todo, ((state_t){.v = agtail(e), .par = e}));
1144 updated = true;
1145 break;
1146 }
1147 }
1148 if (updated) {
1149 continue;
1150 }
1151
1152 if (top->par) {
1153 x_cutval(top->par);
1154 }
1155 LIST_DROP_BACK(&todo);
1156 }
1157
1158 LIST_FREE(&todo);
1159}
1160
1169
1170/*
1171* Initializes DFS range attributes (par, low, lim) over tree nodes such that:
1172* ND_par(n) - parent tree edge
1173* ND_low(n) - min DFS index for nodes in sub-tree (>= 1)
1174* ND_lim(n) - max DFS index for nodes in sub-tree
1175*/
1176static int dfs_range_init(node_t *v) {
1177 int lim = 0;
1178
1179 LIST(dfs_state_t) todo = {0};
1180
1181 ND_par(v) = NULL;
1182 ND_low(v) = 1;
1183 const dfs_state_t root = {.v = v, .par = NULL, .lim = 1};
1184 LIST_PUSH_BACK(&todo, root);
1185
1186 while (!LIST_IS_EMPTY(&todo)) {
1187 bool pushed_new = false;
1188 dfs_state_t *const s = LIST_BACK(&todo);
1189
1190 while (ND_tree_out(s->v).list[s->tree_out_i]) {
1191 edge_t *const e = ND_tree_out(s->v).list[s->tree_out_i];
1192 ++s->tree_out_i;
1193 if (e != s->par) {
1194 node_t *const n = aghead(e);
1195 ND_par(n) = e;
1196 ND_low(n) = s->lim;
1197 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1198 LIST_PUSH_BACK(&todo, next);
1199 pushed_new = true;
1200 break;
1201 }
1202 }
1203 if (pushed_new) {
1204 continue;
1205 }
1206
1207 while (ND_tree_in(s->v).list[s->tree_in_i]) {
1208 edge_t *const e = ND_tree_in(s->v).list[s->tree_in_i];
1209 ++s->tree_in_i;
1210 if (e != s->par) {
1211 node_t *const n = agtail(e);
1212 ND_par(n) = e;
1213 ND_low(n) = s->lim;
1214 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1215 LIST_PUSH_BACK(&todo, next);
1216 pushed_new = true;
1217 break;
1218 }
1219 }
1220 if (pushed_new) {
1221 continue;
1222 }
1223
1224 ND_lim(s->v) = s->lim;
1225
1226 lim = s->lim;
1227 LIST_DROP_BACK(&todo);
1228
1229 if (!LIST_IS_EMPTY(&todo)) {
1230 LIST_BACK(&todo)->lim = lim + 1;
1231 }
1232 }
1233
1234 LIST_FREE(&todo);
1235
1236 return lim + 1;
1237}
1238
1239/*
1240 * Incrementally updates DFS range attributes
1241 */
1242static int dfs_range(node_t * v, edge_t * par, int low)
1243{
1244 int lim = 0;
1245
1246 if (ND_par(v) == par && ND_low(v) == low) {
1247 return ND_lim(v) + 1;
1248 }
1249
1250 LIST(dfs_state_t) todo = {0};
1251
1252 ND_par(v) = par;
1253 ND_low(v) = low;
1254 const dfs_state_t root = {.v = v, .par = par, .lim = low};
1255 LIST_PUSH_BACK(&todo, root);
1256
1257 while (!LIST_IS_EMPTY(&todo)) {
1258 bool processed_child = false;
1259 dfs_state_t *const s = LIST_BACK(&todo);
1260
1261 while (ND_tree_out(s->v).list[s->tree_out_i]) {
1262 edge_t *const e = ND_tree_out(s->v).list[s->tree_out_i];
1263 ++s->tree_out_i;
1264 if (e != s->par) {
1265 node_t *const n = aghead(e);
1266 if (ND_par(n) == e && ND_low(n) == s->lim) {
1267 s->lim = ND_lim(n) + 1;
1268 } else {
1269 ND_par(n) = e;
1270 ND_low(n) = s->lim;
1271 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1272 LIST_PUSH_BACK(&todo, next);
1273 }
1274 processed_child = true;
1275 break;
1276 }
1277 }
1278 if (processed_child) {
1279 continue;
1280 }
1281
1282 while (ND_tree_in(s->v).list[s->tree_in_i]) {
1283 edge_t *const e = ND_tree_in(s->v).list[s->tree_in_i];
1284 ++s->tree_in_i;
1285 if (e != s->par) {
1286 node_t *const n = agtail(e);
1287 if (ND_par(n) == e && ND_low(n) == s->lim) {
1288 s->lim = ND_lim(n) + 1;
1289 } else {
1290 ND_par(n) = e;
1291 ND_low(n) = s->lim;
1292 const dfs_state_t next = {.v = n, .par = e, .lim = s->lim};
1293 LIST_PUSH_BACK(&todo, next);
1294 }
1295 processed_child = true;
1296 break;
1297 }
1298 }
1299 if (processed_child) {
1300 continue;
1301 }
1302
1303 ND_lim(s->v) = s->lim;
1304
1305 lim = s->lim;
1306 LIST_DROP_BACK(&todo);
1307
1308 if (!LIST_IS_EMPTY(&todo)) {
1309 LIST_BACK(&todo)->lim = lim + 1;
1310 }
1311 }
1312
1313 LIST_FREE(&todo);
1314
1315 return lim + 1;
1316}
1317
1318#ifdef DEBUG
1319void tchk(network_simplex_ctx_t *ctx)
1320{
1321 edge_t *e;
1322
1323 size_t n_cnt = 0;
1324 size_t e_cnt = 0;
1325 for (node_t *n = agfstnode(ctx->G); n; n = agnxtnode(ctx->G, n)) {
1326 n_cnt++;
1327 for (int i = 0; (e = ND_tree_out(n).list[i]); i++) {
1328 e_cnt++;
1329 if (SLACK(e) > 0)
1330 fprintf(stderr, "not a tight tree %p", e);
1331 }
1332 }
1333 if (e_cnt != LIST_SIZE(&ctx->Tree_edge))
1334 fprintf(stderr, "something missing\n");
1335}
1336
1337static void dump_node(FILE *sink, node_t *n) {
1338 if (ND_node_type(n)) {
1339 fprintf(sink, "%p", n);
1340 }
1341 else
1342 fputs(agnameof(n), sink);
1343}
1344
1345static void dump_graph (graph_t* g)
1346{
1347 edge_t *e;
1348 FILE *const fp = fopen ("ns.gv", "w");
1349 fprintf (fp, "digraph \"%s\" {\n", agnameof(g));
1350 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
1351 fputs(" \"", fp);
1352 dump_node(fp, n);
1353 fputs("\"\n", fp);
1354 }
1355 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
1356 for (int i = 0; (e = ND_out(n).list[i]); i++) {
1357 fputs(" \"", fp);
1358 dump_node(fp, n);
1359 fputs("\"", fp);
1360 node_t *const w = aghead(e);
1361 fputs(" -> \"", fp);
1362 dump_node(fp, w);
1363 fputs("\"\n", fp);
1364 }
1365 }
1366
1367 fprintf (fp, "}\n");
1368 fclose (fp);
1369}
1370
1371static node_t *checkdfs(graph_t* g, node_t * n)
1372{
1373 edge_t *e;
1374
1375 if (ND_mark(n))
1376 return NULL;
1377 ND_mark(n) = true;
1378 ND_onstack(n) = true;
1379 for (int i = 0; (e = ND_out(n).list[i]); i++) {
1380 node_t *const w = aghead(e);
1381 if (ND_onstack(w)) {
1382 dump_graph (g);
1383 fprintf(stderr, "cycle: last edge %p %s(%p) %s(%p)\n", e, agnameof(n), n,
1384 agnameof(w), w);
1385 return w;
1386 }
1387 else {
1388 if (!ND_mark(w)) {
1389 node_t *const x = checkdfs(g, w);
1390 if (x) {
1391 fprintf(stderr,"unwind %p %s(%p)\n", e, agnameof(n), n);
1392 if (x != n) return x;
1393 fprintf(stderr,"unwound to root\n");
1394 fflush(stderr);
1395 abort();
1396 return NULL;
1397 }
1398 }
1399 }
1400 }
1401 ND_onstack(n) = false;
1402 return NULL;
1403}
1404
1405void check_cycles(graph_t * g)
1406{
1407 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
1408 ND_mark(n) = false;
1409 ND_onstack(n) = false;
1410 }
1411 for (node_t *n = GD_nlist(g); n; n = ND_next(n))
1412 checkdfs(g, n);
1413}
1414#endif /* DEBUG */
static agxbuf last
last message
Definition agerror.c:31
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define MIN(a, b)
Definition arith.h:28
#define MAX(a, b)
Definition arith.h:33
#define Low(n)
Definition bcomps.c:56
#define right(i)
Definition closest.c:74
#define NORMAL
Definition const.h:24
static char * err
Definition delaunay.c:532
#define left
Definition dthdr.h:12
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
static tctype tchk[][2]
Definition gdefs.h:78
static bool Verbose
Definition gml2gv.c:26
void free(void *)
#define SIZE_MAX
Definition gmlscan.c:347
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:198
char * agget(void *obj, char *name)
Definition attr.c:447
#define ED_tree_index(e)
Definition types.h:600
#define ED_minlen(e)
Definition types.h:592
#define agtail(e)
Definition cgraph.h:977
#define ED_weight(e)
Definition types.h:603
#define ED_cutvalue(e)
Definition types.h:581
#define aghead(e)
Definition cgraph.h:978
void agerrorf(const char *fmt,...)
Definition agerror.c:167
int agerr(agerrlevel_t level, const char *fmt,...)
Definition agerror.c:157
@ AGPREV
Definition cgraph.h:946
#define GD_nlist(g)
Definition types.h:393
#define ND_tree_in(n)
Definition types.h:533
#define ND_rank(n)
Definition types.h:523
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:50
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:43
#define ND_lim(n)
Definition types.h:504
#define ND_next(n)
Definition types.h:510
#define ND_par(n)
Definition types.h:518
#define ND_node_type(n)
Definition types.h:511
#define ND_tree_out(n)
Definition types.h:534
#define ND_low(n)
Definition types.h:505
#define ND_in(n)
Definition types.h:501
#define ND_out(n)
Definition types.h:515
#define ND_priority(n)
Definition types.h:522
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:145
Arithmetic helper functions.
#define SWAP(a, b)
Definition gv_math.h:134
@ tree
Definition gvgen.c:35
table Syntax error
Definition htmlparse.y:288
#define ND_onstack(n)
Definition acyclic.c:31
#define ND_mark(n)
Definition acyclic.c:30
static Agedge_t * top(edge_stack_t *sp)
Definition tred.c:76
type-generic dynamically expanding list
#define LIST(type)
Definition list.h:55
#define LIST_BACK(list)
Definition list.h:191
#define LIST_POP_FRONT(list)
Definition list.h:394
#define LIST_SIZE(list)
Definition list.h:80
#define LIST_APPEND(list, item)
Definition list.h:120
#define LIST_DROP_BACK(list)
Definition list.h:422
#define LIST_FREE(list)
Definition list.h:370
#define LIST_RESERVE(list, capacity)
Definition list.h:257
#define LIST_POP_BACK(list)
Definition list.h:407
#define LIST_SORT(list, cmp)
Definition list.h:338
#define LIST_SET(list, index, item)
Definition list.h:202
#define LIST_IS_EMPTY(list)
Definition list.h:90
#define LIST_PUSH_BACK(list, item)
Definition list.h:384
#define LIST_GET(list, index)
Definition list.h:155
#define delta
Definition maze.c:136
NEATOPROCS_API void s1(graph_t *, node_t *)
Definition stuff.c:651
static void TB_balance(network_simplex_ctx_t *ctx)
Definition ns.c:814
static subtree_t * merge_trees(network_simplex_ctx_t *ctx, Agedge_t *e)
Definition ns.c:594
static int x_val(edge_t *e, node_t *v, int dir)
Definition ns.c:1072
static void freeTreeList(network_simplex_ctx_t *ctx, graph_t *g)
Definition ns.c:768
static void reset_lists(network_simplex_ctx_t *ctx)
Definition ns.c:763
static subtree_t * STextractmin(STheap_t *heap)
Definition ns.c:563
int rank(graph_t *g, int balance, int maxiter)
Definition ns.c:1029
static int dfs_range(node_t *v, edge_t *par, int low)
Definition ns.c:1242
static int update(network_simplex_ctx_t *ctx, edge_t *e, edge_t *f)
Definition ns.c:708
static void LR_balance(network_simplex_ctx_t *ctx)
Definition ns.c:778
static int increasingrankcmpf(const void *x, const void *y)
Definition ns.c:810
static int tight_subtree_search(network_simplex_ctx_t *ctx, Agnode_t *v, subtree_t *st)
Definition ns.c:331
static void dfs_cutval(node_t *v, edge_t *par)
Definition ns.c:1110
static bool on_heap(const subtree_t *tree)
is this subtree stored in an STheap?
Definition ns.c:318
struct STheap_s STheap_t
static edge_t * dfs_enter_outedge(node_t *v, int Low, int Lim)
Definition ns.c:215
static size_t STheapsize(const STheap_t *heap)
Definition ns.c:532
static Agnode_t * treeupdate(Agnode_t *v, Agnode_t *w, int cutvalue, bool dir)
Definition ns.c:675
static void init_cutvalues(network_simplex_ctx_t *ctx)
Definition ns.c:299
static Agedge_t * inter_tree_edge_search(Agnode_t *v)
Definition ns.c:454
static STheap_t * STbuildheap(subtree_t **elt, size_t size)
Definition ns.c:552
static edge_t * enter_edge(edge_t *e)
Definition ns.c:282
static int decreasingrankcmpf(const void *x, const void *y)
Definition ns.c:798
#define SEQ(a, b, c)
Definition ns.c:44
#define ND_subtree_set(n, value)
Definition ns.c:308
static void init_rank(network_simplex_ctx_t *ctx)
Definition ns.c:146
#define TREE_EDGE(e)
Definition ns.c:45
static void exchange_tree_edges(network_simplex_ctx_t *ctx, edge_t *e, edge_t *f)
Definition ns.c:114
static int feasible_tree(network_simplex_ctx_t *ctx)
Definition ns.c:623
#define ND_subtree(n)
Definition ns.c:307
static subtree_t * STsetFind(Agnode_t *n0)
Definition ns.c:424
static void rerank(Agnode_t *v, int delta)
Definition ns.c:691
static int dfs_range_init(node_t *v)
Definition ns.c:1176
struct subtree_s subtree_t
static void x_cutval(edge_t *f)
Definition ns.c:1043
static int scan_and_normalize(network_simplex_ctx_t *ctx)
Definition ns.c:748
static int add_tree_edge(network_simplex_ctx_t *ctx, edge_t *e)
Definition ns.c:57
static void invalidate_path(node_t *lca, node_t *to_node)
Definition ns.c:90
static edge_t * leave_edge(network_simplex_ctx_t *ctx)
Definition ns.c:179
static subtree_t * find_tight_subtree(network_simplex_ctx_t *ctx, Agnode_t *v)
Definition ns.c:406
static void tree_adjust(Agnode_t *v, Agnode_t *from, int delta)
Definition ns.c:577
static bool init_graph(network_simplex_ctx_t *ctx, graph_t *g)
Definition ns.c:890
#define SLACK(e)
Definition ns.c:43
static subtree_t * STsetUnion(subtree_t *s0, subtree_t *s1)
Definition ns.c:434
static void STheapify(STheap_t *heap, size_t i)
Definition ns.c:534
static void graphSize(graph_t *g, size_t *nn, size_t *ne)
Definition ns.c:926
static edge_t * dfs_enter_inedge(node_t *v, int Low, int Lim)
Definition ns.c:248
int rank2(graph_t *g, int balance, int maxiter, int search_size)
Definition ns.c:951
@ SEARCHSIZE
Definition ns.c:55
static Agedge_t * inter_tree_edge(subtree_t *tree)
Definition ns.c:527
arithmetic overflow helpers
static bool sadd_overflow(int a, int b, int *res)
Definition overflow.h:22
#define PRISIZE_T
Definition prisize_t.h:25
static int nedges
total no. of edges used in routing
Definition routespl.c:32
static bool streq(const char *a, const char *b)
are a and b equal?
Definition streq.h:11
graph or subgraph
Definition cgraph.h:424
Definition ns.c:419
size_t size
Definition ns.c:421
subtree_t ** elt
Definition ns.c:420
local state used by dfs_range*
Definition ns.c:1162
int lim
Definition ns.c:1165
node_t * v
Definition ns.c:1163
int tree_out_i
Definition ns.c:1166
int tree_in_i
Definition ns.c:1167
edge_t * par
Definition ns.c:1164
graph_t * G
Definition ns.c:48
information the ID allocator needs to do its job
Definition id.c:29
node_t * rep
Definition ns.c:311
struct subtree_s * par
Definition ns.c:314
size_t heap_index
required to find non-min elts when merged
Definition ns.c:313
int size
Definition ns.c:312
state for use in tight_subtree_search
Definition ns.c:323
Agnode_t * v
Definition ns.c:324
int out_i
iteration counter through ND_out(v).list
Definition ns.c:326
int in_i
iteration counter through ND_in(v).list
Definition ns.c:325
int rv
result value
Definition ns.c:327
double elapsed_sec(void)
Definition timing.c:23
void start_timer(void)
Definition timing.c:21
#define free_list(L)
Definition types.h:272
Definition grammar.c:90