Graphviz 13.1.3~dev.20250813.1130
Loading...
Searching...
No Matches
blocktree.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <circogen/blocktree.h>
12#include <stdbool.h>
13#include <util/agxbuf.h>
14#include <util/debug.h>
15#include <util/gv_math.h>
16
17static void addNode(block_t * bp, Agnode_t * n)
18{
19 agsubnode(bp->sub_graph, n,1);
20 BLOCK(n) = bp;
21}
22
24{
25 agxbuf name = {0};
26
27 agxbprint(&name, "_block_%d", state->blockCount++);
28 Agraph_t *subg = agsubg(g, agxbuse(&name), 1);
29 agxbfree(&name);
30 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true); //node custom data
31 return subg;
32}
33
34static block_t *makeBlock(Agraph_t * g, circ_state * state)
35{
36 Agraph_t *subg = makeBlockGraph(g, state);
37 block_t *bp = mkBlock(subg);
38
39 return bp;
40}
41
42DEFINE_LIST(estack, Agedge_t*)
43
44/* Current scheme adds articulation point to first non-trivial child
45 * block. If none exists, it will be added to its parent's block, if
46 * non-trivial, or else given its own block.
47 *
48 * FIX:
49 * This should be modified to:
50 * - allow user to specify which block gets a node, perhaps on per-node basis.
51 * - if an articulation point is not used in one of its non-trivial blocks,
52 * dummy edges should be added to preserve biconnectivity
53 * - turn on user-supplied blocks.
54 * - Post-process to move articulation point to largest block
55 */
56static void dfs(Agraph_t *g, Agnode_t *u, circ_state *state, bool isRoot,
57 estack_t *stk) {
58 LOWVAL(u) = VAL(u) = state->orderCount++;
59 for (Agedge_t *e = agfstedge(g, u); e; e = agnxtedge(g, e, u)) {
60 Agnode_t *v = aghead (e);
61 if (v == u) {
62 v = agtail(e);
63 if (!EDGEORDER(e)) EDGEORDER(e) = -1;
64 }
65 else {
66 if (!EDGEORDER(e)) EDGEORDER(e) = 1;
67 }
68
69 if (VAL(v) == 0) { /* Since VAL(root) == 0, it gets treated as artificial cut point */
70 PARENT(v) = u;
71 estack_push_back(stk, e);
72 dfs(g, v, state, false, stk);
73 LOWVAL(u) = imin(LOWVAL(u), LOWVAL(v));
74 if (LOWVAL(v) >= VAL(u)) { /* u is an articulation point */
76 Agnode_t *np;
77 Agedge_t *ep;
78 do {
79 ep = estack_pop_back(stk);
80 if (EDGEORDER(ep) == 1)
81 np = aghead (ep);
82 else
83 np = agtail (ep);
84 if (!BLOCK(np)) {
85 if (!block)
86 block = makeBlock(g, state);
87 addNode(block, np);
88 }
89 } while (ep != e);
90 if (block) { /* If block != NULL, it's not empty */
91 if (!BLOCK(u) && blockSize (block) > 1)
92 addNode(block, u);
93 if (isRoot && BLOCK(u) == block)
94 insertBlock(&state->bl, block);
95 else
96 appendBlock(&state->bl, block);
97 }
98 }
99 } else if (PARENT(u) != v) {
100 LOWVAL(u) = imin(LOWVAL(u), VAL(v));
101 }
102 }
103 if (isRoot && !BLOCK(u)) {
104 block_t *block = makeBlock(g, state);
105 addNode(block, u);
106 insertBlock(&state->bl, block);
107 }
108}
109
110static void find_blocks(Agraph_t * g, circ_state * state)
111{
112 Agnode_t *root = NULL;
113
114 /* check to see if there is a node which is set to be the root
115 */
116 if (state->rootname) {
117 root = agfindnode(g, state->rootname);
118 }
119 if (!root && state->N_root) {
120 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
121 if (late_bool(ORIGN(n), state->N_root, false)) {
122 root = n;
123 break;
124 }
125 }
126 }
127
128 if (!root)
129 root = agfstnode(g);
130 GV_DEBUG("root = %s", agnameof(root));
131 estack_t stk = {0};
132 dfs(g, root, state, true, &stk);
133 estack_free(&stk);
134}
135
136/* Construct block tree by peeling nodes from block list in state.
137 * When done, return root. The block list is empty
138 * FIX: use largest block as root
139 */
141{
142 block_t *next;
143
144 find_blocks(g, state);
145
146 block_t *bp = state->bl.first; // if root chosen, will be first
147 /* Otherwise, just pick first as root */
148 block_t *root = bp;
149
150 /* Find node with minimum VAL value to find parent block */
151 /* FIX: Should be some way to avoid search below. */
152 for (bp = bp->next; bp; bp = next) {
153 Agnode_t *n;
155 Agnode_t *child;
156 Agraph_t *subg = bp->sub_graph;
157
158 child = n = agfstnode(subg);
159
160 int min = VAL(n);
161 parent = PARENT(n);
162 for (n = agnxtnode(subg, n); n; n = agnxtnode(subg, n)) {
163 if (VAL(n) < min) {
164 child = n;
165 min = VAL(n);
166 parent = PARENT(n);
167 }
168 }
170 CHILD(bp) = child;
171 next = bp->next; /* save next since list insertion destroys it */
172 appendBlock(&BLOCK(parent)->children, bp);
173 }
174 initBlocklist(&state->bl); /* zero out list */
175 return root;
176}
177
179{
180 for (block_t *child = bp->children.first, *next; child; child = next) {
181 next = child->next;
182 freeBlocktree(child);
183 }
184
185 freeBlock(bp);
186}
187
188#ifdef DEBUG
189static void indent(int i)
190{
191 while (i--)
192 fputs(" ", stderr);
193}
194
195void print_blocktree(block_t * sn, int depth)
196{
197 indent(depth);
198 Agraph_t *g = sn->sub_graph;
199 fprintf(stderr, "%s:", agnameof(g));
200 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
201 fprintf(stderr, " %s", agnameof(n));
202 }
203 fputs("\n", stderr);
204
205 depth++;
206 for (block_t *child = sn->children.first; child; child = child->next) {
207 print_blocktree(child, depth);
208 }
209}
210
211#endif
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:77
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:233
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:306
static Agraph_t * mkBlock(Agraph_t *g, bcstate *stp)
Definition bcomps.c:140
void insertBlock(blocklist_t *bl, block_t *bp)
add block at beginning
Definition block.c:58
int blockSize(block_t *sp)
Definition block.c:39
void freeBlock(block_t *sp)
Definition block.c:31
void initBlocklist(blocklist_t *bl)
Definition block.c:17
void appendBlock(blocklist_t *bl, block_t *bp)
add block at end
Definition block.c:45
static void find_blocks(Agraph_t *g, circ_state *state)
Definition blocktree.c:110
static block_t * makeBlock(Agraph_t *g, circ_state *state)
Definition blocktree.c:34
block_t * createBlocktree(Agraph_t *g, circ_state *state)
Definition blocktree.c:140
static Agraph_t * makeBlockGraph(Agraph_t *g, circ_state *state)
Definition blocktree.c:23
static void dfs(Agraph_t *g, Agnode_t *u, circ_state *state, bool isRoot, estack_t *stk)
Definition blocktree.c:56
void freeBlocktree(block_t *bp)
Definition blocktree.c:178
static void addNode(block_t *bp, Agnode_t *n)
Definition blocktree.c:17
#define CHILD(b)
Definition block.h:50
#define BLOCK(n)
Definition circular.h:85
#define LOWVAL(n)
Definition circular.h:88
#define EDGEORDER(e)
Definition circular.h:78
#define SET_PARENT(n)
Definition circular.h:112
#define ORIGN(n)
Definition circular.h:82
#define PARENT(n)
Definition circular.h:84
#define VAL(n)
Definition circular.h:87
#define parent(i)
Definition closest.c:81
bool late_bool(void *obj, attrsym_t *attr, bool defaultValue)
Definition utils.c:94
helpers for verbose/debug printing
#define GV_DEBUG(...)
Definition debug.h:39
node NULL
Definition grammar.y:181
#define agtail(e)
Definition cgraph.h:988
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:96
#define aghead(e)
Definition cgraph.h:989
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:87
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:48
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:41
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:252
#define agfindnode(g, n)
Definition types.h:611
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
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
Definition rec.c:89
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Definition subg.c:53
static void indent(int ix)
Definition gv2gml.c:94
Arithmetic helper functions.
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:29
#define DEFINE_LIST(name, type)
Definition list.h:22
graph or subgraph
Definition cgraph.h:424
Definition block.h:26
blocklist_t children
Definition block.h:33
Agraph_t * sub_graph
Definition block.h:29
block_t * next
Definition block.h:28
block_t * first
Definition block.h:22
char * rootname
Definition circular.h:23
int blockCount
Definition circular.h:19
attrsym_t * N_root
Definition circular.h:22
blocklist_t bl
Definition circular.h:17