Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Golomb Encoding for b=2n and b!=2n
Golomb encoding is a data compression technique used to encode non-negative integers with specific distributions. It was introduced by Solomon W. Golomb in 1966 and has applications in video compression, image compression, and data storage. This article explores Golomb encoding for two cases: when the base is a power of 2 (b=2^n) and when it's not (b?2^n).
Golomb Encoding for b=2^n (Base is Power of 2)
When the base is a power of 2, Golomb encoding becomes simpler. Let's see how it works with b = 4 (2^2).
Steps for Power of 2 Base
Step 1: Calculate quotient and remainder
For a number n = 12 and base b = 4 ?
n = 12
b = 4
q = n // b # quotient
r = n % b # remainder
print(f"n = {n}, b = {b}")
print(f"q = {q}, r = {r}")
n = 12, b = 4 q = 3, r = 0
Step 2: Encode the quotient using unary coding
The quotient (q=3) is encoded as 3 consecutive 1s followed by a 0, giving us "1110".
Step 3: Encode the remainder using binary coding
Since b = 4 = 2^2, we need 2 bits. The remainder r = 0 becomes "00" in binary.
The final Golomb encoding is the concatenation: "1110" + "00" = "111000"
Golomb Encoding for b?2^n (Base is Not Power of 2)
When the base is not a power of 2, we use a modified encoding approach. Let's use b = 7 as an example.
Steps for Non-Power of 2 Base
Step 1: Calculate quotient and remainder
For n = 23 and b = 7 ?
n = 23
b = 7
q = n // b # quotient
r = n % b # remainder
print(f"n = {n}, b = {b}")
print(f"q = {q}, r = {r}")
n = 23, b = 7 q = 3, r = 2
Step 2: Encode the quotient
Same as before, q = 3 becomes "1110" in unary.
Step 3: Encode the remainder using Rice coding
We calculate k as the smallest integer where 2^k ? b. For b = 7, k = 3.
Range R = 2^k - b = 8 - 7 = 1.
Since r = 2 ? R = 1, we encode (r + R) = 3 using k = 3 bits: "011".
The final encoding: "1110" + "011" = "1110011"
Complete Implementation
Here's a complete Python implementation of Golomb encoding ?
def unary_encoding(q):
"""Encode quotient using unary coding."""
return "1" * q + "0"
def binary_encoding(r, k):
"""Encode remainder using Rice coding."""
R = 2 ** (k - 1)
if r < R:
return bin(r)[2:].zfill(k - 1)
else:
return bin(r + R)[2:].zfill(k)
def golomb_encoding(n, b):
"""Perform Golomb encoding for number n with base b."""
q = n // b
r = n % b
# Encode quotient
unary_part = unary_encoding(q)
# Encode remainder
if b & (b - 1) == 0: # Power of 2 check
bits_needed = (b - 1).bit_length()
binary_part = bin(r)[2:].zfill(bits_needed)
else:
k = b.bit_length()
binary_part = binary_encoding(r, k)
return unary_part + binary_part
# Test both cases
print("Power of 2 base:")
result1 = golomb_encoding(12, 4)
print(f"Golomb encoding of 12 with base 4: {result1}")
print("\nNon-power of 2 base:")
result2 = golomb_encoding(23, 7)
print(f"Golomb encoding of 23 with base 7: {result2}")
Power of 2 base: Golomb encoding of 12 with base 4: 111000 Non-power of 2 base: Golomb encoding of 23 with base 7: 1110011
Comparison
| Base Type | Remainder Encoding | Complexity |
|---|---|---|
| Power of 2 (b=2^n) | Simple binary with fixed bits | Low |
| Non-power of 2 (b?2^n) | Rice coding with variable bits | Medium |
Conclusion
Golomb encoding efficiently compresses non-negative integers by combining unary coding for quotients and optimized binary coding for remainders. The power-of-2 case uses simple binary encoding, while non-power-of-2 bases require Rice coding for optimal compression.
