<p>This is not an answer, but too long for a comment. I'm trying to explain why the question, as posed, may be hard to understand.</p>

<p>There are two ways to define computational complexity for a device <em>X</em>.</p>

<p>The first and most natural way is <em>intrinsic</em>. One needs to say how the device <em>X</em> uses the input, so that we may later look at how the size <em>n</em> of the input affects the run time of the device. One also needs to say what counts as an <em>operation</em> (or <em>step</em>). Then we simply let the device run on the input and count operations.</p>

<p>The second is <em>extrinsic</em>. We define computational complexity for another device <em>Y</em> and then we program <em>Y</em> to act as a simulator for <em>X</em>. Since there may be multiple ways for <em>Y</em> to simulate <em>X</em>, we need to add that we are supposed to use the best one. Let me say the same with other words: We say that <em>X</em> takes $O(f(n))$ time on an input of size <em>n</em> if there exists a simulator of <em>X</em> implemented on machine <em>Y</em> that takes $f(n)$ time.</p>

<p>For example, an intrinsic definition for NFA says that it takes <em>n</em> steps to process a string of length <em>n</em>; an extrinsic definition that uses a RAM machine as device <em>Y</em> says that the best known upper bound is <em>probably</em> what David Eppstein answered. (Otherwise it would be strange that (1) the best practical implementation pointed in the other answer does not use the better alternative and (2) no one here indicated a better alternative.) Note also that strictly speaking your device <em>X</em> is the regular expression, but since the NFA has the same size it is safe to take it as being the device <em>X</em> you are looking at.</p>

<p>Now, when you use the second kind of definition it makes little sense to ask how restricting the features of device <em>X</em> affects the running time. It does however make sense to ask how restricting the features of device <em>Y</em> affects the running time. Obviously, allowing more powerful machines <em>Y</em> might allow us to simulate <em>X</em> faster. So, if we assume one of the most powerful machines that could be implemented (this rules out nondeterministic machines, for example) and come up with a lower bound $\Omega(f(n))$, then we know that <em>no</em> less powerful machine could do better.</p>

<p>So, in a sense, the best answer you could hope for is a proof in something like the cell probe model that simulating an NFA needs a certain amount of time. (Note that if you take into account the conversion NFA to DFA you need time to write down the big DFA, so memory isn't the only issue there.)</p>

CC BY-SA 2.5
2010-09-17T08:50:37.600Z
2010-09-17T08:50:37.600Z
Radu GRIGore
<p>Even if you believe that there's nothing new or old to be learned about regular expression matching, check out one of the most beautiful papers I've come across for a long time: <a href="http://sebfisch.github.com/haskell-regexp/regexp-play.pdf" rel="nofollow" title="A play on regular expressions">A play on regular expressions</a> by S Fischer, F Huch, and T Wilke, ICFP 2010.</p>

<p>(MMT Chakravarty deserves the credit for recommending this paper.)</p>

<p>EDIT: The reason why this paper is relevant is that it describes a new technique (based on Glushkov's from the 60ies) that avoids constructing the full NFA (let alone the DFA) corresponding to the RE. What is done instead resembles running a marking algorithm similar to the well-known one for deciding acceptance of a word by an NFA on the syntax tree of the RE. Performance measurements suggest that this is competitive, even with google's recently published re2 library.</p>

CC BY-SA 2.5
2011-02-06T10:02:05.633Z
A nice paper to read!!
Hsien-Chih Chang 張顯之
CC BY-SA 2.5
2011-02-06T01:29:18.740Z
2011-02-06T09:56:10.723Z
2011-02-06T09:56:10.723Z
Kai
<p>It's easy enough to trade off time for space, as follows.</p>

<p>Convert the regular expression to an NFA — for concreteness in comparing algorithms, we'll assume that $r$ is the number of NFA states, so that your $O(rs)$ time bound for directly simulating the NFA is valid and your $O(2^r)$ space bound for running the converted DFA are also valid whenever you're working in a RAM that can address that much memory.</p>

<p>Now, partition the states of the NFA (arbitrarily) into $k$ subsets $S_i$ of at most $\lceil r/k\rceil$ states each. Within each subset $S_i$, we can index subsets $A_i$ of $S_i$ by numbers from $0$ to $2^{\lceil r/k\rceil}-1$.</p>

<p>Build a table $T[i,j,c,A_i]$ where $i$ and $j$ are in the range from 0 to $k-1$, $c$ is an input symbol, and $A_i$ is (the numerical index of) a subset of $S_i$. The value stored in the table is (the numerical index of) a subset of $S_j$: a state $y$ is in $T[i,j,c,A_i]$ if and only if $y$ belongs to $S_j$ and there is a state in $A_i$ that transitions to $y$ on input symbol $c$.</p>

<p>To simulate the NFA, maintain $k$ indices, one for each $S_i$, specifying the subset $A_i$ of the states in $S_i$ that can be reached by some prefix of the input. For each input symbol $c$, use the tables to look up, for each pair $i,j$, the set of states in $S_j$ that can be reached from a state in $A_i$ by a transition on $c$, and then use a bitwise binary or operation on the numerical indices of these sets of states to combine them into a single subset of states of $S_j$. So, each step of the simulation takes time $O(k^2)$, and the total time for the simulation is $O(sk^2)$.</p>

<p>The space required is the space for all the tables, which is $O(k^2 2^{r/k})$. The time and space analysis is valid on any RAM that can address that much memory and that can do binary operations on words that are large enough to address that much memory.</p>

<p>The time-space tradeoff you get from this doesn't perfectly match the NFA simulation, because of the quadratic dependence on $k$. But then, I'm skeptical that $O(rs)$ is the right time bound for the NFA simulation: how do you simulate a single step of the NFA faster than looking at all of the (possibly quadratically many) transitions allowed from a currently active state to another state? Shouldn't it be $O(r^2 s)$?</p>

<p>In any case by letting $k$ vary you can get time bounds on a continuum between the DFA and NFA bounds, with less space than the DFA.</p>

CC BY-SA 3.0
2017-04-20T20:19:57.880Z
Here is an alternative way to parametrize this.  Suppose we are on a RAM machine with word width $w$, where $w \ge \lg r$.  Then the NFA simulation takes $O(s r^2)$ time and $O(r/w)$ space.  The DFA simulation isn't possible if $r \ge w$ (not enough space available).  The construction in this answer sets $k \approx \lceil r/w \rceil$ and takes $O(sr^2/w^2)$ time and uses all the space available (i.e., something in the vicinity of $2^w$ space).  It is basically exploiting the bit-parallelism available in a RAM machine to do the NFA simulation faster.
D.W.
CC BY-SA 2.5
2010-09-14T10:33:43.150Z
I think your correction is correct, and your answer does answer my asked question. However, the question I wanted to ask is how much additional computational power helps. (Eg, with a counter you can match a string $a^k$ in O(1) space.) If you don't mind, I'll leave the question open for a little while longer to see if anyone knows  the answer to that....
Neel Krishnaswami
CC BY-SA 2.5
2010-09-14T15:18:40.567Z
@Neel: *If* David's solution is the best a RAM machine can do, *then* stacks, counters, etc. won't help. (But, of course, he only gave upper bounds, not lower bounds.)
Radu GRIGore
CC BY-SA 2.5
2010-09-15T20:03:54.777Z
As far as I can tell, my solution does use "additional power": it is based on table lookups and integer indices, something that is not available in the DFA or NFA models. So I don't really understand how it's not answering that part of the question.
David Eppstein
CC BY-SA 2.5
2010-09-13T21:31:05.240Z
2010-09-13T21:41:41.590Z
2010-09-13T21:41:41.590Z
David Eppstein
<p>It's well-known that a regular expression can be recognized by a nondeterministic finite automaton of size proportional to the regular expression, or by a deterministic FA which is potentially exponentially larger. Furthermore, given a string $s$ and a regular expression $r$, the NFA can test membership in time proportional to $|s| \cdot |r|$, and the DFA can test membership in time proportional to $|s|$. The slowdown for the NFA arises from the fact that essentially we need to track the sets of possible states the automaton could be in, and the exponentially blowup for the DFA arises from the fact that its states are elements of the powerset of the states of the NFA.</p>

<p>Is it possible to efficiently (ie, in time better than $O(|r| \cdot |s|)$, and space better than $O(2^{|r|})$) recognize regular expressions, if we allow using more powerful machines than finite automata? (For example, are there succinctness gains to recognizing regular languages with pushdown automata, or counter machines?) </p>

CC BY-SA 2.5
2010-09-10T11:57:54.130Z
When you say that the "NFA can test membership in time proportional to $|s|\cdot|r|$" you mean that a (deterministic) RAM machine that simulates the NFA in the obvious way takes so much time? Or is there some other way to define the "run time of an NFA" that does not refer to another computational model? (Apart from the sensible but not very useful definition that says that the runtime of any NFA for the string $s$ is $|s|$.)
Radu GRIGore
CC BY-SA 2.5
2010-09-10T12:04:49.057Z
Yes, that's the right interpretation of my question.
Neel Krishnaswami
CC BY-SA 2.5
2010-09-10T13:01:28.730Z
Then it seems to me more natural to simply ask this: Is there an algorithm (on a RAM machine) that decides if a string $s$ is in the language defined by the regular expression $r$ that works in $o(|s|\cdot|r|)$ time and $o(2^{|r|})$ space? (Especially if you define the runtime of a pushdown automata also in terms of a RAM machine.)
Radu GRIGore
CC BY-SA 2.5
2010-09-12T22:55:36.280Z
I don't understand the problem exactly. Is the input a string s and a regular expression r, and the problem is to decide whether s is in the language defined by the regular expression r?
Robin Kothari
CC BY-SA 2.5
2010-09-13T08:01:01.393Z
@Robin: yes, that's it. I would like to know if you can match regular expressions more efficiently than finite automata can by using more computational power, or if extra features (e.g. stack, RAM) simply don't help.
Neel Krishnaswami
CC BY-SA 2.5
2010-09-14T10:35:05.073Z
@Magnus: you need 2^r space to determinize a regexp. Your suggestion is equivalent to simulating an NFA, which is slower than running a DFA directly (see David Eppstein's answer for a correct bound).
Neel Krishnaswami
<fl.formal-languages><automata-theory><regular-expressions>
Are there "small" machines which can efficiently match regular expressions?
Neel Krishnaswami
# Are there "small" machines which can efficiently match regular expressions?

(Asked by: username_0 on 2010-09-10)

It's well-known that a regular expression can be recognized by a nondeterministic finite automaton of size proportional to the regular expression, or by a deterministic FA which is potentially exponentially larger. Furthermore, given a string $s$ and a regular expression $r$, the NFA can test membership in time proportional to $|s| \cdot |r|$, and the DFA can test membership in time proportional to $|s|$. The slowdown for the NFA arises from the fact that essentially we need to track the sets of possible states the automaton could be in, and the exponentially blowup for the DFA arises from the fact that its states are elements of the powerset of the states of the NFA.

Is it possible to efficiently (ie, in time better than $O(|r| \cdot |s|)$, and space better than $O(2^{|r|})$) recognize regular expressions, if we allow using more powerful machines than finite automata? (For example, are there succinctness gains to recognizing regular languages with pushdown automata, or counter machines?)

**Tags:** fl.formal-languages, automata-theory, regular-expressions
> **Comments:**
> **username_1**: When you say that the "NFA can test membership in time proportional to $|s|\cdot|r|$" you mean that a (deterministic) RAM machine that simulates the NFA in the obvious way takes so much time? Or is there some other way to define the "run time of an NFA" that does not refer to another computational model? (Apart from the sensible but not very useful definition that says that the runtime of any NFA for the string $s$ is $|s|$.)  
> **username_0**: Yes, that's the right interpretation of my question.  
> **username_1**: Then it seems to me more natural to simply ask this: Is there an algorithm (on a RAM machine) that decides if a string $s$ is in the language defined by the regular expression $r$ that works in $o(|s|\cdot|r|)$ time and $o(2^{|r|})$ space? (Especially if you define the runtime of a pushdown automata also in terms of a RAM machine.)  
> **username_2**: I don't understand the problem exactly. Is the input a string s and a regular expression r, and the problem is to decide whether s is in the language defined by the regular expression r?  
> **username_0**: @Robin: yes, that's it. I would like to know if you can match regular expressions more efficiently than finite automata can by using more computational power, or if extra features (e.g. stack, RAM) simply don't help.  
> **username_0**: @Magnus: you need 2^r space to determinize a regexp. Your suggestion is equivalent to simulating an NFA, which is slower than running a DFA directly (see David Eppstein's answer for a correct bound).  

---

## 3 answers

### Answer by username_5 on 2010-09-13. Score: 20 | Accepted answer

It's easy enough to trade off time for space, as follows.

Convert the regular expression to an NFA — for concreteness in comparing algorithms, we'll assume that $r$ is the number of NFA states, so that your $O(rs)$ time bound for directly simulating the NFA is valid and your $O(2^r)$ space bound for running the converted DFA are also valid whenever you're working in a RAM that can address that much memory.

Now, partition the states of the NFA (arbitrarily) into $k$ subsets $S\_i$ of at most $\lceil r/k\rceil$ states each. Within each subset $S\_i$, we can index subsets $A\_i$ of $S\_i$ by numbers from $0$ to $2^{\lceil r/k\rceil}-1$.

Build a table $T[i,j,c,A\_i]$ where $i$ and $j$ are in the range from 0 to $k-1$, $c$ is an input symbol, and $A\_i$ is (the numerical index of) a subset of $S\_i$. The value stored in the table is (the numerical index of) a subset of $S\_j$: a state $y$ is in $T[i,j,c,A\_i]$ if and only if $y$ belongs to $S\_j$ and there is a state in $A\_i$ that transitions to $y$ on input symbol $c$.

To simulate the NFA, maintain $k$ indices, one for each $S\_i$, specifying the subset $A\_i$ of the states in $S\_i$ that can be reached by some prefix of the input. For each input symbol $c$, use the tables to look up, for each pair $i,j$, the set of states in $S\_j$ that can be reached from a state in $A\_i$ by a transition on $c$, and then use a bitwise binary or operation on the numerical indices of these sets of states to combine them into a single subset of states of $S\_j$. So, each step of the simulation takes time $O(k^2)$, and the total time for the simulation is $O(sk^2)$.

The space required is the space for all the tables, which is $O(k^2 2^{r/k})$. The time and space analysis is valid on any RAM that can address that much memory and that can do binary operations on words that are large enough to address that much memory.

The time-space tradeoff you get from this doesn't perfectly match the NFA simulation, because of the quadratic dependence on $k$. But then, I'm skeptical that $O(rs)$ is the right time bound for the NFA simulation: how do you simulate a single step of the NFA faster than looking at all of the (possibly quadratically many) transitions allowed from a currently active state to another state? Shouldn't it be $O(r^2 s)$?

In any case by letting $k$ vary you can get time bounds on a continuum between the DFA and NFA bounds, with less space than the DFA.

> **Comments:**
> **username_0**: I think your correction is correct, and your answer does answer my asked question. However, the question I wanted to ask is how much additional computational power helps. (Eg, with a counter you can match a string $a^k$ in O(1) space.) If you don't mind, I'll leave the question open for a little while longer to see if anyone knows  the answer to that....  
> **username_1**: @Neel: *If* David's solution is the best a RAM machine can do, *then* stacks, counters, etc. won't help. (But, of course, he only gave upper bounds, not lower bounds.)  
> **username_5**: As far as I can tell, my solution does use "additional power": it is based on table lookups and integer indices, something that is not available in the DFA or NFA models. So I don't really understand how it's not answering that part of the question.  
> **username_6**: Here is an alternative way to parametrize this.  Suppose we are on a RAM machine with word width $w$, where $w \ge \lg r$.  Then the NFA simulation takes $O(s r^2)$ time and $O(r/w)$ space.  The DFA simulation isn't possible if $r \ge w$ (not enough space available).  The construction in this answer sets $k \approx \lceil r/w \rceil$ and takes $O(sr^2/w^2)$ time and uses all the space available (i.e., something in the vicinity of $2^w$ space).  It is basically exploiting the bit-parallelism available in a RAM machine to do the NFA simulation faster.  

---

### Answer by username_1 on 2010-09-17. Score: 4

This is not an answer, but too long for a comment. I'm trying to explain why the question, as posed, may be hard to understand.

There are two ways to define computational complexity for a device *X*.

The first and most natural way is *intrinsic*. One needs to say how the device *X* uses the input, so that we may later look at how the size *n* of the input affects the run time of the device. One also needs to say what counts as an *operation* (or *step*). Then we simply let the device run on the input and count operations.

The second is *extrinsic*. We define computational complexity for another device *Y* and then we program *Y* to act as a simulator for *X*. Since there may be multiple ways for *Y* to simulate *X*, we need to add that we are supposed to use the best one. Let me say the same with other words: We say that *X* takes $O(f(n))$ time on an input of size *n* if there exists a simulator of *X* implemented on machine *Y* that takes $f(n)$ time.

For example, an intrinsic definition for NFA says that it takes *n* steps to process a string of length *n*; an extrinsic definition that uses a RAM machine as device *Y* says that the best known upper bound is *probably* what David Eppstein answered. (Otherwise it would be strange that (1) the best practical implementation pointed in the other answer does not use the better alternative and (2) no one here indicated a better alternative.) Note also that strictly speaking your device *X* is the regular expression, but since the NFA has the same size it is safe to take it as being the device *X* you are looking at.

Now, when you use the second kind of definition it makes little sense to ask how restricting the features of device *X* affects the running time. It does however make sense to ask how restricting the features of device *Y* affects the running time. Obviously, allowing more powerful machines *Y* might allow us to simulate *X* faster. So, if we assume one of the most powerful machines that could be implemented (this rules out nondeterministic machines, for example) and come up with a lower bound $\Omega(f(n))$, then we know that *no* less powerful machine could do better.

So, in a sense, the best answer you could hope for is a proof in something like the cell probe model that simulating an NFA needs a certain amount of time. (Note that if you take into account the conversion NFA to DFA you need time to write down the big DFA, so memory isn't the only issue there.)


---

### Answer by username_3 on 2011-02-06. Score: 4

Even if you believe that there's nothing new or old to be learned about regular expression matching, check out one of the most beautiful papers I've come across for a long time: [A play on regular expressions](http://sebfisch.github.com/haskell-regexp/regexp-play.pdf "A play on regular expressions") by S Fischer, F Huch, and T Wilke, ICFP 2010.

(MMT Chakravarty deserves the credit for recommending this paper.)

EDIT: The reason why this paper is relevant is that it describes a new technique (based on Glushkov's from the 60ies) that avoids constructing the full NFA (let alone the DFA) corresponding to the RE. What is done instead resembles running a marking algorithm similar to the well-known one for deciding acceptance of a word by an NFA on the syntax tree of the RE. Performance measurements suggest that this is competitive, even with google's recently published re2 library.

> **Comments:**
> **username_4**: A nice paper to read!!  

---

