0% found this document useful (0 votes)
239 views187 pages

Automata

.

Uploaded by

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

Automata

.

Uploaded by

Ola Badawy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‫ﺑﺳم ﷲ اﻟرﺣﻣن اﻟرﺣﯾم‬

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M3

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

State Diagram for M4

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

State Diagram for M4

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

State Diagram for M4

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

State Diagram for M4

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

• After each symbol, you must decide to ACCEPT or NOT.


• How to do it?
• What information you need to remember about the string so far.

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, ..., ε}

a Testing our DFA


b b
Input string: baba
qeven qodd

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, ..., ε}

a Testing our DFA


b b

qeven qodd Input string: baba

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, ..., ε}

a Testing our DFA


b b

qeven qodd Input string: baba

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, ..., ε}

a Testing our DFA


b b

qeven qodd Input string: baba


a

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, ..., ε}

a Testing our DFA


b b

qeven qodd Input string: baba


a

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

(NFA) ‫( و‬DFA) ‫ﻣﻠﺧص اﻟﻔروﻗﺎت ﺑﯾن‬


1) In DFA, we must have one arrow for each label. In NFA, we can have zero, one,
two, or more arrows for each label.
2) In NFA, we have an arrow with label ε (empty string). But in DFA, we can’t have
an arrow with label ε. All arrow in DFA have labels from the alphabet.
5/17/17 AbuFatimah 8
How does an NFA work? ‫ﻛﯿﻔﯿﺔ ﻋﻤﻞ اﻵﻟﺔ‬
• Nondeterministic Machine, NFA2
• Input string = abbbaba

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
‫ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم‬

Lesson6 ‫اﻟﺪرس اﻟﺴﺎدس‬


Minimum DFA
AbuFatimah
PhD student in computer science
‫طﺎﻟب دﻛﺗوراه ﻋﻠوم ﺣﺎﺳب‬

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

• 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.

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

q0
q1
q00
G1
q10
q11
G2
5/20/17 AbuFatimah 7
‫ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ‬
G1 (final states)
0 1
q01
G2 (non-final states)
0 1

q0
q1
q00
q10
q11
5/20/17 AbuFatimah 8
‫ﻧﻤﻸ اﻟﺠﺪول ﻋﻠﻰ ﺣﺴﺐ اﻟﺮﺳﻤﺔ ﻟﻜﻞ ﺣﺎﻟﺔ‬
G1 (final states)
0 1
q01 G2(q10)
G2 (non-final states)
0 1

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

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

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

G22 (non-final states)


0 1 G21 0

qε G21 G22
q1 G21 G22
0
q11 G21 G11
5/20/17 AbuFatimah 20
‫ﻧﻔﺲ اﻟﺮﺳﻢ اﻟﺒﯿﺎﻧﻲ ﻣﻦ اﻟﺪرس اﻟﺮاﺑﻊ‬
‫‪1‬‬

‫‪G22‬‬ ‫‪1‬‬

‫‪0‬‬ ‫‪1‬‬ ‫‪G1‬‬

‫‪G21‬‬ ‫‪0‬‬

‫‪0‬‬

‫‪5/20/17‬‬ ‫‪AbuFatimah‬‬ ‫‪21‬‬


The End (‫) اﻟﻨﮭﺎﯾﺔ‬
• Resource:
• “Introduction to the
Theory of
Computation” by
Sipser, 3rd edition.

• Chapter 1: Regular
Languages
• 1.1 Finite Automata

5/20/17 AbuFatimah 22
‫ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم‬

‫‪Lesson7‬‬
‫‪Understanding NFA‬‬
‫‪AbuFatimah‬‬
‫طﺎﻟب دﻛﺗوراه ﻓﻲ ﺗﺧﺻص ﻋﻠوم اﻟﺣﺎﺳب‬

‫‪12/14/2019‬‬ ‫‪AbuFatimah‬‬ ‫‪1‬‬


‫ ﺗﺘﺒﻊ اﻵﻟﺔ‬:‫ ﻣﺜﺎل‬Tracing NFA with empty string
• Nondeterministic Machine, NFA3
• Does this NFA accept string = baa?

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.

