cd /news/machine-learning/dna-sequence-alignment-and-kings · home topics machine-learning article
[ARTICLE · art-45779] src=johndcook.com ↗ pub= topic=machine-learning verified=true sentiment=· neutral

DNA Sequence Alignment and Kings

A blog post by John D. Cook connects central Delannoy numbers, which count king moves on a chessboard, to DNA sequence alignment. The Delannoy numbers D(m,n) represent the number of possible alignments between two DNA strands of lengths m and n. The post demonstrates that memoization speeds up the recursive computation of D(12,15) by over a million times.

read1 min views1 publishedJul 1, 2026
DNA Sequence Alignment and Kings
Image: Johndcook (auto-discovered)

This morning I wrote a post that included the central Delannoy numbers. The nth central Delannoy number D n counts the number of ways a king can move from one corner of a chessboard to the diagonally opposite corner without backtracking.

The more general Delannoy numbers D m,n are the analogy for an

m×

nrectangular board, not necessarily square.

D m,n is also the number of possible sequence alignments for a strand of DNA with

mbase pairs and a strand with

nbase pairs [1]. At each step in the alignment process, you can introduce a gap in the first strand, the second strand or neither, which is analogous to the king who can move N, E, or NE at each step.

The Delannoy numbers can be computed recursively:

def D(m, n):
    if m == 0 or n == 0:
        return 1
    return D(m - 1, n) + D(m, n - 1) + D(m - 1, n - 1)

The code above can be sped up tremendously by adding the decorator

@lru_cache(maxsize=None)

above the function definition to turn on memoization. I did an experiment computing D12,15 with and without memoization and the times were 77.1805 seconds and 0.000062 seconds respectively, i.e. memoization made the code over a million times faster.

Incidentally, D12,15 = 2653649025 and so there are a lot of ways to align even short sequences unless you place some restriction on the permissible alignments.

[1] Torres, A., Cabada, A., & Nieto, J. J. (2003). An exact formula for the number of alignments between two DNA sequences. DNA Sequence, 14(6), 427–430. https://doi.org/10.1080/10425170310001617894

── more in #machine-learning 4 stories · sorted by recency
── more on @john d. cook 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/dna-sequence-alignme…] indexed:0 read:1min 2026-07-01 ·