Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Automata

The document provides an introduction to Automata Theory, focusing on finite automata and formal languages. It explains key concepts such as alphabets, strings, languages, and the definitions and applications of Deterministic Finite Automata (DFA) and Non-deterministic Finite Automata (NFA). Additionally, it outlines the differences between DFAs and NFAs, emphasizing their equivalence in language recognition power.

Uploaded by

Suhas L
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Automata

The document provides an introduction to Automata Theory, focusing on finite automata and formal languages. It explains key concepts such as alphabets, strings, languages, and the definitions and applications of Deterministic Finite Automata (DFA) and Non-deterministic Finite Automata (NFA). Additionally, it outlines the differences between DFAs and NFAs, emphasizing their equivalence in language recognition power.

Uploaded by

Suhas L
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

Go, change the world

Finite Automata and Formal


Languages(18CS52)

DEPARTMENT OF CSE
R V College of Engineering
Go, change the world

Introduction to Automata Theory


Unit 1

2
Go, change the world

What is Automata Theory?


• Study of abstract computing devices, or “machines”
• Automaton = an abstract computing device
• Note: A “device” need not even be a physical hardware!
• A fundamental question in computer science:
• Find out what different models of machines can do and cannot do
• The theory of computation
• Computability vs. Complexity

3
Go, change the world

Alphabet
An alphabet is a finite, non-empty set of symbols
• We use the symbol ∑ (sigma) to denote an alphabet
• Examples:
• Binary: ∑ = {0,1}
• All lower case letters: ∑ = {a,b,c,..z}
• Alphanumeric: ∑ = {a-z, A-Z, 0-9}
• Digits:∑ = { 0-9}

4
Go, change the world

Strings
A string or word is a finite sequence of symbols chosen from ∑
• Empty string is  (or “epsilon”)

• Length of a string w, denoted by “|w|”, is equal to the number of (non-


) characters in the string
• E.g., x = 010100 |x| = 6
• x = 01  0  1  00  |x| = ?

• xy = concatentation of two strings x and y

5
Go, change the world

Powers of an alphabet
Let ∑ be an alphabet.

• ∑k = the set of all strings of length k

• ∑* = ∑0 U ∑1 U ∑2 U …

• ∑+ = ∑1 U ∑2 U ∑3 U …

6
Go, change the world

Languages
L is a said to be a language over alphabet ∑, only if L  ∑*
➔ this is because ∑* is the set of all strings (of all possible length including 0) over the given
alphabet ∑
Examples:
1. Let L be the language of all strings consisting of n 0’s followed by n 1’s:
L = {, 01, 0011, 000111,…}
2. Let L be the language of all strings of with equal number of 0’s and 1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}

Definition: Ø denotes the Empty language

• Let L = {}; Is L=Ø? NO


7
Go, change the world

Finite Automata-Applications
❖Software for designing and checking the behavior of digital circuits

❖Lexical analyzer of a typical compiler

❖Software for scanning large bodies of text (e.g., web pages) for pattern
finding

❖Software for verifying systems of all types that have a finite number of states
(e.g., stock market transaction, communication/network protocol)

8
Go, change the world

Finite Automata : Examples


action
• On/Off switch
state

• Model to recognize word “then”

Start state Transition Intermediate Final state


state

9
Go, change the world

Finite Automaton (FA)


• A state diagram that comprehensively captures all possible states and
transitions that a machine can take while responding to a stream or
sequence of input symbols

• Recognizer for “Regular Languages”

• Deterministic Finite Automata (DFA)


• The machine can exist in only one state at any given time
• Non-deterministic Finite Automata (NFA)
• The machine can exist in multiple states at the same time
10
Go, change the world

Deterministic Finite Automata - Definition


• A Deterministic Finite Automaton (DFA) consists of:
• Q ==> a finite set of states
• ∑ ==> a finite set of input symbols (alphabet)
• q0 ==> a start state
• F ==> set of accepting states
• δ ==> a transition function, which is a mapping between Q x ∑ ==> Q
• A DFA is defined by the 5-tuple:
• {Q, ∑ , q0,F, δ }

11
Go, change the world

What does a DFA do on reading an input


