nearest_neighbor_graph.cpp 1.6 KB

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