Automata
Automata
Lesson1
Deterministic Finite Automata
(DFA)
AbuFatimah
PhD student in computer science
1/23/17 AbuFatimah 1
Theory of Computation اﻟﻨﻈﺮﯾﺔ اﻟﺤﺴﺎﺑﯿﺔ
• Languages اﻟﻠﻐـــﺎت
• Regular Languages اﻟﻠﻐﺎت اﻟﻣﻧﺗظﻣﺔ
• Deterministic Finite Automata (DFA)
• We use these methods to recognize regular languages
1/23/17 AbuFatimah 2
What is a Language?
• A language is a set of strings over an alphabet.
• For example, alphabet {0, 1}
• Language L1 = {1, 0, 11, 00}
• Language L2 = {01, 111, 000}
• Language L3 = {ε} the empty string
• Language L4 = ∅ , the empty language, with no strings
1/23/17 AbuFatimah 3
What is a Regular Language?اﻟﻠﻐﺎت اﻟﻤﻨﺘﻈﻤﺔ
• Regular language is one that can be recognized by a
• Deterministic Finite Automata (DFA), or
• Non-Deterministic Finite Automat (NFA), or
• Regular Expressions (Regex).
• For example:
• L1 = {strings that end with 1}, e.g. {01, 11, 10101, 100001, …}
• L2 = {strings that start and end with 0}, e.g. {0, 00, 010, 011110, …}
• L3 = {strings that have even number of 1’s},
• e.g. {11, 101, 1010, 1111,… }
1/23/17 AbuFatimah 4
Deterministic Finite Automata (DFA)
What is the formal description of M1?
• Example 1: M1
• This DFA is a 5-tuple: (Q, å, d, q0, F)
1. Q = set of states = {q0, q1}
2. å = alphabet = {0, 1}
3. d = transition function = (state
diagram/table)
4. q0 = first state = q0
5. F = set of final states = {q1}.
1/23/17 AbuFatimah 5
Deterministic Finite Automata (DFA)
• Example 1: M1
• d = transition function
• State Diagram (figure)
• Transition Table
0 1
q0 q0 q1
*q1 q0 q1
1/23/17 AbuFatimah 6
Deterministic Finite Automata (DFA)
• Transition Table
0 1
q0 q0 q1
*q1 q0 q1
• What is the language
recognized/accepted by M1?
• e.g {01, 011, 0101, 01011, 1, 11, 101,
1001…}
• L1 = {over (0, 1)|strings that end with 1}
1/23/17 AbuFatimah 7
The End اﻟﻨﮭﺎﯾﺔ
• Languages اﻟﻠﻐـــﺎت
• Regular Languages اﻟﻠﻐﺎت اﻟﻣﻧﺗظﻣﺔ
• Deterministic Finite Automata (DFA)
• Used to recognized regular languages.
• This DFA is a 5-tuple: (Q, å, d, q0, F)
• d = transition function = (state diagram, or transition table)
• L1 = {over (0, 1)|strings that end with 1}
1/23/17 AbuFatimah 8
ﺑﺳم ﷲ اﻟرﺣﻣن اﻟرﺣﯾم
Lesson2
Deterministic Finite Automata
(DFA)
AbuFatimah
PhD student in computer science
1/23/17 AbuFatimah 1
Review
• Transition Table
0 1
q0 q0 q1
*q1 q0 q1
• What is the language recognized/accepted
by M1?
• e.g {01, 011, 0101, 01011, 1, 11, 101,
State Diagram for M1
1001…}
• L(M1) = {over (0, 1)|strings that end with 1}
1/23/17 AbuFatimah 2
More examples of DFA
• Example2: M2
• What is the language recognized by
1 M2?
0 1
q0 q1
0
State Diagram for M2
1/23/17 AbuFatimah 3
Machine 2
Input string: ε • Example2: M2
• What is the language recognized by
1 M2?
0 1
• e.g. {ε,
q0 q1
0
State Diagram for M2
1/23/17 AbuFatimah 4
Checking some accepted strings
Input string: 0 • Example2: M2
• What is the language recognized by
1 M2?
0 1
• e.g. {ε, 0,
q0 q1
0
State Diagram for M2
1/23/17 AbuFatimah 5
Checking some accepted strings
Input string: 00 • Example2: M2
• What is the language recognized by
1 M2?
0 1
• e.g. {ε, 0, 00,
q0 q1
0
State Diagram for M2
1/23/17 AbuFatimah 6
Checking some accepted strings
Input string: 1 • Example2: M2
• What is the language recognized by
1 M2?
0 1
• e.g. {ε, 0, 00,
q0 q1
0
State Diagram for M2
1/23/17 AbuFatimah 7
Machine 2
Input string: 10 • Example2: M2
• What is the language recognized by
1 M 2?
0 1
• e.g {ε, 0, 00, 10, …}
q0 q1 • L(M2) = { over (0, 1)| strings that end
with 0 or ε, empty string}
0
State Diagram for M2
1/23/17 AbuFatimah 8
Machine 3
What is the formal description of M3?
• Example3: M3
• This DFA is a 5-tuple: (Q, å, d, q0, F)
q0
a a b b 1. Q = {q0, q1, q2, q3, q4}
q1 q3 2. å = {a, b}
b a b a 3. d = (state diagram/transition table)
4. q0 = q0
q2 q4
a 5. F = {q1, q3}.
b
State Diagram for M3
1/23/17 AbuFatimah 9
Machine 3
• Example3: M3
• d = transition function = (state
q0
a a b b diagram/table)
q1 q3 a b
q0 q1 q3
b a b a
*q1 q1 q2
q2 q4 q2 q1 q2
a *q3 q4 q3
b
q4 q4 q3
State Diagram for M3
Transition table for M3
1/23/17 AbuFatimah 10
Machine 3
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 11
Machine 3
Input string: a • Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a,
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 12
Machine 3
Input string: aa
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 13
Machine 3
Input string: ab
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 14
Machine 3
Input string: aba
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa, aba, ..
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 15
Machine 3
Input string: b
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa, aba, ..,
b,
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 16
Machine 3
Input string: bb
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa, aba, ..,
b, bb,
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 17
Machine 3
Input string: bba
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa, aba, ..,
b, bb,
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 18
Machine 3
Input string: bbab
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa, aba, ..,
b, bb, bbab
b a b a
q2 q4
a
b
1/23/17 AbuFatimah 19
Machine 3
• Example3: M3
• What is the language recognized by
q0
a a b b M 3?
q1 q3
• Some accepted strings = {a, aa, aba, ..,
b, bb, bbab, …}
b a b a
• M3 accepts strings that start and end
q2 q4
with a, or start and end with b.
b
a • L(M3) = {over (a, b)| strings that start
and end with the same symbol}
State Diagram for M3
1/23/17 AbuFatimah 20
The End اﻟﻨﮭﺎﯾﺔ
0 1 1
q0 q1
0
State Diagram for M2
1/23/17 AbuFatimah 21
ﺑﺳم ﷲ اﻟرﺣﻣن اﻟرﺣﯾم
Lesson3
Examples and Design of DFA
AbuFatimah
PhD student in computer science
1/23/17 AbuFatimah 1
Machine 4:
• What is the language recognized/accepted
by M4?
0 1 0
q0 q1
1/23/17 AbuFatimah 2
Review
• Transition Table
0 1
q0 q0 q1
*q1 q0 q1
• What is the language recognized/accepted
by M1?
• e.g {01, 011, 0101, 01011, 1, 11, 101,
State Diagram for M1
1001…}
• L(M1) = {over (0, 1)|strings that end with 1}
1/23/17 AbuFatimah 3
Machine 4:
1- Input string: 1 • What is the language recognized/accepted
by M4?
0 1 0 • Accepted strings = {1,
q0 q1
1/23/17 AbuFatimah 4
Machine 4:
2- Input string: 11 • What is the language recognized/accepted
by M4?
0 1 0 • Accepted strings = {1,
q0 q1
1/23/17 AbuFatimah 5
Machine 4:
3- Input string: 10 • What is the language recognized/accepted
by M4?
0 1 0 • Accepted strings = {1, 10
q0 q1
1/23/17 AbuFatimah 6
Machine 4:
• What is the language recognized/accepted
by M4?
0 1 0 • Accepted strings = {1, 10, 111, 1011, 11111,
01, 010, …}
q0 q1 • Notice that all strings have odd number of
1’s.
1
• L(M4) = {strings with odd number of 1’s}
State Diagram for M4
١ ﺗﻛرار ﻓردي ﻟﻠﻌدد
1/23/17 AbuFatimah 7
Machine 5:
• What is the language recognized/accepted
by M5?
0 1 0 • Accepted strings = {ε, 0, 11, 101, 1111, 011,
0110, …}
q0 q1 • Notice that all strings have even number of
1’s.
1
• L(M5) = {strings with even number of 1’s, or
State Diagram for M5
the empty string}
١ ﺗﻛرار زوﺟﻲ ﻟﻠﻌدد
1/23/17 AbuFatimah 8
Design (ﺗﺼﻤﯿﻢ ) DFA
• Design is a creative process, no simple rule for all DFA
• A method: imagine you’re a machine
• You see only one symbol at a time of the string 0 1 1 0 1
1/23/17 AbuFatimah 9
Design DFA for strings with odd # of a’s
• Example:
• Design a DFA, M6
• Alphabet = {a, b}
• L(M6) = {all strings with odd number of a’s} a ﺗﻛرار ﻓردي ﻟﻠرﻣز
• Let’s see some examples:
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
1/23/17 AbuFatimah 10
Design DFA for strings with odd # of a’s
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
a
b b
a b
qeven qodd
q_even q_odd q_even
a *q_odd q_even q_odd
1/23/17 AbuFatimah 11
Design DFA for strings with odd # of a’s
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
1/23/17 AbuFatimah 12
Design DFA for strings with odd # of a’s
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
1/23/17 AbuFatimah 13
Design DFA for strings with odd # of a’s
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
1/23/17 AbuFatimah 14
Design DFA for strings with odd # of a’s
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
1/23/17 AbuFatimah 15
Design DFA for strings with odd # of a’s
• Accepted strings [odd a’s] = {a, aaa, ab, ba, abaa, bbbba, …}
• Rejected strings [even a’s] = {aa, b, bb, baba, baa, aaaa, ..., ε}
1/23/17 AbuFatimah 16
The End () اﻟﻨﮭﺎﯾﺔ
• Resource:
• “Introduction to the
Theory of
Computation” by
Sipser, 3rd edition.
• Chapter 1: Regular
Languages
• 1.1 Finite Automata
1/23/17 AbuFatimah 17
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
Lesson4
Design of DFA
AbuFatimah
PhD student in computer science
1/23/17 AbuFatimah 1
Machine 6: strings that end with 01
• Problem: Design a DAF for :
• L(M6) = {over (0,1)| strings that end with 01}
• E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
• If string ends with 0,
• There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0]
State Diagram for M6
q1 [ends with 0]
*q2 [ends with 01]
1/23/17 AbuFatimah 2
Machine 6: strings that end with 01
• Problem: Design a DAF for :
• L(M6) = {over (0,1)| strings that end with 01}
0 • E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
• If string ends with 0,
• There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0] q1
q1 [ends with 0]
State Diagram for M6
*q2 [ends with 01]
1/23/17 AbuFatimah 3
Machine 6: strings that end with 01
• Problem: Design a DAF for :
0 • L(M6) = {over (0,1)| strings that end with 01}
0 • E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
• If string ends with 0,
• There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0] q1
q1 [ends with 0] q1
*q2 [ends with 01]
State Diagram for M6
1/23/17 AbuFatimah 4
Machine 6: strings that end with 01
• Problem: Design a DAF for :
0 • L(M6) = {over (0,1)| strings that end with 01}
0 • E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
0 • If string ends with 0,
• There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0] q1
q1 [ends with 0] q1
*q2 [ends with 01] q1
State Diagram for M6
1/23/17 AbuFatimah 5
Machine 6: strings that end with 01
• Problem: Design a DAF for :
0 • L(M6) = {over (0,1)| strings that end with 01}
0 • E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
0 • If string ends with 0,
1 • There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0] q1
q1 [ends with 0] q1 q2
*q2 [ends with 01] q1
State Diagram for M6
1/23/17 AbuFatimah 6
Machine 6: strings that end with 01
• Problem: Design a DAF for :
0 • L(M6) = {over (0,1)| strings that end with 01}
0 • E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
0 • If string ends with 0,
1 1 • There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0] q1
q1 [ends with 0] q1 q2
*q2 [ends with 01] q1 q0
State Diagram for M6
1/23/17 AbuFatimah 7
Machine 6: strings that end with 01
• Problem: Design a DAF for :
1 0 • L(M6) = {over (0,1)| strings that end with 01}
0 • E.g (01, 101, 1101, 0001, 10101)
q0 q1 • Information to remember:
0 • If string ends with 0,
1 1 • There is 1 after the 0
q2
0 1
q0 [doesn’t end with 0] q1 q0
q1 [ends with 0] q1 q2
*q2 [ends with 01] q1 q0
State Diagram for M6
1/23/17 AbuFatimah 8
Machine 7: strings that don’t end with 01
• Problem: Design a DAF for :
1 0 • L(M7) = {over (0,1)| strings that don’t end with
01}
0
q0 q1 • E.g (ε, 0, 11, 110, 000, 1010)
0 • Information to remember:
1 1 • If string ends with 0,
q2 • There is 1 after the 0
0 1
*q0 [doesn’t end with 0] q1 q0
*q1 [ends with 0] q1 q2
State Diagram for M7 q2 [ends with 01] q1 q0
1/23/17 AbuFatimah 9
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0]
q1 [ends with 1]
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 10
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0]
q1 [ends with 1]
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 11
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0]
q1 [ends with 1]
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 12
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 q1 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0]
q1 [ends with 1]
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 13
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 q1 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0] q00
q1 [ends with 1]
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 14
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 q1 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0] q00 q01
q1 [ends with 1]
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 15
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 q1 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0] q00 q01
q1 [ends with 1] q10
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 16
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 q1 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0] q00 q01
q1 [ends with 1] q10 q11
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00]
• Consider all possibilities for last two
*q01 [ends with 01] symbols:
q10 [ends with 10] • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11]
1/23/17 AbuFatimah 17
Machine 6: another solution ﺣﻞ آﺧﺮ
states 0 1 • L(M6) = {over (0,1)| strings that end with 01}
qε [ends with ε] q0 q1 • E.g (01, 101, 1101, 0001, 10101)
q0 [ends with 0] q00 q01
q1 [ends with 1] q10 q11
• E.g (ε, 0, 11, 110, 000, 1010)
q00 [ends with 00] q00 q01
• Consider all possibilities for last two
*q01 [ends with 01] q10 q11 symbols:
q10 [ends with 10] q00 q01 • 00, 01, 10, 11, 1, 0, ε
q11 [ends with 11] q10 q11
1/23/17 AbuFatimah 18
Machine 6: strings that end with 01 0
states 0 1 0
q00
qε [ends with ε] q0 q1 1
q0 1
q0 [ends with 0] q00 q01 0
0
q1 [ends with 1] q10 q11 q01
q00 [ends with 00] q00 q01 qε 0 1
*q01 [ends with 01] q10 q11
q10 [ends with 10] q00 q01 q10
0
1 1
q11 [ends with 11] q10 q11 0
q1 1
q11
1/23/17 AbuFatimah 19
1
Machine 7: strings that don’t end with 01
0
states 0 1 0
q00
*qε [ends with ε] q0 q1 1
q0 1
*q0 [ends with 0] q00 q01 0 0
*q1 [ends with 1] q10 q11 q01
*q00 [ends with 00] q00 q01 qε 0 1
q01 [ends with 01] q10 q11
*q10 [ends with 10] q00 q01 q10
0
1 1
*q11 [ends with 11] q10 q11 0
q1 1
q11
1/23/17 AbuFatimah 20
1
The End () اﻟﻨﮭﺎﯾﺔ
• Resource:
• “Introduction to the
Theory of
Computation” by
Sipser, 3rd edition.
• Chapter 1: Regular
Languages
• 1.1 Finite Automata
1/23/17 AbuFatimah 21
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
Lesson5
Nondeterministic Finite Automata
(NFA)
AbuFatimah
PhD student in computer science
5/17/17 AbuFatimah 1
What is a Deterministic Machine (e.g. DFA)
• Deterministic machine () آﻟﺔ ﺣﺘﻤﯿﺔ:
• When the machine is at a given state, for a specific input, it gives one output
at each time.
• For example:
5/17/17 AbuFatimah 2
Nondeterministic Machine (NFA)
• Nondeterministic machine (آﻟﺔ ﻏﯾر ﺣﺗﻣﯾﺔ ):
• When the machine is at a given state, for a specific input, it may give more
than one output at each time.
• Every deterministic finite automata (DFA), is considered a nondeterministic
finite automata (NFA)
DFA
NFA
5/17/17 AbuFatimah 3
Nondeterministic Machine (NFA)
• Nondeterministic machine (آﻟﺔ ﻏﯾر ﺣﺗﻣﯾﺔ ):
• When the machine in a given state, for a specific input, it may give more than
one output at each time.
5/17/17 AbuFatimah 4
More about NFA state diagram ١ رﻗم، اﻟﺧﺻﺎﺋص واﻟﻔروﻗﺎت
1) from state q1 to state q2, we have two arrow with label 1, but in DFA, we can only
have one arrow for each label.
5/17/17 AbuFatimah 5
More about NFA state diagram ٢ رﻗم، اﻟﺧﺻﺎﺋص واﻟﻔروﻗﺎت
1 0
2) In state q2, we don’t have an arrow with label 1, but in DFA, we must have an
arrow for each label. Also in state q3 with label 0.
5/17/17 AbuFatimah 6
More about NFA state diagram ٣ رﻗم، اﻟﺧﺻﺎﺋص واﻟﻔروﻗﺎت
ε = empty string
3) In state q2, we have an arrow with label ε, but in DFA, we can’t have an arrow
with label ε.
This arrow means, at state q2, we can move to state q3 without reading any input.
5/17/17 AbuFatimah 7
What is the difference between DFA and NFA?
1 0
ε = empty string
5/17/17 AbuFatimah 9
• Nondeterministic Machine, NFA2
• Input string = abbbaba
abb
abbb abbba
0 abbbab abbbaba
ab 0 0 0
0 0
0 abbbaba
a abbba
1
1 abbbab
0
a 2 abbbaba
1
3
ab one copy reached the final state, so
2 the machine accepts this input string
abb
5/17/17 AbuFatimah 10
• Nondeterministic Machine, NFA2
• Input string = abbbaba
abb
abbb abbba
0 abbbab abbbaba
ab 0 0 0
0 0
0 abbbaba
a abbba
1
1 abbbab
0
a 2 abbbaba
1
3
ab one copy reached the final state, so
2 the machine accepts this input string
abb
5/17/17 AbuFatimah 11
The End () اﻟﻨﮭﺎﯾﺔ
• Resource:
• “Introduction to the
Theory of
Computation” by
Sipser, 3rd edition.
• Chapter 1: Regular
Languages
• 1.2 Nondeterminism
5/17/17 AbuFatimah 12
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
5/20/17 AbuFatimah 1
ﻣن اﻟدرس اﻟراﺑﻊ
Machine 6: strings that end with 01 0
states 0 1 0
q00
qε [ends with ε] q0 q1 1
q0
q0 [ends with 0] q00 q01 0 1
0
q1 [ends with 1] q10 q11 q01
q00 [ends with 00] q00 q01 qε 0 1
*q01 [ends with 01] q10 q11
q10 [ends with 10] q00 q01 q10
0
1 1
q11 [ends with 11] q10 q11 0
q1 1
q11
5/20/17 AbuFatimah 2
1
How to minimize a DFA ﻛﯿﻔﯿﺔ ﺗﻘﻠﯿﻞ ﺣﺠﻢ دي إف اي
• Most exam questions need a DFA of minimum size.
• Algorithm to minimize a DFA
1. Partition all states into two groups:
a. Group 1 (G1) contains all final states.
b. Group 2 (G2) contains all non-final states.
5/20/17 AbuFatimah 3
How to minimize a DFA ﻛﯿﻔﯿﺔ ﺗﻘﻠﯿﻞ ﺣﺠﻢ دي إف اي
Machine 6: strings that end with 01
G1
5/20/17 AbuFatimah 4
How to minimize a DFA ﻛﯿﻔﯿﺔ ﺗﻘﻠﯿﻞ ﺣﺠﻢ دي إف اي
• Algorithm to minimize a DFA
1. Partition all states into two groups:
a. Group 1 (G1) contains all final states.
b. Group 2 (G2) contains all non-final states.
G2
5/20/17 AbuFatimah 5
How to minimize a DFA ﻛﯿﻔﯿﺔ ﺗﻘﻠﯿﻞ ﺣﺠﻢ دي إف اي
2. for each group, check
whether all states in the
group go to the same next
group for each input
symbol.
• If not, partition it into
smaller groups so that all
states in each smaller group
go to the same next group
for each input symbol. G1
• Repeat step 2 until no more
partitions. G2
5/20/17 AbuFatimah 6
ﻧﻘﻮم ﺑﺈﻧﺸﺎء ﺟﺪول ﻟﻜﻞ ﻣﺠﻤﻮﻋﺔ
G1 (final states)
0 1
q01
G2 (non-final states)
0 1
qε
q0
q1
q00
G1
q10
q11
G2
5/20/17 AbuFatimah 7
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01
G2 (non-final states)
0 1
qε
q0
q1
q00
q10
q11
5/20/17 AbuFatimah 8
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01 G2(q10)
G2 (non-final states)
0 1
qε
q0
q1
q00
q10
q11
5/20/17 AbuFatimah 9
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε
q0
q1
q00
q10
q11
5/20/17 AbuFatimah 10
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0)
q0
q1
q00
q10
q11
5/20/17 AbuFatimah 11
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0) G2(q0)
q0
q1
q00
q10
q11
5/20/17 AbuFatimah 12
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0) G2(q0)
q0 G2(q00) G1(q01)
q1
q00
q10
q11
5/20/17 AbuFatimah 13
ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0) G2(q0)
q0 G2(q00) G1(q01)
q1 G2(q10) G2(q11)
q00 G2(q00) G1(q01)
q10 G2(q00) G1(q01)
q11 G2(q10) G2(q11)
5/20/17 AbuFatimah 14
اﻵن ﻧﻘﺴﻢ اﻟﻤﺠﻤﻮﻋﺘﯿﻦ اﻟﻰ ﻣﺠﻤﻮﻋﺎت أﺻﻐﺮ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0) G2(q0)
q0 G2(q00) G1(q01)
q1 G2(q10) G2(q11)
q00 G2(q00) G1(q01)
q10 G2(q00) G1(q01)
q11 G2(q10) G2(q11)
5/20/17 AbuFatimah 15
اﻵن ﻧﻘﺴﻢ اﻟﻤﺠﻤﻮﻋﺘﯿﻦ اﻟﻰ ﻣﺠﻤﻮﻋﺎت أﺻﻐﺮ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0) G2(q0)
q0 G2(q00) G1(q01)
q1 G2(q10) G2(q11)
q00 G2(q00) G1(q01) G21
q10 G2(q00) G1(q01)
q11 G2(q10) G2(q11)
5/20/17 AbuFatimah 16
اﻵن ﻧﻘﺴﻢ اﻟﻤﺠﻤﻮﻋﺘﯿﻦ اﻟﻰ ﻣﺠﻤﻮﻋﺎت أﺻﻐﺮ
G1 (final states)
0 1
q01 G2(q10) G2 (q11)
G2 (non-final states)
0 1
qε G2(q0) G2(q0)
q0 G2(q00) G1(q01)
q1 G2(q10) G2(q11)
q00 G2(q00) G1(q01) G21
G22 q10 G2(q00) G1(q01)
q11 G2(q10) G2(q11)
5/20/17 AbuFatimah 17
G1 (final states)
0 1 ﻧﻤﻸ اﻟﺠﺪول ﻟﻠﺘﻘﺴﯿﻤﺎت اﻟﺠﺪﯾﺪة
q01
G21 (non-final states)
0 1
q0
q00
q10
G22 (non-final states)
0 1
qε
q1
q11
5/20/17 AbuFatimah 18
G1 (final states)
0 1 ﻧﻤﻸ اﻟﺠﺪول ﻟﻠﺘﻘﺴﯿﻤﺎت اﻟﺠﺪﯾﺪة
q01 G21 G22
G21 (non-final states)
0 1
q0 G21 G1
q00 G21 G1
q10 G21 G1
G22 (non-final states)
0 1
qε G21 G22
q1 G21 G22
q11 G21 G11
5/20/17 AbuFatimah 19
G1 (final states)
0 1 ﻧﺮﺳﻢ اﻟﺮﺳﻤﺔ اﻟﻤﺨﺘﺼﺮة
q01 G21 G22
1
G21 (non-final states)
0 1
q0 G21 G1 G22 1
q00 G21 G1
q10 G21 G1 0 1 G1
qε G21 G22
q1 G21 G22
0
q11 G21 G11
5/20/17 AbuFatimah 20
ﻧﻔﺲ اﻟﺮﺳﻢ اﻟﺒﯿﺎﻧﻲ ﻣﻦ اﻟﺪرس اﻟﺮاﺑﻊ
1
G22 1
G21 0
0
• Chapter 1: Regular
Languages
• 1.1 Finite Automata
5/20/17 AbuFatimah 22
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
Lesson7
Understanding NFA
AbuFatimah
طﺎﻟب دﻛﺗوراه ﻓﻲ ﺗﺧﺻص ﻋﻠوم اﻟﺣﺎﺳب
12/14/2019 AbuFatimah 2
• Does this NFA accept string = baa?
one copy reached the final state, so
the machine accepts this input string
baa NFA3
0
0
baa We follow the ε arrow after each
ba input symbol is read.
baa 1
0 ε
ba
b 1 2
baa
1
0 ba
ε baa
1 2 3
12/14/2019 AbuFatimah 3
• Does this NFA accept string = baa?
one copy reached the final state, so
the machine accepts this input string
baa
0 NFA3
0
baa We follow the ε arrow after each
ba input symbol is read.
baa 1
0 ε
ba
b 1 2
baa
1
0 ba
ε baa
1 2 3
12/14/2019 AbuFatimah 4
ﺗﺘﺒﻊ اﻵﻟﺔ:۲ ﻣﺜﺎلTracing NFA with empty string
• Nondeterministic Machine, NFA4
• Does this NFA accept string = 0110?
q0
0
1
ε
0 q1 q2
0, 1
NFA4
12/14/2019 AbuFatimah 5
• Nondeterministic Machine, NFA4
• Does this NFA accept string = 0110? q0
0
1
ε
0
q0 0 q1 q2
0, 1
ε
0 NFA4
q2 q0
01
q1
ﺗﺗﺑﻊ اﻵﻟﺔ Tracing 0110
011 q0
q2
12/14/2019 AbuFatimah 6
• Nondeterministic Machine, NFA4
• Does this NFA accept string = 0110? q0
• Yes
0
1
ε
0
q0 0 q1 q2
0, 1
ε
0 NFA4
q2 q0
01
q1
ﺗﺗﺑﻊ اﻵﻟﺔ Tracing 0110
011 q0
q2
12/14/2019 AbuFatimah 7
ﺗﺘﺒﻊ اﻵﻟﺔ:۲ ﻣﺜﺎلTracing NFA with empty string
• Nondeterministic Machine, NFA4
• What does this machine accept?
• accepts:
• ε, 0, 110, 1010, 100. q0
• Rejects: 0
1
• 1, 11, 01 ε
0 q1 q2
0, 1
12/14/2019 AbuFatimah 8
The End ()اﻟﻨﮭﺎﯾﺔ
• Resource:
• “Introduction to the Theory of
Computation” by Sipser, 3rd
edition.
12/14/2019 AbuFatimah 9
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
Lesson8
Design of NFA
AbuFatimah
PhD student in computer science
12/14/2019 AbuFatimah 1
Example 1: [string (a, …, bab, …, aa)]
• Please construct an NFA for the following language.
• L = { x over {a, b} | x starts with symbol a, contains substring bab, and
ends with aa}
• Examples: ababaa, aabbabbbbaa, a[x]bab[x]aa
12/14/2019 AbuFatimah 2
Example 1: [string (a, …, bab, …, aa)]
• Please construct an NFA for the following language.
• L = { x over {a, b} | x starts with symbol a, contains substring bab, and
ends with aa}
• Examples: ababaa, aabbabbbbaa, a[x]bab[x]aa
a
q0
12/14/2019 AbuFatimah 3
Example 1: [string (a, …, bab, …, aa)]
• Please construct an NFA for the following language.
• L = { x over {a, b} | x starts with symbol a, contains substring bab, and
ends with aa}
• Examples: ababaa, aabbabbabaa, a[x]bab[x]aa
b a b
q1 q2 q3 q4
12/14/2019 AbuFatimah 4
Example 1: [string (a, …, bab, …, aa)]
• Please construct an NFA for the following language.
• L = { x over {a, b} | x starts with symbol a, contains substring bab, and
ends with aa}
• Examples: ababaa, aabbabbbbaa, a[x]bab[x]aa
a a
q5 q6
12/14/2019 AbuFatimah 5
Example1: [string (a, …, bab, …, aa)]
اﻟﺤﻞ اﻟﻨﮭﺎﺋﻲ
• Please construct an NFA for the following language.
• L = { x over {a, b} | x starts with symbol a, contains substring bab, and
ends with aa}
• Examples: ababaa, aabbabbbbaa, a[x]bab[x]aa
a, b a, b
a b a b a a
q0 q1 q2 q3 q4 q5 q6
12/14/2019 AbuFatimah 6
Example 2: [contains substring abababab]
• Please construct an NFA for the following language.
• L = { x over {a, b} | x contains substring abababab}
• e.g. abababab, bbbababababbbb, aaababababa
a, b a, b
a b a b a b a b
q0 q1 q2 q3 q4 q5 q6 q7 q8
12/14/2019 AbuFatimah 7
Example 3: [substring 010, or 101]
• Please design an NFA to recognize the following language:
• L = {x over {0, 1} | x contains substring 010 or substring 101}
• e.g. 010, 101, 1010, 00010, 000101, 111010, 10101010,
12/14/2019 AbuFatimah 8
Example 3: [substring 010, or 101]
• Please design an NFA to recognize the following language:
• L = {x over {0, 1} | x contains substring 010 or substring 101}
• e.g. 010, 101, 1010, 00010, 000101, 111010, 10101010,
1 0
0 s1 s2
12/14/2019 AbuFatimah 9
Example 3: [substring 010, or 101]
• Please design an NFA to recognize the following language:
• L = {x over {0, 1} | x contains substring 010 or substring 101}
• e.g. 010, 101, 1010, 00010, 000101, 111010, 10101010,
1 0
0 s1 s2
0 1
1 s3 s4
12/14/2019 AbuFatimah 10
Example3: [substring 010, or 101]
اﻟﺤﻞ اﻟﻨﮭﺎﺋﻲ
• Please design an NFA to recognize the following language:
• L = {x over {0, 1} | x contains substring 010 or substring 101}
• e.g. 010, 101, 1010, 00010, 000101, 111010, 10101010,
1 0
0, 1 0 s2 0, 1
s1
s0 s5
0 1
1 s3 s4
12/14/2019 AbuFatimah 11
The End ()اﻟﻨﮭﺎﯾﺔ
• Resource:
• “Introduction to the
Theory of
Computation” by
Sipser, 3rd edition.
• Chapter 1: Regular
Languages
• 1.2 Nondeterminism
12/14/2019 AbuFatimah 12
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
Lesson9
Converting NFA to DFA
AbuFatimah
PhD student in computer science
7/11/17 AbuFatimah 1
Which is stronger?
DFA or NFA
• NFA might be easier
to design and to
understand. Because
we don’t need to
worry about all
conditions.
7/11/17 AbuFatimah 2
Which is stronger?
DFA or NFA
• However, NFA and
DFA have the same
power and can
recognize the same
class of languages.
• So, every NFA can be
converted to DFA.
7/11/17 AbuFatimah 3
Example 1: NFA with one state
NFA DFA
0
0
{n0}
n0
0 1
*{n0} {n0} {}
0 1 1
{} {} {}
*n0 n0
{} 0,
1
7/11/17 AbuFatimah 4
Example2: NFA with two states
NFA DFA
0,
1 1
{n0} {}
n0
0 1 0 0
0 1
n0 n1
{n0} {n1} {}
*n1 n1 n1
n1 {n1} {} {} {}
0,
1 *{n1} {n1} {n1}
0, 1
7/11/17 AbuFatimah 5
0 1
0,
1 1
{n0} {}
n0
0 1 0 1 0
1
n0 n1
*n1 n1 n0, n1 1
n1 0 {n1} {n0,n1}
0,
1
0
7/11/17 AbuFatimah
DFA 6
ﺗذﻛﯾـــــر
0 abbbaba
a abbba
1
1 abbbab
0
a 2 abbbaba
1
3
ab one copy reached the final state, so
2 the machine accepts this input string
abb
7/11/17 AbuFatimah 7
• Convert this NFA to a DFA
states a b
0 0, 1 0
1 2
2 3 b
3 3 3 b a
a b
{0} {0, 1} {0, 2}
states a b a a
{0} {0, 1} {0}
a
{0, 1} {0, 1} {0, 2} {0, 1, 3}
{0,1,3}
{0, 2} {0, 1, 3} {0} b {0,3}
{0, 3}
7/11/17 AbuFatimah 8
• Does this DFA accept this string?
• string = abbbaba
• Do it yourself
• ﺣﺎول ﺗﺘﺄﻛﺪ ﺑﻨﻔﺴﻚ
b
b a
a b
{0} {0, 1} {0, 2}
states a b a a
{0} {0, 1} {0}
a
{0, 1} {0, 1} {0, 2} {0, 1, 3}
{0,1,3}
{0, 2} {0, 1, 3} {0} b {0,3}
{0, 3}
7/11/17 AbuFatimah 9
state 0 1 ε
s
q0 q1 q2
Example 5: convert NFA to DFA
q1 q1,q2 q2
q2 q0
NFA
q0
0
1
ε
0 q1 q2
0, 1
NFA4
7/12/17 AbuFatimah 10
state 0 1 ε
s
q0 q1 q2
Example 5: convert NFA to DFA
DFA
q1 q1,q2 q2
q2 q0
{}
q0
0
0 1 1
1 1
ε {q1} {q2}
1
0 0
0 q1 q2
0, 1 0
{q1, q2} {q0,q1,q2}
NFA4 1
7/11/17 AbuFatimah 11
The End () اﻟﻨﮭﺎﯾﺔ
• Resource:
• “Introduction to the Theory of
Computation” by Sipser, 3rd
edition.
7/11/17 AbuFatimah 12
ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم
Lesson10
Regular Expressions
AbuFatimah
PhD student in computer science
NFA Regex
Lesson11
Examples of Regular Expressions
AbuFatimah
PhD student in computer science
^ asserts the start of the string. .ھذا اﻟﺳؤال ﺑﻧﻔﺳك • ﺣﺎول ﺗﺣل
(?!.*ba) is a negative lookahead assertion that ensures the string does not
contain the substring "ba" anywhere.
.* matches any number of characters (except newline).
$ asserts the end of the string.
In regular expressions, the dot (.) is a special character that matches any single character except a newline. It acts as a
placeholder for any character in a pattern.
For example, if you have the pattern "c.t," it would match "cat," "cut," "cet," or any other three-character string where the
second character is anything except a newline.
Lesson12
Convert Regex to NFA
AbuFatimah
PhD student in computer science
• Example 2: b
0 1
• regex = b
a b
0 1 0 1
a b
0 1 0 1
a ε b
0 1 2 3
• regex = a*
• First, we show NFA for regex = a
a ε b
0 1 2 3
ε a ε b
0 1 2 3 4
b
0 1
ε ε
ε a ε b
0 1 2 3 4 5
ε a ε ε b
0 1 2 3 4 5
Lesson13
Convert DFA/NFA to Regex
AbuFatimah
PhD student in computer science @ UNL
a∪b a a
aba∪aa b
s0 s1 s0 s1
GNFA 1 NFA 1
7 January 2018 AbuFatimah Slide 3 out of 20
Generalized Nondeterministic Finite Automata
(GNFA)
GNFA NFA
1 In transition arrows, we can have any regex In transition arrow, we can have only alphabet letters
or ε .
2 Reads block of input symbols at a time Reads only one input symbol at a time
a∪b a a
aba∪aa b
s0 s1 s0 s1
GNFA 1 NFA 1
7 January 2018 AbuFatimah Slide 4 out of 20
Generalized Nondeterministic Finite Automata
(GNFA)
GNFA NFA
3 Has only one final state. May have more than one final state.
a∪b a
a
s1
aba∪aa s0
s0 s1 b s1
s0
s1
s2
GNFA 1 NFA 1 NFA 2
7 January 2018 AbuFatimah Slide 5 out of 20
Generalized Nondeterministic Finite Automata
(GNFA)
GNFA NFA
3 Has only one final state. May have more than one final state.
4 The final state is different from the start state. The final state may be the same/different from the
start state.
a∪b a
a
s1
aba∪aa s0
s0 s1 b s1
s0
s1
s2
GNFA 1 NFA 1 NFA 2
7 January 2018 AbuFatimah Slide 6 out of 20
Generalized Nondeterministic Finite Automata
(GNFA)
GNFA NFA
5 The start state only has outgoing transition The start state may have incoming or outgoing
arrows. arrows.
6 The start state has no incoming transition
arrows.
a∪b a
a
s1
aba∪aa s0 a
s0 s1 s1
s0
b
GNFA 1 NFA 1 NFA 2
s1
s2
7 January 2018 AbuFatimah Slide 7 out of 20
Example 1: convert DFA into regex
a a
b
s0 s1
• step 1: b
• add a new start and a new s0 s1
final state.
• merge the two transitions
from s1 to s1 into one b
transition. a
ε b ε
st s0 s1 s1
fi
a∪b
• step 2: a
• remove s0 by
finding an ε b ε
equivalent regex st s0 s1 s1
fi
a∪b
a* b ε
st s1 s1
fi
a∪b
• step 3: a* b ε
• remove s1 by finding an st s1 s1
fi
equivalent regex
a∪b
a* b(a∪b)*
st s1
fi
a a
b
s0 s1 regex = a*b(a∪b)*
b
s0 s1
a
a
s2
a a
s2
a b* a