string?
• Input: a word w in ∑*
• Question: Is w acceptable by the DFA?
• Steps:
• Start at the “start state” q0
• For every input symbol in the sequence w do
• Compute the next state from the current state, given the current input symbol in w and
the transition function
• If after all symbols in w are consumed, the current state is one of the
accepting states (F) then accept w;
• Otherwise, reject w.

12
Go, change the world

Regular Languages
• Let L(A) be a language recognized by a DFA A.
• Then L(A) is called a “Regular Language”.

Regular
Context-
(DFA)
free Context-
(PDA) sensitive Recursively-
(LBA) enumerable
(TM)

• A hierarchy of classes of formal languages


13
Go, change the world

Example #1
• Build a DFA for the following language:
• L = {w | w is a binary string that contains 01 as a substring}
• Steps for building a DFA to recognize L:
• ∑ = {0,1}
• Decide on the states: Q
• Designate start state and final state(s)
• δ: Decide on the transitions:
• “Final” states == same as “accepting states”
• Other states == same as “non-accepting states”

14
Go, change the world
Regular expression: (0+1)*01(0+1)*

DFA for strings containing 01

• What makes this DFA deterministic? • Q = {q0,q1,q2}

1 0 0,1 • ∑ = {0,1}
• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Accepting • Transition table
state symbols
0 1
q0 q1 q0
• What if the language allows

states
q1 q1 q2
empty strings? *q2 q2 q2
15
Go, change the world

Example #2
• Build a DFA for the following language:
L = { w | where leftmost symbol differs from rightmost symbol, ∑ ={a,b}}

16
Go, change the world

Example #3
• Build a DFA for the following language:
L = { Except containing substring aab, ∑ ={a , b} }

17
Go, change the world
Example #4
• Build a DFA for the following language:
L = {W: |W| mod 5 !=0, W €(0,1)}

18
Go, change the world
Example #5
• Build a DFA for the following language:
L={ab3Wb2 : W € {a,b}*}

19
Go, change the world
Example #6
• Build a DFA for the following language:
L = {aWa | ∑ ={a , b} }

20
Go, change the world

Extension of transitions (δ) to Paths (δ)


• δ (q,w) = destination state from state q on input string w

• δ (q,wa) = δ (δ(q,w), a)

21
Go, change the world

Extension of transitions (δ) to Paths (δ)


• δ (q,w) = destination state from state q on input string w

• δ (q,wa) = δ (δ(q,w), a)

22
Go, change the world

Extension of transitions (δ) to Paths (δ)


• using the input sequence w=10101

δ (q0, ) =q0

δ (q0, 1) =δ (δ (q0, ) ,1)= δ (q0,1)=q0

δ (q0, 10) =δ (δ (q0, 1) ,0)= δ (q0,0)=q1


δ (q0, 101) =δ (δ (q0, 10) ,1)= δ (q1,1)=q2

δ (q0, 1010) =δ (δ (q0, 101) ,0)= δ (q2,1)=q2


δ (q0, 10101) =δ (δ (q0, 1010) ,1)= δ (q2,1)=q2
23
Go, change the world

Extension of transitions (δ) to Paths (δ)


• using the input sequence w=1001

24
Go, change the world

Extension of transitions (δ) to Paths (δ)


• using the input sequence w=1001

δ (q0, ) =q0

δ (q0, 1) =δ (δ (q0, ) ,1)= δ (q0,1)=q0

δ (q0, 10) =δ (δ (q0, 1) ,0)= δ (q0,0)=q1


δ (q0, 100) =δ (δ (q0, 10) ,0)= δ (q1,0)=q1

δ (q0, 1001) =δ (δ (q0, 100) ,1)= δ (q1,1)=q2

25
Go, change the world

Language of a DFA -Definition


A DFA A accepts string w if there is a path from q0 to an accepting (or
final) state that is labeled by w

• i.e., L(A) = { w | δ(q0,w)  F }

• I.e., L(A) = all strings that lead to an accepting state from q 0

26
Go, change the world

Non-deterministic Finite Automata (NFA)


• A Non-deterministic Finite Automaton (NFA)
• is of course “non-deterministic”
• Implying that the machine can exist in more than one state at the same time
• Transitions could be non-deterministic

1 qj
qi … • Each transition function therefore
1 maps to a set of states
qk

27
Go, change the world

Non-deterministic Finite Automata (NFA)


