36static void check_cycles(
graph_t * g);
39#define LENGTH(e) (ND_rank(aghead(e)) - ND_rank(agtail(e)))
40#define SLACK(e) (LENGTH(e) - ED_minlen(e))
41#define SEQ(a,b,c) ((a) <= (b) && (b) <= (c))
42#define TREE_EDGE(e) (ED_tree_index(e) >= 0)
56 agerrorf(
"add_tree_edge: missing tree edge\n");
71 agerrorf(
"add_tree_edge: empty outedge list\n");
79 agerrorf(
"add_tree_edge: empty inedge list\n");
103 agerrorf(
"invalidate_path: skipped over LCA\n");
125 for (j = 0; j <= i; j++)
132 for (j = 0; j <= i; j++)
155 node_queue_t Q = {0};
156 node_queue_reserve(&Q,
N_nodes);
161 node_queue_push_back(&Q, v);
164 while (!node_queue_is_empty(&Q)) {
165 v = node_queue_pop_front(&Q);
168 for (i = 0; (e =
ND_in(v).list[i]); i++)
170 for (i = 0; (e =
ND_out(v).list[i]); i++) {
172 node_queue_push_back(&Q,
aghead(e));
228 for (i = 0; (e =
ND_out(v).list[i]); i++) {
250 for (i = 0; (e =
ND_in(v).list[i]); i++) {
299#define ND_subtree(n) (subtree_t*)ND_par(n)
300#define ND_subtree_set(n,value) (ND_par(n) = (edge_t*)value)
323 for (i = 0; (e =
ND_in(v).list[i]); i++) {
332 for (i = 0; (e =
ND_out(v).list[i]); i++) {
366 while (s0->
par && s0->
par != s0) {
377 for (r0 = s0; r0->
par && r0->
par != r0; r0 = r0->
par);
378 for (r1 =
s1; r1->
par && r1->
par != r1; r1 = r1->
par);
379 if (r0 == r1)
return r0;
383 else if (r1->
size < r0->
size) r = r0;
398 if (best &&
SLACK(best) == 0)
return best;
399 for (i = 0; (e =
ND_out(v).list[i]); i++) {
401 if (
aghead(e) == from)
continue;
406 if (best == 0 ||
SLACK(e) <
SLACK(best)) best = e;
412 for (i = 0; (e =
ND_in(v).list[i]); i++) {
414 if (
agtail(e) == from)
continue;
419 if (best == 0 ||
SLACK(e) <
SLACK(best)) best = e;
438 const size_t left = 2 * (i + 1) - 1;
439 const size_t right = 2 * (i + 1);
441 if (left < heap->size && elt[
left]->size < elt[smallest]->size) smallest =
left;
442 if (right < heap->size && elt[
right]->size < elt[smallest]->size) smallest =
right;
446 elt[i] = elt[smallest];
447 elt[smallest] = temp;
453 }
while (i < heap->size);
460 for (
size_t i = 0; i <
heap->size; i++)
heap->elt[i]->heap_index = i;
474 heap->elt[0]->heap_index = 0;
488 for (i = 0; (e =
ND_tree_in(v).list[i]); i++) {
538 size_t subtree_count = 0;
578 for (
size_t i = 0; i < subtree_count; i++)
free(
tree[i]);
655 agerrorf(
"update: mismatched lca in treeupdates\n");
660 int lca_low =
ND_low(lca);
674 int Minrank = INT_MAX;
675 int Maxrank = INT_MIN;
737#pragma GCC diagnostic push
738#pragma GCC diagnostic ignored "-Wcast-qual"
743#pragma GCC diagnostic pop
759#pragma GCC diagnostic push
760#pragma GCC diagnostic ignored "-Wcast-qual"
765#pragma GCC diagnostic pop
780 int low, high, choice;
781 int inweight, outweight;
788 assert(Maxrank >= 0);
789 int *nrank =
gv_calloc((
size_t)Maxrank + 1,
sizeof(
int));
790 if ( (
s =
agget(
G,
"TBbalance")) ) {
791 if (
streq(
s,
"min")) adj = 1;
792 else if (
streq(
s,
"max")) adj = 2;
795 if (
ND_in(n).size == 0 && adj == 1) {
798 if (
ND_out(n).size == 0 && adj == 2) {
819 inweight = outweight = 0;
822 for (
size_t i = 0; (e =
ND_in(n).list[i]); i++) {
826 for (
size_t i = 0; (e =
ND_out(n).list[i]); i++) {
833 if (inweight == outweight)
834 ND_rank(n) = (adj == 1? low : high);
837 if (inweight == outweight) {
839 for (
int i = low + 1; i <= high; i++)
840 if (nrank[i] < nrank[choice])
863 for (
size_t i = 0; (e =
ND_out(n).list[i]); i++)
870 bool feasible =
true;
874 for (i = 0; (e =
ND_in(n).list[i]); i++) {
883 for (i = 0; (e =
ND_out(n).list[i]); i++);
903 for (i = 0; (e =
ND_out(n).list[i]); i++) {
926 char *ns =
"network simplex: ";
935 fprintf(stderr,
"%s %d nodes %d edges maxiter=%d balance=%d\n", ns,
936 nn, ne, maxiter, balance);
943 if (search_size >= 0)
969 if (
Verbose && iter % 100 == 0) {
970 if (iter % 1000 == 100)
972 fprintf(stderr,
"%d ", iter);
973 if (iter % 1000 == 0)
1006 if ((
s =
agget(g,
"searchsize")))
1007 search_size = atoi(
s);
1011 return rank2 (g, balance, maxiter, search_size);
1031 for (i = 0; (e =
ND_out(v).list[i]); i++)
1033 agerrorf(
"overflow when computing edge weight sum\n");
1036 for (i = 0; (e =
ND_in(v).list[i]); i++)
1038 agerrorf(
"overflow when computing edge weight sum\n");
1090 for (i = 0; (e =
ND_tree_in(v).list[i]); i++)
1147 for (i = 0; (e =
ND_tree_in(v).list[i]); i++)
1168 fprintf(stderr,
"not a tight tree %p", e);
1172 fprintf(stderr,
"something missing\n");
1175void check_fast_node(
node_t * n)
1179 while (nptr && nptr != n)
1181 assert(nptr !=
NULL);
1184static void dump_node(FILE *sink,
node_t *n) {
1186 fprintf(sink,
"%p", n);
1192static void dump_graph (
graph_t* g)
1197 FILE* fp = fopen (
"ns.gv",
"w");
1198 fprintf (fp,
"digraph \"%s\" {\n",
agnameof(g));
1205 for (i = 0; (e =
ND_out(n).list[i]); i++) {
1210 fputs(
" -> \"", fp);
1216 fprintf (fp,
"}\n");
1230 for (i = 0; (e =
ND_out(n).list[i]); i++) {
1234 fprintf(stderr,
"cycle: last edge %lx %s(%lx) %s(%lx)\n",
1244 fprintf(stderr,
"unwind %lx %s(%lx)\n",
1247 if (x != n)
return x;
1248 fprintf(stderr,
"unwound to root\n");
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
static void * gv_alloc(size_t size)
static NORETURN void graphviz_exit(int status)
static int cnt(Dict_t *d, Dtlink_t **set)
char * agget(void *obj, char *name)
void agerrorf(const char *fmt,...)
int agerr(agerrlevel_t level, const char *fmt,...)
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Agnode_t * agfstnode(Agraph_t *g)
Agraph_t * agraphof(void *obj)
char * agnameof(void *)
returns a string descriptor for the object.
#define DEFINE_LIST(name, type)
NEATOPROCS_API void s1(graph_t *, node_t *)
static void freeTreeList(graph_t *g)
static int update(edge_t *e, edge_t *f)
static int x_val(edge_t *e, node_t *v, int dir)
static void init_cutvalues(void)
static subtree_t * STextractmin(STheap_t *heap)
static int feasible_tree(void)
int rank(graph_t *g, int balance, int maxiter)
static int dfs_range_init(node_t *v, edge_t *par, int low)
static subtree_t * merge_trees(Agedge_t *e)
static int dfs_range(node_t *v, edge_t *par, int low)
static void reset_lists(void)
static void TB_balance(void)
static void dfs_enter_inedge(node_t *v)
static int increasingrankcmpf(const void *x, const void *y)
static subtree_t * find_tight_subtree(Agnode_t *v)
static void dfs_cutval(node_t *v, edge_t *par)
static bool on_heap(const subtree_t *tree)
is this subtree stored in an STheap?
static bool init_graph(graph_t *g)
static size_t STheapsize(const STheap_t *heap)
static STheap_t * STbuildheap(subtree_t **elt, size_t size)
static edge_t * enter_edge(edge_t *e)
static int decreasingrankcmpf(const void *x, const void *y)
static void init_rank(void)
static int scan_and_normalize(void)
#define ND_subtree_set(n, value)
static Agnode_t * treeupdate(Agnode_t *v, Agnode_t *w, int cutvalue, int dir)
static void graphSize(graph_t *g, int *nn, int *ne)
static subtree_t * STsetFind(Agnode_t *n0)
static void rerank(Agnode_t *v, int delta)
static void LR_balance(void)
struct subtree_s subtree_t
static void x_cutval(edge_t *f)
static int add_tree_edge(edge_t *e)
static Agedge_t * inter_tree_edge_search(Agnode_t *v, Agnode_t *from, Agedge_t *best)
static void exchange_tree_edges(edge_t *e, edge_t *f)
static edge_t * leave_edge(void)
static void invalidate_path(node_t *lca, node_t *to_node)
static void tree_adjust(Agnode_t *v, Agnode_t *from, int delta)
static subtree_t * STsetUnion(subtree_t *s0, subtree_t *s1)
static int tight_subtree_search(Agnode_t *v, subtree_t *st)
static void STheapify(STheap_t *heap, size_t i)
int rank2(graph_t *g, int balance, int maxiter, int search_size)
static void dfs_enter_outedge(node_t *v)
static Agedge_t * inter_tree_edge(subtree_t *tree)
arithmetic overflow helpers
static bool sadd_overflow(int a, int b, int *res)
#define PRISIZE_T
PRIu64 alike for printing size_t
static int nedges
total no. of edges used in routing
static bool streq(const char *a, const char *b)
are a and b equal?
size_t heap_index
required to find non-min elts when merged