Graphviz 16.1.3~dev.20261128.0221
Loading...
Searching...
No Matches
compute_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
11#include "config.h"
12
13#include <neatogen/digcola.h>
14#include <stddef.h>
15#include <util/alloc.h>
16#include <util/sort.h>
17#ifdef DIGCOLA
18#include <neatogen/kkutils.h>
19
20/*
21 * This function partitions the graph nodes into levels
22 * according to the minimizer of the hierarchy energy.
23 *
24 * To allow more flexibility we define a new level only
25 * when the hierarchy energy shows a significant jump
26 * (to compensate for noise).
27 * This is controlled by two parameters: 'abs_tol' and
28 * 'relative_tol'. The smaller these two are, the more
29 * levels we'll get.
30 * More specifically:
31 * We never consider gaps smaller than 'abs_tol'
32 * Additionally, we never consider gaps smaller than 'abs_tol'*<avg_gap>
33 *
34 * The output is an ordering of the nodes according to
35 * their levels, as follows:
36 * First level:
37 * ordering[0],ordering[1],...ordering[levels[0]-1]
38 * Second level:
39 * ordering[levels[0]],ordering[levels[0]+1],...ordering[levels[1]-1]
40 * ...
41 * Last level:
42 * ordering[levels[num_levels-1]],ordering[levels[num_levels-1]+1],...ordering[n-1]
43 *
44 * Hence, the nodes were partitioned into 'num_levels'+1
45 * levels.
46 *
47 * Please note that 'ordering[levels[i]]' contains the first node at level i+1,
48 * and not the last node of level i.
49 */
50int
51compute_hierarchy(vtx_data * graph, int n, double abs_tol,
52 double relative_tol, double *given_coords,
53 int **orderingp, int **levelsp, size_t *num_levelsp)
54{
55 double *y;
56 int i, rv=0;
57 int *ordering;
58 int *levels;
59 double tol; /* node 'i' precedes 'j' in hierarchy iff y[i]-y[j]>tol */
60 double hierarchy_span;
61
62 /* compute optimizer of hierarchy energy: 'y' */
63 if (given_coords) {
64 y = given_coords;
65 } else {
66 y = gv_calloc(n, sizeof(double));
67 if (compute_y_coords(graph, n, y, n)) {
68 rv = 1;
69 goto finish;
70 }
71 }
72
73 // sort nodes according to their y-ordering
74 *orderingp = ordering = gv_calloc(n, sizeof(int));
75 for (i = 0; i < n; i++) {
76 ordering[i] = i;
77 }
78 sort_place(y, ordering, (size_t)n);
79
80 /* compute tolerance
81 * take the maximum between 'abs_tol' and a fraction of the average gap
82 * controlled by 'relative_tol'
83 */
84 hierarchy_span = y[ordering[n - 1]] - y[ordering[0]];
85 tol = MAX(abs_tol, relative_tol * hierarchy_span / (n - 1));
86 /* 'hierarchy_span/(n-1)' - average gap between consecutive nodes */
87
88
89 /* count how many levels the hierarchy contains (a SINGLE_LINK clustering */
90 /* alternatively we could use COMPLETE_LINK clustering) */
91 size_t num_levels = 0;
92 for (i = 1; i < n; i++) {
93 if (y[ordering[i]] - y[ordering[i - 1]] > tol) {
94 num_levels++;
95 }
96 }
97 *num_levelsp = num_levels;
98 if (num_levels == 0) {
99 *levelsp = levels = gv_calloc(1, sizeof(int));
100 levels[0] = n;
101 } else {
102 int count = 0;
103 *levelsp = levels = gv_calloc(num_levels, sizeof(int));
104 for (i = 1; i < n; i++) {
105 if (y[ordering[i]] - y[ordering[i - 1]] > tol) {
106 levels[count++] = i;
107 }
108 }
109 }
110finish:
111 if (!given_coords) {
112 free(y);
113 }
114
115 return rv;
116}
117
118#endif /* DIGCOLA */
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define MAX(a, b)
Definition arith.h:33
void free(void *)
Agraph_t * graph(char *name)
Definition gv.cpp:34
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
static const double tol