3

I'm studying some theory of computation at the moment and, as is implied, it is very theoretical.

I can convert from regex to NFAs to DFAs pretty easily, I can understand that.

But since all NFAs can be converted to DFAs and (I'm pretty sure) grep commands in UNIX use regex to determine matching strings, what is the most used Finite Automata, DFA or NFA?

In my experience (not much), DFAs are generally much simpler to use when representing a regular language, and are also deterministic so should always be chosen over NFAs.

NFAs branch off to multiple outcomes, require recursive functions and just seem more awkward to me.

I know compilers is another practical use of finite automata.

My, question... why learn/use both. DFAs seem perfectly fine to me.

Thanks for any answers!

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114
  • read [Why is non-determinism useful concept?](http://cs.stackexchange.com/questions/22472/why-is-non-determinism-useful-concept/22481#22481) – Grijesh Chauhan Dec 01 '15 at 04:55

2 Answers2

5

DFAs are generally faster and more scaleable. Determinizing and Minimizing a NFA is sometimes costly. So it could be skipped if the automaton is used only once.

The advantages of NFAs (Thompson-NFAs, Glushkov-NFAs, bit parallel NFAs) are:

  • they can be expressed more concisely
  • they can record submatches (e.g. for regex replace)
  • they can be translated on the fly to a non-minimized DFA

Besides, Regex-NFAs used in common programming languages (Backtracking-NFA, e.g. in Python, Perl, Java, .NET, not in grep):

  • are even slower than upper NFAs
  • support greedy, nongreedy and possesive modes
  • but can use lookaheads/lookbehinds
  • and can use backreferences (and these cannot be translated to a DFA)

Compilers almost always use minimized DFAs for lexing. Regex Search uses DFAs or hybrid DFA/NFAs (the latter for submatch recognition). The kind of NFA used in Programming languages is the most powerful (regarding the features), but also the slowest.

CoronA
  • 7,717
  • 2
  • 26
  • 53
  • Maybe add that some regular languages represented as DFA will causes state explosion, e.g. some unbound matching – Mattias Wadman Oct 21 '15 at 14:03
  • Also there can be a resource tradeoff. Determinization and minimization can be time and memory intensive so it's only interesting if the resulting automaton will be used a lot. – Mattias Wadman Oct 21 '15 at 14:11
  • 1
    I added the resource tradeoff to my answer. Am I right that the first comment refers to the fact that DFAs states are only limited by O(2^n) where n is the number of states in the NFA? – CoronA Oct 22 '15 at 05:29
0

I think it is simpler to convert a regression to NFA than DFA. It is difficult to directly convert a regression to DFA.

windkl
  • 13
  • 4