Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Choosing Edge Weights for Cell MSTs

Pseudotime and Trajectory Inference · Tutorial 365 of 700

Choosing Edge Weights for Cell MSTs

Learn how distance definitions and local-scale adjustments determine the edges of a cell minimum spanning tree, and how to compare the resulting trees.

Advanced 10 min read

What You'll Learn

  • Define Euclidean, cosine, and locally scaled edge weights for cells represented in a fixed feature space.
  • Calculate how alternative weights change the ordering of candidate edges.
  • Apply Kruskal’s algorithm to construct and compare minimum spanning trees.
  • Explain why local scaling can adjust for density while also introducing sensitivity to neighborhood choices.
  • Diagnose ties, zero vectors, disconnected candidate graphs, and unstable edge selections.

Why Edge Weights Matter

A minimum spanning tree (MST) connects all vertices in a graph using edges with minimum possible total weight, without forming cycles. For a cell MST, the vertices represent cells and the edge weights represent dissimilarities between cell states. The tree is therefore not determined by the cell coordinates alone: it also depends on how dissimilarity is defined.

A change in weights can change which edges are selected, even when the cells and their representation stay fixed. Euclidean distance responds to differences in both direction and magnitude. Cosine dissimilarity compares direction and discounts magnitude. A locally scaled weight adjusts a distance according to neighborhood scales, which can reduce the influence of uneven cell density. These choices encode different geometric priorities; none is automatically appropriate for every trajectory.

Definition: Let \(x_i\in\mathbb{R}^p\) be the feature vector for cell \(i\), for \(i=1,\ldots,n\). An edge between cells \(i\) and \(j\) has a nonnegative weight \(w_{ij}=w_{ji}\). An MST of a connected weighted graph is a spanning tree whose sum of edge weights is no larger than that of any other spanning tree of the graph. A spanning tree connects every vertex and contains no cycles.

The graph may be complete, with a candidate edge for every pair of cells, or it may be a specified sparse candidate graph. In either case, an MST exists only if the candidate graph is connected. If the graph is disconnected, there is no tree spanning all its vertices unless additional edges are introduced. The weights discussed below change edge costs; they do not by themselves determine which pairs are eligible as candidate edges.

Three Choices of Edge Weight

For fixed cell vectors, Euclidean distance is

$$ d_{ij}^{E}=\lVert x_i-x_j\rVert_2 =\sqrt{\sum_{r=1}^{p}(x_{ir}-x_{jr})^2}. $$

It is measured in the units of the feature coordinates. A change in a coordinate’s scale can affect the distances, so the representation and any preprocessing must be held fixed when comparing trees. Euclidean distance is a metric: in particular, it satisfies the triangle inequality.

For nonzero vectors, cosine dissimilarity is

$$ d_{ij}^{C}=1-\frac{x_i^\mathsf{T}x_j}{\lVert x_i\rVert_2\lVert x_j\rVert_2}. $$

It ranges from 0 to 2. It is 0 when two vectors point in the same direction and 2 when they point in opposite directions. Because their lengths do not enter the normalized dot product, multiplying either vector by a positive constant leaves the cosine dissimilarity unchanged. Cosine dissimilarity is not generally a metric, but an MST can still be constructed from its symmetric nonnegative edge weights. It is undefined for a zero vector, which must be handled before using this formula.

A locally scaled weight divides a base distance by a scale associated with each endpoint. One common choice is

$$ w_{ij}^{L}=\frac{d_{ij}}{\sqrt{s_i s_j}}, $$

where \(d_{ij}\) is a chosen base distance and \(s_i>0\) is a local scale for cell \(i\). For example, \(s_i\) can be the distance from cell \(i\) to its \(k\)th nearest neighbor in the same representation. A large local scale indicates that neighbors are relatively far away; a small one indicates a denser neighborhood. This symmetric definition gives a dimensionless weight when \(s_i\) and \(d_{ij}\) are measured in the same units. Other local-scaling conventions exist, so the formula and scale definition should be reported rather than referred to only as “local scaling.”

