---
abstract: |
    We introduce a new class of non-standard variable-length codes, called *adaptive codes*. This class of codes associates a variable-length codeword to the symbol being encoded depending on the previous symbols in the input data string. An efficient algorithm for constructing adaptive codes of order one is presented. Then, we introduce a natural generalization of adaptive codes, called *GA codes*.

    [**Keywords:**]{} adaptive mechanisms, compression rate, data compression, entropy, prefix codes, variable-length codes.
title: '****'
---

Introduction
============

The theory of variable-length codes [@bp1] originated in concrete problems of information transmission. Especially by its language theoretic branch, the field has produced a great number of results, most of them with multiple applications in engineering and computer science. Intuitively, a *variable-length code* is a set of strings such that any concatenation of these strings can be uniquely decoded. We introduce a new class of non-standard variable-length codes, called *adaptive codes*, which associate a variable-length codeword to the symbol being encoded depending on the previous symbols in the input data string. The paper is organized into six sections. After this introductory section, the definition of adaptive codes and several theoretical remarks are given in Section 2, as well as some characterization results for adaptive codes. The main results of this paper are presented in Section 3, where we focus on designing an algorithm for constructing adaptive codes of order one. In Section 4, we compute the entropy bounds for this algorithm. A natural generalization of adaptive codes is presented in Section 5. Finally, the last section contains a few concluding remarks. Before ending this introductory section, let us present some useful notation used throughout the paper [@rs1; @as1], and then review some basic concepts. We denote by $|S|$ the *cardinality* of a set $S$; if $x$ is a string of finite length, then $|x|$ denotes the length of $x$. The *empty string* is denoted by $\lambda$. For an alphabet $\Sigma$, we denote by $\Sigma^{*}$ the set $\bigcup_{n=0}^{\infty}\Sigma^{n}$, and by $\Sigma^{+}$ the set $\bigcup_{n=1}^{\infty}\Sigma^{n}$, where $\Sigma^{0}$ is defined by $\{\lambda\}$. Let us denote by $\Sigma^{\leq n}$ the set $\bigcup_{i=0}^{n}\Sigma^{i}$ and by $\Sigma^{\geq n}$ the set $\bigcup_{i=n}^{\infty}\Sigma^{i}$. Let $X$ be a finite and nonempty subset of $\Sigma^{+}$, and $w\in\Sigma^{+}$. A *decomposition of w* over $X$ is any sequence of words $u_{1}, u_{2}, \ldots, u_{h}$ with $u_{i}\in X$, $1\leq i\leq h$, such that $w=u_{1}u_{2}\ldots u_{h}$. A *code* over $\Sigma$ is any nonempty set $C\subseteq\Sigma^{+}$ such that each word $w\in\Sigma^{+}$ has at most one decomposition over $C$. A *prefix code* over $\Sigma$ is any code $C$ over $\Sigma$ such that no word in $C$ is proper prefix of another word in $C$.

Adaptive Codes
==============

In this section we introduce a new class of non-standard variable-length codes, called adaptive codes. These codes are based on adaptive mechanisms, that is, the variable-length codeword associated to the symbol being encoded depends on the previous symbols in the input data string.

Let $\Sigma$ and $\Delta$ be two alphabets. A function ${c:\Sigma\times\Sigma^{\leq{n}}\rightarrow\Delta^{+}}$, with $n\geq{1}$, is called an if its unique homomorphic extension ${\overline{c}:\Sigma^{*}\rightarrow\Delta^{*}}$ given by:

-   $\overline{c}(\lambda)=\lambda$,

-   $\overline{c}({\sigma_{1}\sigma_{2}\ldots\sigma_{m}})=$ $c(\sigma_{1},\lambda)$ $c(\sigma_{2},\sigma_{1})$ $\ldots$ $c(\sigma_{n-1},{\sigma_{1}\sigma_{2}\ldots\sigma_{n-2}})$ $c(\sigma_{n},{\sigma_{1}\sigma_{2}\ldots\sigma_{n-1}})$ $c(\sigma_{n+1},{\sigma_{1}\sigma_{2}\ldots\sigma_{n}})$ $c(\sigma_{n+2},\sigma_{2}\sigma_{3}\ldots\sigma_{n+1})$ $c(\sigma_{n+3},\sigma_{3}\sigma_{4}\ldots\sigma_{n+2})\ldots$ $c(\sigma_{m},\sigma_{m-n}\sigma_{m-n+1}\ldots\sigma_{m-1})$,

