| 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 |
|
| 12 |
/************************************************ |
| 13 |
|
| 14 |
Functions for computing the high-dimensional |
| 15 |
embedding and the PCA projection. |
| 16 |
|
| 17 |
************************************************/ |
| 18 |
|
| 19 |
#include "config.h" |
| 20 |
|
| 21 |
#include <neatogen/dijkstra.h> |
| 22 |
#include <neatogen/bfs.h> |
| 23 |
#include <neatogen/kkutils.h> |
| 24 |
#include <neatogen/embed_graph.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <time.h> |
| 28 |
#include <util/alloc.h> |
| 29 |
|
| 30 |
void embed_graph(vtx_data * graph, int n, int dim, DistType *** Coords, |
| 31 |
int reweight_graph) |
| 32 |
{ |
| 33 |
/* Compute 'dim'-dimensional high-dimensional embedding (HDE) for the 'n' nodes |
| 34 |
The embedding is based on choosing 'dim' pivots, and associating each |
| 35 |
coordinate with a unique pivot, assigning it to the graph-theoretic distances |
| 36 |
of all nodes from the pivots |
| 37 |
*/ |
| 38 |
|
| 39 |
int i, j; |
| 40 |
int node; |
| 41 |
DistType *storage = gv_calloc(n * dim, sizeof(DistType)); |
| 42 |
DistType *dist = gv_calloc(n, sizeof(DistType)); // this vector stores the |
| 43 |
// distances of each nodes |
| 44 |
// to the selected “pivots” |
| 45 |
float *old_weights = graph[0].ewgts; |
| 46 |
DistType max_dist = 0; |
| 47 |
|
| 48 |
/* this matrix stores the distance between each node and each "pivot" */ |
| 49 |
DistType **coords = *Coords = gv_calloc(dim, sizeof(DistType *)); |
| 50 |
for (i = 0; i < dim; i++) |
| 51 |
coords[i] = storage + i * n; |
| 52 |
|
| 53 |
if (reweight_graph) { |
| 54 |
compute_new_weights(graph, n); |
| 55 |
} |
| 56 |
|
| 57 |
/* select the first pivot */ |
| 58 |
node = rand() % n; |
| 59 |
|
| 60 |
if (reweight_graph) { |
| 61 |
ngdijkstra(node, graph, n, coords[0]); |
| 62 |
} else { |
| 63 |
bfs(node, graph, n, coords[0]); |
| 64 |
} |
| 65 |
|
| 66 |
for (i = 0; i < n; i++) { |
| 67 |
dist[i] = coords[0][i]; |
| 68 |
if (dist[i] > max_dist) { |
| 69 |
node = i; |
| 70 |
max_dist = dist[i]; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/* select other dim-1 nodes as pivots */ |
| 75 |
for (i = 1; i < dim; i++) { |
| 76 |
if (reweight_graph) { |
| 77 |
ngdijkstra(node, graph, n, coords[i]); |
| 78 |
} else { |
| 79 |
bfs(node, graph, n, coords[i]); |
| 80 |
} |
| 81 |
max_dist = 0; |
| 82 |
for (j = 0; j < n; j++) { |
| 83 |
dist[j] = MIN(dist[j], coords[i][j]); |
| 84 |
if (dist[j] > max_dist) { |
| 85 |
node = j; |
| 86 |
max_dist = dist[j]; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
free(dist); |
| 93 |
|
| 94 |
if (reweight_graph) { |
| 95 |
restore_old_weights(graph, n, old_weights); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/* Make each axis centered around 0 */ |
| 101 |
void center_coordinate(DistType ** coords, int n, int dim) |
| 102 |
{ |
| 103 |
int i, j; |
| 104 |
double sum, avg; |
| 105 |
for (i = 0; i < dim; i++) { |
| 106 |
sum = 0; |
| 107 |
for (j = 0; j < n; j++) { |
| 108 |
sum += coords[i][j]; |
| 109 |
} |
| 110 |
avg = sum / n; |
| 111 |
for (j = 0; j < n; j++) { |
| 112 |
coords[i][j] -= (DistType) avg; |
| 113 |
} |
| 114 |
} |
| 115 |
} |