When local scales are estimated from the cells, the neighborhood parameter \(k\), the distance used to find neighbors, and the handling of ties or zero scales all matter. A zero scale makes the displayed formula undefined, so implementations need a principled positive-scale rule, such as a stated floor. That adjustment can affect the resulting tree and should not be hidden.

Constructing and Comparing the Trees

For any of the three weights, Kruskal’s algorithm gives a direct way to construct an MST: sort candidate edges from smallest to largest, then add an edge if it joins two components that are not yet connected. Skip an edge if adding it would create a cycle. Stop when \(n-1\) edges have been added. With tied weights, more than one MST may be optimal; a tie-breaking rule can select one tree, but it does not make that tree uniquely determined by the weights.

A useful justification for this procedure is the cut property. A cut divides the vertices into two nonempty sets. If an edge of strictly smallest weight crosses that cut, it belongs to every MST. To see why, consider an MST that omits the edge. Adding it creates a cycle, and that cycle must include another edge crossing the same cut. Replacing that other edge by the strictly lighter crossing edge keeps the graph connected, removes the cycle, and lowers the total weight. This contradicts the claim that the original tree was minimum. Ties weaken the conclusion: a minimum-weight crossing edge is safe to add, but it need not be in every MST.

1
Fix the representation and candidate graph.
Use the same cell vectors and eligible pairs for each weight comparison, unless the purpose is explicitly to compare candidate graphs as well.
2
Compute the weights.
Apply the stated distance formula; for local scaling, also state how each \(s_i\) is obtained.
3
Build one MST per weighting.
Sort edges, add cycle-free edges in order, and record any ties that allow alternative trees.
4
Compare edge sets and total costs.
Report which edges change and under which weight definition. Total costs from differently scaled weights are not directly comparable as measures of biological fit.

Worked Example: Euclidean and Cosine Weights Select Different Edges

Suppose three hypothetical cells have two-dimensional vectors \(A=(1,0)\), \(B=(10,0.5)\), and \(C=(9,1)\). Use the complete graph, so each of the three cell pairs is eligible.

Euclidean distances. The three values are

$$ d_{AB}^{E}=\sqrt{(10-1)^2+(0.5-0)^2} =\sqrt{81.25}\approx 9.014, $$
$$ d_{BC}^{E}=\sqrt{(9-10)^2+(1-0.5)^2} =\sqrt{1.25}\approx 1.118, \qquad d_{AC}^{E}=\sqrt{(9-1)^2+(1-0)^2} =\sqrt{65}\approx 8.062. $$

The two shortest edges are \(BC\) and \(AC\), so they form the Euclidean MST. They connect all three cells without a cycle.

Cosine dissimilarities. The vectors are nonzero, so the cosine formula applies. For \(A,B\),

$$ d_{AB}^{C} =1-\frac{10}{1\sqrt{100.25}} \approx 0.001248. $$

For the other pairs,

$$ d_{AC}^{C} =1-\frac{9}{\sqrt{82}} \approx 0.006116, \qquad d_{BC}^{C} =1-\frac{90.5}{\sqrt{100.25}\sqrt{82}} \approx 0.00184. $$

The two shortest cosine dissimilarities are \(AB\) and \(BC\), so the cosine MST contains those edges. The Euclidean tree instead contains \(BC\) and \(AC\). The difference is interpretable from the formulas: Euclidean distance sees \(A\) and \(B\) as far apart because their magnitudes differ substantially, whereas their directions are close, giving a small cosine dissimilarity.

Worked Example: Local Scaling Changes Edge Ranks

Use the same three hypothetical cells and the Euclidean base distance, but let \(s_i\) be the distance from cell \(i\) to its nearest other cell. From the distances above, \(s_A=\sqrt{65}\approx 8.062\), \(s_B=\sqrt{1.25}\approx 1.118\), and \(s_C=\sqrt{1.25}\approx 1.118\). The scales are positive, so the local-weight formula is defined.

For \(BC\), both endpoints have scale \(\sqrt{1.25}\), giving

$$ w_{BC}^{L} =\frac{\sqrt{1.25}}{\sqrt{\sqrt{1.25}\sqrt{1.25}}}=1. $$

For \(AC\) and \(AB\), the denominator is the same:

