00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "dijkstra.h"
00027 #include "bfs.h"
00028 #include "kkutils.h"
00029 #include "embed_graph.h"
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <time.h>
00033
00034
00035 void embed_graph(vtx_data * graph, int n, int dim, DistType *** Coords,
00036 int reweight_graph)
00037 {
00038
00039
00040
00041
00042
00043
00044 int i, j;
00045 int node;
00046 DistType *storage = N_GNEW(n * dim, DistType);
00047 DistType **coords = *Coords;
00048 DistType *dist = N_GNEW(n, DistType);
00049
00050 float *old_weights = graph[0].ewgts;
00051 Queue Q;
00052 DistType max_dist = 0;
00053
00054 if (coords != NULL) {
00055 free(coords[0]);
00056 free(coords);
00057 }
00058
00059
00060 *Coords = coords = N_GNEW(dim, DistType *);
00061 for (i = 0; i < dim; i++)
00062 coords[i] = storage + i * n;
00063
00064 if (reweight_graph) {
00065 compute_new_weights(graph, n);
00066 }
00067
00068
00069 node = rand() % n;
00070
00071 mkQueue(&Q, n);
00072 if (reweight_graph) {
00073 dijkstra(node, graph, n, coords[0]);
00074 } else {
00075 bfs(node, graph, n, coords[0], &Q);
00076 }
00077
00078 for (i = 0; i < n; i++) {
00079 dist[i] = coords[0][i];
00080 if (dist[i] > max_dist) {
00081 node = i;
00082 max_dist = dist[i];
00083 }
00084 }
00085
00086
00087 for (i = 1; i < dim; i++) {
00088 if (reweight_graph) {
00089 dijkstra(node, graph, n, coords[i]);
00090 } else {
00091 bfs(node, graph, n, coords[i], &Q);
00092 }
00093 max_dist = 0;
00094 for (j = 0; j < n; j++) {
00095 dist[j] = MIN(dist[j], coords[i][j]);
00096 if (dist[j] > max_dist) {
00097 node = j;
00098 max_dist = dist[j];
00099 }
00100 }
00101
00102 }
00103
00104 free(dist);
00105
00106 if (reweight_graph) {
00107 restore_old_weights(graph, n, old_weights);
00108 }
00109
00110 }
00111
00112
00113 void center_coordinate(DistType ** coords, int n, int dim)
00114 {
00115 int i, j;
00116 double sum, avg;
00117 for (i = 0; i < dim; i++) {
00118 sum = 0;
00119 for (j = 0; j < n; j++) {
00120 sum += coords[i][j];
00121 }
00122 avg = sum / n;
00123 for (j = 0; j < n; j++) {
00124 coords[i][j] -= (DistType) avg;
00125 }
00126 }
00127 }