Sparse Mutual k-NN Affinity Graph

Quick Overview

Build a sparse mutual k-nearest-neighbor graph with RBF edge weights from a NumPy array and return it as a SciPy CSR matrix. It tests neighbor selection with a tie rule, symmetric graph construction, input validation and how affinity graphs feed spectral clustering.

Sparse Mutual k-NN Affinity Graph

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `mutual_knn_affinity(X, k, gamma=1.0)` for an `(n, d)` array `X`. Keep an edge `(i, j)` only when each point is among the other's `k` nearest neighbours (self excluded). Return an n × n `scipy.sparse` CSR matrix with weight `exp(-gamma * ||x_i - x_j||²)` on kept edges and `0` elsewhere. Examples: ```python X = [[0.0], [1.0], [3.0], [7.0]] W = mutual_knn_affinity(X, k=1, gamma=0.5) # nearest neighbours: 0->1, 1->0, 2->1, 3->2, so only 0<->1 is mutual # dense W: W[0, 1] = W[1, 0] = exp(-0.5), all other entries 0 X = [[0.0], [1.0], [2.0]] W = mutual_knn_affinity(X, k=1, gamma=1.0) # point 1 is equally far from 0 and 2; the tie goes to index 0 # nearest neighbours: 0->1, 1->0, 2->1, so only 0<->1 is mutual # dense W: W[0, 1] = W[1, 0] = exp(-1), all other entries 0 ``` ```hint Directed first Build the plain "j is among i's k nearest" relation first, then ask which operation on that relation leaves only the pairs that agree in both directions. ``` ```hint Self and ties Think about how to keep a point out of its own neighbour list, and which sort keeps the lowest index first among equal distances. ``` ### Constraints & Assumptions - Distances are squared Euclidean. Distance ties are broken by lowest index. - The result is symmetric with a zero diagonal and format `"csr"`. - Raise `ValueError` if `X` is not 2-D, if `k` is not an integer or is outside `1 <= k <= n - 1`, or if `gamma <= 0`. - `n` is at most a few hundred, so a dense `O(n²)` distance computation is acceptable. ### What a Strong Answer Covers - Validation of the shape of `X`, the type and range of `k`, and the sign of `gamma`. - Neighbour selection that excludes the point itself and follows the lowest-index tie rule. - A mutual relation that makes the result symmetric with a zero diagonal by construction. - RBF weights on kept edges only, returned in CSR format. - The time and memory cost, and which step limits n. ### Follow-up Questions - How would you scale this to n = 10⁶ points? - Mutual k-NN can leave points isolated. Why does that matter for spectral clustering? - How would you choose `gamma` without tuning by hand?

Overview: Build a sparse mutual k-nearest-neighbor graph with RBF edge weights from a NumPy array and return it as a SciPy CSR matrix. It tests neighbor selection with a tie rule, symmetric graph construction, input validation and how affinity graphs feed spectral clustering.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement mutual_knn_affinity(X, k, gamma=1.0) for an (n, d) array X. Keep an edge (i, j) only when each point is among the other's k nearest neighbours (self excluded). Return an n × n scipy.sparse CSR matrix with weight exp(-gamma * ||x_i - x_j||²) on kept edges and 0 elsewhere.

Examples:

X = [[0.0], [1.0], [3.0], [7.0]]
W = mutual_knn_affinity(X, k=1, gamma=0.5)
# nearest neighbours: 0->1, 1->0, 2->1, 3->2, so only 0<->1 is mutual
# dense W: W[0, 1] = W[1, 0] = exp(-0.5), all other entries 0

X = [[0.0], [1.0], [2.0]]
W = mutual_knn_affinity(X, k=1, gamma=1.0)
# point 1 is equally far from 0 and 2; the tie goes to index 0
# nearest neighbours: 0->1, 1->0, 2->1, so only 0<->1 is mutual
# dense W: W[0, 1] = W[1, 0] = exp(-1), all other entries 0

Constraints & Assumptions

  • Distances are squared Euclidean. Distance ties are broken by lowest index.
  • The result is symmetric with a zero diagonal and format "csr" .
  • Raise ValueError if X is not 2-D, if k is not an integer or is outside 1 <= k <= n - 1 , or if gamma <= 0 .
  • n is at most a few hundred, so a dense O(n²) distance computation is acceptable.

What a Strong Answer Covers Guidance

  • Validation of the shape of X , the type and range of k , and the sign of gamma .
  • Neighbour selection that excludes the point itself and follows the lowest-index tie rule.
  • A mutual relation that makes the result symmetric with a zero diagonal by construction.
  • RBF weights on kept edges only, returned in CSR format.
  • The time and memory cost, and which step limits n.

Follow-up Questions Guidance

  • How would you scale this to n = 10⁶ points?
  • Mutual k-NN can leave points isolated. Why does that matter for spectral clustering?
  • How would you choose gamma without tuning by hand?