Does an algorithm exist which can determine whether one regular language matches any input another regular language matches?

前端 未结 4 1997
礼貌的吻别
礼貌的吻别 2020-12-08 10:58

Let\'s say we have regular expressions:

  • Hello W.*rld
  • Hello World
  • .* World
  • .* W.*

I would like to minimize the number

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 11:42

    This problem is called "inclusion" or "subsumption" of regular expressions, because what you are asking for, is whether the set of words matched by one regexp includes (or subsumes) the set of words matched by the other regex. Equality is a different question which usually means whether two regexps matches exactly the same words, i.e. that they are functionally equivalent. For example "a*" includes "aa*", while they are not equal.

    All known algorithms for regexp inclusion are the worst case take time exponential in the size of the regexp. But the standard algorithm is like this:

    Input r1 and r2 Output Yes if r1 includes r2

    1. Create DFA(r1) and DFA(r2)
    2. Create Neg(DFA(r1)) (which matches exactly those words r1 dont match)
    3. Create Neg(DFA(r1))x DFA(r2) (which matches exactly those words matched by Neg(DFA(r1)) and DFA(r2))
    4. Check that the automaton made in 3. does not match any word

    This works, since what you are checking is that there are no words matched by r2 that are not matched by r1.

提交回复
热议问题