0% found this document useful (0 votes)
5 views32 pages

Lecture # 04 (NFA)

The document discusses Nondeterministic Finite Automata (NFA), a theoretical model in automata theory that allows multiple transitions for the same input and can move to new states without consuming input. It outlines the components of an NFA, how it operates, and provides several examples of NFAs designed to recognize specific patterns in binary strings. Key differences between NFAs and Deterministic Finite Automata (DFA) are also highlighted, emphasizing the non-deterministic behavior of NFAs.

Uploaded by

zawars0005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views32 pages

Lecture # 04 (NFA)

The document discusses Nondeterministic Finite Automata (NFA), a theoretical model in automata theory that allows multiple transitions for the same input and can move to new states without consuming input. It outlines the components of an NFA, how it operates, and provides several examples of NFAs designed to recognize specific patterns in binary strings. Key differences between NFAs and Deterministic Finite Automata (DFA) are also highlighted, emphasizing the non-deterministic behavior of NFAs.

Uploaded by

zawars0005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Theory of Programming Languages

(CSC-161)
Mr. Faizan Ali (Teaching Assistant)

Department of Computer Systems Engineering,


BS in (Computer Science)
Mehran University of Engineering and Technology, Jamshoro.
Lecture # 04
(NFA- Nondeterministic Finite Automata)
NFA

• A Nondeterministic Finite Automaton (NFA) is a theoretical model of computation used in automata


theory. It is similar to a Deterministic Finite Automaton (DFA) but differs in that:
1. Multiple transitions for the same input: A state in an NFA can have multiple transitions for the
same input symbol.
2. Transitions (ε): An NFA can move to a new state without consuming any input (ε-moves).
3. Non-deterministic behavior: The automaton can be in multiple states at the same time, unlike a
DFA, which has only one active state at any moment.

Components of an NFA
• An NFA is defined by five elements:
• (Q, Σ, δ, q₀, F)
Cont..

• A Nondeterministic finite automata DFA is defined by 5 symbols {Q, Σ, δ, S(q₀), F}


1. States (Q): Q is a finite and non empty set of states.
2. Alphabet (Σ): Σ is a finite non empty set of finite input symbols (alphabet).
3. Transition Function (δ): Defines how states changes based on input.
4. Start State (S-q₀): Where the automaton begins. S is initial state (always one)
5. Final States (F): Accepting states that determine valid inputs.

 How if NFA works:


•The NFA starts at the initial state q₀.
•For each input symbol, the machine can move to multiple states or take ε-transitions.
•If at least one path leads to an accepting state in F after reading the full input, the string is
accepted.
Cont..

Example-01
• NDFA (Q, Σ, δ, q₀, F)

1 0 0

q0 1 q1 0 q2

• Is this called a deterministic finite automaton?

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.

Processes input in a single path. Explores multiple paths simultaneously.

Requires more states. Often fewer states, more compact.

Accepted if one path reaches a final state. Accepted if at least one path reaches a final
state.
NFA Designing
Nondeterministic Finite Automata Designing

• Design an NFA (Nondeterministic Finite Automaton)


• Alphabet (Σ) – The set of input symbols (e.g., {0,1} for binary).
• States (Q) – The finite set of states.
• Start State (q₀) – The initial state.
• Final States (F) – The set of accepting states.
• Transitions (δ) – The state transitions, including multiple transitions for the same input or ε-
moves.

• Examples:
• Strings ending with “10"
• Strings containing "ab"
• Any given regular expression pattern
Cont..

• NFA for recognizing strings that end with "10“

0,1

q0 1 q1 0,1 q2 Final

• What this design present?


a a
q0 q1

q2 a,b
NFA Examples

NFA Example 01:

• 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

• Transition function δ is defined as


• δ (q0, 0) = q0
• δ (q0, 1) = q0,q1 0,1
• δ (q1, 0) = q2
• δ (q1, 1) = q2 q0 1 q1 0,1 q2 Final
• δ (q2, 0) = ϕ
• δ (q2, 1) = ϕ
Cont..

• Transition Table (Non-Deterministic Finite Automata)

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

• Transition function δ is defined as


• δ (q0, 0) = q0,q1
• δ (q0, 1) = q0
• δ (q1, 0) = fi
• δ (q1, 1) = q2
• δ (q2, 0) = ϕ
• δ (q2, 1) = ϕ
Cont..

• Transition Table for the above Non-Deterministic Finite Automata is:

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

• Transition function δ is defined as


• δ (q0, 0) = q0
• δ (q0, 1) = q0, q1
• δ (q1, 0) = fi
• δ (q1, 1) = q2
• δ (q2, 0) = q3
• δ (q2, 1) = ϕ
• δ (q3, 0) = q3
• δ (q3, 1) = q3
Cont..

• Transition Table (Non-Deterministic Finite Automata is)

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

• Transition function δ is defined as


• δ (q0, 0) = q0 • δ (q2, 1) = ϕ
• δ (q0, 1) = q0, q1 • δ (q3, 0) = ϕ
• δ (q1, 0) = fi • δ (q3, 1) = q4
• δ (q1, 1) = q2 • δ (q4, 0) = q4
• δ (q2, 0) = q3 • δ (q4, 1) = q4
Cont..

• Transition Table (Non-Deterministic Finite Automata is)

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

• Transition function δ is defined as


• δ (q0, 0) = q0 • δ (q2, 1) = ϕ
• δ (q0, 1) = q0,q1 • δ (q3, 0) = q4
• δ (q1, 0) = ϕ • δ (q3, 1) = ϕ
• δ (q1, 1) = q2 • δ (q4, 0) = q4
• δ (q2, 0) = q3 • δ (q4, 1) =q4
Cont..

• Transition Table (Non-Deterministic Finite Automata is)

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

• Transition function δ is defined as


• δ (q2, 0) = q2
• δ (q0, 0) = q0,q1
• δ (q2, 1) = q2
• δ (q0, 1) = q0,q3
• δ (q3, 0) = q4
• δ (q1, 0) = ϕ
• δ (q3, 1) = ϕ
• δ (q1, 1) = q2
• δ (q4, 0) = q4
• δ (q1, 1) = q2
• δ (q4, 1) =q4
• δ (q1, 1) = q2
• δ (q1, 1) = q2
Cont..

• Transition Table (Non-Deterministic Finite Automata is)

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

• Transition function δ is defined as


• δ (q0, 0) = ϕ
• δ (q0, 1) = q1
• δ (q1, 0) = q2
• δ (q1, 1) = q1
• δ (q2, 0) = q2
• δ (q2, 1) = q1
Cont..

• Transition Table (Non-Deterministic Finite Automata is)

States 0 1

q0 – q1

q1 q2 q1

q2 q2 q1

1 0

q0 0 q1 1 q2

0
"Thank You!"

You might also like