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

◆ minimum_spanning_tree()

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

Compute minimum spanning tree (Prim's algorithm)

Parameters
pointsDxN matrix where each column is a point
Returns
Edge list forming MST

Returns exactly (n-1) edges forming tree of minimum total length that connects all points. Undirected acyclic graph.

Complexity: O(n² log n) with priority queue

Definition at line 116 of file ProximityGraphs.cpp.

117{
118 Eigen::Index n = points.cols();
119 if (n < 2) {
120 return {};
121 }
122
123 EdgeList mst_edges;
124 mst_edges.reserve(n - 1);
125
126 std::vector<bool> in_mst(n, false);
127 std::priority_queue<Edge, std::vector<Edge>, std::greater<>> pq;
128
129 in_mst[0] = true;
130
131 for (Eigen::Index j = 1; j < n; ++j) {
132 double dist = distance(points.col(0), points.col(j));
133 pq.push({ 0, static_cast<size_t>(j), dist });
134 }
135
136 while (!pq.empty() && mst_edges.size() < static_cast<size_t>(n - 1)) {
137 Edge e = pq.top();
138 pq.pop();
139
140 if (in_mst[e.b])
141 continue;
142
143 mst_edges.emplace_back(e.a, e.b);
144 in_mst[e.b] = true;
145
146 for (Eigen::Index j = 0; j < n; ++j) {
147 if (!in_mst[j]) {
148 double dist = distance(points.col(e.b), points.col(j));
149 pq.push({ e.b, static_cast<size_t>(j), dist });
150 }
151 }
152 }
153
154 MF_DEBUG(Journal::Component::Kinesis, Journal::Context::Runtime,
155 "minimum_spanning_tree: {} points, generated {} edges",
156 n, mst_edges.size());
157
158 return mst_edges;
159}
#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: