Graphviz 13.0.0~dev.20241220.2304
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/gv_math.h>
15
16static void addNode(block_t * bp, Agnode_t * n)
17{
18 agsubnode(bp->sub_graph, n,1);
19 BLOCK(n) = bp;
20}
21
23{
24 agxbuf name = {0};
25
26 agxbprint(&name, "_block_%d", state->blockCount++);
27 Agraph_t *subg = agsubg(g, agxbuse(&name), 1);
28 agxbfree(&name);
29 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true); //node custom data
30 return subg;
31}
32
33static block_t *makeBlock(Agraph_t * g, circ_state * state)
34{
35 Agraph_t *subg = makeBlockGraph(g, state);
36 block_t *bp = mkBlock(subg);
37
38 return bp;
39}
40
41DEFINE_LIST(estack, Agedge_t*)
42
43/* Current scheme adds articulation point to first non-trivial child
44 * block. If none exists, it will be added to its parent's block, if
45 * non-trivial, or else given its own block.
46 *
47 * FIX:
48 * This should be modified to:
49 * - allow user to specify which block gets a node, perhaps on per-node basis.
50 * - if an articulation point is not used in one of its non-trivial blocks,
51 * dummy edges should be added to preserve biconnectivity
52 * - turn on user-supplied blocks.
53 * - Post-process to move articulation point to largest block
54 */
55static void dfs(Agraph_t *g, Agnode_t *u, circ_state *state, bool isRoot,
56 estack_t *stk) {
57 LOWVAL(u) = VAL(u) = state->orderCount++;
58 for (Agedge_t *e = agfstedge(g, u); e; e = agnxtedge(g, e, u)) {
59 Agnode_t *v = aghead (e);
60 if (v == u) {
61 v = agtail(e);
62 if (!EDGEORDER(e)) EDGEORDER(e) = -1;
63 }
64 else {
65 if (!EDGEORDER(e)) EDGEORDER(e) = 1;
66 }
67
68 if (VAL(v) == 0) { /* Since VAL(root) == 0, it gets treated as artificial cut point */
69 PARENT(v) = u;
70 estack_push_back(stk, e);
71 dfs(g, v, state, false, stk);
72 LOWVAL(u) = imin(LOWVAL(u), LOWVAL(v));
73 if (LOWVAL(v) >= VAL(u)) { /* u is an articulation point */
75 Agnode_t *np;
76 Agedge_t *ep;
77 do {
78 ep = estack_pop_back(stk);
79 if (EDGEORDER(ep) == 1)
80 np = aghead (ep);
81 else
82 np = agtail (ep);
83 if (!BLOCK(np)) {
84 if (!block)
85 block = makeBlock(g, state);
86 addNode(block, np);
87 }
88 } while (ep != e);
89 if (block) { /* If block != NULL, it's not empty */
90 if (!BLOCK(u) && blockSize (block) > 1)
91 addNode(block, u);
92 if (isRoot && BLOCK(u) == block)
93 insertBlock(&state->bl, block);
94 else
95 appendBlock(&state->bl, block);
96 }
97 }
98 } else if (PARENT(u) != v) {
99 LOWVAL(u) = imin(LOWVAL(u), VAL(v));
100 }
101 }
102 if (isRoot && !BLOCK(u)) {
103 block_t *block = makeBlock(g, state);
104 addNode(block, u);
105 insertBlock(&state->bl, block);
106 }
107}
108
109static void find_blocks(Agraph_t * g, circ_state * state)
110{
111 Agnode_t *root = NULL;
112
113 /* check to see if there is a node which is set to be the root
114 */
115 if (state->rootname) {
116 root = agfindnode(g, state->rootname);
117 }
118 if (!root && state->N_root) {
119 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
120 if (late_bool(ORIGN(n), state->N_root, false)) {
121 root = n;
122 break;
123 }
124 }
125 }
126
127 if (!root)
128 root = agfstnode(g);
129 if (Verbose)
130 fprintf (stderr, "root = %s\n", 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:78
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:234
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:307
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:109
static block_t * makeBlock(Agraph_t *g, circ_state *state)
Definition blocktree.c:33
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:22
static void dfs(Agraph_t *g, Agnode_t *u, circ_state *state, bool isRoot, estack_t *stk)
Definition blocktree.c:55
void freeBlocktree(block_t *bp)
Definition blocktree.c:178
static void addNode(block_t *bp, Agnode_t *n)
Definition blocktree.c:16
#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:80
bool late_bool(void *obj, attrsym_t *attr, bool defaultValue)
Definition utils.c:91
static bool Verbose
Definition gml2gv.c:23
node NULL
Definition grammar.y:163
#define agtail(e)
Definition cgraph.h:880
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:94
#define aghead(e)
Definition cgraph.h:881
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:85
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:259
#define agfindnode(g, n)
Definition types.h:611
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
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:58
static void indent(int ix)
Definition gv2gml.c:96
Arithmetic helper functions.
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:27
#define DEFINE_LIST(name, type)
Definition list.h:26
graph or subgraph
Definition cgraph.h:425
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