@@ -320,6 +320,50 @@ template <typename vamana_t> class VamanaIndex {
320
320
321
321
}
322
322
323
+ GraphNode<T> findMedoid (const Graph<T>& graph) {
324
+ // Get the total number of nodes in the graph
325
+ const int node_count = graph.getNodesCount ();
326
+
327
+ // Initialize a variable to track the minimum average distance found so far
328
+ float min_average_distance = std::numeric_limits<float >::max ();
329
+
330
+ // Pointer to store the node with the minimum average distance (medoid candidate)
331
+ GraphNode<T>* medoid_node = nullptr ;
332
+
333
+ // Iterate over all nodes to find the medoid
334
+ // A medoid is defined as the node with the minimum sum of distances to all other nodes
335
+ for (int i = 0 ; i < node_count; ++i) {
336
+ // Get the current node as a candidate for the medoid
337
+ GraphNode<T>* candidate_node = graph.getNode (i);
338
+
339
+ // Variable to accumulate the total distance from this candidate node to all other nodes
340
+ float total_distance = 0.0 ;
341
+
342
+ // Calculate the sum of distances from this candidate node to every other node
343
+ for (int j = 0 ; j < node_count; ++j) {
344
+ // Skip distance calculation if the nodes are the same (distance to itself is 0)
345
+ if (i != j) {
346
+ total_distance += euclideanDistance (candidate_node->getData (), graph.getNode (j)->getData ());
347
+ }
348
+ }
349
+
350
+ // Compute the average distance for this candidate node
351
+ // This is done by dividing the total distance by the number of other nodes
352
+ float average_distance = total_distance / (node_count - 1 );
353
+
354
+ // Check if this candidate node has a smaller average distance than the current minimum
355
+ // If so, update the minimum distance and set this node as the current medoid candidate
356
+ if (average_distance < min_average_distance) {
357
+ min_average_distance = average_distance;
358
+ medoid_node = candidate_node;
359
+ }
360
+ }
361
+
362
+ // Return the node with the smallest average distance, which is the medoid
363
+ // The medoid represents the node that minimizes the total distance to all other nodes
364
+ return *medoid_node;
365
+ }
366
+
323
367
/* *
324
368
* @brief tests a specific Vamana index and prints its accuracy. Specifically this method is used to evaluate
325
369
* a Vamana Index Graph, by searching inside the graph for the nearest neighbors of a given query point, and
0 commit comments