• Chapter 1: Regular Languages


• 1.2 Nondeterminism

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

Example3: NFA with 2 states {n0}


{}
{n1}
{}
{}
{}
*{n1} {n1} {n1}

NFA *{n0, n1} {n1} {n0, n1}

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
‫ﺗذﻛﯾـــــر‬

• 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
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}

*{0, 1, 3} {0, 1, 3} {0, 2, 3} b a


*{0, 2, 3} {0, 1, 3} {0, 3}
*{0, 3} {0, 1, 3} {0, 3} b {0,2,3}
{0, 2, 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}

*{0, 1, 3} {0, 1, 3} {0, 2, 3} b a


*{0, 2, 3} {0, 1, 3} {0, 3}
*{0, 3} {0, 1, 3} {0, 3} b {0,2,3}
{0, 2, 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

NFA {q0, q2}


{q0, q2}
0, 1

{}
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.

• Chapter 1: Regular Languages


• 1.2 Nondeterminism

7/11/17 AbuFatimah 12
‫ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم‬

Lesson10
Regular Expressions
AbuFatimah
PhD student in computer science

22 October 2017 AbuFatimah Slide 1 out of 10


What is Regular Expression (Regex)?
• Expression to describe a regular language.
DFA

NFA Regex

22 October 2017 AbuFatimah Slide 2 out of 10


Basic examples of regex
• e.g: L3 = 1* U 0*
• K* = Kn for n >= 0 = ε, 1, 0, 11, 00, 1111, 00000, …
L1 = 0* L4 = (1 U 0)
= ε, 0, 00, 0000, …. = 1, 0
L2 = 1*
= ε, 1, 111, 11111, …

22 October 2017 AbuFatimah Slide 3 out of 10


More examples
• e.g:
L5 = 01*
= 0, 01, 0111, 011111, …
L6 = (0 ∪ 1)*
= (0 U 1)(0 U 1)(0 U 1) … (0 U 1)
= ε, 1, 0, 10, 111, 0001, 01010, 1111, …
= all strings of 0s or 1s. Here our language alphabet is Σ = {0, 1}.
L6 = (0 ∪ 1)* = Σ* = all string in the alphabet

22 October 2017 AbuFatimah Slide 4 out of 10


Formal Definition of Regex – examples next
1. a for some a in the alphabet Σ,
2. ε,
3. ∅,
4. (R1∪R2), where R1 and R2 are regex. [Union]
5. (R1 ◦ R2), where R1 and R2 are regex. [Concatenation]
6. (R1*), where R is a regex. [Star]

22 October 2017 AbuFatimah Slide 5 out of 10


1. a for some a in the alphabet Σ, for Σ = {0, 1}.
• 0 is a regex. 1 is a regex
2. ε, the empty string is a regex
3. ∅, the empty language is a regex
4. (R1∪R2), where R1 and R2 are regex. [Union]
• assume R1 = 1*, R2 = 0*, then (1* U 0*) is a regex
• assume R1 = 1, R2 = ε, then (1 U ε) is a regex
5. (R1 ◦ R2) = R1R2, where R1 and R2 are regex. [Concatenation]
• assume R1 = 1*, R2 = 0*, then (1*0*) is a regex
• assume R1 = 0, R2 = 1*, then (01*) is a regex
6. (R*), where R is a regex. [Star]
• if R = 1, then 1* is a regex
• if R = (1 U 0), then (1 U 0)* is a regex

22 October 2017 AbuFatimah Slide 6 out of 10


Concatenation Operation [‫ﻋﻤﻠﯿﺔ اﻟﺮﺑﻂ‬ ]
• Concatenation of two strings is joining them together
• String x = “aa”. String y = “bb”.
• Concatenating x and y = xy = “aabb”
• Concatenation of two languages is joining each string in the first
language with all strings in the second language.
• L1 = {aa, bb}, L2 = {a, b}
• L1 ◦ L2 = L1L2
• = {aaa, aab, bba, bbb}
• L2 ◦ L1 = L2L1
• = {aaa, abb, baa, bbb}

22 October 2017 AbuFatimah Slide 7 out of 10


The Star (*) of a language
• The star of a language = • S1 • S* = …
multiple concatenations of • = {ba} • S* = {ε, ba,
itself baba, bababa,
• S2 = SS
• S* = Sn (n >= 0) = babababa, …}
concatenate S with itself • = {baba}
n-1 times • S3 = SSS
• Example: • = {bababa}
• S = {ba}
• S0
• = {ε}

22 October 2017 AbuFatimah Slide 8 out of 10


The Star (*) of a language
• Example: • M3 = MMM
• M = {bb, aa}. • = {bbbb, bbaa, aabb, aaaa} {bb, aa}.
• M0 = {ε} • = {bbbbbb, bbbbaa, bbaabb, bbaaaa,
aabbbb, aabbaa, aaaabb, aaaaaa}.
• M1 = {bb, aa}.
• M* = …
• M2 = MM
• = {ε, bb, aa, bbbb, bbaa, aabb, aaaa,
• = {bb, aa} {bb, aa}. bbbbbb, bbbbaa, bbaabb, bbaaaa,
• = {bbbb, bbaa, aabb, aaaa}. aabbbb, aabbaa, aaaabb, aaaaaa, …}

22 October 2017 AbuFatimah Slide 9 out of 10


The End (‫) اﻟﻨﮭﺎﯾﺔ‬
• Resource:
• “Introduction to the Theory of
Computation” by Sipser, 3rd edition.

• Chapter 1: Regular Languages


• 1.3 Regular Expressions

22 October 2017 AbuFatimah Slide 10 out of 10


‫ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم‬

Lesson11
Examples of Regular Expressions

AbuFatimah
PhD student in computer science

7 November 2017 AbuFatimah Slide 1 out of 8 1


Alphabet Σ = {a, b}. regex Σ* = (a U b)*
• Q: Design a regex for a language where: ‫• ﻗم ﺑﺗﺻﻣﯾم ﺗﻌﺑﯾر ﻧﻣطﻲ ﺑﺣﯾث‬
1- All strings start with a: a ‫ ﻛل اﻟﻧﺻوص ﺗﺑدأ ﺑﺣرف‬-١
• example strings: ab, a, aaa, abbb, aba aΣ*
• regex = aΣ*
2- All strings end with b: b ‫ ﻛل اﻟﻧﺻوص ﺗﻧﺗﮭﻲ ﺑﺣرف‬-٢
• example strings: b, ab, bb, bab, aaab Σ*b
• regex = Σ*b
3- All strings start with a or end with b:
• example strings: ab, a, b, bab, aba
‫ او‬a ‫ ﻛل اﻟﻧﺻوص ﺗﺑدأ ﺑﺣرف‬-٣
• regex = aΣ* U Σ*b
b ‫ﺗﻧﺗﮭﻲ ﺑﺣرف‬
aΣ* U Σ*b

7 November 2017 AbuFatimah Slide 2 out of 8 2


regex Σ* = (a U b)*
• Q: Design a regex for a language where: ‫• ﻗم ﺑﺗﺻﻣﯾم ﺗﻌﺑﯾر ﻧﻣطﻲ ﺑﺣﯾث‬
4- All strings contain a single b: ‫ ﻛل اﻟﻧﺻوص ﺗﺣﺗوي ﻋﻠﻰ‬-٤
• example strings: b, ba, ab, aaab ‫ واﺣد ﻓﻘط‬b ‫ﺣرف‬
• regex = a* b a* a* b a*
5- All strings contain at least one b: ‫ ﻛل اﻟﻧﺻوص ﺗﺣﺗوي ﻋﻠﻰ‬-٥
• example strings: b, bb, aba, bba ‫ واﺣد ﻋﻠﻰ اﻷﻗل‬b ‫ﺣرف‬
• regex = Σ* b Σ* Σ* b Σ*
6- All strings contain substring aab: ‫ ﻛل اﻟﻧﺻوص ﺗﺣﺗوي ﻋﻠﻰ ﻧص‬-٦
• example strings: aab, aaba, bbaab aab ‫ﻓرﻋﻲ‬
• regex = Σ* aab Σ* Σ* aab Σ*

7 November 2017 AbuFatimah Slide 3 out of 8 3


• Q: Design a regex for a language where: ‫• ﻗم ﺑﺗﺻﻣﯾم ﺗﻌﺑﯾر ﻧﻣطﻲ‬
7- All strings start and end with a: ‫ﺑﺣﯾث‬
• example strings: a, aa, aba, abaaaba, abbbba ‫ ﻛل اﻟﻧﺻوص ﺗﺑدأ وﺗﻧﺗﮭﻲ‬-٧
• regex = a Σ* a a ‫ﺑﺣرف‬
• regex = a Σ* a U a a Σ* a U a
8- All strings start and end with b: ‫ ﻛل اﻟﻧﺻوص ﺗﺑدأ وﺗﻧﺗﮭﻲ‬-٨
• example strings: b, bb, bab, bababab, baaaab b ‫ﺑﺣرف‬
• regex = b Σ* b U b b Σ* b U b
9- All strings start and end with the same ‫ ﻛل اﻟﻧﺻوص ﺗﺑدأ وﺗﻧﺗﮭﻲ‬-٩
symbol: ‫ﺑﻧﻔس اﻟرﻣز‬
• example strings: a, b, aba, bab, babab, ababa aΣ* U Σ*b •
• regex: a U b U (a Σ* a) U (b Σ* b)

7 November 2017 AbuFatimah Slide 4 out of 8 4


• Q: Design a regex for a language where: ‫• ﻗم ﺑﺗﺻﻣﯾم ﺗﻌﺑﯾر ﻧﻣطﻲ‬
10- All strings are of even length (multiple of 2): ‫ﺑﺣﯾث‬
• example strings: ε, aa, bb, ab, ba, aabb, bbaa ‫ ﻛل اﻟﻧﺻوص ذات طول‬-١٠
• regex = Σ Σ = (a U b)(a U b) (٢ ‫زوﺟﻲ )ﻣن ﻣﺿﺎﻋﻔﺎت‬
• regex = (Σ Σ)* = ((a U b)(a U b))* (Σ Σ)*
11- All strings are of odd length (1, 3, 5, 7, 9, …): ‫ ﻛل اﻟﻧﺻوص ذات طول‬-١١
• example strings: b, a, bab, aaa, bbbaa (٢ ‫ﻓردي )ﻻ ﯾﻘﺑل اﻟﻘﺳﻣﺔ ﻋﻠﻰ‬
• regex = (Σ Σ Σ)* = ((a U b)(a U b)(a U b))* (Σ Σ)* Σ
• regex = Σ* = (a U b)* ‫ ﻛل اﻟﻧﺻوص ذات طول‬-١٢
• regex = (Σ Σ)* Σ, note: (even + 1) = odd ٣ ‫ﻣن ﻣﺿﺎﻋﻔﺎت‬
12- All strings are of length multiple of 3: (Σ Σ Σ)*
• example strings: ε, aaa, bbb, aba, bab, aaabbb
• regex = (Σ Σ Σ)* = ((a U b)(a U b)(a U b))*

7 November 2017 AbuFatimah Slide 5 out of 8 5


Homework Question1
• Q1: Design a regex for a language where All strings don’t
contain substring ba. ^(?!.*ba).*$
Explanation:

^ 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.

7 November 2017 AbuFatimah Slide 6 out of 8 6


Homework Question2
• Q2: For each of the following languages, give two strings that
are members and two strings that are not members, a total
of four strings for each part. Alphabet Σ = {a, b}.
A. a(ba)*b Members:"ab" ,"ababaab" , Non-members:"aa", "abb"
B. (ε U a)b Members:"b" , "ab" , Non-members:"a" , "ba"
C. a* U b* Members:"a" , "bbb" , Non-members:"ab" , "baa"
D. Σ* a Σ* b Σ* a Σ* Members:"aabb" , "abbaaa" , Non-members:"abb" , "baaa"
.‫• ﺣﺎول ﺗﺣل ھذا اﻟﺳؤال ﺑﻧﻔﺳك‬
.‫ وﻧﺻوص ﻏﯾر ﻣﻘﺑوﻟﺔ‬،‫• ﻗم ﺑﻛﺗﺎﺑﺔ أﻣﺛﻠﺔ ﻋﻠﻰ ﻧﺻوص ﺗﻛون ﻣﻘﺑوﻟﺔ ﻣن ھذا اﻟﺗﻌﺑﯾر اﻟﻧﻣطﻲ‬

7 November 2017 AbuFatimah Slide 7 out of 8 7


The End (‫) اﻟﻨﮭﺎﯾﺔ‬
• Resource:
• “Introduction to the Theory of
Computation” by Sipser, 3rd edition.

• Chapter 1: Regular Languages


• 1.3 Regular Expressions

7 November 2017 AbuFatimah Slide 8 out of 8 8


‫ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم‬

Lesson12
Convert Regex to NFA
AbuFatimah
PhD student in computer science

7 November 2017 AbuFatimah Slide 1 out of 15


Example 1&2: convert regex to an NFA
• Example 1:
a
• regex = a 0 1

• Example 2: b
0 1
• regex = b

7 November 2017 AbuFatimah Slide 2 out of 15


Example 3: convert regex=ab to an NFA
• Example 3: :‫ﻣﻼﺣظﺔ‬
• regex = ab regex = ab = (a.b)
• concatenation

a b
0 1 0 1

7 November 2017 AbuFatimah Slide 3 out of 15


Example 3: convert regex=ab to an NFA
• Example 3: :‫ﻣﻼﺣظﺔ‬
• regex = ab regex = ab = (a.b)

a b
0 1 0 1

7 November 2017 AbuFatimah Slide 4 out of 15


Example 3: convert regex=ab to an NFA
• Example 3: :‫ﻣﻼﺣظﺔ‬
• regex = ab regex = ab = (a.b)
• now we connect join them

a ε b
0 1 2 3

7 November 2017 AbuFatimah Slide 5 out of 15


Example 4: convert regex=a* to an NFA
• Example 4:
a
0 1

• regex = a*
• First, we show NFA for regex = a

7 November 2017 AbuFatimah Slide 6 out of 15


Example 4: convert regex=a* to an NFA
ε
• Example 4:
ε a
0 1 2

‫ﺣل آﺧر ﺻﺣﯾﺢ‬


‫وﯾﺳﺗﺧدم‬
• regex = a* λ ε=λ
• First, we show NFA for regex = a
a
• Then, we show NFA for regex = a* 1 2

7 November 2017 AbuFatimah Slide 7 out of 15


Example 5: convert regex=(ab)* to an NFA
• Example 5:
• regex = (ab)*
• First, we show NFA for regex = ab

a ε b
0 1 2 3

7 November 2017 AbuFatimah Slide 8 out of 15


Example 5: convert regex=(ab)* to an NFA
• Example 5:
• regex = (ab)*
• First, we show NFA for regex = ab
• Then, we show NFA for regex = (ab)*
ε

ε a ε b
0 1 2 3 4

7 November 2017 AbuFatimah Slide 9 out of 15


Convert regex with union operation
• Example 6:
a
• regex = (a ∪ b) 0 1
• First we have two NFA’s:
• for a, and for b

b
0 1

‫ رﻣزﯾن ﯾﺳﺗﺧدﻣﺎ ﻟﻌﻣﻠﯾﺔ اﻹﺗﺣﺎد‬:‫ﻣﻼﺣظﺔ‬


Note: (a ∪ b) = (a + b)

7 November 2017 AbuFatimah Slide 10 out of 15


Convert regex with union operation
• Example 6:
a
• regex = (a ∪ b) ε 1 2
• First we have two NFA’s:
• for a, and for b 0

• Then, we join them


ε b
3 4

7 November 2017 AbuFatimah Slide 11 out of 15


Example 7: convert a*b* to an NFA
• Example 7:
• regex = a*b*
• we show NFA’s for a* and b*

ε ε

ε a ε b
0 1 2 3 4 5

7 November 2017 AbuFatimah Slide 12 out of 15


Example 7: convert a*b* to an NFA
• Example 7:
• regex = a*b*
• we show NFA’s for a* and b*
• e.g string = aaaaa, string2 = bbbbb
ε ε

ε a ε ε b
0 1 2 3 4 5

7 November 2017 AbuFatimah Slide 13 out of 15


Homework ‫ﺳﺆال اﻟﻮاﺟﺐ‬
Convert the following regex = (ab ∪ a)* to NFA

7 November 2017 AbuFatimah Slide 14 out of 15


The End (‫) اﻟﻨﮭﺎﯾﺔ‬
• Resource:
• “Introduction to the Theory of
Computation” by Sipser, 3rd edition.

• Chapter 1: Regular Languages


• 1.3 Regular Expressions

7 November 2017 AbuFatimah Slide 15 out of 15


‫ﺑﺳـم ﷲ اﻟرﺣﻣـن اﻟرﺣﯾــم‬

Lesson13
Convert DFA/NFA to Regex
AbuFatimah
PhD student in computer science @ UNL

7 January 2018 AbuFatimah Slide 1 out of 20


Equivalent Regular Expression (Regex)?
• We have described how to
convert a regex to NFA (lesson12)
• Today, we’ll describe how to
convert NFA to regex DFA

• First, we have to convert


DFA/NFA to a Generalized NFA
(GNFA)
NFA Regex

7 January 2018 AbuFatimah Slide 2 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 ε .

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

7 January 2018 AbuFatimah Slide 8 out of 20


Example1: convert DFA into regex
a a

• 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

7 January 2018 AbuFatimah Slide 9 out of 20


Example1: convert DFA into regex

• 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

7 January 2018 AbuFatimah Slide 10 out of 20


Example1: convert DFA into regex

• step 3: a* b ε
• remove s1 by finding an st s1 s1
fi
equivalent regex
a∪b

a* b(a∪b)*
st s1
fi

7 January 2018 AbuFatimah Slide 11 out of 20


Example1: convert DFA into regex

• we finish when we have


only one transition arrow
from the start to the final
state. st
a* b(a∪b)*
s1
fi
• The transition label is an
equivalent regex

7 January 2018 AbuFatimah Slide 12 out of 20


Example1: convert DFA into regex
• solution

a a

b
s0 s1 regex = a*b(a∪b)*

7 January 2018 AbuFatimah Slide 13 out of 20


Steps to convert DFA/NFA to regex
1. Add a new start state and a new final state
2. remove a state and replace with an equivalent regex
3. repeat 2 until there’s only one transition arrow from the start to
the final state.
4. Your regex is the label of the transition arrow

7 January 2018 AbuFatimah Slide 14 out of 20


Example 2: Convert NFA to regex
• Step 1: add a new start and final states

b
s0 s1

a
a
s2

8 January 2018 AbuFatimah Slide 15 out of 20


Example 2: Convert NFA to regex
• Step 2: remove s2 and find equivalent regex
ε b ε
st s0 s1 s1
fi

a a
s2

8 January 2018 AbuFatimah Slide 16 out of 20


Example 2: Convert NFA to regex
• Step 3: Merge the two transition arrows from s0 to s1
ε b ε
st s0 s1 s1
fi

a b* a

7 January 2018 AbuFatimah Slide 17 out of 20


Example 2: Convert NFA to regex
• Step 4: remove s0 and s1 to connect the start and final states.
ε b ∪ a b* a ε
st s0 s1 s1
fi

7 January 2018 AbuFatimah Slide 18 out of 20


Example 2: Convert NFA to regex
• Final solution
b ∪ a b* a
st s1
fi

7 January 2018 AbuFatimah Slide 19 out of 20


The End – ‫اﻟﻨﮭﺎﯾﺔ‬
• Resource:
• “Introduction to the Theory of
Computation” by Sipser, 3rd edition.

• Chapter 1: Regular Languages


• 1.3 Regular Expressions

7 January 2018 AbuFatimah Slide 20 out of 20

You might also like