## Archive for the ‘Computer Science’ Category

### Interview with László Lovász

May 11, 2013

On November 28, 2011, Avi Wigderson interviewed László Lovász. A total of 27 short videos can be watched at the Simons Foundation website. I particularly liked the discussion on the LLL algorithm, and also on semidefinite relaxations.

__________

Related:

### Sipser on P versus NP

December 22, 2012

On October 3, 2006 Michael Sipser was at Harvard to give the following lecture on the P versus NP problem:

This talk is arguably the very best introduction to computational complexity I have ever come across. I highly recommend it.

### Deterministic Finite Automata in Haskell

October 2, 2012

Consider the deterministic finite automaton [1] illustrated below

[ state diagram courtesy of Michael Sipser [1] ]

Henceforth, we will call this automaton $M_1$. Note that $M_1$ has three states, labeled $q_1$, $q_2$, and $q_3$. The start state is $q_1$ (note the arrow coming from nowhere) and the accept state is $q_2$ (note the double circle). There are two possible inputs, labeled $0$ and $1$, and, depending on the input, the automaton will jump from one state to another. In the diagram above these state transitions are depicted using arrows connecting two states.

Suppose that the automaton $M_1$ receives an input string such as $1101$ (which the automaton reads from left to right). Since the start state is $q_1$ and the first input symbol is $1$, the automaton will jump to state $q_2$. The second input symbol is $1$, so the automaton will remain at $q_2$. The automaton reads the third input symbol, which is $0$, and then jumps from state $q_2$ to state $q_3$. The last input symbol is $1$ and thus the automaton jumps from state $q_3$ to state $q_2$. Hence, the state sequence corresponding to the input string $1101$ is the following

$q_1 \xrightarrow{1} q_2 \xrightarrow{1} q_2 \xrightarrow{0} q_3 \xrightarrow{1} q_2$

After reading the last symbol in the input string, $M_1$ produces an output. Since the final state $q_2$ is the accept state, we have that the automaton produces the output “accept”. Were the final state not $q_2$, the automaton would produce the output “reject”. We conclude that this automaton accepts the input string $1101$. What other input strings does $M_1$ accept? Michael Sipser answers this question in [1]:

Experimenting with this machine on a variety of input strings reveals that it accepts the strings $1$, $01$, $11$, and $0101010101$. In fact, $M_1$ accepts any string that ends with a $1$, as it goes to its accept state $q_2$ whenever it reads the symbol $1$. In addition, it accepts strings $100$, $0100$, $110000$, and $0101000000$, and any string that ends with an even number of $0$s following the last $1$. It rejects other strings, such as $0$, $10$, $101000$.

A set of strings is called a language [1]. The set of all input strings accepted by the deterministic finite automaton $M_1$ is a language which we denote by $L (M_1)$.

__________

Formal definition

A deterministic finite automaton (DFA) consists of a finite set of states $Q$, a finite input alphabet $\Sigma$ that tells us what the allowed input symbols are, a transition function $\delta : Q \times \Sigma \to Q$ that tells us how to jump from one state to another, a start state $q_0 \in Q$, and a set of accept states $A \subseteq Q$. A deterministic finite automaton (DFA) is thus a $5$-tuple of the form

$(Q, \Sigma, \delta, q_0, A)$

The deterministic finite automaton $M_1$ which we discussed earlier in this post is defined formally as follows

$M_1 := (\{q_1, q_2, q_3\}, \{0,1\}, \delta, q_1, \{q_2\})$

where the transition function $\delta: \{q_1, q_2, q_3\} \times \{0,1\} \to \{q_1, q_2, q_3\}$ is defined enumeratively as

$\delta (q_1, 0) = q_1$,        $\delta (q_2, 0) = q_3$,        $\delta (q_3, 0) = q_2$

$\delta (q_1, 1) = q_2$,        $\delta (q_2, 1) = q_2$,        $\delta (q_3, 1) = q_2$

Alternatively, we could view each state transition $q_i \xrightarrow{\sigma} q_j$ as an ordered triple $(q_i, \sigma, q_j)$, which might be easier to implement in some programming languages.

__________

Computing state sequences

