Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
nearest_neighbor_graph.cpp
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#include "config.h"
12
13#include <sparse/general.h>
14#include <sparse/SparseMatrix.h>
17#include <vector>
18
19SparseMatrix nearest_neighbor_graph(int nPts, int num_neighbors,
20 const std::vector<double> &x) {
21 /* Gives a nearest neighbor graph of a list of dim-dimendional points. The result is a sparse matrix
22 of nPts x nPts, with num_neigbors entries per row.
23
24 nPts: number of points
25 num_neighbors: number of neighbors needed
26 dim: dimension == 4
27 x: nPts*dim vector. The i-th point is x[i*dim : i*dim + dim - 1]
28
29 */
31
32#ifdef HAVE_ANN
33 int nz;
34 int k = num_neighbors;
35
36 /* need to *2 as we do two sweeps of neighbors, so could have repeats */
37 std::vector<int> irn(nPts * k * 2);
38 std::vector<int> jcn(nPts * k * 2);
39 std::vector<double> val(nPts * k * 2);
40
41 nearest_neighbor_graph_ann(nPts, num_neighbors, x, nz, irn, jcn, val);
42
43 A = SparseMatrix_from_coordinate_arrays(nz, nPts, nPts, irn.data(),
44 jcn.data(), val.data(),
45 MATRIX_TYPE_REAL, sizeof(double));
46#else
47 (void)nPts;
48 (void)num_neighbors;
49 (void)x;
50#endif
51
52 return A;
53}
SparseMatrix SparseMatrix_from_coordinate_arrays(int nz, int m, int n, int *irn, int *jcn, void *val0, int type, size_t sz)
@ MATRIX_TYPE_REAL
#define A(n, t)
Definition expr.h:76
node NULL
Definition grammar.y:149
SparseMatrix nearest_neighbor_graph(int nPts, int num_neighbors, const std::vector< double > &x)
void nearest_neighbor_graph_ann(int nPts, int k, const std::vector< double > &x, int &nz0, std::vector< int > &irn, std::vector< int > &jcn, std::vector< double > &val)