Top Banner
CS1010: Theory of Computation Lorenzo De Stefani Fall 2019 Lecture 3: Regular Expression
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

Mar 15, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

DFAà GNFAexample

1a

2a, b

b

1a

2 a È b

b

ε

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

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

GFAà RegularExpression

• WhileGNFAhasstatesotherthan“Start”and“Accept”– Pickastateqandremove itfromtheGNFA– Repairthetransitionsbycombiningtheregularexpressionsbyconcatenation

1a

2 a È b

b

ε

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

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

EquivalenceofRegularExpressionsandFA

Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit

Twodirectionsweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular

– Ifalanguageisregularthenthereexisits aregularexpressionthatdescribesit

9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 21