Lecture # 04 (NFA)
Lecture # 04 (NFA)
(CSC-161)
Mr. Faizan Ali (Teaching Assistant)
Components of an NFA
• An NFA is defined by five elements:
• (Q, Σ, δ, q₀, F)
Cont..
Example-01
• NDFA (Q, Σ, δ, q₀, F)
1 0 0
q0 1 q1 0 q2
q0
q0
Why Nondeterministic finite automata?
q0 0
0 q0
0
0
q2
q1
• You now have multiple choices, which represent a nondeterministic finite automaton (NFA).
• There are multiple possible moves at each step.
• That’s why we use an NFA for regular languages (Accept/Reject).
• δ x Σ => Q
• (q0, q0, q0) x (0.1) -> 2Q -> 23 =8
• q0 0 0
• q0 1 q0
• q1 0 q1
• q1 1 q2
• q2 0 q 0 q1
• q2 1 q 0 q2
q1 q2
DFA and NFA
DFA NDFA/NFA
One transition per input symbol per state. Multiple transitions per input or ε (null) moves.
Only one possible next state. Multiple choices possible next states.
Accepted if one path reaches a final state. Accepted if at least one path reaches a final
state.
NFA Designing
Nondeterministic Finite Automata Designing
• Examples:
• Strings ending with “10"
• Strings containing "ab"
• Any given regular expression pattern
Cont..
0,1
q0 1 q1 0,1 q2 Final
q2 a,b
NFA Examples
• Design an NFA with ∑ = {0, 1} for all binary strings where the second last bit is 1.
• Solution
• The language generated by this example will include all strings in which the second-last bit is 1.
• L= {10, 010, 000010, 11, 101011……..}
• NFA automaton machine accepts all strings where the second last bit is 1.
0,1
q0 1 q1 0,1 q2 Final
Cont..
• Where,
• {q0, q1, q2} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q2} refers to the set of final states
States 0 1
q0 q0 q0,q1
q1 q2 q2
q2 – –
0,1
q0 1 q1 0,1 q2 Final
NFA Example 02:
• NFA with input alphabet ∑ = {0, 1} that accepts all the strings that end with 01.
• Solution
• The language generated by this example will include all strings that end with 01.
• L= {01, 0101, 0000101, 101, 101001……..}
0,1
q0 0 q1 1 q2 Final
• NFA automaton machine accepts all strings that end with 01.
Cont..
• Where,
• {q0, q1, q2} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q2} refers to the set of final states
States 0 1
q0 q0,q1 q0
q1 – q2
q2 – –
0,1
q0 0 q1 1 q2 Final
NFA Example 03:
• Construct an NFA with ∑ = {0, 1} in which each string must contain “double ‘1’ is followed by single ‘0’.
Solution
• The language generated by this example will include all strings that must contain “double ‘1’ is
followed by single ‘0’.
• L= {110, 0110, 1110, 10100110……..}
0,1 0,1
q0 1 q1 1 q2 0 q3 Final
Cont..
• Where,
• {q0, q1, q2,q3} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q3} refers to the set of final states
States 0 1
q0 q0 q0,q2
q1 – q2
q2 q3 –
q3 q3 q3
0,1 0,1
q0 1 q1 1 q2 0 q3 Final
NFA Example 04:
• Design an NFA with ∑ = {0, 1} such that every string includes the substring 1101.
Solution
• The language generated by this example will include all strings that must contain substring 1101.
• L= {1101, 110101, 1101000101, 01101, 1011011……..}
0,1
q0 1 q1 1 q2 0 q3
q4 Final
0,1
Cont..
• Where,
• {q0, q1, q2,q3,q4} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q4} refers to the set of final states
States 0 1
q0 q0 q0,q1
q1 – q2
q2 q3 –
q3 – q4
q4 q4 q4
NFA Example 05:
Construct an NFA with ∑ = {0, 1} in which each string must contain “11” followed by “00”.
Solution
• The language generated by this example will include each string must contain “11” followed by
“00”.
• L= {1100, 01100, 11100, 011001, 00110001 ……..}
0,1
q0 1 q1 1 q2 0 q3
q4 Final
Cont..
• Where,
• {q0, q1, q2, q3, q4} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q4} refers to the set of final states
States 0 1
q0 q0 q0,q1
q1 – q2
q2 q3 –
q3 q4 –
q4 q4 q4
NFA Example 06:
• Construct an NFA with ∑ = {0, 1}, where each string must contain either “01” or “10”.
• Solution
• The language generated by this example will include each string must contain either “01” or “10”.
• L= {01, 10, 001, 110, 1110, 0001………..}
0,1 0,1
q0 0 q1 1 q2
1
q3
Final q4
Cont..
• Where,
• {q0, q1, q2, q3, q4} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q2,q4} refers to the set of final states
States 0 1
q0 q0,q1 q0,q3
q1 – q2
q2 q2 q2
q3 q4 –
q4 q4 q4
NFA Example 08:
• Construct an NFA with ∑ = {0, 1} that accepts all strings that begin with 1 and end with 0.
• Solution
• The language generated by this example will include all strings that begin with 1 and end with 0.
• L= {10, 110, 100, 1100, 1110, 1000 ………}
1 0
q0 0 q1 1 q2
0
Cont..
• Where,
• {q0, q1, q2} refers to the set of states
• {0,1} refers to the set of input alphabets
• δ refers to the transition function
• q0 refers to the initial state
• {q2} refers to the set of final states
States 0 1
q0 – q1
q1 q2 q1
q2 q2 q1
1 0
q0 0 q1 1 q2
0
"Thank You!"