Slides - Computer Science - University of Colorado Boulder

CSCI3434:Theoryof
Computation
Lecture02:RegularLanguagesandFiniteAutomata
AshutoshTrivedi([email protected])
DepartmentofComputerScience,UniversityofColoradoBoulder
Alphabet,Strings,andLanguages
• Analphabet Σ = {𝑎, 𝑏, 𝑐}isafinitesetofletters/symbols.
• Astringoveranalphabet Σ isfinitesequenceofsymbols,e.g.
• sequences𝑐𝑎𝑏, 𝑏𝑎𝑎, and𝑎𝑎𝑎 aresomestringsoverΣ = {𝑎, 𝑏, 𝑐}
• sequences𝜖, 0, 1, 00,and01 aresomestringsoverΣ = {0, 1}
• Σ∗ isthesetofallstringsoverΣ,e.g.𝑎𝑎𝑏𝑏𝑎𝑎 ∈ Σ∗ ,
• Naturally,Alanguage 𝐿 isacollection/setofstringsoversome
alphabet,i.e.𝐿 ⊆ Σ∗ e.g.,
• 𝐿4546 = {𝑤 ∈ Σ ∗ ∶ 𝑤is of even length}
• 𝐿 9: ;: = {𝑤 ∈ Σ ∗ : 𝑤is of the form 𝑎6 𝑏 6for 𝑛 ≥ 0}
ProgramstoAcceptLanguages
• Wesaythataprogramaccepts alanguage,ifforeverystringinput,it
returnsyes,ifthestringisinthelanguageandno otherwise.
• Let’swriteprogramstoacceptlanguages𝐿4546 and𝐿 9: ; : .
• Let’sconsidermorefancylanguages:
•
•
•
•
Languageofallthreecolorablegraphs
Languageofallprimenumbers
Languageofallsortedarrays
Languageofalltruetheoremsinagivenlogic
• Canwewriteprogramstoacceptallthelanguages?
• DecidableandUndecidableLanguages
Existenceofundecidablelanguages.
Proof.
1. Setofallprogramsoversomeinstruction:
• 𝑃 = {𝜖, 0, 1, 00, 01, 10, 11, … }overbinaryalphabetΣ = {0, 1}.
2. SetofallstringsoveralphabetΣ = 0, 1 :
• 𝑆 = {𝜖, 0, 1, 00, 01, 10, 11, … }
3. SetofalllanguagesoveralphabetΣ = 0, 1 are2D e,g,
• 𝐿E = 0, 1 , 𝐿F = 00, 11, 11, 10 , 𝐿G = ∅
4. FromCantor’stheorem,weknowthat 2D > 𝑆 .
5. Theremustbesomelanguagesforwhichonecannotwriteacceptor
programs,i.e.undecidablelanguages.
ProgrammingExercise-I
StringMatchingProblem𝑀𝐴𝑇𝐶𝐻(𝐴, 𝐵)
Input:Listsofstrings𝐴 = 𝑠E , 𝑠F , … , 𝑠6 and𝐵 = 𝑡E, 𝑡F , … , 𝑡6 ofequallength.
Output:YES ifthereisasequenceofcombiningelementsthatproducessame
stringforbothlists.NO,otherwise.
Formally,decidewhetherthereexistsafinitesequence(ofanylength)
1 ≤ 𝑖E, 𝑖F, … , 𝑖V ≤ 𝑛
suchthat
𝑠WX 𝑠WY … 𝑠Z[ = 𝑡WX 𝑡WY … 𝑡W: .
ProgrammingExercise-I
Example1:
Input:𝐴 = 110,0011,0110 and𝐵 = 110110,00,110 .
Output:YES.
Sincesequence2,3,1givesthesamestrings
𝑠F 𝑠G𝑠E = 00110110110 and𝑡F𝑡G 𝑡E = 00110110110
Example2:
𝐴 = 0011,11,1101 and𝐵 = 101,011,110 .
Example3:
𝐴 = 100,0,1 and𝐵 = 1,100,0 .
FiniteStateAutomata
Coin
FiniteStateAutomata
𝐶
𝑆
No_coin
Ready_dispence
Not_ready
• Introducedfirstbytwoneuro-psychologistsWarrenS.McCulloughand
WalterPittsin1943asamodelforhumanbrain.
• Finiteautomatacannaturallymodelmicroprocessors andevensoftware
programsworkingonvariableswithboundeddomain
• Captureso-calledregularlanguagesthatoccurinmanydifferentfields
(regularexpression,monadicsecond-orderlogic,algebra)
• Nicetheoreticalproperties
• Applicationsindigitalcircuit/protocolverification, compilers,pattern
recognition,etc.
Calculemus!
• Letusobserveourmentalprocesswhilewecomputethefollowing:
•
•
•
•
•
•
•
•
•
Recognizelanguageofstringsofanevenlength.
Recognizelanguageofbinarystringswithanevennumberof0’s.
Recognizelanguageofbinarystringswithanoddnumberof0’s.
Recognizelanguageofstringscontainingyouridentikey.
Recognizelanguageofbinary(decimal)stringsmultipleof2.
Recognizelanguageofbinary(decimal)stringsmultipleof3.
Recognizelanguageofbinarystringswithequalnumberof0’sand1’s.
Recognizelanguageofbinarystringsoftheform06 16
Recognizelanguageofbinarystringswithaprimenumberof1’s
FiniteStateAutomata:Examples
1.Automatonacceptingstringsofevenlength:
0, 1
𝐸
𝑂
0, 1
FiniteStateAutomata:Examples
2.Automatonacceptingstringswithanevennumberof1’s:
1
0
𝑂
𝐸
1
0
FiniteStateAutomata:Examples
3.Automatonacceptingbinarystringscharacterizinganevennumber:
1
0
𝑂
𝐸
0
1
FiniteStateAutomata:Definition
1
0
𝐸
𝑂
1
Afinitestateautomatonisatuple
(𝑆, Σ, 𝛿, 𝑠_ , 𝐹),where:
• 𝑆isafinitesetcalledthestates,
0
• Σ isafinitesetcalledthealphabet,
• 𝛿 ∶ 𝑆×Σ → 𝑆isthetransitionfunction,
• 𝑠_ ∈ 𝑆isthestartstate, and
• 𝐹 ⊆ 𝑆 isthesetofacceptstates.
Example:Theautomatoninthefigure abovecanberepresentedas(𝑆, Σ, 𝛿, 𝑠_ , 𝐹),where
• 𝑆 = {𝐸, 𝑂}, Σ = {0,1}, 𝑠_ = 𝐸, 𝐹 = {𝐸},
• andtransitionfunction 𝛿 issuchthat
• 𝛿(𝐸, 0) = 𝐸,
• 𝛿(𝐸, 1) = 𝑂,and
• 𝛿(𝑂, 0) = 𝑂,
• 𝛿(𝑂, 1) = 𝐸.
StateDiagram
Let’sdrawthestatediagramofthefollowingautomaton(𝑆, Σ, 𝛿, 𝑠E , 𝐹):
• 𝑆 = {𝑠E , 𝑠F , 𝑠G }
• Σ = {0,1},
• 𝛿isgiveninatabularformbelow:
𝑆
0
1
𝑠E
𝑠E
𝑠F
𝑠F
𝑠G
𝑠F
𝑠G
𝑠F
𝑠F
• 𝑠E istheinitialstate,and
• 𝐹 = {𝑠F }.
Whichlanguagedoesitaccept?
Semantics(Meaning)ofFiniteStateAutomata
1
0
𝑂
𝐸
1
0
Afinitestateautomaton(DFA) isatuple
(𝑆, Σ, 𝛿, 𝑠_ , 𝐹),where:
• 𝑆isafinitesetcalledthestates,
• Σ isafinitesetcalledthealphabet,
• 𝛿 ∶ 𝑆×Σ → 𝑆isthetransitionfunction,
• 𝑠_ ∈ 𝑆isthestartstate, and
• 𝐹 ⊆ 𝑆 isthesetofacceptstates.
• Acomputation orarun ofaDFAonastring𝑤 = 𝑎_ 𝑎E … 𝑎6cE isthefinitesequence
𝑠_ , 𝑎E , 𝑠E , 𝑎F , … , 𝑎6cE , 𝑠6 where𝑠0 isthestartingstate,and𝛿(𝑠WcE , 𝑎W ) = 𝑠WdE .
• Arunisaccepting ifthelaststateoftheunique computation isanacceptstate,i.e.𝑠6 ∈ 𝐹.
• The Language ofaDFA𝐴
𝐿(𝐴) = {𝑤 ∶theunique runof𝐴on𝑤isaccepting}.
• Alanguageiscalledregular ifitisacceptedbyafinitestateautomata.
Examples
•
•
•
•
•
•
•
•
•
•
•
Recognizelanguageofstringsofanevenlength.
Recognizelanguageofbinarystringswithanevennumberof0’s.
Recognizelanguageofbinarystringswithanoddnumberof0’s.
Recognizelanguageofstringscontainingyouridentikey.
Recognizelanguageofbinary(decimal)stringsmultipleof2.
Recognizelanguageofbinary(decimal)stringsmultipleof3.
Recognizelanguageofbinarystringswithequalnumberof0’sand1’s.
Recognizelanguageofbinarystringsoftheform06 16
Recognizelanguageofbinarystringswithaprimenumberof1’s
Recognizelanguageofbinarystringsthatendwitha0.
Recognizelanguageofbinarystringsthatbeginwitha1.
PropertiesofRegularLanguages
• LetAandBbelanguages (remembertheyaresets).Wedefinethe
followingoperationsonthem:
• Union:𝐴 ∪ 𝐵 = {𝑤 ∶ 𝑤 ∈ 𝐴or𝑤 ∈ 𝐵}
• Concatenation:𝐴𝐵 = {𝑤𝑣 ∶ 𝑤 ∈ 𝐴and𝑣 ∈ 𝐵}
• Closure (Kleene Closure,orStar):
A∗ = {𝑤E 𝑤F … 𝑤h ∶ 𝑘 ≥ 0and𝑤W ∈ 𝐴}.
Or,𝐴∗ =∪Wj_ 𝐴W where𝐴_ = ∅, 𝐴E = 𝐴, 𝐴F = 𝐴𝐴,andsoon.
• Definethenotionofasetbeingclosedunderanoperation(say,
ℕand×).
Theorem.Regularlanguagesareclosedunder union, intersection,concatenation,andKleene star.