Graphviz 13.0.0~dev.20241220.2304
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 <sparse/general.h>
12#include <sparse/SparseMatrix.h>
15#include <vector>
16
17SparseMatrix nearest_neighbor_graph(int nPts, int num_neighbors,
18 const std::vector<double> &x) {
19 /* Gives a nearest neighbor graph of a list of dim-dimendional points. The result is a sparse matrix
20 of nPts x nPts, with num_neigbors entries per row.
21
22 nPts: number of points
23 num_neighbors: number of neighbors needed
24 dim: dimension == 4
25 x: nPts*dim vector. The i-th point is x[i*dim : i*dim + dim - 1]
26
27 */
28 int nz;
29 int k = num_neighbors;
30
31 /* need to *2 as we do two sweeps of neighbors, so could have repeats */
32 std::vector<int> irn(nPts * k * 2);
33 std::vector<int> jcn(nPts * k * 2);
34 std::vector<double> val(nPts * k * 2);
35
36 nearest_neighbor_graph_ann(nPts, num_neighbors, x, nz, irn, jcn, val);
37
38 return SparseMatrix_from_coordinate_arrays(nz, nPts, nPts, irn.data(),
39 jcn.data(), val.data(),
40 MATRIX_TYPE_REAL, sizeof(double));
41}
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
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)