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

Go to the documentation of this file.
00001 /* $Id: opt_arrangement.c,v 1.2 2006/04/28 20:33:44 ellson Exp $ $Revision: 1.2 $ */
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 #include "digcola.h"
00018 #ifdef DIGCOLA
00019 #include "matrix_ops.h"
00020 #include "conjgrad.h"
00021 
00022 static void
00023 construct_b(vtx_data* graph, int n, double *b) 
00024 {
00025         /* construct a vector - b s.t. -b[i]=\sum_j -w_{ij}*\delta_{ij}
00026          * (the "balance vector")
00027          * Note that we build -b and not b, since our matrix is not the 
00028          * real laplacian L, but its negation: -L. 
00029          * So instead of solving Lx=b, we will solve -Lx=-b
00030      */
00031         int i,j;
00032 
00033         double b_i = 0;
00034         
00035         for (i=0; i<n; i++) {
00036                 b_i = 0;
00037                 if (graph[0].edists==NULL) {
00038                         continue;
00039                 }
00040                 for (j=1; j<graph[i].nedges; j++) { /* skip the self loop */
00041                         b_i += graph[i].ewgts[j]*graph[i].edists[j];
00042                 }
00043                 b[i] = b_i;
00044         }
00045 }
00046 
00047 #define hierarchy_cg_tol 1e-3
00048 
00049 void 
00050 compute_y_coords(vtx_data* graph, int n, double* y_coords, int max_iterations)
00051 {
00052         /* Find y coords of a directed graph by solving L*x = b */
00053         int i,j;        
00054         double* b = N_NEW(n, double);
00055     double tol = hierarchy_cg_tol;
00056         int nedges = 0;
00057         float* uniform_weights;
00058         float* old_ewgts = graph[0].ewgts;
00059         
00060         construct_b(graph, n, b);
00061 
00062         init_vec_orth1(n, y_coords);
00063 
00064         for (i=0; i<n; i++) {
00065                 nedges += graph[i].nedges;
00066         }
00067         
00068         /* replace original edge weights (which are lengths) with uniform weights */
00069         /* for computing the optimal arrangement */
00070         uniform_weights = N_GNEW(nedges,float);
00071         for (i=0; i<n; i++) {
00072                 graph[i].ewgts = uniform_weights;
00073                 uniform_weights[0] = (float)-(graph[i].nedges-1);
00074                 for (j=1; j<graph[i].nedges; j++) {
00075                         uniform_weights[j] = 1;
00076                 }
00077                 uniform_weights += graph[i].nedges;
00078         }
00079         
00080         conjugate_gradient(graph, y_coords, b, n, tol, max_iterations);
00081 
00082         /* restore original edge weights */
00083         free (graph[0].ewgts);  
00084         for (i=0; i<n; i++) {
00085                 graph[i].ewgts = old_ewgts;
00086                 old_ewgts += graph[i].nedges;
00087         }
00088 
00089         free(b);
00090 }
00091 
00092 #endif /* DIGCOLA */
00093 

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