Debian Codegraph @

lib/neatogen/kkutils.c

raw
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 "config.h"
12
13 #include <neatogen/bfs.h>
14 #include <neatogen/dijkstra.h>
15 #include <neatogen/kkutils.h>
16 #include <stdlib.h>
17 #include <math.h>
18 #include <util/alloc.h>
19 #include <util/sort.h>
20
21 size_t common_neighbors(vtx_data *graph, int u, int *v_vector) {
22 // count number of common neighbors of 'v_vector' and 'u'
23 int neighbor;
24 size_t num_shared_neighbors = 0;
25 for (size_t j = 1; j < graph[u].nedges; j++) {
26 neighbor = graph[u].edges[j];
27 if (v_vector[neighbor] > 0) {
28 // a shared neighbor
29 num_shared_neighbors++;
30 }
31 }
32 return num_shared_neighbors;
33 }
34 void fill_neighbors_vec_unweighted(vtx_data * graph, int vtx, int *vtx_vec)
35 {
36 /* a node is NOT a neighbor of itself! */
37 /* unlike the other version of this function */
38 for (size_t j = 1; j < graph[vtx].nedges; j++) {
39 vtx_vec[graph[vtx].edges[j]] = 1;
40 }
41 }
42
43 void empty_neighbors_vec(vtx_data * graph, int vtx, int *vtx_vec)
44 {
45 /* a node is NOT a neighbor of itself! */
46 /* unlike the other version of this function */
47 for (size_t j = 1; j < graph[vtx].nedges; j++) {
48 vtx_vec[graph[vtx].edges[j]] = 0;
49 }
50 }
51
52 /// assumes the graph has weights
53 static DistType **compute_apsp_dijkstra(vtx_data * graph, int n)
54 {
55 int i;
56
57 DistType *storage = gv_calloc((size_t)(n * n), sizeof(DistType));
58 DistType **dij = gv_calloc(n, sizeof(DistType*));
59 for (i = 0; i < n; i++)
60 dij[i] = storage + i * n;
61
62 for (i = 0; i < n; i++) {
63 ngdijkstra(i, graph, n, dij[i]);
64 }
65 return dij;
66 }
67
68 static DistType **compute_apsp_simple(vtx_data * graph, int n)
69 {
70 /* compute all pairs shortest path */
71 /* for unweighted graph */
72 int i;
73 DistType *storage = gv_calloc((size_t)(n * n), sizeof(DistType));
74
75 DistType **dij = gv_calloc(n, sizeof(DistType*));
76 for (i = 0; i < n; i++) {
77 dij[i] = storage + i * n;
78 }
79 for (i = 0; i < n; i++) {
80 bfs(i, graph, n, dij[i]);
81 }
82 return dij;
83 }
84
85 DistType **compute_apsp(vtx_data * graph, int n)
86 {
87 if (graph->ewgts)
88 return compute_apsp_dijkstra(graph, n);
89 else
90 return compute_apsp_simple(graph, n);
91 }
92
93 DistType **compute_apsp_artificial_weights(vtx_data *graph, int n) {
94 DistType **Dij;
95 /* compute all-pairs-shortest-path-length while weighting the graph */
96 /* so high-degree nodes are distantly located */
97
98 float *old_weights = graph[0].ewgts;
99
100 compute_new_weights(graph, n);
101 Dij = compute_apsp_dijkstra(graph, n);
102 restore_old_weights(graph, n, old_weights);
103 return Dij;
104 }
105
106
107 /**********************/
108 /* */
109 /* Quick Sort */
110 /* */
111 /**********************/
112
113 double distance_kD(double **coords, int dim, int i, int j)
114 {
115 /* compute a k-D Euclidean distance between 'coords[*][i]' and 'coords[*][j]' */
116 double sum = 0;
117 int k;
118 for (k = 0; k < dim; k++) {
119 sum +=
120 (coords[k][i] - coords[k][j]) * (coords[k][i] - coords[k][j]);
121 }
122 return sqrt(sum);
123 }
124
125 static int fcmpf(const void *a, const void *b, void *context) {
126 const int *ip1 = a;
127 const int *ip2 = b;
128 float *fvals = context;
129 float d1 = fvals[*ip1];
130 float d2 = fvals[*ip2];
131 if (d1 < d2) {
132 return -1;
133 }
134 if (d1 > d2) {
135 return 1;
136 }
137 return 0;
138 }
139
140 void quicksort_placef(float *place, int *ordering, int first, int last)
141 {
142 if (first < last) {
143 gv_sort(ordering + first, last - first + 1, sizeof(ordering[0]), fcmpf, place);
144 }
145 }
146
147 static int cmp(const void *a, const void *b, void *context) {
148 const int *x = a;
149 const int *y = b;
150 const double *place = context;
151
152 if (place[*x] < place[*y]) {
153 return -1;
154 }
155 if (place[*x] > place[*y]) {
156 return 1;
157 }
158 return 0;
159 }
160
161 void quicksort_place(double *place, int *ordering, int size) {
162 gv_sort(ordering, size, sizeof(ordering[0]), cmp, place);
163 }
164
165 void compute_new_weights(vtx_data * graph, int n)
166 {
167 /* Reweight graph so that high degree nodes will be separated */
168
169 int i;
170 size_t nedges = 0;
171 int *vtx_vec = gv_calloc(n, sizeof(int));
172 size_t deg_i, deg_j;
173 int neighbor;
174
175 for (i = 0; i < n; i++) {
176 nedges += graph[i].nedges;
177 }
178 float *weights = gv_calloc(nedges, sizeof(float));
179
180 for (i = 0; i < n; i++) {
181 graph[i].ewgts = weights;
182 fill_neighbors_vec_unweighted(graph, i, vtx_vec);
183 deg_i = graph[i].nedges - 1;
184 for (size_t j = 1; j <= deg_i; j++) {
185 neighbor = graph[i].edges[j];
186 deg_j = graph[neighbor].nedges - 1;
187 weights[j] =
188 (float)(deg_i + deg_j - 2 * common_neighbors(graph, neighbor, vtx_vec));
189 }
190 empty_neighbors_vec(graph, i, vtx_vec);
191 weights += graph[i].nedges;
192 }
193 free(vtx_vec);
194 }
195
196 void restore_old_weights(vtx_data * graph, int n, float *old_weights)
197 {
198 int i;
199 free(graph[0].ewgts);
200 graph[0].ewgts = NULL;
201 if (old_weights != NULL) {
202 for (i = 0; i < n; i++) {
203 graph[i].ewgts = old_weights;
204 old_weights += graph[i].nedges;
205 }
206 }
207 }