Given an input string $\sigma = \sigma_1 \sigma_2 \dots \sigma_n$ over a given alphabet $\Sigma$, how do we obtain the corresponding state sequence? I solved that problem last January. I will not repeat myself here, but the crux of the matter is that the final state can be obtained using a left-fold

$\text{foldl} \, \delta \, q_0\, [\sigma_1, \sigma_2, \dots, \sigma_{n}]$

whereas the entire state sequence can be computed using a left-scan

$\text{scanl} \, \delta \, q_0\, [\sigma_1, \sigma_2, \dots, \sigma_{n}]$

Lacking a better name, I called this procedure the “scanl trick“, as I used the Haskell function scanl to implement it. Please let me know if you find a more sophisticated name for this “trick”.

__________

data State = Q1 | Q2 | Q3 deriving (Read, Show, Eq)

type Input = Integer

-- define state-transition function
delta :: State -> Input -> State
delta Q1 0 = Q1
delta Q1 1 = Q2
delta Q2 0 = Q3
delta Q2 1 = Q2
delta Q3 0 = Q2
delta Q3 1 = Q2
delta  _ _ = error "Invalid input!"

-- define initial state
initialstate :: State
initialstate = Q1

-- create list of accept states
acceptstates :: [State]
acceptstates = [Q2]

-- create infinite list of input sequences
inputss :: [[Input]]
inputss = concat $iterate g [[]] where g = concat . map (\xs -> [ xs ++ [s] | s <- [0,1]]) -- create accept predicate isAccepted :: [Input] -> Bool isAccepted inputs = finalstate elem acceptstates where finalstate = foldl delta initialstate inputs -- compute language recognized by the DFA language :: [[Input]] language = filter isAccepted inputss Some remarks about this script are in order. Please note that: • Sets are represented by lists. Strings are represented by lists, too. The latter is more natural than the former. Sets of strings become lists of lists. • A new data type is created to represent the states, which we denote by Q1, Q2, and Q3. The input symbols are integers. • Note that Input is a type synonym, inputs is a list of inputs symbols (i.e., an input string), and inputss is a list of lists of inputs (i.e., a list of input strings). Yes, it is a bit confusing. • The final state is computed using the higher-order function foldl (left-fold). We check if the final state is an accept state using the list membership predicate elem. If you have any objections to this script, please do let me know. Let us now test it! We load it into GHCi and voilà: *Main> -- check list of input strings *Main> take 31$ inputss
[[],[0],[1],
[0,0],[0,1],[1,0],[1,1],
[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0],[1,1,1],
[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,0,1,1],
[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1],
[1,0,0,0],[1,0,0,1],[1,0,1,0],[1,0,1,1],
[1,1,0,0],[1,1,0,1],[1,1,1,0],[1,1,1,1]]
*Main> -- compute the language of the automaton
*Main> -- (let us extract some 40 input strings only)
*Main> take 40 language
[[1],[0,1],[1,1],
[0,0,1],[0,1,1],[1,0,0],[1,0,1],[1,1,1],
[0,0,0,1],[0,0,1,1],[0,1,0,0],[0,1,0,1],
[0,1,1,1],[1,0,0,1],[1,0,1,1],[1,1,0,0],
[1,1,0,1],[1,1,1,1],[0,0,0,0,1],[0,0,0,1,1],
[0,0,1,0,0],[0,0,1,0,1],[0,0,1,1,1],[0,1,0,0,1],
[0,1,0,1,1],[0,1,1,0,0],[0,1,1,0,1],[0,1,1,1,1],
[1,0,0,0,0],[1,0,0,0,1],[1,0,0,1,1],[1,0,1,0,0],
[1,0,1,0,1],[1,0,1,1,1],[1,1,0,0,1],[1,1,0,1,1],
[1,1,1,0,0],[1,1,1,0,1],[1,1,1,1,1],[0,0,0,0,0,1]]

The list of input strings is exactly the same I posted yesterday. The input strings in the language $L (M_1)$, or, to put it more precisely, the input strings in $L (M_1)$ that are displayed above either end with a $1$, or end “with an even number of $0$s following the last $1$“, as mentioned by Sipser in [1]. Why is that? The last $1$ in the input string puts $M_1$ in state $q_2$, and an even number of $0$s after that $1$ lead to transitions from $q_2$ to $q_3$ and back to $q_2$, e.g.,

