VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 09: Heaps and top-k

Lesson 9.10 of 12 in this module Unrolled Pro

k Nearest Neighbours by Distance

Classify a query by a majority vote of its k nearest labelled examples. From sorting every training point by distance for each query (O(n log n) per query), to a max-heap of size k (O(n log k)), to abandoning a distance as soon as its partial sum exceeds the current k-th best, and the expansion ‖q − x‖² = ‖q‖² + ‖x‖² − 2q·x that turns many queries into one matrix product. Then distance-weighted votes, choosing k by leave-one-out, a k-d tree that skips whole regions, and approximate indexes for embeddings.

about 15 min 5 Python examples 2 quick checks

Problem. You have n labelled training points in d dimensions. For a query point, find its k nearest training points (Euclidean distance) and predict the majority label among them. Do this for many queries.

text
train = (1, 1) A, (2, 1) A, (1, 2) A, (6, 5) B, (7, 7) B, (6, 7) B
query (2, 2), k = 3   →   nearest: (2, 1), (1, 2), (1, 1)   →   A
query (6, 6), k = 3   →   B

k-NN has no training step: all the work happens at query time, so the cost per query is the algorithm. Lesson 9.4 found the k closest points once; here the same selection runs for every query, which changes where the time goes.

ML context. k-NN is a strong baseline for classification on embeddings (label a new text by its nearest labelled neighbours), the core of retrieval-augmented generation (retrieve the k nearest chunks), duplicate detection, and evaluation of embedding quality (k-NN accuracy on held-out data). Each deployment eventually faces the same question: exact scan, or an index?


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "k Nearest Neighbours by Distance"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee