---
title: "Normalized compression distance"
description: "Estimate string distance from compressed sizes with the RLE-based normalized compression distance."
---

> Documentation Index
> Fetch the complete documentation index at: https://luozhouyang.github.io/python-string-similarity/llms.txt
> Use this file to discover all available pages before exploring further.

# Normalized compression distance

Normalized compression distance (NCD) estimates how similar two strings are by comparing the compressed sizes of each string and of their concatenation. `NormalizedCompressionDistance` provides this measure with a run-length encoder.

## Formula

```
NCD(s0, s1) = (C(s0 + s1) - min(C(s0), C(s1))) / max(C(s0), C(s1))
```

`C(x)` is the compressed size of `x`, and `+` is concatenation. The implementation evaluates both concatenation orders, `C(s0 + s1)` and `C(s1 + s0)`, and uses the smaller compressed size. That makes the result symmetric by construction. If both compressed sizes are zero (both inputs are empty), the distance is `0.0`.

## Usage

```python
from strsimpy import NormalizedCompressionDistance

ncd = NormalizedCompressionDistance()  # compressor="rle"
print(ncd.compressor)
print(ncd.distance("aaaa", "aaab"))
```

will produce:

```
rle
0.3333333333333333
```

`compressor` is the only constructor option. Only `"rle"` is implemented, and any other value raises `ValueError`. The zlib, bz2, and lzma compressors are not shipped, so the Rust core stays dependency-free.

## The RLE compressor

Run-length encoding replaces a run of more than two equal characters with the decimal digits of the run length followed by the character, for example `"3a"`. Runs of one or two characters are emitted unchanged. The compressed size counts characters in the encoded form, not bytes, and the encoder operates on Unicode code points, so a multi-byte character counts once. The empty string has compressed size 0.

| Input | Encoded form | Compressed size |
| --- | --- | --- |
| `"abc"` | `"abc"` | 3 |
| `"aaa"` | `"3a"` | 2 |
| `"aaaa"` | `"4a"` | 2 |
| `"aaab"` | `"3ab"` | 3 |
| `"a" * 100` | `"100a"` | 4 |
| `"上海"` | `"上海"` | 2 |

## Reference values

```python
from strsimpy import NormalizedCompressionDistance

ncd = NormalizedCompressionDistance()
print(ncd.distance("aaaa", "aaab"))
print(ncd.distance("abc", "abc"))
print(ncd.distance("aaa", "aaa"))
print(ncd.distance("a" * 100, "a" * 99 + "b"))
print(ncd.distance("", "abc"))
print(ncd.distance("", ""))
```

will produce:

```
0.3333333333333333
1.0
0.0
0.25
1.0
0.0
```

The first value is exactly `1/3`. The `"a" * 100` pair scores `0.25` because `"a" * 100` and `"a" * 99 + "b"` both compress to size 4 (`"100a"` and `"99a" + "b"`), while their concatenation compresses to size 5 (`"199a" + "b"`).

## Behavior and caveats

- **Identical strings need not score 0.** `distance("abc", "abc")` is `1.0`, while `distance("aaa", "aaa")` is `0.0`. RLE cannot exploit repeated substrings that are not runs: `C("abc")` is 3 and `C("abcabc")` is 6, so the formula gives `(6 - 3) / 3 = 1.0`; in contrast, `C("aaa")` and `C("aaaaaa")` are both 2, so the formula gives `0.0`. The implementation does not special-case equal non-empty inputs; the value always comes from the formula.
- **Values are not guaranteed to lie in `[0, 1]`.** The result depends on the compressor and should not be treated as a bounded similarity score.
- **The result is symmetric.** Both concatenation orders are tried and the smaller compressed size is used, so `distance(s0, s1) == distance(s1, s0)`.
- **Only `distance()` is provided.** The class has no `similarity()` method, and the value is a distance: smaller values mean the inputs compress together better. The scale is specific to the compressor.

## Related

- [Sequence alignment](https://luozhouyang.github.io/python-string-similarity/algorithms/alignment/) exposes raw alignment scores, which are also unnormalized.
- [SIFT4](https://luozhouyang.github.io/python-string-similarity/algorithms/experimental/) is an experimental general-purpose distance.
- [Documentation home](https://luozhouyang.github.io/python-string-similarity/)

Source: https://luozhouyang.github.io/python-string-similarity/algorithms/compression/index.mdx