$q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2$

or

$q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2$

or

$q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2 \xrightarrow{0} q_3 \xrightarrow{0} q_2$

It would also be interesting to take a look at the state sequences corresponding to the input strings in $L(M_1)$. We compute these state sequences using the infamous “scanl trick“:

*Main> -- create list of sequences of states
*Main> let statess = map (\xs -> scanl delta initialstate xs) language
*Main> -- take the "first" 20 state sequences
*Main> take 20 statess
[[Q1,Q2],[Q1,Q1,Q2],[Q1,Q2,Q2],
[Q1,Q1,Q1,Q2],[Q1,Q1,Q2,Q2],[Q1,Q2,Q3,Q2],
[Q1,Q2,Q3,Q2],[Q1,Q2,Q2,Q2],[Q1,Q1,Q1,Q1,Q2],
[Q1,Q1,Q1,Q2,Q2],[Q1,Q1,Q2,Q3,Q2],[Q1,Q1,Q2,Q3,Q2],
[Q1,Q1,Q2,Q2,Q2],[Q1,Q2,Q3,Q2,Q2],[Q1,Q2,Q3,Q2,Q2],
[Q1,Q2,Q2,Q3,Q2],[Q1,Q2,Q2,Q3,Q2],[Q1,Q2,Q2,Q2,Q2],
[Q1,Q1,Q1,Q1,Q1,Q2],[Q1,Q1,Q1,Q1,Q2,Q2]]

Note that all state trajectories end in state Q2, as we expected.

__________

References

[1] Michael Sipser, Introduction to the Theory of Computation (2nd edition), Thomson Course Technology, 2006.

### Generating all words over an alphabet

October 1, 2012

An alphabet $\Sigma$ is a finite set of symbols. A word $w$ of length $n$ over an alphabet $\Sigma$ is a sequence of symbols from $\Sigma$, i.e.,

$w : \{0,1,\dots,n-1\} \to \Sigma$

Alternatively, we can view a word $w$ of length $n$ over the alphabet $\Sigma$ as an $n$-tuple (i.e., an ordered list with $n$ elements) over $\Sigma$, i.e., $w \in \Sigma^n$. The set of all finite words over $\Sigma$, including the empty word $\varepsilon$, is denoted by $\Sigma^*$, where $*$ is the Kleene star.

We thus encounter the following problem:

Problem: Given an alphabet $\Sigma$, how do we generate all the words over $\Sigma$? In other words, given $\Sigma$, how do we generate the Kleene closure $\Sigma^*$?

The following Haskell script solves this problem:

g :: [a] -> [[a]] -> [[a]]
g alphabet = concat . map (\xs -> [ xs ++ [s] | s <- alphabet])

allwords :: [a] -> [[a]]
allwords alphabet = concat $iterate (g alphabet) [[]] where alphabet must be a finite list, otherwise the execution of the script won’t ever terminate. The script above, although short, uses a lot of machinery: list comprehension, higher-order functions map and iterate, concatenation of lists of lists, and partial function application. Let us test this script. First, we load it into GHCi. Then we can run a GHCi session: *Main> -- define alphabet *Main> let alphabet = [0,1] *Main> -- define function f *Main> let f = g alphabet *Main> -- check type *Main> :t f f :: [[Integer]] -> [[Integer]] *Main> -- apply f to several lists of lists *Main> f [[]] [[0],[1]] *Main> (f . f) [[]] [[0,0],[0,1],[1,0],[1,1]] *Main> (f . f . f) [[]] [[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]] So far, so good. Suppose that we would like to find all words over alphabet $\Sigma_2 := \{0,1\}$ whose length is less than or equal to $4$. How many such words are there? There are $|\Sigma_2|^k = 2^k$ words of length $k$. Hence, there are $\displaystyle\sum_{k=0}^n 2^k = \frac{2^{n+1} -1}{2-1} = 2^{n+1} - 1$ binary words of length less than or equal to $n$. Hence, if we make $n = 4$, then we obtain $2^{n+1}-1 = 31$. Continuing our GHCi session: *Main> take 31$ allwords alphabet
[[],[0],[1],
[0,0],[0,1],[1,0],[1,1],
[0,0,0],[0,0,1],[0,1,0],[0,1,1],
[1,0,0],[1,0,1],[1,1,0],[1,1,1],
[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,0,1,1],
[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1],
[1,0,0,0],[1,0,0,1],[1,0,1,0],[1,0,1,1],
[1,1,0,0],[1,1,0,1],[1,1,1,0],[1,1,1,1]]

