55 if (col >= me->
ncols) {
59 const size_t index =
row * me->
ncols + col;
60 const size_t byte_index = index / 8;
61 const size_t bit_index = index % 8;
62 return (me->
data[byte_index] >> bit_index) & 1;
77 const size_t ncols =
zmax(me->
ncols, col + 1);
78 const size_t bits = nrows * ncols;
79 const size_t bytes = bits / 8 + (bits % 8 == 0 ? 0 : 1);
80 uint8_t *
const data =
gv_alloc(bytes);
83 for (
size_t r = 0; r < me->
nrows; ++r) {
84 for (
size_t c = 0; c < me->
ncols; ++c) {
88 const size_t index = r * ncols + c;
89 const size_t byte_index = index / 8;
90 const size_t bit_index = index % 8;
91 data[byte_index] |= (uint8_t)(UINT8_C(1) << bit_index);
100 assert(row < me->nrows);
101 assert(col < me->ncols);
103 const size_t index =
row * me->
ncols + col;
104 const size_t byte_index = index / 8;
105 const size_t bit_index = index % 8;
106 me->
data[byte_index] |= (uint8_t)(UINT8_C(1) << bit_index);
110#define MARK(v) (ND_mark(v))
111#define saveorder(v) (ND_coord(v)).x
112#define flatindex(v) ((size_t)ND_low(v))
117static int edgeidcmpf(
const void *,
const void *);
145static int ordercmpf(
const void *,
const void *);
146static int64_t
ncross(
void);
148void check_rs(
graph_t * g,
int null_ok);
149void check_order(
void);
151void node_in_root_vlist(
node_t * n);
165#if defined(DEBUG) && DEBUG > 1
169 fprintf (stderr,
" ");
175static void nname(
node_t *v, FILE *stream) {
180 fprintf(stream,
"v_%p", v);
190 fprintf (stderr,
"digraph A {\n");
192 fprintf (stderr,
" subgraph {rank=same ");
193 for (i = 0; i <
GD_rank(g)[r].n; i++) {
196 fputs(
" -> ", stderr);
199 if (i > 1) fprintf (stderr,
" [style=invis]}\n");
200 else fprintf (stderr,
" }\n");
203 for (i = 0; i <
GD_rank(g)[r].n; i++) {
205 for (j = 0; (e =
ND_out(v).list[j]); j++) {
207 fputs(
" -> ", stderr);
213 fprintf (stderr,
"}\n");
215static void dumpr (
graph_t* g,
int edges)
222 fprintf (stderr,
"[%d] ", r);
223 for (i = 0; i <
GD_rank(g)[r].n; i++) {
228 fprintf (stderr,
"\n");
230 if (edges == 0)
return;
232 for (i = 0; i <
GD_rank(g)[r].n; i++) {
234 for (j = 0; (e =
ND_out(v).list[j]); j++) {
236 fputs(
" -> ", stderr);
251#define ND_x(n) (((info_t*)AGDATA(n))->x)
252#define ND_lo(n) (((info_t*)AGDATA(n))->lo)
253#define ND_hi(n) (((info_t*)AGDATA(n))->hi)
254#define ND_np(n) (((info_t*)AGDATA(n))->np)
255#define ND_idx(n) (ND_order(ND_np(n)))
269#define isBackedge(e) (ND_idx(aghead(e)) > ND_idx(agtail(e)))
277 if (
agdegree(g,n,1,0) == 0)
return n;
292 for (e =
agfstout(g, n); e; e = nxte) {
327 bool haveBackedge =
false;
345 if (!haveBackedge)
return;
347 sg =
agsubg(g,
"comp", 1);
353 if (
getComp(g, n, sg, indices)) {
358 for (i = 0; i < sz; i++) {
360 rk->
v[indices[i]] = arr[i];
389 for (j = 0; j < rk->
n; j++) {
441 for (nc = 0, comp = 0; comp <
GD_comp(g).size; comp++) {
484 const size_t bits = initial_rows * initial_columns;
485 const size_t bytes = bits / 8 + (bits % 8 == 0 ? 0 : 1);
486 uint8_t *
const data =
gv_alloc(bytes);
487 *rv = (
adjmatrix_t){.
nrows = initial_rows, .ncols = initial_columns, .data = data};
527 for (i = ne = 0; (e =
ND_out(n).list[i]); i++)
531 for (i = ne = 0; (e =
ND_in(n).list[i]); i++)
540 qsort(sortlist, ne,
sizeof(sortlist[0]),
edgeidcmpf);
541 for (ne = 1; (f = sortlist[ne]); ne++) {
542 e = sortlist[ne - 1];
571 const char *ordering;
575 if (
streq(ordering,
"out"))
577 else if (
streq(ordering,
"in"))
579 else if (ordering[0])
580 agerrorf(
"ordering '%s' not recognized for node '%s'.\n", ordering,
agnameof(n));
598 if (
streq(ordering,
"out"))
600 else if (
streq(ordering,
"in"))
602 else if (ordering[0])
603 agerrorf(
"ordering '%s' not recognized.\n", ordering);
673 for (e2 =
ND_in(w).list; *e2; e2++) {
678 for (e1 =
ND_in(v).list; *e1; e1++) {
690 int inv,
cross = 0, t;
692 for (e2 =
ND_out(w).list; *e2; e2++) {
696 for (e1 =
ND_out(v).list; *e1; e1++) {
724 GD_rank(g)[r].candidate =
false;
725 for (i = 0; i <
GD_rank(g)[r].n - 1; i++) {
741 if (c1 < c0 || (c0 > 0 && reverse && c1 == c0)) {
745 GD_rank(g)[r].candidate =
true;
749 GD_rank(g)[r - 1].candidate =
true;
753 GD_rank(g)[r + 1].candidate =
true;
765 GD_rank(g)[r].candidate =
true;
774 }
while (
delta >= 1);
778 const int endpass = 2;
779 int maxthispass = 0, iter, trying, pass;
780 int64_t cur_cross, best_cross;
783 cur_cross = best_cross =
ncross();
786 cur_cross = best_cross = INT64_MAX;
787 for (pass = startpass; pass <= endpass; pass++) {
798 if ((cur_cross =
ncross()) <= best_cross) {
800 best_cross = cur_cross;
804 if (cur_cross > best_cross)
806 cur_cross = best_cross;
809 for (iter = 0; iter < maxthispass; iter++) {
812 "mincross: pass %d iter %d trying %d cur_cross %" PRId64
" best_cross %"
814 pass, iter, trying, cur_cross, best_cross);
820 if ((cur_cross =
ncross()) <= best_cross) {
824 best_cross = cur_cross;
830 if (cur_cross > best_cross)
832 if (best_cross > 0) {
846 for (i = 0; i <
GD_rank(g)[r].n; i++) {
863 for (i = 0; i <
GD_rank(g)[r].n; i++) {
878 for (
size_t c = 0; c <
GD_comp(g).size; c++) {
907 for (i = 0; i <
GD_rank(g)[r].n; i++) {
912 "merge2: graph %s, rank %d has only %d < %d nodes\n",
941 for (i = 0; i <
GD_rank(g)[r].n; i++) {
957 fprintf(stderr,
"mincross %s: %" PRId64
" crossings, %.2f secs.\n",
1039 node_in_root_vlist(v);
1199 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1211 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1238 for (r = low + 1; r < high; r++)
1257 agerrorf(
"install_in_rank, line %d: %s %s rank %d i = %d an = 0\n",
1277 agerrorf(
"install_in_rank, line %d: ND_order(%s) [%d] > GD_rank(Root)[%d].an [%d]\n",
1282 agerrorf(
"install_in_rank, line %d: rank %d not in rank range [%d,%d]\n",
1288 agerrorf(
"install_in_rank, line %d: GD_rank(g)[%d].v + ND_order(%s) [%d] > GD_rank(g)[%d].av + GD_rank(Root)[%d].an [%d]\n",
1303 node_queue_t q = {0};
1311 for (i = 0; (e =
ND_out(n).list[i]); i++)
1313 for (i = 0; (e =
ND_in(n).list[i]); i++)
1322 const bool walkbackwards = g !=
agroot(g);
1325 if (walkbackwards) {
1333 otheredges = pass == 0 ?
ND_in(n).list :
ND_out(n).list;
1334 if (otheredges[0] !=
NULL)
1362 int num_nodes_1 =
GD_rank(g)[i].n - 1;
1363 int half_num_nodes_1 = num_nodes_1 / 2;
1364 for (j = 0; j <= half_num_nodes_1; j++)
1365 exchange(vlist[j], vlist[num_nodes_1 - j]);
1379 for (
size_t i = 0; i <
ND_out(n0).size; i++) {
1387 for (
size_t i = 0; i <
ND_in(n0).size; i++) {
1388 e =
ND_in(n0).list[i];
1412static void postorder(
graph_t *g,
node_t *v, nodes_t *list,
int r) {
1421 postorder(g,
aghead(e), list, r);
1430 int i, r, local_in_cnt, local_out_cnt, base_order;
1432 nodes_t temprank = {0};
1438 if (
GD_rank(g)[r].n == 0)
continue;
1440 for (i = 0; i <
GD_rank(g)[r].n; i++)
1445 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1449 local_in_cnt = local_out_cnt = 0;
1450 for (
size_t j = 0; j <
ND_flat_in(v).size; j++) {
1454 for (
size_t j = 0; j <
ND_flat_out(v).size; j++) {
1458 if (local_in_cnt == 0 && local_out_cnt == 0)
1461 if (!
MARK(v) && local_in_cnt == 0) {
1462 postorder(g, v, &temprank, r);
1471 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1477 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1480 for (
size_t j = 0; (e =
ND_flat_out(v).list[j]); j++) {
1501 int changed = 0, nelt;
1505 for (nelt =
GD_rank(g)[r].n - 1; nelt >= 0; nelt--) {
1509 while (lp < ep &&
ND_mval(*lp) < 0)
1514 bool sawclust =
false;
1515 bool muststay =
false;
1516 for (rp = lp + 1; rp < ep; rp++) {
1531 const double p1 =
ND_mval(*lp);
1532 const double p2 =
ND_mval(*rp);
1533 if (p1 > p2 || (p1 >= p2 && reverse)) {
1540 if (!hasfixed && !reverse)
1553 int r, other, first,
last, dir;
1555 bool reverse = pass % 4 < 2;
1557 if (pass % 2 == 0) {
1571 for (r = first; r !=
last + dir; r += dir) {
1573 bool hasfixed =
medians(g, r, other);
1574 reorder(g, r, reverse, hasfixed);
1584 bool is_out = dir > 0;
1585 for (i = 0; (e = l.
list[i]); i++) {
1587 for (j = i + 1; (f = l.
list[j]); j++) {
1592 for (j = i + 1; (f = l.
list[j]); j++) {
1602 int top, bot, max, i, k;
1614 for (i = 0; (e =
ND_out(rtop[
top]).list[i]); i++) {
1619 for (i = 0; (e =
ND_out(rtop[
top]).list[i]); i++) {
1631 for (bot = 0; bot <
GD_rank(g)[r + 1].n; bot++) {
1647 count +=
GD_rank(g)[r].cache_nc;
1687 for (i = 1; (e = fl[i]); i++)
1697 for (i = 1; (e = fl[i]); i++)
1708#define VAL(node,port) (MC_SCALE * ND_order(node) + (port).order)
1712 int i, j0, lspan, rspan, *list;
1715 bool hasfixed =
false;
1719 for (i = 0; i <
GD_rank(g)[r0].n; i++) {
1723 for (j0 = 0; (e =
ND_out(n).list[j0]); j0++) {
1727 for (j0 = 0; (e =
ND_in(n).list[j0]); j0++) {
1739 ND_mval(n) = (list[0] + list[1]) / 2;
1749 rspan = list[j - 1] - list[
rm];
1750 lspan = list[lm] - list[0];
1754 double w = list[lm] * (double)rspan + list[
rm] * (
double)lspan;
1755 ND_mval(n) = w / (lspan + rspan);
1760 for (i = 0; i <
GD_rank(g)[r0].n; i++) {
1773#pragma GCC diagnostic push
1774#pragma GCC diagnostic ignored "-Wcast-qual"
1779#pragma GCC diagnostic pop
1795#pragma GCC diagnostic push
1796#pragma GCC diagnostic ignored "-Wcast-qual"
1801#pragma GCC diagnostic pop
1815#define VIRTUALNODE 2
1846 agerrorf(
"overflow when calculating virtual weight of edge\n");
1854void check_rs(
graph_t * g,
int null_ok)
1859 fprintf(stderr,
"\n\n%s:\n",
agnameof(g));
1861 fprintf(stderr,
"%d: ", r);
1863 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1866 fprintf(stderr,
"NULL\t");
1876 fprintf(stderr,
"\n");
1880void check_order(
void)
1888 for (i = 0; (v =
GD_rank(g)[r].v[i]); i++) {
1905 p =
agget(g,
"mclimit");
1906 if (p && (f = atof(p)) > 0.0) {
1938 for (i = 0; i <
GD_rank(g)[r].n; i++) {
1953void node_in_root_vlist(
node_t * n)
static agxbuf last
last message
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
static void * gv_alloc(size_t size)
API for compacted arrays of booleans.
static bitarray_t bitarray_new(size_t size_bits)
create an array of the given element length
static void bitarray_clear(bitarray_t *self)
clear all bits in a bit array
static bool bitarray_get(bitarray_t self, size_t index)
get the value of the given element
static void bitarray_set(bitarray_t *self, size_t index, bool value)
set or clear the value of the given element
static void bitarray_reset(bitarray_t *self)
free underlying resources and leave a bit array empty
abstract graph C library, Cgraph API
bool mapbool(const char *p)
char * late_string(void *obj, attrsym_t *attr, char *defaultValue)
void decompose(graph_t *g, int pass)
Agraph_t * dot_root(void *p)
bool is_cluster(Agraph_t *)
void flat_edge(Agraph_t *, Agedge_t *)
void merge_oneway(Agedge_t *, Agedge_t *)
Agedge_t * new_virtual_edge(Agnode_t *, Agnode_t *, Agedge_t *)
Agedge_t * find_flat_edge(Agnode_t *, Agnode_t *)
void delete_flat_edge(Agedge_t *)
static NORETURN void graphviz_exit(int status)
static int cnt(Dict_t *d, Dtlink_t **set)
int agnedges(Agraph_t *g)
int agdegree(Agraph_t *g, Agnode_t *n, int in, int out)
int agnnodes(Agraph_t *g)
char * agget(void *obj, char *name)
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
int agdeledge(Agraph_t *g, Agedge_t *arg_e)
Agedge_t * agnxtin(Agraph_t *g, Agedge_t *e)
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Agedge_t * agfstin(Agraph_t *g, Agnode_t *n)
void agwarningf(const char *fmt,...)
void agerrorf(const char *fmt,...)
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
#define GD_has_flat_edges(g)
Agdesc_t Agstrictdirected
strict directed. A strict graph cannot have multi-edges or self-arcs.
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Agnode_t * agfstnode(Agraph_t *g)
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
int agdelnode(Agraph_t *g, Agnode_t *arg_n)
removes a node from a graph or subgraph.
#define ND_weight_class(n)
char * agnameof(void *)
returns a string descriptor for the object.
int agcontains(Agraph_t *, void *obj)
returns non-zero if obj is a member of (sub)graph
Agraph_t * agroot(void *obj)
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Agraph_t * agfstsubg(Agraph_t *g)
Agraph_t * agnxtsubg(Agraph_t *subg)
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
static void indent(int ix)
Arithmetic helper functions.
static int scale_clamp(int original, double scale)
scale up or down a non-negative integer, clamping to [0, INT_MAX]
static size_t zmax(size_t a, size_t b)
maximum of two sizes
static double cross(double *u, double *v)
static Agedge_t * top(edge_stack_t *sp)
int install_cluster(graph_t *g, node_t *n, int pass, node_queue_t *q)
int expand_cluster(graph_t *subg)
void mark_lowclusters(Agraph_t *root)
#define exchange(h, i, j, index)
type-generic dynamically expanding list
#define LIST_POP_FRONT(list)
#define LIST_APPEND(list, item)
#define LIST_IS_EMPTY(list)
#define LIST_PUSH_BACK(list, item)
#define LIST_REVERSE(list)
#define LIST_GET(list, index)
#define neighbor(t, i, edim, elist)
static int betweenclust(edge_t *e)
static void free_matrix(adjmatrix_t *p)
static bool flat_mval(node_t *n)
static bool inside_cluster(graph_t *g, node_t *v)
static int64_t mincross(graph_t *g, int startpass)
static void init_mccomp(graph_t *g, size_t c)
static void mincross_step(graph_t *g, int pass)
static int topsort(Agraph_t *g, Agraph_t *sg, Agnode_t **arr)
static bool medians(graph_t *g, int r0, int r1)
static void reorder(graph_t *g, int r, bool reverse, bool hasfixed)
static int edgeidcmpf(const void *, const void *)
static void flat_breakcycles(graph_t *g)
static void cleanup2(graph_t *g, int64_t nc)
static bool is_a_normal_node_of(graph_t *g, node_t *v)
static void save_best(graph_t *g)
static void init_mincross(graph_t *g)
static int64_t rcross(graph_t *g, int r)
static Agraph_t * realFillRanks(Agraph_t *g, bitarray_t *ranks, Agraph_t *sg)
static int64_t transpose_step(graph_t *g, int r, bool reverse)
static void fixLabelOrder(graph_t *g, rank_t *rk)
for each pair of nodes (labels), we add an edge
void virtual_weight(edge_t *e)
static void merge2(graph_t *g)
static int64_t mincross_clust(graph_t *g)
static node_t * furthestnode(graph_t *g, node_t *v, int dir)
static int out_cross(node_t *v, node_t *w)
static int ordercmpf(const void *, const void *)
void enqueue_neighbors(node_queue_t *q, node_t *n0, int pass)
static void do_ordering(graph_t *g, bool outflag)
static void matrix_set(adjmatrix_t *me, size_t row, size_t col)
void checkLabelOrder(graph_t *g)
static const double Convergence
int build_ranks(graph_t *g, int pass)
static Agnode_t * findSource(Agraph_t *g, Agraph_t *sg)
void rec_save_vlists(graph_t *g)
static void do_ordering_node(graph_t *g, node_t *n, bool outflag)
static int64_t in_cross(node_t *v, node_t *w)
static adjmatrix_t * new_matrix(size_t initial_rows, size_t initial_columns)
static void flat_search(graph_t *g, node_t *v)
static void ordered_edges(graph_t *g)
static void transpose(graph_t *g, bool reverse)
void rec_reset_vlists(graph_t *g)
static void merge_components(graph_t *g)
int dot_mincross(graph_t *g)
static int64_t ncross(void)
static void mincross_options(graph_t *g)
static int endpoint_class(node_t *n)
static int getComp(graph_t *g, node_t *n, graph_t *comp, int *indices)
static bool left2right(graph_t *g, node_t *v, node_t *w)
static int local_cross(elist l, int dir)
static void flat_reorder(graph_t *g)
static void flat_rev(Agraph_t *g, Agedge_t *e)
static void emptyComp(graph_t *sg)
static bool is_a_vnode_of_an_edge_of(graph_t *g, node_t *v)
static void fillRanks(Agraph_t *g)
static void do_ordering_for_nodes(graph_t *g)
void allocate_ranks(graph_t *g)
static int table[NTYPES][NTYPES]
static void restore_best(graph_t *g)
static bool matrix_get(adjmatrix_t *me, size_t row, size_t col)
static bool constraining_flat_edge(Agraph_t *g, Agedge_t *e)
static int nodeposcmpf(const void *, const void *)
void save_vlist(graph_t *g)
int install_in_rank(graph_t *g, node_t *n)
static bool streq(const char *a, const char *b)
are a and b equal?
Agrec_t * data
stores programmer-defined data, access with AGDATA
implementation of Agrec_t
size_t nrows
how many rows have been allocated?
uint8_t * data
bit-packed backing memory
size_t ncols
how many columns have been allocated?
#define elist_append(item, L)
#define alloc_elist(n, L)