• A Non-deterministic Finite Automaton (NFA) consists of:
• Q ==> a finite set of states
• ∑ ==> a finite set of input symbols (alphabet)
• q0 ==> a start state
• F ==> set of accepting states
• δ ==> a transition function, which is a mapping between Q x ∑ ==> subset of Q ( 2Q )

• An NFA is also defined by the 5-tuple:


• {Q, ∑ , q0,F, δ }

28
Go, change the world

How to use an NFA?


• Input: a word w in ∑*
• Question: Is w acceptable by the NFA?
• Steps:
• Start at the “start state” q0
• For every input symbol in the sequence w do
• Determine all possible next states from all current states, given the current input symbol in w and
the transition function
• If after all symbols in w are consumed and if at least one of the current states is a final state
then accept w;
• Otherwise, reject w.

29
Regular expression: (0+1)*01(0+1)* Go, change the world

NFA for strings containing 01

Why is this non-deterministic? • Q = {q0,q1,q2}

0,1 0,1 •  = {0,1}


• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Final • Transition table
state symbols
0 1
What will happen if at state q1 q0 {q0,q1} {q0}

states
an input of 0 is received? q1 Φ {q2}
*q2 {q2} {q2}
30
Go, change the world

Example #2
• Build an NFA for the following language:
L = { w | w ends in 01}

31
Go, change the world

Example #3
• Build an NFA for the following language:
L = {aWa | ∑ ={a , b} }

32
Go, change the world

Example #4
• Build an NFA for the following language:
L={ab3Wb2 : W € {a,b}*}

33
Go, change the world

Example #5
• Build an NFA for the following language:
L = { w | where leftmost symbol differs from rightmost symbol, ∑
={a,b}}

34
Go, change the world

Example #6
• Build an NFA for the following language:
L = { w | w is ababn or aban |n>=0}

35
Go, change the world

Extension of δ to NFA Paths


• Basis: δ (q,) = {q}

• Induction:
• Let δ (q0,w) = {p1,p2…,pk}
• δ (pi,a) = Si for i=1,2...,k

• Then, δ (q0,wa) = S1 U S2 U … U Sk

36
Go, change the world

Language of an NFA
• An NFA accepts w if there exists at least one path from the start state
to an accepting (or final) state that is labeled by w

• L(N) = { w | δ(q0,w) ∩ F ≠ Φ }

37
Go, change the world

Advantages & Caveats for NFA


• Great for modeling regular expressions
• String processing - e.g., grep, lexical analyzer

• Could a non-deterministic state machine be implemented in practice?


• Probabilistic models could be viewed as extensions of non-deterministic state machines
(e.g., toss of a coin, a roll of dice)
• They are not the same though
• A parallel computer could exist in multiple “states” at the same time

38
Go, change the world
But, DFAs and NFAs are equivalent in their power to capture langauges !!

Differences: DFA vs. NFA


• DFA • NFA
1. All transitions are deterministic 1. Some transitions could be non-deterministic
• Each transition leads to exactly • A transition could lead to a subset of
one state states
2. For each state, transition on all 2. Not all symbol transitions need to be
possible symbols (alphabet) should defined explicitly (if undefined will go to an
be defined error state – this is just a design
convenience, not to be confused with “non-
3. Accepts input if the last state visited determinism”)
is in F 3. Accepts input if one of the last states is in F
4. Sometimes harder to construct 4. Generally easier than a DFA to construct
because of the number of states
5. Practical implementations limited but
5. Practical implementation is feasible emerging (e.g., Micron automata processor)

39
Go, change the world

Equivalence of DFA & NFA


• Theorem:
• A language L is accepted by a DFA if and only if it is accepted by an NFA.
Should be true

for any L Proof:
1. If part:
• Prove by showing every NFA can be converted to an equivalent DFA (in the next few
slides…)

2. Only-if part is trivial:


• Every DFA is a special case of an NFA where each state has exactly one transition for
every input symbol. Therefore, if L is accepted by a DFA, it is accepted by a
corresponding NFA.

40
Go, change the world

Proof for the if-part


• If-part: A language L is accepted by a DFA if it is accepted by an NFA
• rephrasing…
• Given any NFA N, we can construct a DFA D such that L(N)=L(D)
• How to convert an NFA into a DFA?
• Observation: In an NFA, each transition maps to a subset of states
• Idea: Represent:
each “subset of NFA_states” ➔ a single “DFA_state”
Subset construction

41
Go, change the world

NFA to DFA by subset construction

• Let N = {QN,∑,δN,q0,FN}
• Goal: Build D={QD,∑,δD,{q0},FD} s.t. L(D)=L(N)
• Construction:
1. QD= all subsets of QN (i.e., power set)
2. FD=set of subsets S of QN s.t. S∩FN≠Φ
3. δD: for each subset S of QN and for each input symbol a in ∑:
• δD(S,a) = U δN(p,a)

p in s

42
Idea: To avoid enumerating all of
Go, change the world
power set, do
“lazy creation of states”
NFA to DFA construction: Example

• L = {w | w ends in 01}
1 0
NFA: DFA: 0 1
0,1 [q0] [q0,q1] [q0,q2]
0
0 1 1
q0 q1 q2

δD 0 1 δD 0 1
δN 0 1
Ø Ø Ø [q0] [q0,q1] [q0]
q0 {q0,q1} {q0} [q0] {q0,q1} {q0} [q0,q1] [q0,q1] [q0,q2]
q1 Ø {q2} [q1] Ø {q2} *[q0,q2] [q0,q1] [q0]
*q2 Ø Ø *[q2] Ø Ø
[q0,q1] {q0,q1} {q0,q2}
*[q0,q2] {q0,q1} {q0} 0. Enumerate all possible subsets
*[q1,q2] Ø {q2} 1. Determine transitions
*[q0,q1,q2] {q0,q1} {q0,q2} 2. Retain only those states
reachable from {q0}
43
Go, change the world

NFA to DFA: Repeating the example using LAZY


CREATION
• L = {w | w ends in 01}
1 0
NFA: DFA: 0 1
0,1 [q0] [q0,q1] [q0,q2]
0
0 1 1
q0 q1 q2

δN 0 1 δD 0 1
q0 {q0,q1} {q0} [q0] [q0,q1] [q0]
q1 Ø {q2} [q0,q1] [q0,q1] [q0,q2]
*q2 Ø Ø *[q0,q2] [q0,q1] [q0]

Main Idea:
Introduce states as you go
(on a need basis)
44
Go, change the world

Correctness of subset construction

Theorem: If D is the DFA constructed from NFA N by subset


construction, then L(D)=L(N)
• Proof:
• Show that δD({q0},w) ≡ δN(q0,w} , for all w
• Using induction on w’s length:
• Let w = xa
• δD({q0},xa) ≡ δD( δN(q0,x}, a ) ≡ δN(q0,w}

45
Go, change the world

A bad case where #states(DFA)>>#states(NFA)


• L = {w | w is a binary string s.t., the k th symbol from its end is a 1}

• NFA has k+1 states

• But an equivalent DFA needs to have at least 2k states

(Pigeon hole principle)


• m holes and >m pigeons
• => at least one hole has to contain two or more pigeons

46
Go, change the world

Applications
• Text indexing
• inverted indexing
• For each unique word in the database, store all locations that contain it using
an NFA or a DFA
• Find pattern P in text T
• Example: Google querying
• Extensions of this idea:
• PATRICIA tree, suffix tree

47
Go, change the world

A few subtle properties of DFAs and NFAs


• The machine never really terminates.
• It is always waiting for the next input symbol or making transitions.
• The machine decides when to consume the next symbol from the input and when
to ignore it.
• (but the machine can never skip a symbol)
• => A transition can happen even without really consuming an input symbol (think
of consuming  as a free token) – if this happens, then it becomes an -NFA (see
next few slides).
• A single transition cannot consume more than one (non-) symbol.

48
Go, change the world

FA with -Transitions
• We can allow explicit -transitions in finite automata
• i.e., a transition from one state to another state without consuming any
additional input symbol
• Explicit -transitions between different states introduce non-determinism.
• Makes it easier sometimes to construct NFAs
Definition:  -NFAs are those NFAs with at least one explicit -
transition defined.
•  -NFAs have one more column in their transition table

49
Go, change the world

Example of an -NFA
L = {w | w is empty, or if non-empty will end in 01}

0,1

0 1
• -closure of a state q, ECLOSE(q), is
q0 q1 q2 the set of all states (including itself)
 that can be reached from q by
start q’0 repeatedly making an arbitrary
number of -transitions.
δE 0 1

ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0} ECLOSE(q0)
q1 Ø {q2} {q1} ECLOSE(q1)
*q2 Ø Ø {q2} ECLOSE(q2) 50
Go, change the world

Example of an -NFA
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
L = {w | w is empty, or if non-empty will end in 01}

0,1
Simulate for w=101:
0 1
q0 q1 q2
 q 0’
 
start q’0
q 0’ q0
1 1
δE
q0

0 1 Ø
x 0
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0} q1
q0 {q0,q1} {q0} {q0} ECLOSE(q0) 1
q1 Ø {q2} {q1} q2
*q2 Ø Ø {q2} 51
Go, change the world

Example of another -NFA


To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
0,1
Simulate for w=101:
0 1
q0 q1
1
q2 ?
 
start q’0 q3

δE 0 1

*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}

52
Go, change the world

Equivalency of DFA, NFA, -NFA


• Theorem: A language L is accepted by some -NFA if and only if L is
accepted by some DFA

• Implication:
• DFA ≡ NFA ≡ -NFA
• (all accept Regular Languages)

53
Go, change the world

Eliminating -transitions
Let E = {QE,∑,δE,q0,FE} be an -NFA
Goal: To build DFA D={QD,∑,δD,{qD},FD} s.t. L(D)=L(E)
Construction:
1. QD= all reachable subsets of QE factoring in -closures
2. qD = ECLOSE(q0)
3. FD=subsets S in QD s.t. S∩FE≠Φ
4. δD: for each subset S of QE and for each input symbol a∑:
• Let R= U δE(p,a) // go to destination states
p in s

• δD(S,a) = U ECLOSE(r) // from there, take a union


r in R of all their -closures

54
Go, change the world

Example: -NFA ➔ DFA


L = {w | w is empty, or if non-empty will end in 01}

0,1

0 1
q0 q1 q2

start q’0

δE δD
0 1
 0 1

*q’0 Ø Ø {q’0,q0} *{q’0,q0}


q0 {q0,q1} {q0} {q0} …
q1 Ø {q2} {q1}
*q2 Ø Ø {q2} 55
Go, change the world

Example: -NFA ➔ DFA


L = {w | w is empty, or if non-empty will end in 01}

0,1 0
0
0 1 {q0,q1}
q0 q1 q2 0 1 {q0,q2}
 0
1
start start {q’0, q0} 1
q’0 q0 1
ECLOSE union
δE δD
0 1
 0 1

*q’0 Ø Ø {q’0,q0} *{q’0,q0} {q0,q1} {q0}


q0 {q0,q1} {q0} {q0} {q0,q1} {q0,q1} {q0,q2}
q1 Ø {q2} {q1} {q0} {q0,q1} {q0}
*q2 Ø Ø {q2} *{q0,q2} {q0,q1} {q0} 56
Go, change the world

Equivalence & Minimization


of DFAs

57
Go, change the world

Applications of interest
• Comparing two DFAs:
• L(DFA1) == L(DFA2)?

• How to minimize a DFA?


1. Remove unreachable states
2. Identify & condense equivalent states into one

58
Go, change the world

When to call two states in a DFA “equivalent”?

Two states p and q are said to be equivalent iff:


i) Any string w accepted by starting at p is also accepted by starting at q;
p
w
q
AND
i) Any string w rejected by starting at p is also rejected by starting at q.

p
w
q

➔ p≡q
59
Go, change the world

Computing equivalent states in a DFA


Table Filling Algorithm

A =
0 1
B = =
0 1 0
A C E G C x x =
1 0 1
0 1 D x x x =
1 1 0 E x x x x =
B D F H
1 0 F x x x x x =
0
Pass #0 G x x x = x x =
1. Mark accepting states ≠ non-accepting states
Pass #1
H x x = x x x x =
1. Compare every pair of states A B C D E F G H
2. Distinguish by one symbol transition
3. Mark = or ≠ or blank(tbd)
Pass #2
1. Compare every pair of states
2. Distinguish by up to two symbol transitions (until different or same or tbd)
…. 60
(keep repeating until table complete)
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E =
B D F H
1 0 F =
0
G =
H =
A B C D E F G H

