<p>The supremum bit density will either be achieved by a finite word $v$ in the language, or by the limiting bit density of some sequence $u v w, u v^2 w, u v^3 w, \ldots$ of words in the language, which equals the bit density of $v$. In both cases, we have that $|v| \leq n$ without loss of generality, where $n$ is the number of states in the finite automaton.</p>

<p>Below I give you a polytime algorithm to find $v$. To make the algorithm simpler, I assume the automaton is deterministic. This isn't really essential.</p>

<p><strong>Algorithm:</strong> Find $v$.</p>

<pre><code>Let {1,2,...,n} be the set of states.
Let delta be the transition functor.
Let 1 be the initial state.
Let F be the set of accepting states.
Let X be the set of states reachable from the initial state.
Let Y be the set of states that can reach an accepting state.

Let A be an (n+1)-by-(n+1)-by-(n+1) array of strings or NULL.
Set A[i,i,0] = "" for all i in {1,2,...,n}.
Set A[i,j,0] = NULL for all i != j in {1,2,...,n}.
For k in {1,2,...,n} do:
    For i in {1,2,...,n} do:
        For j in {1,2,...,n} do:
            Let W0 = "0" ++ A[delta(i,0), j, k-1]  (or NULL, if A[delta(i,0), j, k-1] is NULL).
            Let W1 = "1" ++ A[delta(i,1), j, k-1]  (or NULL, if A[delta(i,0), j, k-1] is NULL).
            If density(W0) &gt; density(W1) then:     (let density(NULL) = -1 by convention)
                Set A[i,j,k] = W0.
            Else:
                Set A[i,j,k] = W1.

Return highest density non-NULL word
    in     { A[1,j,k] : j in F,              k in {1, ..., n} }
    or in  { A[j,j,k] : j in X intersect Y,  k in {1, ..., n} }.
</code></pre>

<p>The idea behind the algorithm is that <code>A[i,j,k]</code> stores the highest density path from state <code>i</code> to state <code>j</code> with length <code>k</code>. We build up this table from <code>k=0</code> to <code>k=n</code>, and then pick the highest density <code>v</code> that matches our criteria (either it is a word in the language, or it is a loop in some word in the language).</p>

CC BY-SA 3.0
2015-01-10T07:10:19.113Z
2015-01-10T07:10:19.113Z
Francisco Mota
<p>First, you mean "sup" rather than "max", because it is easy to construct examples of regular languages, such as 00(011)*00  where there is no max.  (The sup may not be attained.)  </p>

<p>Second, by "FSM" I assume you mean finite automaton.</p>

<p>Then I claim that either the maximum bit density is achieved by a word of length &lt; n, the number of states, or it is given by the maximum bit density over a loop of size at most n.
This means there are only finitely many cases to check, for each DFA.  Probably with a bit more work, one can get a polynomial-time algorithm out of it.</p>

<p>To see the claim, first note that if we compute the bit density of the sequence of words
$(u v^i w)_{i \geq 0}$ then this sequence is either (a) strictly increasing, (b) strictly decreasing, or (c) constant.  If the maximum bit density is attained and occurs at a shortest word x of length $&gt;= n$, by the pumping lemma we can write $x = uvw$ where $|uv| \leq n$.  In each case we get a contradiction.  In (a) there are words with higher bit density, like $uv^2w$; in (b) there is a word with higher bit density, namely $uw$, and in (c) there is a shorter word with the same bit density.  </p>

<p>The remaining case is that the maximum density is not attained.  A slightly more complicated argument, almost exactly the same as in my paper with Schaeffer, then proves the claim about the loops.</p>

<p>The Critical Exponent is Computable for Automatic Sequences. 
Luke Schaeffer, Jeffrey Shallit 
Int. J. Found. Comput. Sci. Vol. 23 No. 8 Pg. 1611-1626<br>
Year: 2012 </p>

