Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Shared Nearest-Neighbor Graphs

Graph-Based Clustering · Tutorial 237 of 700

Shared Nearest-Neighbor Graphs

Learn to calculate and interpret shared-nearest-neighbor similarity, understand its density-related advantage, and recognize when neighborhood overlap may be misleading.

Intermediate 10 min read

What You'll Learn

  • Define a cell’s k-nearest-neighbor set and shared-nearest-neighbor Jaccard similarity.
  • Calculate intersection, union, and similarity for pairs of cells.
  • Explain why neighborhood overlap can be less sensitive to local density than raw distances.
  • Distinguish a symmetric similarity from directed neighbor membership.
  • Identify the effects of k, ties, boundaries, and distance choice on the result.
  • Apply the definition and interpretation in practice problems.

Why Compare Neighborhoods?

A distance between two cells is measured on the scale of the chosen representation and metric. In a dense region of a cell space, nearby cells may be separated by small distances; in a sparse region, cells with similar local relationships may be farther apart. This makes a single raw-distance threshold difficult to interpret uniformly. Shared-nearest-neighbor (SNN) similarity instead asks whether two cells have similar local neighborhoods.

The idea is to compare the sets of cells surrounding a pair, rather than relying only on the distance between the pair. If two cells have many neighbors in common, their local neighborhoods overlap strongly. This can make their relationship less dependent on the absolute scale of distances in a particular region. It does not remove the need to choose a representation, distance metric, or neighborhood size carefully.

Definition: For each cell \(i\), let \(N_k(i)\) be the set of its \(k\) nearest other cells, with the cell itself excluded. For two distinct cells \(i\) and \(j\), their shared-nearest-neighbor Jaccard similarity is $$ S(i,j)=\frac{\lvert N_k(i)\cap N_k(j)\rvert}{\lvert N_k(i)\cup N_k(j)\rvert}. $$ It is between 0 and 1. A value of 0 means the two sets have no cells in common; a value of 1 means the sets are identical.

This tutorial uses the Jaccard definition above. Implementations can use other overlap conventions, so the precise definition should be checked before comparing reported values. With fixed \(k\), each neighborhood has \(k\) members. The cell \(i\) itself can still appear in \(N_k(j)\), and \(j\) in \(N_k(i)\), if the cells are mutual neighbors: only a cell’s own neighborhood excludes itself.

Calculating Neighborhood Overlap

The calculation has three parts: list each cell’s neighbors, find the intersection, and count the union. The intersection contains members present in both sets; the union contains members present in at least one. The denominator matters: a count of shared neighbors alone does not show what fraction of the combined neighborhoods is shared.

Worked Example: Jaccard Similarity for Two Cells

Suppose \(k=4\), and the neighbor sets for two cells are \(N_4(A)=\{B,C,D,E\}\) and \(N_4(F)=\{C,D,E,G\}\). Find their SNN similarity.

The shared cells are \(C,D,E\), so \(\lvert N_4(A)\cap N_4(F)\rvert=3\). The union is \(\{B,C,D,E,G\}\), with five cells. Therefore,

$$ S(A,F)=\frac{3}{5}=0.6000. $$

The two neighborhoods share 60% of their combined members under the Jaccard definition. This is a statement about the sets, not a probability that the cells belong to the same biological population.

For fixed neighborhood size \(k\), if \(c\) members are shared, then the union contains \(2k-c\) members. Thus the Jaccard similarity can also be calculated as \(c/(2k-c)\). This relation is useful for checking arithmetic, but the intersection-over-union definition is the one to apply directly, especially if neighborhood sizes vary.

Worked Example: Comparing Two Overlap Counts

Let \(k=5\). Cells \(P\) and \(Q\) share four neighbors, while cells \(R\) and \(T\) share three. Calculate and compare their Jaccard similarities.

For \(P,Q\), the union size is \(5+5-4=6\). For \(R,T\), it is \(5+5-3=7\). Hence,