Very nice! We could use this in Coding Theory! For example, suppose that we would like to find all binary words of length $8$ whose Hamming weight is equal to $5$. There are

$\displaystyle\binom{8}{5} = \frac{8!}{3! 5!} = 56$

such words. We can find them as follows:

*Main> -- take all words of length less than or equal to 8
*Main>  let words = take 511 \$ allwords alphabet
*Main> -- filter out binary words of length 8
*Main> let wordsL8 = filter (\xs -> length xs == 8) words
*Main> length wordsL8
256
*Main> -- filter out binary words of length 8
*Main> -- and also of Hamming weight equal to 5
*Main> let wordsL8H5 = filter (\xs -> sum xs == 5) wordsL8
*Main> length wordsL8H5
56
*Main> wordsL8H5
[[0,0,0,1,1,1,1,1],[0,0,1,0,1,1,1,1],[0,0,1,1,0,1,1,1],
[0,0,1,1,1,0,1,1],[0,0,1,1,1,1,0,1],[0,0,1,1,1,1,1,0],
[0,1,0,0,1,1,1,1],[0,1,0,1,0,1,1,1],[0,1,0,1,1,0,1,1],
[0,1,0,1,1,1,0,1],[0,1,0,1,1,1,1,0],[0,1,1,0,0,1,1,1],
[0,1,1,0,1,0,1,1],[0,1,1,0,1,1,0,1],[0,1,1,0,1,1,1,0],
[0,1,1,1,0,0,1,1],[0,1,1,1,0,1,0,1],[0,1,1,1,0,1,1,0],
[0,1,1,1,1,0,0,1],[0,1,1,1,1,0,1,0],[0,1,1,1,1,1,0,0],
[1,0,0,0,1,1,1,1],[1,0,0,1,0,1,1,1],[1,0,0,1,1,0,1,1],
[1,0,0,1,1,1,0,1],[1,0,0,1,1,1,1,0],[1,0,1,0,0,1,1,1],
[1,0,1,0,1,0,1,1],[1,0,1,0,1,1,0,1],[1,0,1,0,1,1,1,0],
[1,0,1,1,0,0,1,1],[1,0,1,1,0,1,0,1],[1,0,1,1,0,1,1,0],
[1,0,1,1,1,0,0,1],[1,0,1,1,1,0,1,0],[1,0,1,1,1,1,0,0],
[1,1,0,0,0,1,1,1],[1,1,0,0,1,0,1,1],[1,1,0,0,1,1,0,1],
[1,1,0,0,1,1,1,0],[1,1,0,1,0,0,1,1],[1,1,0,1,0,1,0,1],
[1,1,0,1,0,1,1,0],[1,1,0,1,1,0,0,1],[1,1,0,1,1,0,1,0],
[1,1,0,1,1,1,0,0],[1,1,1,0,0,0,1,1],[1,1,1,0,0,1,0,1],
[1,1,1,0,0,1,1,0],[1,1,1,0,1,0,0,1],[1,1,1,0,1,0,1,0],
[1,1,1,0,1,1,0,0],[1,1,1,1,0,0,0,1],[1,1,1,1,0,0,1,0],
[1,1,1,1,0,1,0,0],[1,1,1,1,1,0,0,0]]

where we used function take (again) and higher-order function filter. The two predicates were built using anonymous functions.

I am happy with this script. Please let me know in case you are not.

__________

Related:

### Combinatorial Explosion

September 16, 2012

An amusing Japanese cartoon on combinatorial explosion:

Tastefully done, I would say. The target audience consists not of this blog’s readers, but rather of their children.

Hat tip: Michael Lugo