Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
embed_graph.c
Go to the documentation of this file.
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 <cgraph/alloc.h>
20#include <neatogen/dijkstra.h>
21#include <neatogen/bfs.h>
22#include <neatogen/kkutils.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <time.h>
27/* #include <math.h> */
28
29void embed_graph(vtx_data * graph, int n, int dim, DistType *** Coords,
30 int reweight_graph)
31{
32/* Compute 'dim'-dimensional high-dimensional embedding (HDE) for the 'n' nodes
33 The embedding is based on choosing 'dim' pivots, and associating each
34 coordinate with a unique pivot, assigning it to the graph-theoretic distances
35 of all nodes from the pivots
36*/
37
38 int i, j;
39 int node;
40 DistType *storage = gv_calloc(n * dim, sizeof(DistType));
41 DistType **coords = *Coords;
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 *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) {
55 }
56
57 /* select the first pivot */
58 node = rand() % n;
59
60 if (reweight_graph) {
61 dijkstra(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 dijkstra(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 */
101void 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}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define MIN(a, b)
Definition arith.h:28
void bfs(int vertex, vtx_data *graph, int n, DistType *dist)
Definition bfs.c:25
static void dijkstra(Dict_t *Q, Agraph_t *G, Agnode_t *n)
Definition dijkstra.c:188
void embed_graph(vtx_data *graph, int n, int dim, DistType ***Coords, int reweight_graph)
Definition embed_graph.c:29
void center_coordinate(DistType **coords, int n, int dim)
static double dist(int dim, double *x, double *y)
void free(void *)
Agraph_t * graph(char *name)
Definition gv.cpp:31
void compute_new_weights(vtx_data *graph, int n)
Definition kkutils.c:165
void restore_old_weights(vtx_data *graph, int n, float *old_weights)
Definition kkutils.c:196
int DistType
Definition sparsegraph.h:37