$$ \sqrt{s_A s_C} =\sqrt{\sqrt{65}\sqrt{1.25}} \approx 3.002. $$

Therefore,

$$ w_{AC}^{L}\approx \frac{8.062}{3.002}=2.685, \qquad w_{AB}^{L}\approx \frac{9.014}{3.002}=3.002. $$

The locally scaled MST contains \(BC\) and \(AC\), as does the Euclidean MST in this example, but the edge ranks and numerical weights differ. This illustrates an important distinction: changing weights does not have to change the selected tree to change the optimization problem. With other data and local scales, the ordering can change enough to select different edges.

Interpreting the Choice and Checking for Problems

Euclidean weights are a natural option when distances in the chosen representation are meaningful and differences in magnitude should count. Cosine weights are useful when directional similarity is the intended comparison and vector magnitude should have little or no influence. Locally scaled weights can reduce the dominance of dense regions by adjusting edge costs relative to local neighborhood distances. In exchange, local scaling introduces choices about neighborhood size and can make sparse regions exert a strong influence on normalized weights.

These are geometric choices, not tests of whether a biological trajectory is true. An MST summarizes a weighted graph; by itself it does not establish a temporal ordering, causal progression, or unique biological path. The inferred edges can also depend on the representation supplied to the distance calculation. The geometry chosen for trajectory inference should therefore be considered alongside the edge-weight formula, not treated as a separate afterthought.

Several checks help reveal when an edge-weighting choice is problematic:

  • Inspect scale and representation. If a few coordinates dominate Euclidean distances because of their units or ranges, the resulting edges may reflect that scaling rather than the intended cell-state geometry.
  • Check cosine inputs. Zero vectors make cosine dissimilarity undefined. Also verify that disregarding magnitude is appropriate for the biological question; a large magnitude difference can be consequential even when directions match.
  • Inspect local scales. Very small or very large \(s_i\) values can strongly change normalized edge weights. Check how scales vary, whether ties occur, and whether a few cells are unusually isolated.
  • Check connectivity and ties. A disconnected candidate graph cannot yield a spanning tree. Repeated edge weights can permit multiple MSTs, so one displayed tree may conceal ambiguity.
  • Compare edge sets under plausible choices. If modest changes to the distance, local neighborhood size, or candidate graph produce very different edges, report that sensitivity. Do not describe one unstable edge as definitively supported.

One practical comparison keeps the cell representation and candidate graph fixed, constructs an MST under each weighting, and tabulates the edges that are shared or changed. For locally scaled weights, repeat the comparison for a small set of scientifically reasonable neighborhood sizes. This comparison diagnoses sensitivity to the weighting choice; it does not identify a universally correct weight or replace biological validation.

Common pitfall: “Cosine dissimilarity is smaller for this pair, so those cells must be closer in every relevant sense.” The conclusion confuses one defined geometry with a general statement about cell states. Cosine dissimilarity compares direction and ignores positive rescaling; Euclidean distance also reflects magnitude. State what each weight measures, then interpret the selected edges in that context.

Check Your Understanding

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

  1. What happens to cosine dissimilarity if one nonzero cell vector is multiplied by a positive constant? Why?
  2. For a locally scaled weight \(d_{ij}/\sqrt{s_i s_j}\), what happens to the edge weight if \(s_i\) increases while \(d_{ij}\) and \(s_j\) stay fixed?
  3. Why can a tied smallest edge across a cut be safe to add to an MST without belonging to every MST?
  4. A candidate graph has two disconnected components. Can Kruskal’s algorithm produce a spanning tree over all its vertices using only the eligible edges? Explain.

Solutions. (1) The cosine dissimilarity is unchanged: both the dot product and the vector norm in the denominator acquire the same positive factor, which cancels. (2) The weight decreases because its denominator increases; specifically, it is proportional to \(1/\sqrt{s_i}\). (3) Adding a minimum-weight crossing edge cannot increase the total weight, but another crossing edge tied at the same minimum can serve in its place, yielding a different MST. (4) No. Edges within the two components cannot connect them, so at least one additional eligible edge between components must be introduced before a spanning tree can exist.

Clinical Biostats