Fix bug in cover tree KNN algorithm

Prior to this change, the find function implementation for the
CoverTree class could have potentially returned the wrong result
in cases where there were multiple points in the dataset
equidistant from p. For example, the current test passed for k=3
but failed to produce the correct result for k=4 (it claimed that
3, 4, 5, and 7 were the 4 closest points to 5 in the dataset
rather than 3, 4, 5, and 6). Sorting the neighbors vector before
collecting the first k values from it resolved this issue.
This commit is contained in:
Kiran Eiden
2022-01-02 19:46:21 -08:00
parent 12c102d02b
commit f93286ffbd
+2 -1
View File
@@ -179,7 +179,8 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
} }
} }
} }
neighbors.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
Ok(neighbors.into_iter().take(k).collect()) Ok(neighbors.into_iter().take(k).collect())
} }