Debian Codegraph @

lib/mingle/nearest_neighbor_graph.cpp

raw
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>
15 #include <mingle/nearest_neighbor_graph_ann.h>
16 #include <mingle/nearest_neighbor_graph.h>
17 #include <vector>
18
19 SparseMatrix 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 */
30 int nz;
31 int k = num_neighbors;
32
33 /* need to *2 as we do two sweeps of neighbors, so could have repeats */
34 std::vector<int> irn(nPts * k * 2);
35 std::vector<int> jcn(nPts * k * 2);
36 std::vector<double> val(nPts * k * 2);
37
38 nearest_neighbor_graph_ann(nPts, num_neighbors, x, nz, irn, jcn, val);
39
40 return SparseMatrix_from_coordinate_arrays(nz, nPts, nPts, irn.data(),
41 jcn.data(), val.data(),
42 MATRIX_TYPE_REAL, sizeof(double));
43 }