CS3102 Theory of Computation Warm up: = { 0,1 | has an odd - - PowerPoint PPT Presentation
CS3102 Theory of Computation Warm up: = { 0,1 | has an odd - - PowerPoint PPT Presentation
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: =
AND to NAND
- AND:
β π = π‘π’ππ π’, ππ0π‘, ππππ0π‘ β π0 = π‘π’ππ π’ β πΊ = {π‘π’ππ π’, ππ0π‘} β π defined as the arrows
- NAND:
β π , π0, π donβt change β πΊ = π β πΊ
- In general, If we can compute a language π
with a FSA, we can compute ππ as well
2 start
No 0s
0,1 1
Some 0s
1
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
- 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
Name Decision Problem Function Language Regex Does this string match this pattern? π π = α0 the string matches 1 the string doesnβ²t π β Ξ£β π matches the pattern}
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 = π‘π’ππ π’ β πΊ = {π‘π’ππ π’, ππ0π‘} β π defined as the arrows
- NAND:
β π , π0, π donβt change β πΊ = π β πΊ
- In general, If we can compute a language π
with a FSA, we can compute ππ as well
13 start
No 0s
0,1 1
Some 0s
1
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?
16 start
0,1 1
Some 0s No 0s
1 π
π΅ππΈ
Even 1 1
Odd
ππππ
Cross-Product Construction
- 2 machines at once!
17 start
0,1 1
Some 0s
No 0s
1 π
π΅ππΈ
Even
1 1
Odd
ππππ
Start Even Some0s Even No0s Even Start Odd Some0s Odd No0s Odd
Cross-Product Construction
- 2 machines at once!
18 start
0,1 1
Some 0s
No 0s
1 π
π΅ππΈ
Even
1 1
Odd
ππππ
Start Even Some0s Even No0s Even Some0s Odd No0s Odd
1 1 1 1 1
Start Odd
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
21
?
Why not both?
Driving to a friendβs house Friend forgets to mention a fork in the directions Which way do you go?
Example Non-deterministic Finite Automaton
- πβππ ππππ‘π’1 = {
| π₯ β 0,1 β the third from last character is a 1}
22
πππ 0,1 start πππ¦π’1 πππ¦π’2 0,1 0,1 1
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
25 start
0,1 1
Some 0s No 0s
1 Even 1 1
Odd
New start
Union Using Non-Determinism
26 start
0,1 1
Some 0s No 0s
1 Even 1 1
Odd
new π π πβͺ = π 1 βͺ π 2 βͺ πππ₯ , Ξ£, πβͺ, πππ₯, πΊ
1 βͺ πΊ 2
πβͺ(π, π) = α π1 π, π if π β π 1 {π2 π, π } if π β π 2 πβͺ πππ₯, π = {π‘π’ππ π’, ππ€ππ}
Whatβs the language?
27
start
final
0, π
NFA Example
{π₯ β 0,1 β|π₯ contains 0101}
28