COS210 - Theoretical Computer Science Finite Automata and Regular Languages (Part 1)
COS210 - Theoretical Computer Science Finite Automata and Regular Languages (Part 1)
1
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
Modelling questions:
What are the possible “states” of the machine?
How does the insertion of coins change the “state” of the machine?
2
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
0 1 2 3 4 5 6 7
3
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
1
0 1 2 3 4 5 6 7
4
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2 2
1 1
0 1 2 3 4 5 6 7
5 5
5
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2 2 2
1 1 1
0 1 2 3 4 5 6 7
5 5 5
6
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2 2 2 2
1 1 1 1
0 1 2 3 4 5 6 7
5
5 5 5
7
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2 2 2 2 2
1 1 1 1 1
0 1 2 3 4 5 6 7
5
5
5 5 5
8
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2 2 2 2 2 2,5
1 1 1 1 1 1
0 1 2 3 4 5 6 7
5
5
5 5 5
9
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2 2 2 2 2 2,5
1 1 1 1 1 1 1,2,5
0 1 2 3 4 5 6 7
5
5
5 5 5
10
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2,5 1,2,5
2 2 2 2 2
1 1 1 1 1 1 1,2,5
0 1 2 3 4 5 6 7
5
5
5 5 5
11
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2,5 1,2,5
2 2 2 2 2
1 1 1 1 1 1 1,2,5
0 1 2 3 4 5 6 7
5
5
5 5 5
12
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Abstract Example
00101111010100011101011001000
which we call an input string
The machine only accepts input strings that end with 00
13
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Deterministic Finite Automaton for Abstract Example
1 0
0
q0 q1 0 q2
1
1 0
0
q0 q1 0 q2
1
16
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
DFA: Formal Definition
Definition
A deterministic finite automaton is a 5-tuple M = (Q, ⌃, , q, F ), where
Q is a finite set, whose elements are called states,
⌃ is a finite set, called the alphabet;
I the elements of ⌃ are called symbols,
: Q ⇥ ⌃ ! Q is a function, called the transition function,
q is an element of Q; it is called the start state,
F is a subset of Q; the elements of F are called accept states.
1 0
0
q0 q1 0 q2
M:
1
1 17
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
DFA Graphically and Formally
1 0
0
q0 q1 0 q2
M:
1
18
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Motivating Example: Vending Machine
2,5 1,2,5
2 2 2 2 2
1 1 1 1 1 1 1,2,5
0 1 2 3 4 5 6 7
5
5
5 5 5
19
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Deterministic Finite Automaton for the Vending Machine
R1 R2 R5
q0 q1 q2 q5
q1 q2 q3 q6
q2 q3 q4 q7
... ... ... ...
20
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Abstract Example 2
21
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
DFA for Abstract Example 2
1 0
0
q0 q1 0 q2
1
22
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
DFA for Abstract Example 2
1 0
0
q0 q1 0 q2
1
1
1
q3 1 q4
1 1 1 1
q3 1 q4
1 0 1 0 1 1
q3 1 q4
General form:
Definition
Let M = (Q, ⌃, , q, F ) be an automaton and let w = w1 . . . wn be a string
over ⌃. A run of M over w is a sequence of states q0 , . . . , qn such that
q0 = q,
(qi , wi+1 ) = qi+1 , for all i < n.
(the transitions along q0 , . . . , qn are labelled with w1 . . . wn )
A string w is accepted by M if the following holds for the run over w :
qn 2 F .
Otherwise a string is rejected by M.
27
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Runs and Acceptance
Definition
Let M = (Q, ⌃, , q, F ) be an automaton and let w = w1 . . . wn be a string
over ⌃. A run of M over w is a sequence of states q0 , . . . , qn such that
q0 = q,
(qi , wi+1 ) = qi+1 , for all i < n.
(the transitions along q0 , . . . , qn are labelled with w1 . . . wn )
Example:A string w is accepted by M if the following holds for the run over w :
For input w = 101100
qn 2run
The corresponding F .is q , q , q , q , q , q , q
0 0 1 0 0 1 2
Otherwise a string is rejected by M.
28
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Runs and Acceptance
Definition
Let M = (Q, ⌃, , q, F ) be an automaton and let w = w1 . . . wn be a string
over ⌃. A run of M over w is a sequence of states q0 , . . . , qn such that
q0 = q,
(qi , wi+1 ) = qi+1 , for all i < n.
(the transitions along q0 , . . . , qn are labelled with w1 . . . wn )
A string w is accepted by M if the following holds for the run over w :
qn 2 F .
Otherwise a string is rejected by M.
29
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Runs and Acceptance
Definition
Let M = (Q, ⌃, , q, F ) be an automaton and let w = w1 . . . wn be a string
over ⌃. A run of M over w is a sequence of states q0 , . . . , qn such that
q0 = q,
(qi , wi+1 ) = qi+1 , for all i < n.
(the transitions along q0 , . . . , qn are labelled with w1 . . . wn )
Example:A string w is accepted by M if the following holds for the run over w :
For input w = 101100
qn 2run
The corresponding F .is q , q , q , q , q , q , q
0 0 1 0 0 1 2
Otherwise a string is rejected by M.
30
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Runs and Acceptance
Definition
Let M = (Q, ⌃, , q, F ) be an automaton and let w = w1 . . . wn be a string
over ⌃. A run of M over w is a sequence of states q0 , . . . , qn such that
q0 = q,
(qi , wi+1 ) = qi+1 , for all i < n.
(the transitions along q0 , . . . , qn are labelled with w1 . . . wn )
A string w is accepted by M if the following holds for the run over w :
qn 2 F .
Otherwise a string is rejected by M.
31
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Regular Languages
Definition
Let M = (Q, ⌃, , q, F ) be an automaton. The language L(M) of M is
the set of all strings that are accepted by M:
Definition
A language L is called regular, if there exists a finite automaton M such
that
L = L(M).
qxyz
the last symbol was z, the second last was y, the third last was x
we start in q000 (no 1’s encountered so far)
the accepting states are q100 , q101 , q110 , q111
33
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Example
qxyz
the last symbol was z, the second last was y, the third last was x
we start in q000 (no 1’s encountered so far)
the accepting states are q100 , q101 , q110 , q111
34
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Example
qxyz
the last symbol was z, the second last was y, the third last was x
we start in q000 (no 1’s encountered so far)
the accepting states are q100 , q101 , q110 , q111
35
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Example
qxyz
the last symbol was z, the second last was y, the third last was x
we start in q000 (no 1’s encountered so far)
the accepting states are q100 , q101 , q110 , q111
36
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm
Example
37
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
38
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0
1
39
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0
000 100 010 110
0
1
1
40
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0
000 100 010 110
0
1
1 0
41
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0 0
000 100 010 110
1 0
1
1 0
42
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0 0
000 100 010 110
1 0 0
1
1 0
1
001 101 011 111
1
43
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0 0
0 0
000 100 010 110
1 0 0
1
1 1 0
1
001 101 011 111
1
44
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Example
0 0
0 0
000 100 010 110
1 0 0
1 0
1 1 0
1
001 101 011 111
1
1 1
45
Finite Automata and Regular Languages ©Cleghorn, Marshall, Timm
Regular
ExampleLanguages
the last symbol was z, the second last was y, the third last was x
How we start inthat
to prove q000a (no 1’s encountered
language L is regular? so far)
the accepting
Construct states areMq100
finite automaton , q101
with L(M), q110
=, Lq111
(proof by construction).
46
Finite Automata and Regular Languages c Cleghorn, Marshall, Timm