for all ${\sigma_{1}\sigma_{2}\ldots\sigma_{m}}\in\Sigma^{+}$, is injective.

Let us take an example in order to better understand the adaptive mechanisms presented in the definition above.

Let $\Sigma=\{{\texttt{\textup{a}}},{\texttt{\textup{b}}}\}$, $\Delta=\{0,1\}$ be alphabets, and ${c:\Sigma\times\Sigma^{\leq{2}}\rightarrow\Delta^{+}}$ a function given by the table below.

One can easily verify that the function $\overline{c}$ is injective, and according to Definition 2.1, $c$ is an adaptive code of order two. Let $x={\texttt{\textup{abaa}}}\in\Sigma^{+}$. Using the definition above, we encode $x$ by $\overline{c}(x)=c({\texttt{\textup{a}}},\lambda)c({\texttt{\textup{b}}},{\texttt{\textup{a}}})c({\texttt{\textup{a}}},{\texttt{\textup{ab}}})c({\texttt{\textup{a}}},{\texttt{\textup{ba}}})=0101$.

Let ${c:\Sigma\times\Sigma^{\leq{n}}\rightarrow\Delta^{+}}$ be an adaptive code of order $n$, $n\geq{1}$. We denote by $C_{c, \sigma_{1}\sigma_{2}\ldots\sigma_{h}}$ the set

$\{c(\sigma,\sigma_{1}\sigma_{2}\ldots\sigma_{h}) \mid \sigma\in\Sigma\}$,

for all $\sigma_{1}\sigma_{2}\ldots\sigma_{h}\in\Sigma^{\leq{n}}-\{\lambda\}$, and by $C_{c, \lambda}$ the set $\{c(\sigma,\lambda) \mid \sigma\in\Sigma\}$. We write $C_{\sigma_{1}\sigma_{2}\ldots\sigma_{h}}$ instead of $C_{c, \sigma_{1}\sigma_{2}\ldots\sigma_{h}}$, and $C_{\lambda}$ instead of $C_{c, \lambda}$ whenever there is no confusion.

If $w\in\Sigma^{+}$ then we denote by $w(i)$ the $i$-th symbol of $w$. In the rest of this paper we denote by ${\it AC}(\Sigma,\Delta,n)$ the set

$\{{c:\Sigma\times\Sigma^{\leq{n}}\rightarrow\Delta^{+}} \mid$ $c$ is an adaptive code of order $n\}$.

Let $\Sigma$ and $\Delta$ be two alphabets, and let ${c:\Sigma\times\Sigma^{\leq{n}}\rightarrow\Delta^{+}}$ be a function. If $C_{{\sigma_{1}\sigma_{2}\ldots\sigma_{h}}}$ is a prefix code, for all ${\sigma_{1}\sigma_{2}\ldots\sigma_{h}}\in\Sigma^{\leq{n}}$, then $c\in{{\it AC}(\Sigma,\Delta,n)}$.

**Proof** Let us assume that $C_{{\sigma_{1}\sigma_{2}\ldots\sigma_{h}}}$ is prefix code, for all ${\sigma_{1}\sigma_{2}\ldots\sigma_{h}}\in\Sigma^{\leq{n}}$, but $c\notin{{\it AC}(\Sigma,\Delta,n)}$. By Definition 2.1, the unique homomorphic extension of c, denoted by $\overline{c}$, is not injective. This implies that $\exists$ $u\sigma u', u\sigma'u''\in\Sigma^{+}$, with $\sigma,\sigma '\in\Sigma$ and $u,u',u''\in\Sigma^{*}$, such that $\sigma\neq\sigma'$ and

