MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ nearest_neighbor_graph()

EdgeList MayaFlux::Kinesis::nearest_neighbor_graph ( const Eigen::MatrixXd &  points)

Compute nearest neighbor graph.

Parameters
pointsDxN matrix where each column is a point
Returns
Edge list of nearest neighbor edges

Connects each point to its single nearest neighbor. Directed graph: point i connects to nearest neighbor j, but j may connect to a different point k.

Complexity: O(n²) brute force

Definition at line 206 of file ProximityGraphs.cpp.

207{
208 Eigen::Index n = points.cols();
209 if (n < 2) {
210 return {};
211 }
212
213 EdgeList edges;
214 edges.reserve(n);
215
216 for (Eigen::Index i = 0; i < n; ++i) {
217 double min_dist_sq = std::numeric_limits<double>::max();
218 size_t nearest = i;
219
220 for (Eigen::Index j = 0; j < n; ++j) {
221 if (i == j)
222 continue;
223
224 double dist_sq = distance_squared(points.col(i), points.col(j));
225 if (dist_sq < min_dist_sq) {
226 min_dist_sq = dist_sq;
227 nearest = static_cast<size_t>(j);
228 }
229 }
230
231 if (nearest != static_cast<size_t>(i)) {
232 edges.emplace_back(static_cast<size_t>(i), nearest);
233 }
234 }
235
236 MF_DEBUG(Journal::Component::Kinesis, Journal::Context::Runtime,
237 "nearest_neighbor_graph: {} points, generated {} edges", n, edges.size());
238
239 return edges;
240}
#define MF_DEBUG(comp, ctx,...)

References MayaFlux::Journal::Kinesis, MF_DEBUG, and MayaFlux::Journal::Runtime.

Referenced by generate_proximity_graph().

+ Here is the caller graph for this function: