L = for every prefix s' of s, $mid n_0(s')-n_1(s') mid leq 2 $ is regular?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite













Given language:
L = s∈(0+1)* is regular?





My Try:



Somewhere it explained as:
here we need just 6 states to recognize L.



#0 - #1 = 0
#0 - #1 = 1
#0 - #1 = 2
#0 - #1 = -1
#0 - #1 = -2


If the difference goes above 2 or below -2, we go to a dead state. All other states are accepting. This transition to dead state is possible because of the words "for every prefix s' of s" in L and that is what makes this language regular.




Can you explain please, how we count number of $0's$ and $1's$ in strings of the language?




AFAIK : I think, we need for two variables for that, I got confusion. would you include NFA/DFA for language ?










share|cite|improve this question



























    up vote
    1
    down vote

    favorite













    Given language:
    L = s∈(0+1)* is regular?





    My Try:



    Somewhere it explained as:
    here we need just 6 states to recognize L.



    #0 - #1 = 0
    #0 - #1 = 1
    #0 - #1 = 2
    #0 - #1 = -1
    #0 - #1 = -2


    If the difference goes above 2 or below -2, we go to a dead state. All other states are accepting. This transition to dead state is possible because of the words "for every prefix s' of s" in L and that is what makes this language regular.




    Can you explain please, how we count number of $0's$ and $1's$ in strings of the language?




    AFAIK : I think, we need for two variables for that, I got confusion. would you include NFA/DFA for language ?










    share|cite|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      Given language:
      L = s∈(0+1)* is regular?





      My Try:



      Somewhere it explained as:
      here we need just 6 states to recognize L.



      #0 - #1 = 0
      #0 - #1 = 1
      #0 - #1 = 2
      #0 - #1 = -1
      #0 - #1 = -2


      If the difference goes above 2 or below -2, we go to a dead state. All other states are accepting. This transition to dead state is possible because of the words "for every prefix s' of s" in L and that is what makes this language regular.




      Can you explain please, how we count number of $0's$ and $1's$ in strings of the language?




      AFAIK : I think, we need for two variables for that, I got confusion. would you include NFA/DFA for language ?










      share|cite|improve this question
















      Given language:
      L = s∈(0+1)* is regular?





      My Try:



      Somewhere it explained as:
      here we need just 6 states to recognize L.



      #0 - #1 = 0
      #0 - #1 = 1
      #0 - #1 = 2
      #0 - #1 = -1
      #0 - #1 = -2


      If the difference goes above 2 or below -2, we go to a dead state. All other states are accepting. This transition to dead state is possible because of the words "for every prefix s' of s" in L and that is what makes this language regular.




      Can you explain please, how we count number of $0's$ and $1's$ in strings of the language?




      AFAIK : I think, we need for two variables for that, I got confusion. would you include NFA/DFA for language ?







      discrete-mathematics formal-languages automata regular-language regular-expressions






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Sep 4 at 11:12

























      asked Nov 22 '15 at 11:16









      Mithlesh Upadhyay

      2,83182556




      2,83182556




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          We don’t actually count the $0$s and $1$s: we just keep track of the difference. The difference $n_0(s')-n_1(s')$ increases by $1$ every time we read a $0$ and decreases by $1$ every time we read a $1$. If the difference is always in the set $-2,-1,0,1,2$, we want to accept the string, and if it ever goes outside that set, we want to reject the string, so we need only six states: one for each value of the difference that is in that set, and one for being outside the set.



          Let $q_0$ be the initial state. We’ll design the DFA so that it’s in state $q_0$ whenever it has read the same number of $0$s and $1$s. There will also be the following states:



          • $q_1$; the machine will be in this state when the number of $0$s read is one more than the number of $1$s;

          • $q_2$; the machine will be in this state when the number of $0$s read is two more than the number of $1$s;

          • $q_-1$; the machine will be in this state when the number of $0$s read is one less than the number of $1$s;

          • $q_-2$; the machine will be in this state when the number of $0$s read is two less than the number of $1$s; and

          • $q_infty$; the machine will be in this state when the numbers of $0$s and $1$s read so far differ by more than $2$.

          The meanings assigned to the states tell you what most of the transitions must be. For instance, in state $q_0$ reading a $0$ must put the machine in state $q_1$, while reading a $1$ must put it in state $q_-1$. State $q_infty$ is a garbage (or trap) state that collects the words that are not to be accepted. The full set of transitions is:



          $$beginarrayll
          q_0overset0longrightarrow q_1&q_0overset1longrightarrow q_-1\
          q_1overset0longrightarrow q_2&q_1overset1longrightarrow q_0\
          q_2overset0longrightarrow q_infty&q_2overset1longrightarrow q_1\
          q_-1overset0longrightarrow q_0&q_-1overset1longrightarrow q_-2\
          q_-2overset0longrightarrow q_-1&q_-2overset1longrightarrow q_infty\
          q_inftyoverset0,1longrightarrow q_infty
          endarray$$



          States $q_0,q_1,q_2,q_-1$, and $q_-2$ are all acceptor states; $q_infty$ is not.






          share|cite|improve this answer




















          • Thanks for nice explanation.
            – Mithlesh Upadhyay
            Nov 23 '15 at 13:24







          • 1




            @Mithlesh: You're welcome.
            – Brian M. Scott
            Nov 23 '15 at 16:18










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "69"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1540903%2fl-s%25e2%2588%258801-for-every-prefix-s-of-s-mid-n-0s-n-1s-mid-leq-2%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          We don’t actually count the $0$s and $1$s: we just keep track of the difference. The difference $n_0(s')-n_1(s')$ increases by $1$ every time we read a $0$ and decreases by $1$ every time we read a $1$. If the difference is always in the set $-2,-1,0,1,2$, we want to accept the string, and if it ever goes outside that set, we want to reject the string, so we need only six states: one for each value of the difference that is in that set, and one for being outside the set.



          Let $q_0$ be the initial state. We’ll design the DFA so that it’s in state $q_0$ whenever it has read the same number of $0$s and $1$s. There will also be the following states:



          • $q_1$; the machine will be in this state when the number of $0$s read is one more than the number of $1$s;

          • $q_2$; the machine will be in this state when the number of $0$s read is two more than the number of $1$s;

          • $q_-1$; the machine will be in this state when the number of $0$s read is one less than the number of $1$s;

          • $q_-2$; the machine will be in this state when the number of $0$s read is two less than the number of $1$s; and

          • $q_infty$; the machine will be in this state when the numbers of $0$s and $1$s read so far differ by more than $2$.

          The meanings assigned to the states tell you what most of the transitions must be. For instance, in state $q_0$ reading a $0$ must put the machine in state $q_1$, while reading a $1$ must put it in state $q_-1$. State $q_infty$ is a garbage (or trap) state that collects the words that are not to be accepted. The full set of transitions is:



          $$beginarrayll
          q_0overset0longrightarrow q_1&q_0overset1longrightarrow q_-1\
          q_1overset0longrightarrow q_2&q_1overset1longrightarrow q_0\
          q_2overset0longrightarrow q_infty&q_2overset1longrightarrow q_1\
          q_-1overset0longrightarrow q_0&q_-1overset1longrightarrow q_-2\
          q_-2overset0longrightarrow q_-1&q_-2overset1longrightarrow q_infty\
          q_inftyoverset0,1longrightarrow q_infty
          endarray$$



          States $q_0,q_1,q_2,q_-1$, and $q_-2$ are all acceptor states; $q_infty$ is not.






          share|cite|improve this answer




















          • Thanks for nice explanation.
            – Mithlesh Upadhyay
            Nov 23 '15 at 13:24







          • 1




            @Mithlesh: You're welcome.
            – Brian M. Scott
            Nov 23 '15 at 16:18














          up vote
          1
          down vote



          accepted










          We don’t actually count the $0$s and $1$s: we just keep track of the difference. The difference $n_0(s')-n_1(s')$ increases by $1$ every time we read a $0$ and decreases by $1$ every time we read a $1$. If the difference is always in the set $-2,-1,0,1,2$, we want to accept the string, and if it ever goes outside that set, we want to reject the string, so we need only six states: one for each value of the difference that is in that set, and one for being outside the set.



          Let $q_0$ be the initial state. We’ll design the DFA so that it’s in state $q_0$ whenever it has read the same number of $0$s and $1$s. There will also be the following states:



          • $q_1$; the machine will be in this state when the number of $0$s read is one more than the number of $1$s;

          • $q_2$; the machine will be in this state when the number of $0$s read is two more than the number of $1$s;

          • $q_-1$; the machine will be in this state when the number of $0$s read is one less than the number of $1$s;

          • $q_-2$; the machine will be in this state when the number of $0$s read is two less than the number of $1$s; and

          • $q_infty$; the machine will be in this state when the numbers of $0$s and $1$s read so far differ by more than $2$.

          The meanings assigned to the states tell you what most of the transitions must be. For instance, in state $q_0$ reading a $0$ must put the machine in state $q_1$, while reading a $1$ must put it in state $q_-1$. State $q_infty$ is a garbage (or trap) state that collects the words that are not to be accepted. The full set of transitions is:



          $$beginarrayll
          q_0overset0longrightarrow q_1&q_0overset1longrightarrow q_-1\
          q_1overset0longrightarrow q_2&q_1overset1longrightarrow q_0\
          q_2overset0longrightarrow q_infty&q_2overset1longrightarrow q_1\
          q_-1overset0longrightarrow q_0&q_-1overset1longrightarrow q_-2\
          q_-2overset0longrightarrow q_-1&q_-2overset1longrightarrow q_infty\
          q_inftyoverset0,1longrightarrow q_infty
          endarray$$



          States $q_0,q_1,q_2,q_-1$, and $q_-2$ are all acceptor states; $q_infty$ is not.






          share|cite|improve this answer




















          • Thanks for nice explanation.
            – Mithlesh Upadhyay
            Nov 23 '15 at 13:24







          • 1




            @Mithlesh: You're welcome.
            – Brian M. Scott
            Nov 23 '15 at 16:18












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          We don’t actually count the $0$s and $1$s: we just keep track of the difference. The difference $n_0(s')-n_1(s')$ increases by $1$ every time we read a $0$ and decreases by $1$ every time we read a $1$. If the difference is always in the set $-2,-1,0,1,2$, we want to accept the string, and if it ever goes outside that set, we want to reject the string, so we need only six states: one for each value of the difference that is in that set, and one for being outside the set.



          Let $q_0$ be the initial state. We’ll design the DFA so that it’s in state $q_0$ whenever it has read the same number of $0$s and $1$s. There will also be the following states:



          • $q_1$; the machine will be in this state when the number of $0$s read is one more than the number of $1$s;

          • $q_2$; the machine will be in this state when the number of $0$s read is two more than the number of $1$s;

          • $q_-1$; the machine will be in this state when the number of $0$s read is one less than the number of $1$s;

          • $q_-2$; the machine will be in this state when the number of $0$s read is two less than the number of $1$s; and

          • $q_infty$; the machine will be in this state when the numbers of $0$s and $1$s read so far differ by more than $2$.

          The meanings assigned to the states tell you what most of the transitions must be. For instance, in state $q_0$ reading a $0$ must put the machine in state $q_1$, while reading a $1$ must put it in state $q_-1$. State $q_infty$ is a garbage (or trap) state that collects the words that are not to be accepted. The full set of transitions is:



          $$beginarrayll
          q_0overset0longrightarrow q_1&q_0overset1longrightarrow q_-1\
          q_1overset0longrightarrow q_2&q_1overset1longrightarrow q_0\
          q_2overset0longrightarrow q_infty&q_2overset1longrightarrow q_1\
          q_-1overset0longrightarrow q_0&q_-1overset1longrightarrow q_-2\
          q_-2overset0longrightarrow q_-1&q_-2overset1longrightarrow q_infty\
          q_inftyoverset0,1longrightarrow q_infty
          endarray$$



          States $q_0,q_1,q_2,q_-1$, and $q_-2$ are all acceptor states; $q_infty$ is not.






          share|cite|improve this answer












          We don’t actually count the $0$s and $1$s: we just keep track of the difference. The difference $n_0(s')-n_1(s')$ increases by $1$ every time we read a $0$ and decreases by $1$ every time we read a $1$. If the difference is always in the set $-2,-1,0,1,2$, we want to accept the string, and if it ever goes outside that set, we want to reject the string, so we need only six states: one for each value of the difference that is in that set, and one for being outside the set.



          Let $q_0$ be the initial state. We’ll design the DFA so that it’s in state $q_0$ whenever it has read the same number of $0$s and $1$s. There will also be the following states:



          • $q_1$; the machine will be in this state when the number of $0$s read is one more than the number of $1$s;

          • $q_2$; the machine will be in this state when the number of $0$s read is two more than the number of $1$s;

          • $q_-1$; the machine will be in this state when the number of $0$s read is one less than the number of $1$s;

          • $q_-2$; the machine will be in this state when the number of $0$s read is two less than the number of $1$s; and

          • $q_infty$; the machine will be in this state when the numbers of $0$s and $1$s read so far differ by more than $2$.

          The meanings assigned to the states tell you what most of the transitions must be. For instance, in state $q_0$ reading a $0$ must put the machine in state $q_1$, while reading a $1$ must put it in state $q_-1$. State $q_infty$ is a garbage (or trap) state that collects the words that are not to be accepted. The full set of transitions is:



          $$beginarrayll
          q_0overset0longrightarrow q_1&q_0overset1longrightarrow q_-1\
          q_1overset0longrightarrow q_2&q_1overset1longrightarrow q_0\
          q_2overset0longrightarrow q_infty&q_2overset1longrightarrow q_1\
          q_-1overset0longrightarrow q_0&q_-1overset1longrightarrow q_-2\
          q_-2overset0longrightarrow q_-1&q_-2overset1longrightarrow q_infty\
          q_inftyoverset0,1longrightarrow q_infty
          endarray$$



          States $q_0,q_1,q_2,q_-1$, and $q_-2$ are all acceptor states; $q_infty$ is not.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Nov 22 '15 at 23:48









          Brian M. Scott

          449k39497885




          449k39497885











          • Thanks for nice explanation.
            – Mithlesh Upadhyay
            Nov 23 '15 at 13:24







          • 1




            @Mithlesh: You're welcome.
            – Brian M. Scott
            Nov 23 '15 at 16:18
















          • Thanks for nice explanation.
            – Mithlesh Upadhyay
            Nov 23 '15 at 13:24







          • 1




            @Mithlesh: You're welcome.
            – Brian M. Scott
            Nov 23 '15 at 16:18















          Thanks for nice explanation.
          – Mithlesh Upadhyay
          Nov 23 '15 at 13:24





          Thanks for nice explanation.
          – Mithlesh Upadhyay
          Nov 23 '15 at 13:24





          1




          1




          @Mithlesh: You're welcome.
          – Brian M. Scott
          Nov 23 '15 at 16:18




          @Mithlesh: You're welcome.
          – Brian M. Scott
          Nov 23 '15 at 16:18

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1540903%2fl-s%25e2%2588%258801-for-every-prefix-s-of-s-mid-n-0s-n-1s-mid-leq-2%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

          Why am i infinitely getting the same tweet with the Twitter Search API?

          Carbon dioxide