$(*)$ $ $ $\overline{c}(u\sigma u')=\overline{c}(u\sigma'u'')$.

We can rewrite $(*)$ by

$(**)$ $ $ $\overline{c}(u)c(\sigma,P_{n}(u))\overline{c}(u')=$ $\overline{c}(u)c(\sigma',P_{n}(u))\overline{c}(u'')$,

where $P_{n}(u)$ is given by $$P_{n}(u)=
\left\{     
    \begin{array}{ll}
    \lambda             & \textrm{if $u=\lambda$,} \\
    u_{1}\ldots u_{q}   & \textrm{if $u=u_{1}u_{2}\ldots u_{q}$ and $u_{1},u_{2},\ldots,u_{q}\in\Sigma$ and $q\leq{n}$,} \\
    u_{q-n+1}\ldots u_{q}   & \textrm{if $u=u_{1}u_{2}\ldots u_{q}$ and $u_{1},u_{2},\ldots,u_{q}\in\Sigma$ and $q>n$.}
    \end{array} 
\right.$$ By hypothesis, $C_{P_{n}(u)}$ is a prefix code and $c(\sigma,P_{n}(u)),c(\sigma',P_{n}(u))\in{C_{P_{n}(u)}}$. Therefore, the set $\{c(\sigma,P_{n}(u)),c(\sigma',P_{n}(u))\}$ is a prefix code. But the equality $(**)$ can hold if and only if $\{c(\sigma,P_{n}(u)),c(\sigma',P_{n}(u))\}$ is not a prefix set. Hence, our assumption leads to a contradiction. $\diamondsuit$

The converse of Theorem 2.1 does not hold. We can prove this by taking a counter-example. Let us consider $\Sigma=\{{\texttt{\textup{a}}},{\texttt{\textup{b}}}\}$ and $\Delta=\{0,1\}$ two alphabets, and ${c:\Sigma\times\Sigma^{\leq{2}}\rightarrow\Delta^{+}}$ a function given by the table below.

One can verify that the unique homomorphic extension of $c$, denoted by $\overline{c}$, is injective. Therefore, we conclude that the function $c$ is an adaptive code of order two.

Let $\Sigma$, $\Delta$, and ${\it Bool}=\{{\it True}, {\it False}\}$ be alphabets. We define the function ${\it Prefix}:{\it AC}(\Sigma,\Delta,n)\rightarrow {\it Bool}$ by: $${\it Prefix}(c)=
\left\{     
    \begin{array}{ll}
    {\it True}          & \textrm{if $C_{u}$ is a prefix code, for all $u\in\Sigma^{\leq{n}}$,} \\
    {\it False}         & \textrm{otherwise.}
    \end{array} 
\right.$$ The function *Prefix* can now be used to translate the hypothesis in Theorem 2.1: if ${c:\Sigma\times\Sigma^{\leq{n}}\rightarrow\Delta^{+}}$ is a function satisfying ${\it Prefix}(c)={\it True}$, then we conclude that $c\in{{\it AC}(\Sigma,\Delta,n)}$. Let $c\in{{\it AC}(\Sigma,\Delta,n)}$ be an adaptive code satisfying ${\it Prefix}(c)={\it True}$. Then, the algorithm **Decoder** described below requires a linear time.

(340,240) (30,211)[**Decoder**$(c,u)$]{} (30,199)[input:$c\in{{\it AC}(\Sigma,\Delta,n)}$ *such that* ${\it Prefix}(c)={\it True}$ *and* $u\in\Delta^{+}$;]{} (30,187)[output:$w\in\Sigma^{+}$ *such that* $\overline{c}(w)=u$;]{} (30,175)[begin]{} (15,163)[1. $w:=\lambda$; $i:=1$; $Last:=\lambda$; $length:=|u|;$]{} (15,151)[2.while $i\leq{length}$ do]{} (15,139)[begin]{} (15,127)[3. *Let* $\sigma\in\Sigma$ *be the unique symbol of $\Sigma$ with the property*]{} (15,115)[ *that* $c(\sigma,Last)$ *is prefix of* $u(i)\cdot u(i+1)\cdot\ldots\cdot u(length)$;]{} (15,103)[4.$w:=w\cdot\sigma$;]{} (15,91)[5.$i:=i+|c(\sigma,Last)|$;]{} (15,79)[6.if $|Last|<n$]{} (15,67)[7.then $Last:=Last\cdot\sigma$;]{} (15,55)[8.else $ $ $Last:=Last(|Last|-n+2)\cdot\ldots\cdot Last(|Last|)\cdot\sigma$;]{} (15,43)[end]{} (15,31)[9.return $w$;]{} (30,19)[end]{} (8,227)[(1,0)[327]{}]{} (8,9)[(1,0)[327]{}]{} (8,227)[(0,-1)[218]{}]{} (335,227)[(0,-1)[218]{}]{}

In the third step of the algorithm given above, the symbol denoted by $\sigma$ is unique with that property due to the input restrictions.

One can easily verify that the while loop in algorithm **** is iterated $$|u|-\sum_{i=1}^{h}(|c(w_{i},P_{n}(w_{1}w_{2}\ldots w_{i-1}))|-1)$$ times, where $w=w_{1}w_{2}\ldots w_{h}$, and $P_{n}$ is the function given in Theorem 2.1.

In practice, we can use only adaptive codes satisfying the equality ${\it Prefix}(c)={\it True}$, since designing a decoding algorithm for the other case requires additional information and more complicated techniques.

Data Compression using Adaptive Codes
=====================================

The construction of adaptive codes requires different approaches, depending on the structure of the input data strings. In this section, we focus on data compression using adaptive codes of order one.

Let $\Sigma$ be an alphabet and $w=w_{1}w_{2}\ldots w_{h}\in\Sigma^{\geq{2}}$, with $w_{i}\in\Sigma$, for all $i\in\{1,2,\ldots,h\}$. A subword $uu$ of $w$, with $u\in\Sigma$, is called a of w.

Let $\Sigma$ be an alphabet and $w=w_{1}w_{2}\ldots w_{h}\in\Sigma^{\geq{2}}$. It is useful to consider the following notations:

1.  ${\it Pairs}(w)=\{i \mid 1\le i\le |w|-1, w_i=w_{i+1}\}$,

2.  ${\it NRpairs}(w)=|{\it Pairs}(w)|$,

3.  ${\it Prate}(w)=\frac{{\it NRpairs}(w)}{|w|}$.

The main goal of this section is to design an algorithm for constructing adaptive codes of order one, under the assumption that the input data strings have a large number of pairs. Let $\Sigma=\{\sigma_{1},\sigma_{2},\ldots,\sigma_{h}\}$ and $\Delta=\{0,1\}$ be alphabets, $c\in{{\it AC}(\Sigma,\Delta,1)}$ an adaptive code of order one, and $w\in\Sigma^{+}$. We denote by $A_{c}$ the matrix given by:

$
    A_{c} =
    \left(\begin{array}{ccccc}
        c(\sigma_{1},\sigma_{1}) & c(\sigma_{1},\sigma_{2}) & \ldots & c(\sigma_{1},\sigma_{h}) & c(\sigma_{1},\lambda) \\
        c(\sigma_{2},\sigma_{1}) & c(\sigma_{2},\sigma_{2}) & \ldots & c(\sigma_{2},\sigma_{h}) & c(\sigma_{2},\lambda) \\
                         &                  &   \ldots &                    &                   \\                                          
        c(\sigma_{h},\sigma_{1}) & c(\sigma_{h},\sigma_{2}) & \ldots & c(\sigma_{h},\sigma_{h}) & c(\sigma_{h},\lambda) 
        \end{array} \right)
    $.

Let us denote by **Huffman**$({\it EF}(w),n)$ the well-known Huffman’s algorithm [@ds1], where $n\geq{1}$, and ${\it EF}(w)$ is the matrix given below.

$
    {\it EF}(w) =
    \left(\begin{array}{ccccc}
        \sigma_{1}      & \sigma_{2}    & \ldots        & \sigma_{n}    \\
        f(\sigma_{1},w) & f(\sigma_{2},w)   & \ldots        & f(\sigma_{n},w) 
        \end{array} \right)
    $.

We assume that the first row of the matrix ${\it EF}(w)$ contains the symbols which are being encoded, while the second row contains their frequencies, that is, $f(\sigma_{i})$ is the frequency of the symbol $\sigma_{i}$ in $w$. Also, we assume that **Huffman**$({\it EF}(w),n)$ is the matrix given by

$
    \textbf{Huffman}({\it EF}(w),n) =
    \left(\begin{array}{ccccc}
        H(\sigma_{1},w)     & H(\sigma_{2},w)   & \ldots        & H(\sigma_{n},w)   
        \end{array} \right)
    $

where $H(\sigma_{i},w)$ is the codeword associated to the symbol $\sigma_{i}$ by Huffman’s algorithm. The algorithm **Builder** described further on takes linear time, and constructs an adaptive code of order one satisfying ${\it Prefix}(c)={\it True}$.

Let $c:\Sigma\times\Sigma^{\leq{1}}\rightarrow\{0,1\}^{+}$ be a function given by the matrix **Builder**$(c)$. Then, $c\in{{\it AC}(\Sigma,\{0,1\},1)}$ and ${\it Prefix}(c)={\it True}$.

**Proof** Applying the algorithm **Builder** to the function $c$, one can easily verify that ${\it Prefix}(c)={\it True}$. Therefore, according to **Theorem 2.1**, $c$ is an adaptive code of order one, that is, $c\in{{\it AC}(\Sigma,\{0,1\},1)}$. $\diamondsuit$

(340,282) (15,268)[**Builder**(c)]{} (15,256)[input:$c:\Sigma\times\Sigma^{\leq{1}}\rightarrow\{0,1\}^{+}$, $\Sigma=\{\sigma_{1},\sigma_{2},\ldots,\sigma_{h}\}$;]{} (15,244)[output:$A_{c}$ *such that* $c\in{{\it AC}(\Sigma,\{0,1\},1)}$ *and* ${\it Prefix}(c)={\it True}$;]{} (15,232)[begin]{} (0,220)[$1.$for $i:=1$ to $h$ do $A_{c}(i,i):=0$;]{} (0,198)[2. $E:=    \left(
            \begin{array}{cccc}
            \sigma_{2}  & \sigma_{3}    & \ldots    & \sigma_{h} \\
                0   &   0       & \ldots    &   0    
            \end{array}
            \right)
        $;]{} (0,176)[3.$X:=$**Huffman**$(E,h-1)$;]{} (0,164)[4.for $i:=2$ to $h$ do]{} (0,152)[ begin]{} (0,140)[5. $A_{c}(1,i):=1\cdot X(1,i-1)$;]{} (0,128)[6.$X(1,i-1):=1\cdot X(1,i-1)$;]{} (0,116)[7. $A_{c}(i,1):=X(1,i-1)$;]{} (0,104)[ end]{} (0,92)[8.for $j:=2$ to $h$ do]{} (0,80)[ begin]{} (0,68)[9.for $i:=2$ to $j-1$ do $A_{c}(i,j):=X(1,i-1)$;]{} (-5,56)[10.for $i:=j+1$ to $h$ do $A_{c}(i,j):=X(1,i-1)$;]{} (0,44)[ end]{} (-5,32)[11.for $i:=1$ to $h$ do $A_{c}(i,h+1):=A_{c}(1,i)$;]{} (-5,20)[12.return $A_{c}$;]{} (15,8)[end]{} (-8,282)[(1,0)[330]{}]{} (-8,2)[(1,0)[330]{}]{} (-8,282)[(0,-1)[280]{}]{} (322,282)[(0,-1)[280]{}]{}

Let $c:\{{\texttt{\textup{a}}},{\texttt{\textup{b}}},{\texttt{\textup{c}}}\}\times\{{\texttt{\textup{a}}},{\texttt{\textup{b}}},{\texttt{\textup{c}}}\}^{\leq{1}}\rightarrow\{0,1\}^{+}$ be a function. One can verify that $A_{c}$ is the matrix given below.

$A_{c}=\left(
        \begin{array}{cccc}
            0   &   10  &   11  &   0       \\
            11  &   0   &   10  &   10      \\
            10  &   11  &   0   &   11   
        \end{array}
        \right)
    $.

Let $w={\texttt{\textup{abbbcabccaabccabbcba}}}$ be an input data string. It is easy to verify that ${\it Pairs}(w)=\{2,3,8,10,13,16\}$, ${\it NRpairs}(w)=6$, and ${\it Prate}(w)=0.3$. Encoding the string $w$ by $c$ requires the computation of $\overline{c}(w)$. Using Definition 2.1, we get that $|\overline{c}(w)|=33$. Let us apply Huffman’s algorithm to the data string $w$ in order to make a comparison between the results. If we denote by ${\it Huffman}(w)$ the codeword associated to $w$ by Huffman’s algorithm, we get that $|Huffman(w)|=32$. An even better result can be obtained when the input data string has a larger number of pairs, as shown in the following example.

Let $c:\{{\texttt{\textup{a}}},{\texttt{\textup{b}}},{\texttt{\textup{c}}}\}\times\{{\texttt{\textup{a}}},{\texttt{\textup{b}}},{\texttt{\textup{c}}}\}^{\leq{1}}\rightarrow\{0,1\}^{+}$ be an adaptive code of order one given as in the previous example, and $w={\texttt{\textup{abbbccbccaabccaaacba}}}$ an input data string. One can verify that ${\it Pairs}(w)=\{2,3,5,8,10,13,15,16\}$, ${\it NRpairs}(w)=8$, ${\it Prate}(w)=0.4$, and $|\overline{c}(w)|=31$. Encoding the string $w$ by Huffman’s algorithm, we get that $|{\it Huffman}(w)|=34$.

The results obtained in the previous examples are summarized in the table below, which shows that we get substantial improvements for input data strings having a larger number of pairs.

Builder: Entropy Bounds
=======================

In this section, we focus on computing the entropy bounds for the algorithm described in section 3. Given that our algorithm is based on Huffman’s algorithm, let us first recall the entropy bounds for Huffman codes.

Let $\Sigma$ be an alphabet, $x$ a data string of length $n$ over $\Sigma$ and $k$ the length of the encoder output, when the input is $x$. The , denoted by $R(x)$, is defined by $$R(x)=\frac{k}{n}.$$

Let $R(x)$ be the compression rate in codebits per datasample, computed after encoding the data string $x$ by the Huffman algorithm. One can obtain upper and lower bounds on $R(x)$ before encoding the data string $x$ by computing the entropy denoted by $H(x)$. Let $x$ be a data string of length $n$, $(F_{1},F_{2},\ldots,F_{h})$ the vector of frequencies of the symbols in $x$ and $k$ the length of the encoder output. The entropy $H(x)$ of $x$ is defined by $$H(x)=\frac{1}{n}\sum_{i=1}^{h}F_{i}\log_{2}(\frac{n}{F_{i}}).$$ Let $L_{i}$ be the length of the codeword associated to the symbol with the frequency $F_{i}$ by the Huffman algorithm, $1\leq{i}\leq{h}$. Then, the compression rate $R(x)$ can be re-written by $$R(x)=\frac{1}{n}\sum_{i=1}^{h}F_{i}L_{i}.$$ If we relate the entropy $H(x)$ to the compression rate $R(x)$, we obtain the following inequalities: $$H(x)\leq{R(x)}\leq{H(x)+1}.$$ Let $\Sigma=\{\sigma_{1},\sigma_{2},\ldots,\sigma_{t}\}$ be an alphabet and $c:\Sigma\times\Sigma^{\leq{1}}\rightarrow\{0,1\}^{+}$ an adaptive code of order one constructed as shown in section 3. Also, consider $w=w_{1}w_{2}\ldots{w_{s}}\in{\Sigma^{+}}$, $w_{i}\in\Sigma$, $1\leq{i}\leq{s}$, and $p$ the number of symbols occurring in $w$. We denote by $R_{A}(w)$ the compression rate obtained when encoding the string $w$ by $\overline{c}$ and by $H_{A}(w)$ the entropy of $w$. It is useful to consider the following notations:

1.  ${\it EH}(w)=\{i$ $\mid$ $2\leq{i}\leq{s}$ and $w_{i}\neq{w_{i-1}}\}$,

2.  ${\it LNotHuffman}(w)=|c(w_{1},\lambda)|+\sum_{i\in{{\it Pairs}(w)}}|c(w_{i+1},w_{i})|$,

3.  ${\it LHuffman}(w)$ is the entropy of $w_{j_{1}}w_{j_{2}}\ldots{w_{j_{r}}}$, $j_{k}\in{{\it EH}(w)}$, $1\leq{k}\leq{r}$,

4.  $H_{A}(w)={\it LNotHuffman}(w)+{\it LHuffman}(w)$.

It is easy to verify that ${\it LNotHuffman}(w)={\it NRPairs}(w)+|c(w_{1},\lambda)|$. Using the notation above, we get that $${\it LHuffman}(w)=\sum_{i\in{{\it EH}(w)}}\{ \frac{1}{N(w_{i})}\sum_{q\in{{\it Prev}(w_{i})}}[F_{q}(w_{i})(1+\log_{2}\frac{N(w_{i})}{F_{q}(w_{i})})]\},$$ where

-   $N(w_{i})=|\{j$ $\mid$ $j\in{{\it EH}(w)}$ and $w_{j}={w_{i}}\}|$,

-   ${\it Prev}(w_{i})=\{j$ $\mid$ $j+1\in{{\it EH}(w)}$ and $w_{j+1}=w_{i}\}$,

-   $F_{q}(w_{i})=|\{j$ $\mid$ $j\in{{\it EH}(w)}$ and $w_{j}=w_{i}$ and $w_{j-1}=w_{q}\}|$, $q\in{{\it Prev}(w_{i})}$.

Finally, we can relate the entropy $H_{A}(w)$ to the compression rate $R_{A}(w)$ by the following inequalities: $$H_{A}(w)\leq{R_{A}(w)}\leq{H_{A}(w)}+1,$$ where $R_{A}(w)$ is given by $$R_{A}(w)=\frac{|c(w_{1},\lambda)c(w_{2},w_{1})\ldots c(w_{s},w_{s-1})|}{s}.$$

GA Codes
========

In this section, we introduce a natural generalization of adaptive codes (of any order), called *GA codes* (**G**eneralized **A**daptive codes). Theorem 5.1 proves that adaptive codes are particular cases of GA codes.

Let $\Sigma$ and $\Delta$ be two alphabets and $F:N^{*}\times\Sigma^{+}\rightarrow\Sigma^{*}$ a function, where $N$ is the set of natural numbers, and $N^{*}=N-\{0\}$. A function $c_{F}:\Sigma\times\Sigma^{*}\rightarrow\Delta^{+}$ is called a if its unique homomorphic extension $\overline{c_{F}}:\Sigma^{*}\rightarrow\Delta^{*}$ given by

-   $\overline{c_{F}}(\lambda)=\lambda$,

-   $\overline{c_{F}}({\sigma_{1}\sigma_{2}\ldots\sigma_{m}})=c_{F}(\sigma_{1},F(1,{\sigma_{1}\sigma_{2}\ldots\sigma_{m}}))\ldots c_{F}(\sigma_{m},F(m,{\sigma_{1}\sigma_{2}\ldots\sigma_{m}}))$,

for all ${\sigma_{1}\sigma_{2}\ldots\sigma_{m}}\in\Sigma^{+}$, is injective.

The function $F$ in Definition 5.1 is called the of the GA code $c_{F}$. Clearly, a GA code $c_{F}$ can be constructed if its adaptive function $F$ is already constructed.

Let $\Sigma$ and $\Delta$ be two alphabets. We denote by $GAC(\Sigma,\Delta)$ the set $\{c_{F}:\Sigma\times\Sigma^{*}\rightarrow\Delta^{+}$ $\mid$ $c_{F}$ is a GA code$\}$.

Let $\Sigma$ and $\Delta$ be alphabets. Then, $AC(\Sigma,\Delta,n)\subset{GAC(\Sigma,\Delta)}$, for all $n\geq{1}$.

**Proof** Let $c_{F}\in{AC(\Sigma,\Delta,n)}$ be an adaptive code of order $n$, $n\geq{1}$, and $F:N^{*}\times\Sigma^{+}\rightarrow\Sigma^{*}$ a function given by: $$F(i,{\sigma_{1}\sigma_{2}\ldots\sigma_{m}})=
\left\{     
    \begin{array}{ll}
    \lambda             
                    & \textrm{if $i=1$ or $i>m$,}                       \\
    {\sigma_{1}\sigma_{2}\ldots\sigma_{i-1}}       
                    & \textrm{if $2\leq{i}\leq{m}$ and $2\leq{i}\leq{n+1}$,}    \\
    \sigma_{i-n}\sigma_{i-n+1}\ldots\sigma_{i-1}    
                    & \textrm{if $2\leq{i}\leq{m}$ and $i>n+1$,}        
    \end{array} 
\right.$$ for all $i\geq{1}$ and ${\sigma_{1}\sigma_{2}\ldots\sigma_{m}}\in{\Sigma^{+}}$. One can verify that $|F(i,{\sigma_{1}\sigma_{2}\ldots\sigma_{m}})|\leq{n}$, for all $i\geq{1}$ and ${\sigma_{1}\sigma_{2}\ldots\sigma_{m}}\in{\Sigma^{+}}$. According to Definition 2.1, the function $\overline{c_{F}}$ is given by:

-   $\overline{c_{F}}(\lambda)=\lambda$,

-   $\overline{c_{F}}({\sigma_{1}\sigma_{2}\ldots\sigma_{m}})=$ $c_{F}(\sigma_{1},\lambda)$ $c_{F}(\sigma_{2},\sigma_{1})$ $\ldots$ $c_{F}(\sigma_{n-1},{\sigma_{1}\sigma_{2}\ldots\sigma_{n-2}})$ $c_{F}(\sigma_{n},{\sigma_{1}\sigma_{2}\ldots\sigma_{n-1}})$ $c_{F}(\sigma_{n+1},{\sigma_{1}\sigma_{2}\ldots\sigma_{n}})$ $c_{F}(\sigma_{n+2},\sigma_{2}\sigma_{3}\ldots\sigma_{n+1})$ $c_{F}(\sigma_{n+3},\sigma_{3}\sigma_{4}\ldots\sigma_{n+2})\ldots$ $c_{F}(\sigma_{m},\sigma_{m-n}\sigma_{m-n+1}\ldots\sigma_{m-1})$,

for all ${\sigma_{1}\sigma_{2}\ldots\sigma_{m}}\in\Sigma^{+}$. It is easy to remark that

-   $\overline{c_{F}}({\sigma_{1}\sigma_{2}\ldots\sigma_{m}})=c_{F}(\sigma_{1},F(1,{\sigma_{1}\sigma_{2}\ldots\sigma_{m}}))\ldots c_{F}(\sigma_{m},F(m,{\sigma_{1}\sigma_{2}\ldots\sigma_{m}}))$,

for all ${\sigma_{1}\sigma_{2}\ldots\sigma_{m}}\in\Sigma^{+}$, which proves the theorem. $\diamondsuit$

Conclusions and Future Work
===========================

We introduced a new class of non-standard variable-length codes, called adaptive codes, which associate a variable-length codeword to the symbol being encoded depending on the previous symbols in the input data string. The main results of this paper are presented in Section 3, where we have shown that if an input data string $x$ has a significant number of pairs, then a good compression rate is achieved when encoding $x$ by adaptive codes of order one. In a further paper devoted to adaptive codes, we intend to extend the algorithm **Builder** to adaptive codes of any order.
