270 likes | 284 Views
CSCI 3130: Formal languages and automata theory Tutorial 1. Lee Chin Ho. About me. Name: Chin Office: SHB 117 Office hour: Tuesday 11:00 – 12:00 Homework 1 is due on next Thursday. Finite Automata. Finite Automata Q - States S - Alphabets d - Transitions q 0 - Initial state
E N D
CSCI 3130: Formal languagesand automata theoryTutorial 1 Lee Chin Ho
About me • Name: Chin • Office: SHB 117 • Office hour: Tuesday 11:00 – 12:00 • Homework 1 is due on next Thursday
Finite Automata • Finite Automata • Q- States • S - Alphabets • d - Transitions • q0- Initial state • F - Final state(s) / Accepting state(s)
Deterministic Finite Automata • There must be a transition corresponding to each alphabet at each state
Nondeterministic Finite Automata • Can be at multiple states at the same time • Can go to multiple states on one alphabet • May have no transitions on an alphabet (die)
Regular Expression • The symbols Æ andeare regular expressions • Every ainS is a regular expression • If R and S are regular expressions, so are R+S, RS and R* • Remember for R*, * could be 0
Regular Languages • Languages that can be represented by a DFA / NFA / RE • DFA = NFA = RE • When asked if a language L is regular, represent L using a DFA / NFA / RE
Checklist 1. Always pay attention to S 2. For DFA, out degree of each state must be equal|S| e.g. S = {0, 1, 2}, L = {w: w begins with 01} 3. For NFA, pay attention to -transitions 4. Remember to specify the initial state 5. Make sure you have considered the input e.g. L = {w: w contains even number of 01}
Example in Lecture • q2 is a “die” state • After a 1 has appeared, any 0 appears afterwards will die • 0*1* 0 1 0, 1 0 1 q2 q0 q1 S= {0, 1}
Exercise S = {0, 1} L = {w: w is non-empty and the sum of the digits in w is divisible by 5}
Idea • Need to keep track of the sum of digits • Arrive at the state qk when sum of digits = k 0 0 0 0 0 0 1 1 1 1 1 q0 q1 … q2 q4 q3 q5
Idea • Need to keep track of the sum of digits • Arrive at the state qk when sum of digits = k • k divisible by 5 iff k mod 5 = 0 • Keep track of the remainder instead 0 0 0 0 0 1 1 1 1 q0 q1 q2 q4 q3 1
Answer • Handle the input 1 0 0 0 0 0 1 1 1 1 0 q0 q1 qs q2 q4 q3 1
Converting an NFA to a DFA 0 q0 q1 • Eliminate for each state i for each alphabet x for each state j if i can reach j using one xand(s) add an edge from i to j with label x q2 0 q0 q1 q2 0 1 q0 q1 q2 1 q0 q1 q2 1 1 1 1 q0 q1 1 q2 q3 q0 q1 q2 q3 1
Converting an NFA to a DFA • Eliminate • Remember to consider the case when the initial state can reach accepting states on (s) 1 q0 q1 q2 1 q0 q1 q2 1
Converting an NFA to a DFA • Every possible subsets of Q is a state in the DFA • Going to multiple states at the same time in the NFA = going to a subset of Q, which is now a state in the DFA q1 1 1 q0 {q0} {q1, q2} q2 1
Converting an NFA to a DFA • Empty set ∅=die S S ∅
Exercise 0 q0 q1 0 1 0, 1 1 q2
Eliminating -transitions • q0 can reach q0on 0 (q0 -> q1 -> q0) • q0can reach q1on 0 (q0 -> q1) • q1 can reach q0 on 0 (q1 -> q0) • q1 can reach q1 on 0 (q1 -> q1) • The rest of the transitions remain unchanged 0 q0 q1 0 1 0, 1 1 q2
Eliminating -transitions • q0 is the initial state • q0 can reach an accepting state on (i.e. q1) • therefore q0 is also an accepting state 0 0 0 0 q1 q0 q0 q1 0 0 1 0, 1 1 0, 1 1 1 q2 q2
Converting NFA to DFA 0 0 0 q0 q1 0 1 0, 1 1 q2
Converting NFA to DFA • Starting from initial state {q0} 0 {q0} {q0, q1} 1 {q2}
Converting NFA to DFA • Check if every state has |S| outgoing transitions • No • “Fix” {q2} and {q0, q1} • {q0, q1} goes to {q0, q1} ∪{q0, q1} = {q0, q1} on 0 • {q0, q1} goes to {q2} ∪∅ = {q2} on 1 0 {q0} {q0, q1} 0 1 1 {q1} {q2} 0 1 {q1, q2}
Converting NFA to DFA • Check if every state has |S| outgoing transitions • No • “Fix” {q1} and {q1, q2} • {q1, q2} goes to {q0, q1} ∪{q1} = {q0, q1} on 0 • {q1, q2} goes to ∅ ∪ {q1, q2}= {q1, q2} on 1 0 {q0} {q0, q1} 0 1 1 0 {q1} 1 {q2} 0 ∅ 1 0 {q1, q2} 1
Converting NFA to DFA • Check if every state has |S| outgoing transitions • No • “Fix” ∅ • ∅ = die 0 {q0} {q0, q1} 0 1 1 0 {q1} 1 0, 1 {q2} 0 ∅ 1 0 {q1, q2} 1
Converting NFA to DFA 0 0 0 q0 q1 • Check if every state has |S| outgoing transitions • Yes • Accepting states = states that contain q0 or q1 0 1 0, 1 1 q2 0 {q0, q1} {q0} 0 1 1 0 {q1} 1 0, 1 {q2} 0 ∅ 1 0 {q1, q2} 1
Converting NFA to DFA • Done 0 0 {q0, q1} {q0} 0 q0 q1 1 1 0 0 {q1} 1 0, 1 1 1 0, 1 {q2} 0 ∅ q2 1 0 1 {q1, q2}