CC BY-SA 3.0
2015-01-09T18:59:24.843Z
thx nice insight but dont see it as obviously in P from this sketch (& definitely would prefer an _efficient_ algorithm if possible). it sounds from this like it might be related to looking at all $n$-vertex paths in the FSM graph which could be exponential...?
vzn
CC BY-SA 3.0
2015-01-09T21:12:38.203Z
I already said the algorithm I suggested wasn't in P.  (You didn't specify that in original question.)    However, with more work it can probably be done, as in Schaeffer's master's thesis at Waterloo.
Jeffrey Shallit
CC BY-SA 3.0
2015-01-09T16:55:38.847Z
2015-01-09T16:55:38.847Z
Jeffrey Shallit
<p>let $L$ be a regular language defined by a FSM over binary symbols $\{0,1\}$. consider a function $f(x)$ on words/ strings that computes "bit density", defined as the number of $1$'s in a word (<a href="http://en.wikipedia.org/wiki/Hamming_weight" rel="nofollow">"Hamming weight"</a>) divided by the word length.</p>

<blockquote>
  <p>what is an algorithm to compute $\max_{x \in L} f(x)$?</p>
</blockquote>

<p>there is a simple randomized/ estimation algorithm that computes $f(x)$ over random words; am looking for a deterministic one. note that $f(x)$ evaluated on the shortest word in $L$ is not necessarily the same as the global minimum. am also interested in other generalizations of $f(x)$ that are based on similar "simple word metrics".</p>

<p>bkg: this question arose in studying &amp; idea in attacking an old open <a href="http://en.wikipedia.org/wiki/Collatz_conjecture" rel="nofollow">number theoretic problem</a>.</p>

