33 #define LT(p,q) ((p).dist < (q).dist)
34 #define EQ(p,q) ((p).dist == (q).dist)
64 if (s->top>=s->max_size) { \
66 s->data = (Pair*) realloc(s->data, s->max_size*sizeof(Pair)); \
68 s->data[s->top++] = x; \
71 #define pop(s,x) ((s->top==0) ? FALSE : (s->top--, x = s->data[s->top], TRUE))
73 #define read_top(h,x) ((s->top==0) ? FALSE : (x = s->data[s->top-1], TRUE))
75 #define sub(h,i) (h->data[i])
86 #define left(i) (2*(i))
87 #define right(i) (2*(i)+1)
88 #define parent(i) ((i)/2)
89 #define insideHeap(h,i) ((i)<h->heapSize)
90 #define greaterPriority(h,i,j) \
91 (LT(h->data[i],h->data[j]) || ((EQ(h->data[i],h->data[j])) && (rand()%2)))
93 #define exchange(h,i,j) {Pair temp; \
95 h->data[i]=h->data[j]; \
98 #define assign(h,i,j) {h->data[i]=h->data[j]}
100 static void heapify(
PairHeap * h,
int i)
121 static void mkHeap(
PairHeap * h,
int size)
134 static void initHeap(
PairHeap * h,
double *place,
int *ordering,
int n)
151 for (i = 0; i < n - 1; i++) {
152 edge.
left = ordering[i];
153 edge.
right = ordering[i + 1];
154 edge.
dist = place[ordering[i + 1]] - place[ordering[i]];
157 for (j = (n - 1) / 2; j >= 0; j--) {
206 find_closest_pairs(
double *place,
int n,
int num_pairs,
217 int *ordering =
N_GNEW(n,
int);
218 int *inv_ordering =
N_GNEW(n,
int);
220 for (i = 0; i < n; i++) {
224 for (i = 0; i < n; i++) {
225 inv_ordering[ordering[i]] = i;
229 initHeap(&heap, place, ordering, n);
232 for (i = 1; i < n; i++) {
233 left[ordering[i]] = ordering[i - 1];
235 for (i = 0; i < n - 1; i++) {
236 right[ordering[i]] = ordering[i + 1];
240 for (i = 0; i < num_pairs; i++) {
245 if (!extractMax(&heap, &pair)) {
248 push(pairs_stack, pair);
250 left_index = inv_ordering[pair.
left];
251 right_index = inv_ordering[pair.
right];
252 if (left_index > 0) {
253 neighbor = ordering[left_index - 1];
254 if (inv_ordering[right[neighbor]] < right_index) {
256 new_pair.left = neighbor;
257 new_pair.right = pair.
right;
258 new_pair.dist = place[pair.
right] - place[neighbor];
259 insert(&heap, new_pair);
260 right[neighbor] = pair.
right;
261 left[pair.
right] = neighbor;
264 if (right_index < n - 1) {
265 neighbor = ordering[right_index + 1];
266 if (inv_ordering[left[neighbor]] > left_index) {
268 new_pair.left = pair.
left;
269 new_pair.right = neighbor;
270 new_pair.dist = place[neighbor] - place[pair.
left];
271 insert(&heap, new_pair);
272 left[neighbor] = pair.
left;
273 right[pair.
left] = neighbor;
287 for (i = 0; i < graph[u].
nedges; i++) {
288 if (graph[u].edges[i] == v) {
296 if (graph[0].ewgts !=
NULL) {
310 int *degrees =
N_GNEW(n,
int);
311 int top = edges_stack->
top;
312 int new_nedges = 2 * top + n;
314 int *edges =
N_GNEW(new_nedges,
int);
315 float *weights =
N_GNEW(new_nedges,
float);
317 for (i = 0; i < n; i++) {
320 for (i = 0; i <
top; i++) {
321 pair =
sub(edges_stack, i);
322 degrees[pair.
left]++;
323 degrees[pair.
right]++;
327 for (i = 0; i < new_nedges; i++) {
332 for (i = 0; i < n; i++) {
334 new_graph[i].
ewgts = weights;
336 new_graph[i].styles =
NULL;
338 new_graph[i].
edges = edges;
341 weights += degrees[i];
348 while (
pop(edges_stack, pair)) {
358 initStack(&pairs_stack, num_pairs);
359 find_closest_pairs(place, n, num_pairs, &pairs_stack);
360 construct_graph(n, &pairs_stack, graph);
#define exchange(h, i, j)
void closest_pairs2graph(double *place, int n, int num_pairs, vtx_data **graph)
void add_edge(edgelist *list, Agedge_t *e)
void freeStack(nstack_t *s)
void quicksort_place(double *place, int *ordering, int first, int last)
Agraph_t * graph(char *name)
#define greaterPriority(h, i, j)
Agedge_t * edge(Agraph_t *g, Agnode_t *t, Agnode_t *h)