CS3102 Theory of Computation Warm up: πππ = {π¦ β 0,1 β |π¦ has an odd number of 1s} Write a regex for πππ π (i.e. πππ , i.e. the complement of πππ )
AND to NAND β’ AND: β π = π‘π’ππ π’, ππ0π‘, ππππ0π‘ 0,1 β π 0 = π‘π’ππ π’ Some 0s 0 β πΊ = {π‘π’ππ π’, ππ0π‘} β π defined as the arrows 0 start β’ NAND: β π , π 0 , π donβt change β πΊ = π β πΊ 1 1 β’ In general, If we can compute a language π No 0s with a FSA, we can compute π π as well 2
Logistics β’ Homework due Tonight and Friday β’ Youβll have an assignment due the Friday you return from the break (no early deadline) β’ Quiz due Tuesday 3
Last Time β’ Regular Expressions β Equivalent to FSAs (but we havenβt shown that yet) 4
Regular Expressions Name Decision Problem Function Language π β Ξ£ β π matches the pattern} Regex Does this string π π = α 0 the string matches the string doesn β² t match this 1 pattern? β’ A way of describing a language β’ Give a βpatternβ of the strings, every string matching that pattern is in the language β’ Examples: β (π|π)π matches : ππ and ππ β π|π β π matches : π , ππ , ππ , πππ , πππ , πππ , πππ , β¦ 5
FSA = Regex β’ Finite state Automata and Regular Expressions are equivalent models of computing β’ Any language I can represent as a FSA I can also represent as a Regex (and vice versa) β’ How would I show this? 6
Showing FSA β€ Regex β’ Show how to convert any FSA into a Regex for the same language β’ Weβre going to skip this: β Itβs tedious, and people virtually never go this direction in practice, but you can do it (see textbook theorem 9.12) 7
Showing Regex β€ FSA β’ Show how to convert any regex into a FSA for the same language β’ Idea: show how to build each βpieceβ of a regex using FSA 8
βPiecesβ of a Regex β’ Empty String: β Matches just the string of length 0 β Notation: π or ββ β’ Literal Character β Matches a specific string of length 1 β Example: the regex π will match just the string π β’ Alternation/Union β Matches strings that match at least one of the two parts β Example: the regex π|π will match π and π β’ Concatenation β Matches strings that can be dividing into 2 parts to match the things concatenated β Example: the regex π π π will match the strings ππ and ππ β’ Kleene Star β Matches strings that are 0 or more copies of the thing starred β Example: π π π β will match π , π , or either followed by any number of π βs 9
FSA for the empty string 10
FSA for a literal character 11
FSA for Alternation/Union ⒠Tricky⦠⒠What does it need to do? 12
Recall: AND to NAND β’ AND: β π = π‘π’ππ π’, ππ0π‘, ππππ0π‘ 0,1 β π 0 = π‘π’ππ π’ Some 0s 0 β πΊ = {π‘π’ππ π’, ππ0π‘} β π defined as the arrows 0 start β’ NAND: β π , π 0 , π donβt change β πΊ = π β πΊ 1 1 β’ In general, If we can compute a language π No 0s with a FSA, we can compute π π as well 13
Computing Complement β’ If FSA π = (π , Ξ£, π, π 0 , πΊ) computes π β’ Then FSA π β² = (π , Ξ£, π, π 0 , π β πΊ) computes ΰ΄€ π β’ Why? β Consider string π₯ β Ξ£ β β π₯ β π means it ends at some state π β πΊ , which will be non-final in π β² and therefore it will return False β π₯ β π means it ends at some state π β πΊ , which will be final in π β² and therefore it will return True 14
Computing Union β’ Let FSA π 1 = (π 1 , Ξ£, π 1 , π 01 , πΊ 1 ) compute π 1 β’ Let π 2 = (π 2 , Ξ£, π 2 , π 02 , πΊ 2 ) compute π 1 β’ Will there always be some automaton π βͺ to compute π 1 βͺ π 2 β’ What must π βͺ do? β Somehow end up in a final state if either π 1 or π 2 did β Idea: build π βͺ to βsimulateβ both π 1 and π 2 15
Example β’ π΅ππΈ βͺ πππ β What is the resulting language? π π΅ππΈ 0,1 Some 0s 0 π πππ 1 0 0 start Even 0 Odd 1 1 No 0s 1 16
Cross-Product Construction β’ 2 machines at once! Start Start Odd Even π π΅ππΈ 0,1 Some 0s 0 start 0 Some0s Some0s Odd Even 1 No 0s 1 π πππ 1 No0s No0s 0 Even Odd Odd Even 0 17 1
Cross-Product Construction β’ 2 machines at once! Start Start Odd Even π 1 π΅ππΈ 0,1 Some 0s 0 0 1 start 0 0 0 Some0s Some0s Odd Even 1 1 No 0s 1 0 0 π πππ 1 1 No0s No0s 0 Even Odd Odd Even 1 0 18 1
Cross Product Construction β’ Let FSA π 1 = (π 1 , Ξ£, π 1 , π 01 , πΊ 1 ) compute π 1 β’ Let π 2 = (π 2 , Ξ£, π 2 , π 02 , πΊ 2 ) compute π 1 β’ π βͺ = (π 1 Γ π 2 , Ξ£, π βͺ , (π 01 , π 02 ), πΊ βͺ ) computes π 1 βͺ π 2 β π βͺ π 1 , π 2 , π = π 1 π 1 , π , π 2 π 2 , π β πΊ βͺ = π 1 , π 2 β π 1 Γ π 2 π 1 β πΊ 1 or π 2 β πΊ 2 } β’ How could we do intersection? 19
Non-determinism β’ Things could get easier if we βrelaxβ our automata β’ So far: β Must have exactly one transition per character per state β Can only be in one state at a time β’ Non-deterministic Finite Automata: β Allowed to be in multiple (or zero) states! β Can have multiple or zero transitions for a character β Can take transitions without using a character β Models parallel computing 20
Nondeterminism Driving to a friendβs house Friend forgets to mention a fork in the directions Which way do you go? Why not both? ? 21
Example Non-deterministic Finite Automaton β’ πβππ ππππ‘π’1 = { π₯ β 0,1 β the third from last character is a 1} | 0,1 πππ¦π’2 start πππ πππ¦π’1 1 0,1 0,1 22
23
Non-Deterministic Finite State Automaton β’ Implementation: β Finite number of states β One start state β βFinalβ states β Transitions: (partial) function mapping state-character (or epsilon) pairs to sets of states β’ Execution: β Start in the initial βstateβ β Enter every state reachable without consuming input ( π» -transitions) β Read each character once, in order (no looking back) β Transition to new states once per character (based on current states and character) β Enter every state reachable without consuming input ( π» -transitions) β Return True if any state you end is final β’ Return False if every state you end in is non-final 24
Union Using Non-Determinism 0,1 Some 0s 0 New start 0 start 1 1 No 0s 1 0 Even 0 Odd 25 1
Union Using Non-Determinism 0,1 Some 0s 0 π new 0 start π 1 1 No 0s 1 0 π βͺ = π 1 βͺ π 2 βͺ πππ₯ , Ξ£, π βͺ , πππ₯, πΊ 1 βͺ πΊ 2 Even 0 Odd π βͺ (π, π) = α π 1 π, π if π β π 1 {π 2 π, π } if π β π 2 26 π βͺ πππ₯, π = {π‘π’ππ π’, ππ€ππ} 1
Whatβs the language? 0, π final start 27
NFA Example {π₯ β 0,1 β |π₯ contains 0101} 28
Recommend
More recommend