CC BY-SA 3.0
2015-01-09T17:41:23.660Z
Also note that the Parikh image of a regular language is semi-linear; so the set of the pairs $\{ <\#0_w,\#1_w> \mid w \in L\}$ ($\#0_w$ denotes the number of $0$s in $w$) is a union of a finite number of linear sets $\{ c_0 + n_1 c_1 + ... + n_m c_m \mid n_i \in N \}$ where $c_0,...,c_m$ are fixed pairs.
Marzio De Biasi
CC BY-SA 3.0
2015-01-09T19:23:34.670Z
MDB that sounds like an algorithm will upvote it for 1 if you write it out in more detail & post it thx
vzn
<reference-request><co.combinatorics><automata-theory><regular-language><upper-bounds>
computing maximal bit density over a FSM
vzn
# computing maximal bit density over a FSM

(Asked by: username_0 on 2015-01-09)

let $L$ be a regular language defined by a FSM over binary symbols $\{0,1\}$. consider a function $f(x)$ on words/ strings that computes "bit density", defined as the number of $1$'s in a word (["Hamming weight"](http://en.wikipedia.org/wiki/Hamming_weight)) divided by the word length.

> what is an algorithm to compute $\max\_{x \in L} f(x)$?

there is a simple randomized/ estimation algorithm that computes $f(x)$ over random words; am looking for a deterministic one. note that $f(x)$ evaluated on the shortest word in $L$ is not necessarily the same as the global minimum. am also interested in other generalizations of $f(x)$ that are based on similar "simple word metrics".

bkg: this question arose in studying & idea in attacking an old open [number theoretic problem](http://en.wikipedia.org/wiki/Collatz_conjecture).

**Tags:** reference-request, co.combinatorics, automata-theory, regular-language, upper-bounds
> **Comments:**
> **username_1**: Also note that the Parikh image of a regular language is semi-linear; so the set of the pairs $\{ <\#0_w,\#1_w> \mid w \in L\}$ ($\#0_w$ denotes the number of $0$s in $w$) is a union of a finite number of linear sets $\{ c_0 + n_1 c_1 + ... + n_m c_m \mid n_i \in N \}$ where $c_0,...,c_m$ are fixed pairs.  
> **username_0**: MDB that sounds like an algorithm will upvote it for 1 if you write it out in more detail & post it thx  

---

## 2 answers

### Answer by username_3 on 2015-01-09. Score: 11 | Accepted answer

First, you mean "sup" rather than "max", because it is easy to construct examples of regular languages, such as 00(011)\*00 where there is no max. (The sup may not be attained.)

Second, by "FSM" I assume you mean finite automaton.

Then I claim that either the maximum bit density is achieved by a word of length < n, the number of states, or it is given by the maximum bit density over a loop of size at most n.
This means there are only finitely many cases to check, for each DFA. Probably with a bit more work, one can get a polynomial-time algorithm out of it.

To see the claim, first note that if we compute the bit density of the sequence of words
$(u v^i w)\_{i \geq 0}$ then this sequence is either (a) strictly increasing, (b) strictly decreasing, or (c) constant. If the maximum bit density is attained and occurs at a shortest word x of length $>= n$, by the pumping lemma we can write $x = uvw$ where $|uv| \leq n$. In each case we get a contradiction. In (a) there are words with higher bit density, like $uv^2w$; in (b) there is a word with higher bit density, namely $uw$, and in (c) there is a shorter word with the same bit density.

The remaining case is that the maximum density is not attained. A slightly more complicated argument, almost exactly the same as in my paper with Schaeffer, then proves the claim about the loops.

The Critical Exponent is Computable for Automatic Sequences.
Luke Schaeffer, Jeffrey Shallit
Int. J. Found. Comput. Sci. Vol. 23 No. 8 Pg. 1611-1626  
Year: 2012

> **Comments:**
> **username_0**: thx nice insight but dont see it as obviously in P from this sketch (& definitely would prefer an _efficient_ algorithm if possible). it sounds from this like it might be related to looking at all $n$-vertex paths in the FSM graph which could be exponential...?  
> **username_3**: I already said the algorithm I suggested wasn't in P.  (You didn't specify that in original question.)    However, with more work it can probably be done, as in Schaeffer's master's thesis at Waterloo.  

---

### Answer by username_2 on 2015-01-10. Score: 3

The supremum bit density will either be achieved by a finite word $v$ in the language, or by the limiting bit density of some sequence $u v w, u v^2 w, u v^3 w, \ldots$ of words in the language, which equals the bit density of $v$. In both cases, we have that $|v| \leq n$ without loss of generality, where $n$ is the number of states in the finite automaton.
Below I give you a polytime algorithm to find $v$. To make the algorithm simpler, I assume the automaton is deterministic. This isn't really essential.
Algorithm: Find $v$.
Let {1,2,...,n} be the set of states.
Let delta be the transition functor.
Let 1 be the initial state.
Let F be the set of accepting states.
Let X be the set of states reachable from the initial state.
Let Y be the set of states that can reach an accepting state.

Let A be an (n+1)-by-(n+1)-by-(n+1) array of strings or NULL.
Set A[i,i,0] = "" for all i in {1,2,...,n}.
Set A[i,j,0] = NULL for all i != j in {1,2,...,n}.
For k in {1,2,...,n} do:
    For i in {1,2,...,n} do:
        For j in {1,2,...,n} do:
            Let W0 = "0" ++ A[delta(i,0), j, k-1]  (or NULL, if A[delta(i,0), j, k-1] is NULL).
            Let W1 = "1" ++ A[delta(i,1), j, k-1]  (or NULL, if A[delta(i,0), j, k-1] is NULL).
            If density(W0) > density(W1) then:     (let density(NULL) = -1 by convention)
                Set A[i,j,k] = W0.
            Else:
                Set A[i,j,k] = W1.

Return highest density non-NULL word
    in     { A[1,j,k] : j in F,              k in {1, ..., n} }
    or in  { A[j,j,k] : j in X intersect Y,  k in {1, ..., n} }.

The idea behind the algorithm is that A[i,j,k] stores the highest density path from state i to state j with length k. We build up this table from k=0 to k=n, and then pick the highest density v that matches our criteria (either it is a word in the language, or it is a loop in some word in the language).


---