61
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X =
1. Mark X between accepting vs. non-accepting state H X =
A B C D E F G H

62
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X =
1 0 1
0 1 D X =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X X =
1. Mark X between accepting vs. non-accepting state H X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

63
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X X X =
1. Mark X between accepting vs. non-accepting state H X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

64
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X =
0
G X X X X =
1. Mark X between accepting vs. non-accepting state H X X = X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

65
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X =
1. Mark X between accepting vs. non-accepting state H X X = X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

66
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

67
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

68
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X X =
2. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
3. Pass 2:
Look 1-hop away again for distinguishing states or strings
continue….

69
Go, change the world

Table Filling Algorithm - step by step

A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X X =
2. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
3. Pass 2:
Look 1-hop away again for distinguishing states or strings Equivalences:
continue…. • A=B
• C=H
• D=G 70
Go, change the world

Table Filling Algorithm - step by step

0 1 0 1

0 1 0 0 1
A C E G A C E
1 0 1 1 0
0 1 0
1 1 0 1
B D F H D F
1 0 1 0
0

Retrain only one copy for


each equivalence set of states

Equivalences:
• A=B
• C=H
• D=G 71
Go, change the world

Table Filling Algorithm – special case

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E ? =
B D F H
1 0 F =
0
G =
H =

Q) What happens if the input DFA A B C D E F G H


has more than one final state?
Can all final states initially be treated
as equivalent to one another?
72
Go, change the world
Putting it all together …
How to minimize a DFA?
• Goal: Minimize the number of states in a DFA
• Algorithm: Depth-first traversal from the start state

1. Eliminate states unreachable from the start state


Table filling algorithm
2. Identify and remove equivalent states

3. Output the resultant DFA

73
Go, change the world

Are Two DFAs Equivalent?

Unified DFA DFA1

q0 …
Is q0 ≡ q0’?
: if yes, then DFA1≡DFA2
DFA2 : else, not equiv.

q 0’ …

1. Make a new dummy DFA by just putting together both DFAs


2. Run table-filling algorithm on the unified DFA
3. IF the start states of both DFAs are found to be equivalent,
THEN: DFA1≡ DFA2
ELSE: different
74
Go, change the world

Regular Expressions

75
Go, change the world

Regular Expressions vs. Finite Automata

• Offers a declarative way to express the pattern of any string we want to accept
• E.g., 01*+ 10*

• Automata => more machine-like


< input: string , output: [accept/reject] >
• Regular expressions => more program syntax-like

• Unix environments heavily use regular expressions


• E.g., bash shell, grep, vi & other editors, sed
• Perl scripting – good for string processing
• Lexical analyzers such as Lex or Flex
76
Go, change the world

Regular Expressions

Regular = Finite Automata


expressions (DFA, NFA, -NFA)
Syntactical
expressions Automata/machines

Regular
Languages

Formal language
classes

77
Go, change the world

Language Operators
• Union of two languages:
• L U M = all strings that are either in L or M
• Note: A union of two languages produces a third language

• Concatenation of two languages:


• L . M = all strings that are of the form xy
s.t., x  L and y  M
• The dot operator is usually omitted
• i.e., LM is same as L.M

78
Go, change the world
“i” here refers to how many strings to concatenate from the parent language L to produce
strings in the language Li

Kleene Closure (the * operator)


• Kleene Closure of a given language L:
• L0= {}
• L1= {w | for some w  L}
• L2= { w1w2 | w1  L, w2  L (duplicates allowed)}
• Li= { w1w2…wi | all w’s chosen are  L (duplicates allowed)}
• (Note: the choice of each w i is independent)
• L* = Ui≥0 Li (arbitrary number of concatenations)
Example:
• Let L = { 1, 00}
• L0= {}
• L1= {1,00}
• L2= {11,100,001,0000}
• L3= {111,1100,1001,10000,000000,00001,00100,0011}
• L* = L0 U L1 U L2 U …
79
Go, change the world

Kleene Closure (special notes)

• L* is an infinite set iff |L|≥1 and L≠{} Why?

• If L={}, then L* = {} Why?

• If L = Φ, then L* = {} Why?

Σ* denotes the set of all words over an alphabet Σ


• Therefore, an abbreviated way of saying there is an arbitrary
language L over an alphabet Σ is:
• L  Σ*

80
Go, change the world

Building Regular Expressions


• Let E be a regular expression and the language represented by E is
L(E)
• Then:
• (E) = E
• L(E + F) = L(E) U L(F)
• L(E F) = L(E) L(F)
• L(E*) = (L(E))*

81
Go, change the world

Example 1: how to use these regular expression properties and


language operators?
• L = { w | w is a binary string which does not contain two consecutive 0s or two consecutive 1s anywhere)
• E.g., w = 01010101 is in L, while w = 10010 is not in L
• Goal: Build a regular expression for L
• Four cases for w:
• Case A: w starts with 0 and |w| is even
• Case B: w starts with 1 and |w| is even
• Case C: w starts with 0 and |w| is odd
• Case D: w starts with 1 and |w| is odd
• Regular expression for the four cases:
• Case A: (01)*
• Case B: (10)*
• Case C: 0(10)*
• Case D: 1(01)*
• Since L is the union of all 4 cases:
• Reg Exp for L = (01)* + (10)* + 0(10)* + 1(01)*
• If we introduce  then the regular expression can be simplified to:
• Reg Exp for L = ( +1)(01)*( +0)
82
Go, change the world

Some more examples:

• L2 = { w | w is a binary string which ends with 1 does not contain 00}.


• E.g., w = 01010101 is in L, while w = 10010 is not in L
• Goal: Build a regular expression for L2
• Reg Exp for L = (1+01)*(1+01)

• L3 = { w | w is a binary string which ends with 01}.


• Goal: Build a regular expression for L3
• Reg Exp for L = (1+0)*01

83
Go, change the world

Precedence of Operators
• Highest to lowest
• * operator (star)
• . (concatenation)
• + operator

• Example:
• 01* + 1 = ( 0 . ((1)*) ) + 1

84
Go, change the world

Finite Automata (FA) & Regular Expressions (Reg


Ex)
◼To show that they are interchangeable, consider the following
theorems:
◼Theorem 1: For every DFA A there exists a regular expression R such that
Proofs L(R)=L(A)
in the book ◼Theorem 2: For every regular expression R there exists an  -NFA E such that
L(E)=L(R)

 -NFA NFA
Theorem 1
Kleene Theorem

Reg Ex DFA
Theorem 2
85
Go, change the world

DFA Reg Ex
DFA to RE construction Theorem 2

Informally, trace all distinct paths (traversing cycles only once)


from the start state to each of the final states
and enumerate all the expressions along the way

Example: 1 0 0,1

q0 0 q1 1 q2

(1*) 0 (0*) 1 (0 + 1)*

1* 00* 1 (0+1)*
Q) What is the language?

1*00*1(0+1)* 86
Go, change the world
State Elimination
• Consider the figure below, which shows a generic state s about to be eliminated. The labels on all edges are
regular expressions.
• To remove s, we must make labels from each qi to p1 up to pm that include the paths we could have made through
s.
R1m

R11+Q1S*P1
q1 p1 q1 p1
R11

Q1 P1 R1m+Q1S*Pm
S
. . . .
. s . . .
. . . .
QK Pm Rk1+QkS*P1

Rkm
qk pm qk pm
Rkm+QkS*Pm

Rk1
Note: q and p may be the same state!
Go, change the world

DFA to RE via State Elimination (1)


1. Starting with intermediate states and then moving to accepting
states, apply the state elimination process to produce an
equivalent automaton with regular expression labels on the edges.
• The result will be a one or two state automaton with a start state and
accepting state.
Go, change the world

DFA to RE State Elimination (2)


2. If the two states are different, we will have an
automaton that looks like the following:

R U

S
Start

We can describe this automaton as: (R+SU*T)*SU*


Go, change the world

DFA to RE State Elimination (3)


3. If the start state is also an accepting state, then we must also
perform a state elimination from the original automaton that gets
rid of every state but the start state. This leaves the following:

Start

We can describe this automaton as simply R*.


Go, change the world

DFA to RE State Elimination (4)


4. If there are n accepting states, we must repeat the above steps for
each accepting states to get n different regular expressions, R 1, R2,
… Rn. For each repeat we turn any other accepting state to non-
accepting. The desired regular expression for the automaton is
then the union of each of the n regular expressions: R 1 R2…  RN
Go, change the world

DFA→RE Example
• Convert the following to a RE
0 0,1

1 1
Start 3 1 2
• First convert the edges to RE’s:
0

0 0+1

1 1
Start 3 1 2

0
Go, change the world

DFA → RE Example (2)


0 0+1
• Eliminate State 1:

1 1
Start 3 1 2

• To: 0

0+10 0+1
Note edge from 3→3

11
Start 3 2

Answer: (0+10)*11(0+1)*
Go, change the world
Second Example
• Automata that accepts even number of 1’s
0 0 0

1 1
Start 1 2 3
• Eliminate state 2:
1

0 0+10*1

10*1
Start 1 3
Go, change the world

Second Example (2)


0 0+10*1

10*1
Start 1 3

• Two accepting states, turn off state 3 first


0 0+10*1

10*1
Start 1 3

This is just 0*; can ignore going to state 3 since we would “die”
Go, change the world

Second Example (3)


0 0+10*1

10*1
Start 1 3

• Turn off state 1 second:

0 0+10*1

10*1 This is just 0*10*1(0+10*1)*


Start 1 3

Combine from previous slide to get 0* +


0*10*1(0+10*1)*
Go, change the world
 -NFA
RE to -NFA construction
Reg Ex
Theorem 1

◼ Suppose -NFA1 and -NFA2 are the automata for R1 and R2


◼ Three operations to worry about: union
R1 + R2, concatenation R1R2), closure R1*
◼ With -transitions, construction is straightforward
◼ Union: create a new start state, with -transitions into the start states of -NFA1 and -
NFA2; create a new final state, with -transitions from the two final states of -NFA1 and -
NFA2
◼ Concatenation: -transition from final state of -NFA1 to the start state of -NFA2
◼ Closure: closure can be supported by an -transition from final to start state; need a few
more -transitions (why?)

97
Go, change the world

ε S ε
R=S+T

ε ε
T

ε
R=ST
S T

ε ε
R=S*
S

ε
Go, change the world

RE to ε-NFA Example
• Convert R= (ab+a)* to an NFA
• We proceed in stages, starting from simple elements and working our way up

a
a

b
b

a ε b
ab
Go, change the world

RE to ε-NFA Example (2)


ab+a
a ε b
ε ε

a
ε ε

(ab+a)*
a ε b
ε ε
ε ε

a
ε ε

ε
Go, change the world
 -NFA
RE to -NFA construction
Reg Ex
Theorem 1

Example: (0+1)*01(0+1)*

(0+1)* 01 (0+1)*

 

0
 
0

 0 1

 1
  1

 

101
Go, change the world

Algebraic Laws of Regular Expressions


• Commutative:
• E+F = F+E
• Associative:
• (E+F)+G = E+(F+G)
• (EF)G = E(FG)
• Identity:
• E+Φ = E
• E=E=E
• Annihilator:
• ΦE = EΦ = Φ
102
Go, change the world

Algebraic Laws…
• Distributive:
• E(F+G) = EF + EG
• (F+G)E = FE+GE
• Idempotent: E + E = E
• Involving Kleene closures:
• (E*)* = E*
• Φ* =
• * =
• E+ =EE*
• E? =  +E

103
Go, change the world

True or False?
Let R and S be two regular expressions. Then:

1. ((R*)*)* = R* ?

2. (R+S)* = R* + S* ?

3. (RS + R)* RS = (RR*S)* ?

104
Go, change the world

Summary
• DFA –Definition, Transition diagrams & tables
• Regular language
• NFA- Definition, Transition diagrams & tables
• DFA vs. NFA
• NFA to DFA conversion using subset construction
• Equivalency of DFA & NFA
• Removal of redundant states and including dead states
• -transitions in NFA
• Text searching applications
• Simplification of DFAs
• How to remove unreachable states?
• How to identify and collapse equivalent states?
• How to minimize a DFA?
• How to tell whether two DFAs are equivalent? 105
Go, change the world

Summary
• Regular expressions
• Equivalence to finite automata
• DFA to regular expression conversion
• Regular expression to -NFA conversion
• Algebraic laws of regular expressions
• Unix regular expressions and Lexical Analyzer

106

You might also like