zoukankan      html  css  js  c++  java
  • Pumping lemma for regular languages

    http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_languages


    In the theory of formal languages, the pumping lemma for regular languages describes an essential property of all regular languages. Informally, it says that all sufficiently long words in a regular language may be pumped — that is, have a middle section of the word repeated an arbitrary number of times — to produce a new word which also lies within the same language.

    Specifically, the pumping lemma says that for any regular language L there exists a constant p such that any word w in L with length at least p can be split into three substrings, w = xyz, where the middle portion y must not be empty, such that the words xzxyzxyyzxyyyz, … constructed by repeating y an arbitrary number of times (including zero times) are still in L. This process of repetition is known as "pumping". Moreover, the pumping lemma guarantees that the length of xy will be at most p, imposing a limit on the ways in which w may be split. Finite languages trivially satisfy the pumping lemma by having p equal to the maximum string length in L plus one.

    The pumping lemma was first proved by Dana Scott and Michael Rabin in 1959.[1] It was rediscovered shortly after by Yehoshua Bar-HillelMicha A. Perles, and Eli Shamir in 1961.[2] It is useful for disproving the regularity of a specific language in question. It is one of a few pumping lemmas, each with a similar purpose.

    Formal statement[edit source | editbeta]

    Let L be a regular language. Then there exists an integer p ≥ 1 depending only on L such that every string w in L of length at least p (p is called the "pumping length"[3]) can be written as w = xyz (i.e., w can be divided into three substrings), satisfying the following conditions:

    1. |y| ≥ 1;
    2. |xy| ≤ p
    3. for all i ≥ 0, xyiz ∈ L

    y is the substring that can be pumped (removed or repeated any number of times, and the resulting string is always in L). (1) means the loop y to be pumped must be of length at least one; (2) means the loop must occur within the first p characters. |x| must be smaller than p (conclusion of (1) and (2)), apart from that there is no restriction on x and z.

    In simple words, for any regular language L, any sufficiently long word w (in L) can be split into 3 parts. i.e. w = xyz , such that all the strings xykz for k≥0 are also in L.

    Below is a formal expression of the Pumping Lemma.

    
egin{array}{l}                                                                                                                                       
(forall  Lsubseteq Sigma^*)  \                                                                                                                     
quad      (mbox{regular}(L) Rightarrow \                                                                                                           
quad      ((exists pgeq 1) ( (forall win L) ((|w|geq p) Rightarrow \                                                                           
quadquad ((exists x,y,z in Sigma^*) (w=xyz land (|y|geq 1 land |xy|leq p land                                                                
(forall igeq 0)(xy^izin L))))))))                                                                                                                   
end{array}

    Use of lemma[edit source | editbeta]

    The pumping lemma is often used to prove that a particular language is non-regular: a proof by contradiction (of the language's regularity) may consist of exhibiting a word (of the required length) in the language which lacks the property outlined in the pumping lemma.

    For example the language L = {anbn : n ≥ 0} over the alphabet Σ = {ab} can be shown to be non-regular as follows. Let wxyzp, and i be as used in the formal statement for the pumping lemma above. Let w in L be given by w = apbp. By the pumping lemma, there must be some decomposition wxyz with |xy| ≤ p and |y| ≥ 1 such that xyiz in L for every i ≥ 0. Using |xy| ≤ p, we know y only consists of instances of a. Moreover, because |y| ≥ 1, it contains at least one instance of the letter a. We now pump y up: xy2z has more instances of the letter a than the letter b, since we have added some instances of a without adding instances of b. Therefore xy2z is not in L. We have reached a contradiction. Therefore, the assumption that L is regular must be incorrect. Hence L is not regular.

    The proof that the language of balanced (i.e., properly nested) parentheses is not regular follows the same idea. Given p, there is a string of balanced parentheses that begins with more than p left parentheses, so that y will consist entirely of left parentheses. By repeating y, we can produce a string that does not contain the same number of left and right parentheses, and so they cannot be balanced.

    Proof of the pumping lemma[edit source | editbeta]

    For every regular language there is a finite state automaton (FSA) that accepts the language. The number of states in such an FSA are counted and that count is used as the pumping length p. For a string of length at least p, let s0 be the start state and let s1, ..., sp be the sequence of the next p states visited as the string is emitted. Because the FSA has only p states, within this sequence of p + 1 visited states there must be at least one state that is repeated. Write S for such a state. The transitions that take the machine from the first encounter of state S to the second encounter of state S match some string. This string is called y in the lemma, and since the machine will match a string without the y portion, or the string y can be repeated any number of times, the conditions of the lemma are satisfied.

    For example, the following image shows an FSA.

    An automat accepting the language a(bc)*d.svg

    The FSA accepts the string: abcd. Since this string has a length which is at least as large as the number of states, which is four, the pigeonhole principle indicates that there must be at least one repeated state among the start state and the next four visited states. In this example, only q1is a repeated state. Since the substring bc takes the machine through transitions that start at state q1 and end at state q1, that portion could be repeated and the FSA would still accept, giving the string abcbcd. Alternatively, the bc portion could be removed and the FSA would still accept giving the string ad. In terms of the pumping lemma, the string abcd is broken into an x portion a, a y portion bc and a z portion d.

    General version of pumping lemma for regular languages[edit source | editbeta]

    If a language L is regular, then there exists a number p ≥ 1 (the pumping length) such that every string uwv in L with |w| ≥ p can be written in the form

    uwv = uxyzv

    with strings xy and z such that |xy| ≤ p, |y| ≥ 1 and

    uxyizv is in L for every integer i ≥ 0.[4]

    This version can be used to prove many more languages are non-regular, since it imposes stricter requirements on the language.

    Converse of lemma not true[edit source | editbeta]

    Note that while the pumping lemma states that all regular languages satisfy the conditions described above, the converse of this statement is not true: a language that satisfies these conditions may still be non-regular. In other words, both the original and the general version of the pumping lemma give a necessary but not sufficient condition for a language to be regular.

    For example, consider the following language Legin{align}L & = {uvwxy : u,y in {0,1,2,3}^*; v,w,x in {0,1,2,3} and (v=w or v=x or x=w)} \ & cup {w : w in {0,1,2,3}^*and 	ext {precisely 1/7 of the characters in }w 	ext{ are 3's}}end{align}.

    In other words, L contains all strings over the alphabet {0,1,2,3} with a substring of length 3 including a duplicate character, as well as all strings over this alphabet where precisely 1/7 of the string's characters are 3's. This language is not regular but can still be "pumped" with p = 5. Suppose some string s has length at least 5. Then, since the alphabet has only four characters, at least two of the five characters in the string must be duplicates. They are separated by at most three characters.

    • If the duplicate characters are separated by 0 characters, or 1, pump one of the other two characters in the string, which will not affect the substring containing the duplicates.
    • If the duplicate characters are separated by 2 or 3 characters, pump 2 of the characters separating them. Pumping either down or up results in the creation of a substring of size 3 that contains 2 duplicate characters.
    • The second condition of L ensures that L is not regular: i.e., there are an infinite number of strings that are in L but cannot be obtained by pumping some smaller string in L.

    For a practical test that exactly characterizes regular languages, see the Myhill-Nerode theorem. The typical method for proving that a language is regular is to construct either a finite state machine or a regular expression for the language.

    See also[edit source | editbeta]

  • 相关阅读:
    重构改善既有代码设计--重构手法 之重新组织你的函数总结
    重构改善既有代码设计--重构手法09:Substitute Algorithm (替换算法)
    重构改善既有代码设计--重构手法08:Replace Method with Method Object (以函数对象取代函数)
    重构改善既有代码设计--重构手法07:Remove Assignments to Parameters (移除对参数的赋值)
    重构改善既有代码设计--重构手法06:Split Temporary Variable (分解临时变量)
    重构改善既有代码设计--重构手法05:Introduce Explaining Variable (引入解释性变量)
    重构改善既有代码设计--重构手法04:Replace Temp with Query (以查询取代临时变量)
    leetcode-441-Arranging Coins
    leetcode-438-Find All Anagrams in a String
    leetcode-434-Number of Segments in a String
  • 原文地址:https://www.cnblogs.com/threef/p/3251043.html
Copyright © 2011-2022 走看看