The algorithm operates by calculating the distance (e.g., Euclidean, cosine) between a query vector and every vector in a dataset, then selecting the 'k' vectors with the smallest distances. This exact search or brute-force method guarantees perfect accuracy but scales poorly (O(Nd) complexity) as dataset size (N) and dimensionality (d) increase. In agentic memory systems, k-NN provides the ground-truth benchmark for semantic search against which faster, approximate methods like HNSW are measured.