$$ S(P,Q)=\frac{4}{6}=0.6667,\qquad S(R,T)=\frac{3}{7}=0.4286. $$

The first pair has the larger overlap similarity. In general, when both neighborhood sets have size \(k\), similarity increases with the number of shared neighbors. The denominator also changes with the overlap, so the similarity is not simply the shared count divided by \(k\).

Why Local Neighborhoods Can Help with Uneven Density

A k-nearest-neighbor set is defined by rank: it contains the \(k\) closest eligible cells to a given cell. The distance to the \(k\)th neighbor can therefore differ across the data. In a dense region, that distance may be small; in a sparse region, it may be much larger. SNN compares the resulting sets, so two cells can have similar scores when their local neighborhoods overlap to a similar extent, even if their absolute distances are on different scales.

This is a relative, local comparison—not a claim that density is irrelevant. The cells still need to have meaningful neighbors under the selected representation and metric. Neighborhood overlap may also reflect a broad transition between populations, a shared boundary, or noise rather than a coherent biological group.

Worked Example: Similar Overlap at Different Distance Scales

Consider a hypothetical dense region and a hypothetical sparse region. In the dense region, cells \(D_1,D_2\) are separated by distance 0.1, and their five-neighbor sets are \(N_5(D_1)=\{D_3,D_4,D_5,D_6,D_7\}\) and \(N_5(D_2)=\{D_3,D_4,D_5,D_6,D_8\}\). In the sparse region, \(S_1,S_2\) are separated by distance 2.0, and their sets are \(N_5(S_1)=\{S_3,S_4,S_5,S_6,S_7\}\) and \(N_5(S_2)=\{S_3,S_4,S_5,S_6,S_8\}\).

Each pair shares four neighbors, and each union contains six. Thus,

$$ S(D_1,D_2)=\frac{4}{6}=0.6667,\qquad S(S_1,S_2)=\frac{4}{6}=0.6667. $$

The raw pairwise distances differ by a factor of 20, but the neighborhood-overlap scores are equal. This illustrates how SNN can emphasize local arrangement over absolute distance. It does not mean the two pairs are equally close, or that either pair must be a meaningful biological match; the score reports neighborhood overlap only.

Symmetry, Candidate Pairs, and a Short Justification

The underlying kNN relation is directed: \(j\) can be among \(i\)’s nearest neighbors without \(i\) being among \(j\)’s. However, the Jaccard similarity is symmetric because set intersection and union do not depend on the order of their arguments:

$$ N_k(i)\cap N_k(j)=N_k(j)\cap N_k(i),\qquad N_k(i)\cup N_k(j)=N_k(j)\cup N_k(i), $$

so \(S(i,j)=S(j,i)\), whenever the denominator is nonzero. With \(k\geq 1\), the union of two fixed-size neighborhoods is nonempty. Symmetry of the score does not by itself specify which cell pairs an implementation evaluates. One may calculate similarities for all pairs or restrict calculations to candidate pairs selected from a kNN graph. That candidate-pair choice is separate from the similarity formula.

A further useful property follows when both sets have size \(k\). If \(c=\lvert N_k(i)\cap N_k(j)\rvert\), then their union has size \(2k-c\): add the two set sizes, then subtract the \(c\) members counted twice. Therefore,

$$ S(i,j)=\frac{c}{2k-c}. $$

For \(0\leq c\leq k\), the denominator is positive, and the expression increases with \(c\). For example, its derivative with respect to \(c\), treating \(c\) as continuous for this monotonicity check, is \(2k/(2k-c)^2>0\). Thus, for a fixed \(k\), pairs with more shared neighbors always have larger Jaccard similarity. The normalization expresses overlap relative to the union; it does not make the score a calibrated measure of biological similarity.

Worked Example: A Symmetric Score from Asymmetric Membership

