/misc/src/release/graphviz-2.18-1/src/graphviz-2.18/lib/neatogen/embed_graph.c

Go to the documentation of this file.
00001 /* $Id: embed_graph.c,v 1.1.1.1 2004/12/23 04:05:11 ellson Exp $ $Revision: 1.1.1.1 $ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /**********************************************************
00005 *      This software is part of the graphviz package      *
00006 *                http://www.graphviz.org/                 *
00007 *                                                         *
00008 *            Copyright (c) 1994-2004 AT&T Corp.           *
00009 *                and is licensed under the                *
00010 *            Common Public License, Version 1.0           *
00011 *                      by AT&T Corp.                      *
00012 *                                                         *
00013 *        Information and Software Systems Research        *
00014 *              AT&T Research, Florham Park NJ             *
00015 **********************************************************/
00016 
00017 
00018 /************************************************
00019 
00020         Functions for computing the high-dimensional
00021         embedding and the PCA projection.
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 /* #include <math.h> */
00034 
00035 void embed_graph(vtx_data * graph, int n, int dim, DistType *** Coords,
00036                  int reweight_graph)
00037 {
00038 /* Compute 'dim'-dimensional high-dimensional embedding (HDE) for the 'n' nodes
00039   The embedding is based on chossing 'dim' pivots, and associating each
00040   coordinate with a unique pivot, assigning it to the graph-theoretic distances 
00041   of all nodes from the pivots
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);       /* this vector stores  the distances of
00049                                                    each nodes to the selected "pivots" */
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     /* this matrix stores the distance between each node and each "pivot" */
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     /* select the first pivot */
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     /* select other dim-1 nodes as pivots */
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  /* Make each axis centered around 0 */
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 }

Generated on Mon Mar 31 19:03:27 2008 for Graphviz by  doxygen 1.5.1