Graphviz 16.1.3~dev.20261203.0324
Loading...
Searching...
No Matches
hierarchy.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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
12// //
13// This file contains the functions //
14// for constructing and managing the //
15// hierarchy structure //
16// //
18
19#include "config.h"
20
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <math.h>
26#include <time.h>
27#include <assert.h>
28#include <common/arith.h>
29#include <topfish/hierarchy.h>
30#include <util/alloc.h>
31#include <util/list.h>
32#include <util/sort.h>
33
35// Some utilities for //
36// 'maxmatch(..)' //
38
39static double
40unweighted_common_fraction(v_data * graph, int v, int u, float *v_vector)
41{
42// returns: |N(v) & N(u)| / |N(v) or N(u)|
43// v_vector[i]>0 <==> i is neighbor of v or is v itself
44
45 int neighbor;
46 int num_shared_neighbors = 0;
47 int j;
48 for (j = 0; j < graph[u].nedges; j++) {
49 neighbor = graph[u].edges[j];
50 if (v_vector[neighbor] > 0) {
51 // a shared neighobr
52 num_shared_neighbors++;
53 }
54 }
55 // parallel to the weighted version:
56 //return 2*num_shared_neighbors/(graph[v].nedges+graph[u].nedges);
57
58 // more natural
59 return ((double) num_shared_neighbors) / (graph[v].nedges +
60 graph[u].nedges -
61 num_shared_neighbors);
62}
63
64static void fill_neighbors_vec(v_data *graph, int vtx, float *vtx_vec) {
65 int j;
66 if (graph[0].ewgts != NULL) {
67 for (j = 0; j < graph[vtx].nedges; j++) {
68 vtx_vec[graph[vtx].edges[j]] = fabsf(graph[vtx].ewgts[j]); // use fabsf for the self loop
69 }
70 } else {
71 for (j = 0; j < graph[vtx].nedges; j++) {
72 vtx_vec[graph[vtx].edges[j]] = 1;
73 }
74 }
75}
76
77static void
78fill_neighbors_vec_unweighted(v_data * graph, int vtx, float *vtx_vec)
79{
80 // a node is a neighbor of itself!
81 int j;
82 for (j = 0; j < graph[vtx].nedges; j++) {
83 vtx_vec[graph[vtx].edges[j]] = 1;
84 }
85}
86
87static void empty_neighbors_vec(v_data * graph, int vtx, float *vtx_vec)
88{
89 int j;
90 for (j = 0; j < graph[vtx].nedges; j++) {
91 vtx_vec[graph[vtx].edges[j]] = 0;
92 }
93}
94
95
96static int dist3(v_data * graph, int node1, int node2)
97{
98// succeeds if the graph theoretic distance between the nodes is no more than 3
99 int i, j, k;
100 int u, v;
101 for (i = 1; i < graph[node1].nedges; i++) {
102 u = graph[node1].edges[i];
103 if (u == node2) {
104 return 1;
105 }
106 for (j = 1; j < graph[u].nedges; j++) {
107 v = graph[u].edges[j];
108 if (v == node2) {
109 return 1;
110 }
111 for (k = 1; k < graph[v].nedges; k++) {
112 if (graph[v].edges[k] == node2) {
113 return 1;
114 }
115 }
116 }
117 }
118 return 0;
119}
120
121#define A 1.0
122#define B 1.0
123#define C 3.0
124#define D 1.0
125
126static double ddist(ex_vtx_data * geom_graph, int v, int u)
127{
128// Euclidean distance between nodes 'v' and 'u'
129 double x_v = geom_graph[v].x_coord, y_v = geom_graph[v].y_coord,
130 x_u = geom_graph[u].x_coord, y_u = geom_graph[u].y_coord;
131
132 return hypot(x_v - x_u, y_v - y_u);
133}
134
135static int
136maxmatch(v_data * graph, /* array of vtx data for graph */
137 ex_vtx_data * geom_graph, /* array of vtx data for graph */
138 int nvtxs, /* number of vertices in graph */
139 int *mflag, /* flag indicating vtx selected or not */
140 bool dist2_limit
141 )
142/*
143 Compute a matching of the nodes set.
144 The matching is not based only on the edge list of 'graph',
145 which might be too small,
146 but on the wider edge list of 'geom_graph' (which includes 'graph''s edges)
147
148 We match nodes that are close both in the graph-theoretical sense and
149 in the geometry sense (in the layout)
150*/
151{
152 int *order; /* random ordering of vertices */
153 int *iptr, *jptr; /* loops through integer arrays */
154 int vtx; /* vertex to process next */
155 int neighbor; /* neighbor of a vertex */
156 int nmerged = 0; /* number of edges in matching */
157 int i, j; /* loop counters */
158 float max_norm_edge_weight;
159 double inv_size;
160 double *matchability = gv_calloc(nvtxs, sizeof(double));
161 double min_edge_len;
162 double closest_val = -1, val;
163 int closest_neighbor;
164 float *vtx_vec = gv_calloc(nvtxs, sizeof(float));
165 float *weighted_vtx_vec = gv_calloc(nvtxs, sizeof(float));
166
167 // gather statistics, to enable normalizing the values
168 double avg_edge_len = 0, avg_deg_2 = 0;
169 int nedges = 0;
170
171 for (i = 0; i < nvtxs; i++) {
172 avg_deg_2 += graph[i].nedges;
173 for (j = 1; j < graph[i].nedges; j++) {
174 avg_edge_len += ddist(geom_graph, i, graph[i].edges[j]);
175 nedges++;
176 }
177 }
178 avg_edge_len /= nedges;
179 avg_deg_2 /= nvtxs;
180 avg_deg_2 *= avg_deg_2;
181
182 // the normalized edge weight of edge <v,u> is defined as:
183 // weight(<v,u>)/sqrt(size(v)*size(u))
184 // Now we compute the maximal normalized weight
185 if (graph[0].ewgts != NULL) {
186 max_norm_edge_weight = -1;
187 for (i = 0; i < nvtxs; i++) {
188 inv_size = sqrt(1.0 / geom_graph[i].size);
189 for (j = 1; j < graph[i].nedges; j++) {
190 if (graph[i].ewgts[j] * inv_size /
191 sqrt((float) geom_graph[graph[i].edges[j]].size) >
192 max_norm_edge_weight) {
193 max_norm_edge_weight =
194 (float) (graph[i].ewgts[j] * inv_size /
195 sqrt((double)
196 geom_graph[graph[i].edges[j]].size));
197 }
198 }
199 }
200 } else {
201 max_norm_edge_weight = 1;
202 }
203
204 /* Now determine the order of the vertices. */
205 iptr = order = gv_calloc(nvtxs, sizeof(int));
206 jptr = mflag;
207 for (i = 0; i < nvtxs; i++) {
208 *(iptr++) = i;
209 *(jptr++) = -1;
210 }
211
212 // Option 2: sort the nodes beginning with the ones highly approriate for matching
213
214#ifdef DEBUG
215 srand(0);
216#endif
217 for (i = 0; i < nvtxs; i++) {
218 vtx = order[i];
219 matchability[vtx] = graph[vtx].nedges; // we less want to match high degree nodes
220 matchability[vtx] += geom_graph[vtx].size; // we less want to match large sized nodes
221 min_edge_len = 1e99;
222 for (j = 1; j < graph[vtx].nedges; j++) {
223 min_edge_len =
224 MIN(min_edge_len,
225 ddist(geom_graph, vtx,
226 graph[vtx].edges[j]) / avg_edge_len);
227 }
228 matchability[vtx] += min_edge_len; // we less want to match distant nodes
229 matchability[vtx] += ((double) rand()) / RAND_MAX; // add some randomness
230 }
231 sort_place(matchability, order, (size_t)nvtxs);
232 free(matchability);
233
234 // Start determining the matched pairs
235 for (i = 0; i < nvtxs; i++) {
236 vtx_vec[i] = 0;
237 }
238 for (i = 0; i < nvtxs; i++) {
239 weighted_vtx_vec[i] = 0;
240 }
241
242 // relative weights of the different criteria
243 for (i = 0; i < nvtxs; i++) {
244 vtx = order[i];
245 if (mflag[vtx] >= 0) { /* already matched. */
246 continue;
247 }
248 inv_size = sqrt(1.0 / geom_graph[vtx].size);
249 fill_neighbors_vec(graph, vtx, weighted_vtx_vec);
251 closest_neighbor = -1;
252
253 /*
254 We match node i with the "closest" neighbor, based on 4 criteria:
255 (1) (Weighted) fraction of common neighbors (measured on orig. graph)
256 (2) AvgDeg*AvgDeg/(deg(vtx)*deg(neighbor)) (degrees measured on orig. graph)
257 (3) AvgEdgeLen/dist(vtx,neighbor)
258 (4) Weight of normalized direct connection between nodes (measured on orig. graph)
259 */
260
261 for (j = 1; j < geom_graph[vtx].nedges; j++) {
262 neighbor = geom_graph[vtx].edges[j];
263 if (mflag[neighbor] >= 0) { /* already matched. */
264 continue;
265 }
266 // (1):
267 val =
269 vtx_vec);
270
271 if (val == 0 && (dist2_limit || !dist3(graph, vtx, neighbor))) {
272 // graph theoretical distance is larger than 3 (or 2 if '!dist3(graph, vtx, neighbor)' is commented)
273 // nodes cannot be matched
274 continue;
275 }
276 // (2)
277 val +=
278 B * avg_deg_2 / (graph[vtx].nedges *
279 graph[neighbor].nedges);
280
281
282 // (3)
283 val += C * avg_edge_len / ddist(geom_graph, vtx, neighbor);
284
285 // (4)
286 val +=
287 (weighted_vtx_vec[neighbor] * inv_size /
288 sqrt((float) geom_graph[neighbor].size)) /
289 max_norm_edge_weight;
290
291
292
293 if (val > closest_val || closest_neighbor == -1) {
294 closest_neighbor = neighbor;
295 closest_val = val;
296 }
297
298 }
299 if (closest_neighbor != -1) {
300 mflag[vtx] = closest_neighbor;
301 mflag[closest_neighbor] = vtx;
302 nmerged++;
303 }
304 empty_neighbors_vec(graph, vtx, vtx_vec);
305 empty_neighbors_vec(graph, vtx, weighted_vtx_vec);
306 }
307
308 free(order);
309 free(vtx_vec);
310 free(weighted_vtx_vec);
311 return (nmerged);
312}
313
314/* Construct mapping from original graph nodes to coarsened graph nodes */
315static void makev2cv(int *mflag, /* flag indicating vtx selected or not */
316 int nvtxs, /* number of vtxs in original graph */
317 int *v2cv, /* mapping from vtxs to coarsened vtxs */
318 int *cv2v /* mapping from coarsened vtxs to vtxs */
319 )
320{
321 int i, j; /* loop counters */
322
323 j = 0;
324 for (i = 0; i < nvtxs; i++) {
325 if (mflag[i] < 0) { // unmatched node
326 v2cv[i] = j;
327 cv2v[2 * j] = i;
328 cv2v[2 * j + 1] = -1;
329 j++;
330 } else if (mflag[i] > i) { // matched node
331 v2cv[i] = j;
332 v2cv[mflag[i]] = j;
333 cv2v[2 * j] = i;
334 cv2v[2 * j + 1] = mflag[i];
335 j++;
336 }
337 }
338}
339
340static int make_coarse_graph(v_data * graph, /* array of vtx data for graph */
341 int nedges, /* number of edges in graph */
342 v_data ** cgp, /* coarsened version of graph */
343 int cnvtxs, /* number of vtxs in coarsened graph */
344 int *v2cv, /* mapping from vtxs to coarsened vtxs */
345 int *cv2v /* mapping from coarsened vtxs to vtxs */
346 )
347// This function takes the information about matched pairs
348// and use it to contract these pairs and build a coarse graph
349{
350 int j, cv, v, neighbor, cv_nedges;
351 int cnedges = 0; /* number of edges in coarsened graph */
352 v_data *cgraph; /* coarsened version of graph */
353 int *index = gv_calloc(cnvtxs, sizeof(int));
354 float intra_weight;
355 /* An upper bound on the number of coarse graph edges. */
356 int maxCnedges = nedges; // do not subtract (nvtxs-cnvtxs) because we do not contract only along edges
357 int *edges;
358 float *eweights;
359
360 /* Now allocate space for the new graph. Overeallocate and realloc later. */
361 cgraph = gv_calloc(cnvtxs, sizeof(v_data));
362 edges = gv_calloc(2 * maxCnedges + cnvtxs, sizeof(int));
363 eweights = gv_calloc(2 * maxCnedges + cnvtxs, sizeof(float));
364
365 if (graph[0].ewgts != NULL) {
366 // use edge weights
367 for (cv = 0; cv < cnvtxs; cv++) {
368
369 intra_weight = 0;
370
371 cgraph[cv].edges = edges;
372 cgraph[cv].ewgts = eweights;
373
374 cv_nedges = 1;
375 v = cv2v[2 * cv];
376 for (j = 1; j < graph[v].nedges; j++) {
377 neighbor = v2cv[graph[v].edges[j]];
378 if (neighbor == cv) {
379 intra_weight = 2 * graph[v].ewgts[j]; // count both directions of the intra-edge
380 continue;
381 }
382 if (index[neighbor] == 0) { // new neighbor
383 index[neighbor] = cv_nedges;
384 cgraph[cv].edges[cv_nedges] = neighbor;
385 cgraph[cv].ewgts[cv_nedges] = graph[v].ewgts[j];
386 cv_nedges++;
387 } else {
388 cgraph[cv].ewgts[index[neighbor]] += graph[v].ewgts[j];
389 }
390 }
391
392 cgraph[cv].ewgts[0] = graph[v].ewgts[0];
393
394 if ((v = cv2v[2 * cv + 1]) != -1) {
395 for (j = 1; j < graph[v].nedges; j++) {
396 neighbor = v2cv[graph[v].edges[j]];
397 if (neighbor == cv)
398 continue;
399 if (index[neighbor] == 0) { // new neighbor
400 index[neighbor] = cv_nedges;
401 cgraph[cv].edges[cv_nedges] = neighbor;
402 cgraph[cv].ewgts[cv_nedges] = graph[v].ewgts[j];
403 cv_nedges++;
404 } else {
405 cgraph[cv].ewgts[index[neighbor]] +=
406 graph[v].ewgts[j];
407 }
408 }
409 cgraph[cv].ewgts[0] += graph[v].ewgts[0] + intra_weight;
410 }
411 cgraph[cv].nedges = cv_nedges;
412 cgraph[cv].edges[0] = cv;
413 edges += cv_nedges;
414 eweights += cv_nedges;
415 cnedges += cv_nedges;
416
417 for (j = 1; j < cgraph[cv].nedges; j++)
418 index[cgraph[cv].edges[j]] = 0;
419 }
420 } else { // fine graph is unweighted
421 int internal_weight = 0;
422
423 for (cv = 0; cv < cnvtxs; cv++) {
424
425 cgraph[cv].edges = edges;
426 cgraph[cv].ewgts = eweights;
427
428 cv_nedges = 1;
429 v = cv2v[2 * cv];
430 for (j = 1; j < graph[v].nedges; j++) {
431 neighbor = v2cv[graph[v].edges[j]];
432 if (neighbor == cv) {
433 internal_weight = 2;
434 continue;
435 }
436 if (index[neighbor] == 0) { // new neighbor
437 index[neighbor] = cv_nedges;
438 cgraph[cv].edges[cv_nedges] = neighbor;
439 cgraph[cv].ewgts[cv_nedges] = -1;
440 cv_nedges++;
441 } else {
442 cgraph[cv].ewgts[index[neighbor]]--;
443 }
444 }
445 cgraph[cv].ewgts[0] = (float) graph[v].edges[0]; // this is our trick to store the weights on the diag in an unweighted graph
446 if ((v = cv2v[2 * cv + 1]) != -1) {
447 for (j = 1; j < graph[v].nedges; j++) {
448 neighbor = v2cv[graph[v].edges[j]];
449 if (neighbor == cv)
450 continue;
451 if (index[neighbor] == 0) { // new neighbor
452 index[neighbor] = cv_nedges;
453 cgraph[cv].edges[cv_nedges] = neighbor;
454 cgraph[cv].ewgts[cv_nedges] = -1;
455 cv_nedges++;
456 } else {
457 cgraph[cv].ewgts[index[neighbor]]--;
458 }
459 }
460 // we subtract the weight of the intra-edge that was counted twice
461 cgraph[cv].ewgts[0] +=
462 (float) graph[v].edges[0] - internal_weight;
463 // In a case the edge weights are defined as positive:
464 //cgraph[cv].ewgts[0] += (float) graph[v].edges[0]+internal_weight;
465 }
466
467 cgraph[cv].nedges = cv_nedges;
468 cgraph[cv].edges[0] = cv;
469 edges += cv_nedges;
470 eweights += cv_nedges;
471 cnedges += cv_nedges;
472
473 for (j = 1; j < cgraph[cv].nedges; j++)
474 index[cgraph[cv].edges[j]] = 0;
475 }
476 }
477 cnedges -= cnvtxs;
478 cnedges /= 2;
479 free(index);
480 *cgp = cgraph;
481 return cnedges;
482}
483
484static int
486 ex_vtx_data * graph, /* array of vtx data for graph */
487 int nedges, /* number of edges in graph */
488 ex_vtx_data ** cgp, /* coarsened version of graph */
489 int cnvtxs, /* number of vtxs in coarsened graph */
490 int *v2cv, /* mapping from vtxs to coarsened vtxs */
491 int *cv2v /* mapping from coarsened vtxs to vtxs */
492)
493// This function takes the information about matched pairs
494// and use it to contract these pairs and build a coarse ex_graph
495{
496 int cnedges; /* number of edges in coarsened graph */
497 ex_vtx_data *cgraph; /* coarsened version of graph */
498 int j, cv, v, neighbor, cv_nedges;
499 int *index = gv_calloc(cnvtxs, sizeof(int));
500 int *edges;
501
502 /* An upper bound on the number of coarse graph edges. */
503 cnedges = nedges;
504
505 /* Now allocate space for the new graph. Overeallocate and realloc later. */
506 cgraph = gv_calloc(cnvtxs, sizeof(ex_vtx_data));
507 edges = gv_calloc(2 * cnedges + cnvtxs, sizeof(int));
508
509 for (cv = 0; cv < cnvtxs; cv++) {
510
511 cgraph[cv].edges = edges;
512
513 cv_nedges = 1;
514 v = cv2v[2 * cv];
515 for (j = 1; j < graph[v].nedges; j++) {
516 neighbor = v2cv[graph[v].edges[j]];
517 if (neighbor == cv) {
518 continue;
519 }
520 if (index[neighbor] == 0) { // new neighbor
521 index[neighbor] = cv_nedges;
522 cgraph[cv].edges[cv_nedges] = neighbor;
523 cv_nedges++;
524 }
525 }
526 cgraph[cv].size = graph[v].size;
527 cgraph[cv].x_coord = graph[v].x_coord;
528 cgraph[cv].y_coord = graph[v].y_coord;
529 if ((v = cv2v[2 * cv + 1]) != -1) {
530 for (j = 1; j < graph[v].nedges; j++) {
531 neighbor = v2cv[graph[v].edges[j]];
532 if (neighbor == cv)
533 continue;
534 if (index[neighbor] == 0) { // new neighbor
535 index[neighbor] = cv_nedges;
536 cgraph[cv].edges[cv_nedges] = neighbor;
537 cv_nedges++;
538 }
539 }
540 // compute new coord's as a weighted average of the old ones
541 cgraph[cv].x_coord =
542 (cgraph[cv].size * cgraph[cv].x_coord +
543 graph[v].size * graph[v].x_coord) / (cgraph[cv].size +
544 graph[v].size);
545 cgraph[cv].y_coord =
546 (cgraph[cv].size * cgraph[cv].y_coord +
547 graph[v].size * graph[v].y_coord) / (cgraph[cv].size +
548 graph[v].size);
549 cgraph[cv].size += graph[v].size;
550 }
551 cgraph[cv].nedges = cv_nedges;
552 cgraph[cv].edges[0] = cv;
553 edges += cv_nedges;
554
555 for (j = 1; j < cgraph[cv].nedges; j++)
556 index[cgraph[cv].edges[j]] = 0;
557 }
558 free(index);
559 *cgp = cgraph;
560 return cnedges;
561}
562
563static void
565 v_data * graph, /* graph to be matched */
566 ex_vtx_data* geom_graph, /* another graph (with coords) on the same nodes */
567 int nvtxs, /* number of vertices in graph */
568 int nedges, /* number of edges in graph */
569 int geom_nedges, /* number of edges in geom_graph */
570 v_data ** cgraph, /* coarsened version of graph */
571 ex_vtx_data ** cgeom_graph, /* coarsened version of geom_graph */
572 int *cnp, /* number of vtxs in coarsened graph */
573 int *cnedges, /* number of edges in coarsened graph */
574 int *cgeom_nedges, /* number of edges in coarsened geom_graph */
575 int **v2cvp, /* reference from vertices to coarse vertices */
576 int **cv2vp, /* reference from vertices to coarse vertices */
577 bool dist2_limit
578)
579
580/*
581 * This function gets two graphs with the same node set and
582 * constructs two corresponding coarsened graphs of about
583 * half the size
584 */
585{
586 int *mflag; /* flag indicating vtx matched or not */
587 int nmerged; /* number of edges contracted */
588 int *v2cv; /* reference from vertices to coarse vertices */
589 int *cv2v; /* reference from vertices to coarse vertices */
590 int cnvtxs;
591
592 /* Allocate and initialize space. */
593 mflag = gv_calloc(nvtxs, sizeof(int));
594
595 /* Find a maximal matching in the graphs */
596 nmerged = maxmatch(graph, geom_graph, nvtxs, mflag, dist2_limit);
597
598 /* Now construct coarser graph by contracting along matching edges. */
599 /* Pairs of values in mflag array indicate matched vertices. */
600 /* A negative value indicates that vertex is unmatched. */
601
602 *cnp = cnvtxs = nvtxs - nmerged;
603
604 *v2cvp = v2cv = gv_calloc(nvtxs, sizeof(int));
605 *cv2vp = cv2v = gv_calloc(2 * cnvtxs, sizeof(int));
606 makev2cv(mflag, nvtxs, v2cv, cv2v);
607
608 free(mflag);
609
610 *cnedges = make_coarse_graph(graph, nedges, cgraph, cnvtxs, v2cv, cv2v);
611 *cgeom_nedges = make_coarse_ex_graph(geom_graph, geom_nedges, cgeom_graph,
612 cnvtxs, v2cv, cv2v);
613}
614
615static v_data *cpGraph(v_data * graph, int n, int nedges)
616{
617 float *ewgts = NULL;
618 int i, j;
619
620 if (graph == NULL || n == 0) {
621 return NULL;
622 }
623 v_data *cpGraph = gv_calloc(n, sizeof(v_data));
624 int *edges = gv_calloc(2 * nedges + n, sizeof(int));
625 if (graph[0].ewgts != NULL) {
626 ewgts = gv_calloc(2 * nedges + n, sizeof(float));
627 }
628
629 for (i = 0; i < n; i++) {
630 cpGraph[i] = graph[i];
631 cpGraph[i].edges = edges;
632 cpGraph[i].ewgts = ewgts;
633 for (j = 0; j < graph[i].nedges; j++) {
634 edges[j] = graph[i].edges[j];
635 }
636 edges += graph[i].nedges;
637 if (ewgts != NULL) {
638 for (j = 0; j < graph[i].nedges; j++) {
639 ewgts[j] = graph[i].ewgts[j];
640 }
641 ewgts += graph[i].nedges;
642 }
643 }
644 return cpGraph;
645}
646
648{
649 int i, j;
650
651 if (graph == NULL || n == 0) {
652 return NULL;
653 }
655 int *edges = gv_calloc(2 * nedges + n, sizeof(int));
656
657 for (i = 0; i < n; i++) {
658 cpGraph[i] = graph[i];
659 cpGraph[i].edges = edges;
660 for (j = 0; j < graph[i].nedges; j++) {
661 edges[j] = graph[i].edges[j];
662 }
663 edges += graph[i].nedges;
664 }
665 return cpGraph;
666}
667
669 ex_vtx_data *geom_graph, int ngeom_edges,
670 bool dist2_limit) {
671 int cur_level;
672 Hierarchy *hierarchy = gv_alloc(sizeof(Hierarchy));
673 int cngeom_edges = ngeom_edges;
674 static const int min_nvtxs = 20;
675 int nlevels = MAX(5, 10 * (int) log((float) (nvtxs / min_nvtxs))); // just an estimate
676
677 int *nedge = gv_calloc(nlevels, sizeof(int));
678 hierarchy->v2cv = gv_calloc(nlevels, sizeof(int*));
679 hierarchy->cv2v = gv_calloc(nlevels, sizeof(int*));
680
681 LIST_APPEND(&hierarchy->graphs, cpGraph(graph, nvtxs, nedges));
682 LIST_APPEND(&hierarchy->geom_graphs, cpExGraph(geom_graph, nvtxs, ngeom_edges));
683 LIST_APPEND(&hierarchy->nvtxs, nvtxs);
684 nedge[0] = nedges;
685
686 for (cur_level = 0;
687 LIST_GET(&hierarchy->nvtxs, (size_t)cur_level) > min_nvtxs
688 && cur_level < 50 /*nvtxs/10 */ ; cur_level++) {
689 if (cur_level == nlevels - 1) { // we have to allocate more space
690 nedge = gv_recalloc(nedge, nlevels, nlevels * 2, sizeof(int));
691 hierarchy->v2cv = gv_recalloc(hierarchy->v2cv, nlevels, nlevels * 2, sizeof(int*));
692 hierarchy->cv2v = gv_recalloc(hierarchy->cv2v, nlevels, nlevels * 2, sizeof(int*));
693 nlevels *= 2;
694 }
695
696 ngeom_edges = cngeom_edges;
697 LIST_APPEND(&hierarchy->graphs, NULL);
698 LIST_APPEND(&hierarchy->geom_graphs, NULL);
699 LIST_APPEND(&hierarchy->nvtxs, 0);
701 (LIST_GET(&hierarchy->graphs, (size_t)cur_level),
702 LIST_GET(&hierarchy->geom_graphs, (size_t)cur_level),
703 LIST_GET(&hierarchy->nvtxs, (size_t)cur_level), nedge[cur_level],
704 ngeom_edges, LIST_AT(&hierarchy->graphs, (size_t)cur_level + 1),
705 LIST_AT(&hierarchy->geom_graphs, (size_t)cur_level + 1),
706 LIST_AT(&hierarchy->nvtxs, (size_t)cur_level + 1),
707 &nedge[cur_level + 1], &cngeom_edges,
708 &hierarchy->v2cv[cur_level], &hierarchy->cv2v[cur_level + 1],
709 dist2_limit);
710 }
711
712 hierarchy->nlevels = cur_level + 1;
713
714 free(nedge);
715 return hierarchy;
716}
717
718static double
719dist_from_foci(ex_vtx_data * graph, int node, int *foci, int num_foci)
720{
721// compute minimum distance of 'node' from the set 'foci'
722 int i;
723 double distance = ddist(graph, node, foci[0]);
724 for (i = 1; i < num_foci; i++) {
725 distance = MIN(distance, ddist(graph, node, foci[i]));
726 }
727
728 return distance;
729}
730
731/* set_active_levels:
732 * Compute the "active level" field of each node in the hierarchy.
733 * Note that if the active level is lower than the node's level, the node
734 * is "split" in the presentation; if the active level is higher than
735 * the node's level, then the node is aggregated into a coarser node.
736 * If the active level equals the node's level then the node is currently shown
737 */
738void
739set_active_levels(Hierarchy * hierarchy, int *foci_nodes, int num_foci,
741{
742 int min_level = 0;
743
744 ex_vtx_data *graph = LIST_GET(&hierarchy->geom_graphs, (size_t)min_level); // finest graph
745 int n = LIST_GET(&hierarchy->nvtxs, (size_t)min_level);
746
747 // compute distances from foci nodes
748 int *nodes = gv_calloc(n, sizeof(int));
749 double *distances = gv_calloc(n, sizeof(double));
750 for (int i = 0; i < n; i++) {
751 nodes[i] = i;
752 distances[i] = dist_from_foci(graph, i, foci_nodes, num_foci);
753 }
754
755 // sort nodes according to their distance from foci
756 sort_place(distances, nodes, (size_t)n);
757
758 /* compute *desired* levels of fine nodes by distributing them into buckets
759 * The sizes of the buckets is a geometric series with
760 * factor: 'coarsening_rate'
761 */
762 int level = min_level;
763 int group_size = parms->num_fine_nodes * num_foci;
764 int thresh = group_size;
765 for (int i = 0; i < n; i++) {
766 const int vtx = nodes[i];
767 if (i > thresh && level < hierarchy->nlevels - 1) {
768 level++;
769 group_size = (int) (group_size * parms->coarsening_rate);
770 thresh += group_size;
771 }
772 graph[vtx].active_level = level;
773 }
774
775 // Fine-to-coarse sweep:
776 //----------------------
777 // Propagate levels to all coarse nodes and determine final levels
778 // at lowest meeting points. Note that nodes can be active in
779 // lower (finer) levels than what originally desired, since if 'u'
780 // and 'v' are merged, than the active level of '{u,v}' will be
781 // the minimum of the active levels of 'u' and 'v'
782 for (level = min_level + 1; level < hierarchy->nlevels; level++) {
783 ex_vtx_data *const cgraph = LIST_GET(&hierarchy->geom_graphs, (size_t)level);
784 graph = LIST_GET(&hierarchy->geom_graphs, (size_t)level - 1);
785 const int *const cv2v = hierarchy->cv2v[level];
786 n = LIST_GET(&hierarchy->nvtxs, (size_t)level);
787 for (int i = 0; i < n; i++) {
788 const int v = cv2v[2 * i];
789 const int u = cv2v[2 * i + 1];
790 if (u >= 0) { // cv is decomposed from 2 fine nodes
791 if (graph[v].active_level < level
792 || graph[u].active_level < level) {
793 // At least one of the nodes should be active at a lower level,
794 // in this case both children are active at a lower level
795 // and we don't wait till they are merged
796 graph[v].active_level =
797 MIN(graph[v].active_level, level - 1);
798 graph[u].active_level =
799 MIN(graph[u].active_level, level - 1);
800 }
801 // The node with the finer (lower) active level determines the coarse active level
802 cgraph[i].active_level =
803 MIN(graph[v].active_level, graph[u].active_level);
804 } else {
805 cgraph[i].active_level = graph[v].active_level;
806 }
807 }
808 }
809
810 // Coarse-to-fine sweep:
811 //----------------------
812 // Propagate final levels all the way to fine nodes
813 for (level = hierarchy->nlevels - 1; level > 0; level--) {
814 ex_vtx_data *const cgraph = LIST_GET(&hierarchy->geom_graphs, (size_t)level);
815 graph = LIST_GET(&hierarchy->geom_graphs, (size_t)level - 1);
816 const int *const cv2v = hierarchy->cv2v[level];
817 n = LIST_GET(&hierarchy->nvtxs, (size_t)level);
818 for (int i = 0; i < n; i++) {
819 if (cgraph[i].active_level < level) {
820 continue;
821 }
822 // active level has been already reached, copy level to children
823 const int v = cv2v[2 * i];
824 const int u = cv2v[2 * i + 1];
825 graph[v].active_level = cgraph[i].active_level;
826 if (u >= 0) {
827 graph[u].active_level = cgraph[i].active_level;
828 }
829 }
830 }
831 free(nodes);
832 free(distances);
833}
834
835/* findClosestActiveNode:
836 * Given (x,y) in physical coords, check if node is closer to this point
837 * than previous setting. If so, reset values.
838 * If node is not active, recurse down finer levels.
839 * Return closest distance squared.
840 */
841static double
843 int level, double x, double y,
844 double closest_dist, int *closest_node,
845 int *closest_node_level)
846{
847 ex_vtx_data *const graph = LIST_GET(&hierarchy->geom_graphs, (size_t)level);
848
849 if (graph[node].active_level == level)
850 { // node is active
851 double delx = x - graph[node].physical_x_coord;
852 double dely = y - graph[node].physical_y_coord;
853 double dist = delx*delx + dely*dely;
854
855 if (dist < closest_dist)
856 {
857 closest_dist = dist;
858 *closest_node = node;
859 *closest_node_level = level;
860
861
862 }
863 return closest_dist;
864 }
865
866 closest_dist =
867 findClosestActiveNode(hierarchy, hierarchy->cv2v[level][2 * node],
868 level - 1, x, y, closest_dist, closest_node,
869 closest_node_level);
870
871 if (hierarchy->cv2v[level][2 * node + 1] >= 0) {
872 closest_dist =
873 findClosestActiveNode(hierarchy,
874 hierarchy->cv2v[level][2 * node + 1],
875 level - 1, x, y, closest_dist,
876 closest_node, closest_node_level);
877 }
878 return closest_dist;
879}
880
881/* find_leftmost_descendant:
882 * Given coarse node in given level, return representative node
883 * in lower level
884 */
885static int
886find_leftmost_descendant(Hierarchy * hierarchy, int node, int level)
887{
888 while (level > 0)
889 {
890 node = hierarchy->cv2v[level--][2 * node];
891 }
892 return node;
893}
894
895/* find_closest_active_node:
896 * Given x and y in physical coordinate system, determine closest
897 * actual node in graph. Store this in closest_fine_node.
898 */
899void
900find_closest_active_node(Hierarchy * hierarchy, double x, double y,
901 int *closest_fine_node)
902{
903 int i, closest_node, closest_node_level;
904 int top_level = hierarchy->nlevels - 1;
905 double min_dist = 1e20;
906
907 for (i = 0; i < LIST_GET(&hierarchy->nvtxs, (size_t)top_level); i++)
908 {
909 min_dist = findClosestActiveNode(hierarchy, i, top_level, x, y,min_dist, &closest_node, &closest_node_level);
910 }
911 *closest_fine_node =find_leftmost_descendant(hierarchy, closest_node,closest_node_level);
912}
913
914int
915init_ex_graph(v_data * graph1, v_data * graph2, int n,
916 double *x_coords, double *y_coords, ex_vtx_data ** gp)
917{
918 // build ex_graph from the union of edges in 'graph1' and 'graph2'
919 // note that this function does not destroy the input graphs
920
921 ex_vtx_data *geom_graph;
922 int nedges1 = 0, nedges2 = 0;
923 int nedges = 0;
924 int i, j, k, l, first_nedges;
925 int neighbor;
926 for (i = 0; i < n; i++) {
927 nedges1 += graph1[i].nedges;
928 nedges2 += graph2[i].nedges;
929 }
930 int *edges = gv_calloc(nedges1 + nedges2, sizeof(int));
931 *gp = geom_graph = gv_calloc(n, sizeof(ex_vtx_data));
932
933 for (i = 0; i < n; i++) {
934 geom_graph[i].edges = edges;
935 geom_graph[i].size = 1;
936 geom_graph[i].x_coord = (float) x_coords[i];
937 geom_graph[i].y_coord = (float) y_coords[i];
938 geom_graph[i].edges[0] = i;
939 for (j = 1; j < graph1[i].nedges; j++) {
940 edges[j] = graph1[i].edges[j];
941 }
942 first_nedges = k = graph1[i].nedges;
943 for (j = 1; j < graph2[i].nedges; j++) {
944 neighbor = graph2[i].edges[j];
945 for (l = 1; l < first_nedges; l++) {
946 if (edges[l] == neighbor) { // already existed neighbor
947 break;
948 }
949 }
950 if (l == first_nedges) { // neighbor wasn't found
951 edges[k++] = neighbor;
952 }
953 }
954 geom_graph[i].nedges = k;
955 edges += k;
956 nedges += k;
957 }
958 nedges /= 2;
959 return nedges;
960}
961
962/* extract_active_logical_coords:
963 * Preorder scan the hierarchy tree, and extract the logical coordinates of
964 * all active nodes
965 * Store (universal) coords in x_coords and y_coords and increment index.
966 * Return index.
967 */
968size_t extract_active_logical_coords(Hierarchy *hierarchy, int node, int level,
969 double *x_coords, double *y_coords,
970 size_t counter) {
971
972 ex_vtx_data *graph = LIST_GET(&hierarchy->geom_graphs, (size_t)level);
973
974 if (graph[node].active_level == level) { // node is active
975 x_coords[counter] = graph[node].x_coord;
976 y_coords[counter++] = graph[node].y_coord;
977 return counter;
978 }
979
980 counter =
982 hierarchy->cv2v[level][2 * node],
983 level - 1, x_coords, y_coords,
984 counter);
985
986 if (hierarchy->cv2v[level][2 * node + 1] >= 0) {
987 counter =
989 hierarchy->cv2v[level][2 * node +
990 1],
991 level - 1, x_coords, y_coords,
992 counter);
993 }
994 return counter;
995}
996
997/* set_active_physical_coords:
998 * Preorder scan the hierarchy tree, and set the physical coordinates
999 * of all active nodes
1000 */
1001int
1002set_active_physical_coords(Hierarchy * hierarchy, int node, int level,
1003 double *x_coords, double *y_coords, int counter)
1004{
1005
1006 ex_vtx_data *graph = LIST_GET(&hierarchy->geom_graphs, (size_t)level);
1007
1008 if (graph[node].active_level == level) { // node is active
1009 graph[node].physical_x_coord = (float) x_coords[counter];
1010 graph[node].physical_y_coord = (float) y_coords[counter++];
1011 return counter;
1012 }
1013
1014 counter =
1016 hierarchy->cv2v[level][2*node],
1017 level - 1, x_coords, y_coords, counter);
1018
1019 if (hierarchy->cv2v[level][2 * node + 1] >= 0) {
1020 counter =
1022 hierarchy->cv2v[level][2*node + 1],
1023 level - 1, x_coords, y_coords,
1024 counter);
1025 }
1026 return counter;
1027}
1028
1029/* find_physical_coords:
1030 * find the 'physical_coords' of the active-ancestor of 'node'
1031 */
1032void find_physical_coords(Hierarchy *hierarchy, int level, int node, float *x,
1033 float *y) {
1034 int active_level = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].active_level;
1035 while (active_level > level) {
1036 node = hierarchy->v2cv[level][node];
1037 level++;
1038 }
1039
1040 *x = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].physical_x_coord;
1041 *y = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].physical_y_coord;
1042}
1043
1044void
1045find_active_ancestor_info(Hierarchy * hierarchy, int level, int node, int *levell,int *nodee)
1046{
1047 int active_level = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].active_level;
1048 while (active_level > level) {
1049 node = hierarchy->v2cv[level][node];
1050 level++;
1051 }
1052
1053 *nodee = node;
1054 *levell = level;
1055}
1056
1057
1058
1059
1060/* find_old_physical_coords:
1061 * find the 'old_physical_coords' of the old active-ancestor of 'node'
1062 */
1063void find_old_physical_coords(Hierarchy *hierarchy, int level, int node,
1064 float *x, float *y) {
1065 int active_level = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].old_active_level;
1066 while (active_level > level) {
1067 node = hierarchy->v2cv[level][node];
1068 level++;
1069 }
1070
1071 *x = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].old_physical_x_coord;
1072 *y = LIST_GET(&hierarchy->geom_graphs, (size_t)level)[node].old_physical_y_coord;
1073}
1074
1076{
1077 int i;
1079 for (i=0; i<hierarchy->nlevels; i++) {
1080 graph = LIST_GET(&hierarchy->geom_graphs, (size_t)i);
1081 for (int j = 0; j < LIST_GET(&hierarchy->nvtxs, (size_t)i); j++) {
1082 graph->active_level = 0;
1083 graph++;
1084 }
1085 }
1086}
1087
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
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
static double dist(int dim, double *x, double *y)
double distance(double *x, int dim, int i, int j)
Definition general.c:102
void free(void *)
node NULL
Definition grammar.y:181
Agraph_t * graph(char *name)
Definition gv.cpp:34
static double findClosestActiveNode(Hierarchy *hierarchy, int node, int level, double x, double y, double closest_dist, int *closest_node, int *closest_node_level)
Definition hierarchy.c:842
void init_active_level(Hierarchy *hierarchy)
Definition hierarchy.c:1075
#define B
Definition hierarchy.c:122
void set_active_levels(Hierarchy *hierarchy, int *foci_nodes, int num_foci, levelparms_t *parms)
Definition hierarchy.c:739
static double unweighted_common_fraction(v_data *graph, int v, int u, float *v_vector)
Definition hierarchy.c:40
size_t extract_active_logical_coords(Hierarchy *hierarchy, int node, int level, double *x_coords, double *y_coords, size_t counter)
Definition hierarchy.c:968
void find_closest_active_node(Hierarchy *hierarchy, double x, double y, int *closest_fine_node)
Definition hierarchy.c:900
static void fill_neighbors_vec_unweighted(v_data *graph, int vtx, float *vtx_vec)
Definition hierarchy.c:78
static double dist_from_foci(ex_vtx_data *graph, int node, int *foci, int num_foci)
Definition hierarchy.c:719
static int find_leftmost_descendant(Hierarchy *hierarchy, int node, int level)
Definition hierarchy.c:886
Hierarchy * create_hierarchy(v_data *graph, int nvtxs, int nedges, ex_vtx_data *geom_graph, int ngeom_edges, bool dist2_limit)
Definition hierarchy.c:668
int set_active_physical_coords(Hierarchy *hierarchy, int node, int level, double *x_coords, double *y_coords, int counter)
Definition hierarchy.c:1002
void find_old_physical_coords(Hierarchy *hierarchy, int level, int node, float *x, float *y)
Definition hierarchy.c:1063
static void fill_neighbors_vec(v_data *graph, int vtx, float *vtx_vec)
Definition hierarchy.c:64
void find_active_ancestor_info(Hierarchy *hierarchy, int level, int node, int *levell, int *nodee)
Definition hierarchy.c:1045
static double ddist(ex_vtx_data *geom_graph, int v, int u)
Definition hierarchy.c:126
static void empty_neighbors_vec(v_data *graph, int vtx, float *vtx_vec)
Definition hierarchy.c:87
static int make_coarse_ex_graph(ex_vtx_data *graph, int nedges, ex_vtx_data **cgp, int cnvtxs, int *v2cv, int *cv2v)
Definition hierarchy.c:485
static ex_vtx_data * cpExGraph(ex_vtx_data *graph, int n, int nedges)
Definition hierarchy.c:647
#define A
Definition hierarchy.c:121
static void makev2cv(int *mflag, int nvtxs, int *v2cv, int *cv2v)
Definition hierarchy.c:315
static int make_coarse_graph(v_data *graph, int nedges, v_data **cgp, int cnvtxs, int *v2cv, int *cv2v)
Definition hierarchy.c:340
#define C
Definition hierarchy.c:123
static int maxmatch(v_data *graph, ex_vtx_data *geom_graph, int nvtxs, int *mflag, bool dist2_limit)
Definition hierarchy.c:136
static v_data * cpGraph(v_data *graph, int n, int nedges)
Definition hierarchy.c:615
static int dist3(v_data *graph, int node1, int node2)
Definition hierarchy.c:96
static void coarsen_match(v_data *graph, ex_vtx_data *geom_graph, int nvtxs, int nedges, int geom_nedges, v_data **cgraph, ex_vtx_data **cgeom_graph, int *cnp, int *cnedges, int *cgeom_nedges, int **v2cvp, int **cv2vp, bool dist2_limit)
Definition hierarchy.c:564
void find_physical_coords(Hierarchy *hierarchy, int level, int node, float *x, float *y)
Definition hierarchy.c:1032
int init_ex_graph(v_data *graph1, v_data *graph2, int n, double *x_coords, double *y_coords, ex_vtx_data **gp)
Definition hierarchy.c:915
type-generic dynamically expanding list
#define LIST_AT(list, index)
Definition list.h:210
#define LIST_APPEND(list,...)
Definition list.h:151
#define LIST_GET(list, index)
Definition list.h:197
#define neighbor(t, i, edim, elist)
Definition make_map.h:41
static int nedges
total no. of edges used in routing
Definition routespl.c:33
qsort with carried along context
static void sort_place(double *place, int *ordering, size_t size)
sort an array of doubles based on an list of their desired order
Definition sort.h:57
int ** cv2v
Definition hierarchy.h:51
int nlevels
Definition hierarchy.h:40
int ** v2cv
Definition hierarchy.h:46
float x_coord
Definition hierarchy.h:25
int active_level
Definition hierarchy.h:22
float y_coord
Definition hierarchy.h:26
int * edges
Definition hierarchy.h:20
int nedges
Definition sparsegraph.h:24
int * edges
Definition sparsegraph.h:25
float * ewgts
Definition sparsegraph.h:26
static parms_t parms
Definition tlayout.c:77