cs1010: theory of computationcs.brown.edu/courses/csci1010/files/doc/notes/lecture-3-regex.pdf–we...
TRANSCRIPT
![Page 1: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/1.jpg)
CS1010:TheoryofComputation
LorenzoDeStefaniFall2019
Lecture3:RegularExpression
![Page 2: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/2.jpg)
Outline• WhatareRegularExpression• OperatorsinRegularExpressions• EquivalencebetweenRegularExpressionandFiniteStatesAutomata– RegularExpressionà NFA– RegularLanguageà RegularExpression
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 1
FromSipser Chapter1.3
![Page 3: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/3.jpg)
RegularExpressions
• Meansofcharacterizinglanguagesbasedontheregularoperators
• Examples:– (0È 1)0*• A0or1followedbyanynumberof0’s• Concatenationoperatorimplied
–Whatdoes(0È 1)*mean?• Allpossiblestringsof0and1orε• IfS ={0,1},thenequivalenttoS*
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 2
![Page 4: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/4.jpg)
DefinitionofRegularExpressionRisaregularexpressionifRis
1. a,forsomeainalphabetå2. ε3. Æ4. (R1È R2),whereR1andR2areregularexpressions5. (R1× R2),whereR1andR2areregularexpressions6. (R1*),whereR1isaregularexpression
Note:Thisisarecursivedefinition,commonincomputerscience• R1andR2alwayssmallerthanR,sonoissueofinfinite
recursion• Æmeanslanguagedoesnotincludeanystringsandε meansit
includestheemptystring
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 3
![Page 5: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/5.jpg)
Operatorprecedence• *hasprecedenceoverconcatenationandunion
• Concatenationhasprecedenceoverunion• Parenthesesmaychangetheprecedence• Example:(0(0È1)0)*È0
• R1=(0(0È1)0)*;R2=0• R1ÈR2
• R1=(R3)*• R4=0;R5=0È1;R6=0
• R3=R4×R5×R6• R7=0;R8=1;
• R5=R7ÈR8
Isthisdifferentfrom(0(0È1)0)*È(0) ?
Isthisdifferentfrom(00È10)*È0 ?
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 4
![Page 6: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/6.jpg)
Additionalnotationfor*• R+ =R*R=RR*– ConcatenationofatleastonestringfromR
• Rk– ShorhandnotationforconcatenationofkstringsfromR
• R+Èε= R*
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 5
![Page 7: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/7.jpg)
SomeExamples
• 0*10*=– {w|wcontainsasingle1}
• å*1å*=– {w|whasatleastone1}
• 01È 10=– {01,10}
• (0È ε)(1È ε)=– {ε,0,1,01}
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 6
![Page 8: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/8.jpg)
Testingyourunderstanding
• RÈ ⌀ =R• Rε=R• RÈε =Rifε inRor{R, ε}otherwise• R⌀ =⌀
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 7
![Page 9: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/9.jpg)
EquivalenceofRegularExpressionsandFA
Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit
Twodirectionssoweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular
– Ifalanguageisregularthenthereexistsaregularexpressionthatthatdescribesit
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 8
![Page 10: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/10.jpg)
Proof:RegularExpressionè RegularLanguage
• Proofidea:GivenaregularexpressionRdescribingalanguageL,wewill….– ShowthatsomeFArecognizesit– UseNFAsincemaybeeasierandequivalenttoDFA
• Howdowedothis?–WewillusedefinitionofaregularexpressionandshowthatwecanbuildaFAcoveringeachstep.• Steps1,2and3ofdefinition(handlealphabetsymbols,ε,andÆ )• Steps4,5and6(handleunion,concatenation,andstar)
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 9
![Page 11: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/11.jpg)
Proof:RegularExpressionè RegularLanguage
Forsteps1-3weconstructtheFAbelow.Asareminder:
1. a,forsomeainalphabetå2. ε3. Æ
aε Æ
a
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 10
![Page 12: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/12.jpg)
ProofContinued
• Forsteps4-6(union,concatenationandstar)weusetheresultwepreviouslyobtainedshowingthatFAareclosedunderunion,concatenation,andstar
• WehaveshownhowtoconvertaRegularExpressionintoaFAwhichrecognizesthesamelanguage
• Bycorollary,saidlanguageisregular.
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 11
![Page 13: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/13.jpg)
Example:RegularExpressionè NFA
Convert(abÈ a)*toanNFA(example1.56page68)– Outlineofrequiredsteps:• Handlea• Handleb• Handleab• HandleabÈ a• Handle(abÈ a)*
– Sometimesε-transitionsmayappearunnecessaryofconfusing• Besystematic!Alwaysstartincludingε-transitions!
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 12
![Page 14: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/14.jpg)
EquivalenceofRegularExpressionsandFA
Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit
Twodirectionsweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular
– Ifalanguageisregular,thenthereexistsaregulatexpressionthatdescribesit
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 13
![Page 15: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/15.jpg)
Proof:RegularLanguageè RegularExpression
• Proofstrategy:– AregularlanguageisacceptedbyaDFA–WeneedtoshowthatcanconvertanyDFAtoaregularexpression
• Twosteps:–WeconstructaGeneralizedNon-deterministicFiniteStateAutomaton(GNFA)fromaDFA
–WeconvertaGFAintoaRegularExpression
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 14
![Page 16: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/16.jpg)
GNFAinSpecialForm
• GFAsareNFAswheretransitionmaybelabeledwithregularexpressions ratherthanjustsymbolsfromå
• GFAsinspecialformhavethefollowingproperties– Onestartstatewithoutgoingarrowsgoingtoallotherstatesbutnoincomingarrows
– Onesingleacceptstateswithnooutgoingarrowsandarrowsincomingfromanyotherstate
– Allotherstateshavearrowsincomingandoutgoingtoeverystate,includingthemselves
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 15
![Page 17: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/17.jpg)
DFAà GNFA
1. Addanewstartstatewithonearrowlabeledwithε tooldstartstate
2. Addnewacceptstatewitharrowslabeledwithεfromalloldacceptstates
3. Ifanyarrowfromremainingstateshasmultiplelabels,replacethemwithequivalentRegularExpressionE.g.,a,b ->aÈ b
4. AddremainingarrowsmarkedasÆ
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 16
![Page 18: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/18.jpg)
DFAà GNFAexample
1a
2a, b
b
1a
2 a È b
b
Sε
ε
EdgesmarkedwithÆ areredundantandmaybeconfusing!
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 17
A
![Page 19: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/19.jpg)
GNFAà RegularExpression
• WeproceedinaseriesofstepsreducingthenumberofstatesoftheGFAto2(startandaccept)• TheRegularExpressionleftontheonlyremainingarrowisequivalenttotheGFAand,hence,theDFA
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 18
![Page 20: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/20.jpg)
GFAà RegularExpression
• WhileGNFAhasstatesotherthan“Start”and“Accept”– Pickastateqandremove itfromtheGNFA– Repairthetransitionsbycombiningtheregularexpressionsbyconcatenation
1a
2 a È b
b
Sε
ε
S
2 a È b
a*b
ε
S
a*b(a È b)*
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 19
A A
A
![Page 21: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/21.jpg)
Formalizationoftheproof
Thetextbookprovidesarigorousproofbyinduction:– CONVERT:proceduretotransformGNFAGinto
RegularExpression– Statement:L(G)=CONVERT(G)• Base:Ghasonly2states• Inductivehypothesis:StatementholdsifGhasi>=2states• Inductivestep:weshowitholdsifGhasi+1states
1. LetG’denotetheversionofGwithi statesobtainedafteroneapplicationofCONVERT(G)
2. WearguethatifastringisacceptedbyGitwillalsobeacceptedbyG’andviceversa
3. Applyinductivehypothesis
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 20
![Page 22: CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We need to show that can convert any DFA to a regular expression •Two steps: –We](https://reader033.vdocuments.site/reader033/viewer/2022042015/5e73f21f1788886aed2fd91b/html5/thumbnails/22.jpg)
EquivalenceofRegularExpressionsandFA
Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit
Twodirectionsweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular
– Ifalanguageisregularthenthereexisits aregularexpressionthatdescribesit
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 21