Suppose \(N_3(A)=\{B,C,D\}\) and \(N_3(B)=\{C,E,F\}\). Cell \(B\) is in \(A\)’s neighbor set, but \(A\) is not in \(B\)’s set. Calculate the SNN similarity in both orders.

The intersection is \(\{C\}\), and the union is \(\{B,C,D,E,F\}\). Therefore,

$$ S(A,B)=\frac{1}{5}=0.2000,\qquad S(B,A)=\frac{1}{5}=0.2000. $$

The directed membership differs, but the set-overlap score does not. A nonzero score here arises from the shared neighbor \(C\); it does not require mutual nearest-neighbor membership.

Assumptions, Diagnostics, and Limitations

SNN is a descriptive construction, not a hypothesis test. It does not require independent observations or a sampling model to compute. Its interpretation nevertheless depends on substantive choices and conditions:

  • Meaningful representation and distance: the cell profiles or transformed coordinates and the distance metric should represent the relationships of interest. A misleading distance can produce misleading neighbor sets.
  • A specified \(k\): all sets should use a stated neighborhood size and a consistent rule for ties. If ties at the \(k\)th distance are broken differently, the resulting sets and similarities can change.
  • Consistent cell handling: the comparison should use the same eligible cell universe, with self-neighbors treated consistently. Missing or excluded cells can alter both intersections and unions.
  • Local rankings that remain informative: rank-based neighborhoods help accommodate different local distance scales, but they still force every cell to have \(k\) neighbors. They do not establish that a sparse region is as well supported as a dense one.

Useful diagnostics include checking whether the \(k\)th-neighbor distance varies substantially across cells, whether small changes in \(k\) alter important overlaps, and whether many pairs have no shared neighbors. Inspecting ties and the underlying distance structure can reveal unstable neighborhoods. If a result changes sharply under reasonable alternative choices, that sensitivity should be reported rather than hidden.

The main alternative is to compare cells using raw distances. Raw distances preserve absolute scale, which can be important when that scale is meaningful, but a fixed distance can have different local significance in dense and sparse regions. SNN instead compares neighbor-set overlap, gaining some tolerance to local scale differences while discarding information about exact distances. Neither comparison is universally superior: the choice depends on whether absolute separation or local neighborhood agreement is the target.

Common failure modes include choosing \(k\) so small that neighbor sets are unstable, or so large that neighborhoods span distinct local structures; interpreting a high score as proof of a shared cell type; and treating zero overlap as proof that two cells are unrelated. A score of zero means only that the chosen sets have no members in common. It can result from a genuine local separation, a boundary, a small \(k\), or unstable neighborhoods.

Key takeaway: Shared-nearest-neighbor Jaccard similarity is the size of two cells’ neighborhood intersection divided by the size of their union. It summarizes local neighborhood agreement and can be less sensitive than raw distance to uneven local scales, but it remains dependent on the representation, metric, \(k\), and tie-handling choices.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions below.

  1. If two cells have \(k=4\) and share two neighbors, what is their Jaccard similarity?
  2. Can \(S(i,j)\) differ from \(S(j,i)\) when the directed neighbor memberships are asymmetric? Explain.
  3. Two cell pairs have equal SNN similarity but very different raw distances. What can be concluded, and what cannot be concluded?
  4. Name two choices or data features that can make SNN results unstable or misleading.

Solutions. For question 1, the union has size \(4+4-2=6\), so the similarity is \(2/6=0.3333\). For question 2, no: intersection and union are symmetric, so \(S(i,j)=S(j,i)\), even if the directed neighbor memberships differ. For question 3, the pairs have equal neighborhood overlap under the stated construction; it does not follow that their distances, biological relationships, or cell identities are equal. For question 4, examples include sensitivity to \(k\), ties at the \(k\)th-neighbor distance, a poorly chosen representation or metric, and unstable neighborhoods in sparse regions.

Clinical Biostats