Definition
Unary Encoding
Unary encoding represents a non-negative integer by a string whose length is proportional to itself.
A common convention is , so is encoded as .
Length
Under unary encoding, the encoded length of is
Thus the length of the representation is linear in the numeric value.
This contrasts with binary encoding, where the length of is .
Complexity
Unary encoding is important in complexity theory because it removes the exponential gap between a number and its representation length.
If an algorithm loops over all values , then it performs iterations. Under unary encoding, the input already contains symbols for , so such a loop is polynomial in the input length.
Under binary encoding, the same loop may be exponential in the input length, because can be exponential in .
Example
Pseudo-polynomial dynamic programming
A knapsack dynamic programme with capacity often has running time .
If is unary-encoded, then the input length already includes symbols. Hence is polynomial in the unary input length.
If is binary-encoded, then the input contains only bits for the capacity. Hence the same running time can be exponential in the encoded input length.