MELJUN P. CORTES,
MBA,MPA,BSCS,ACS

CSC 3130: Automata theory and formal languages

DFA minimization algorithm

MELJUN COR...
Example
• Construct a DFA over alphabet {0, 1} that accepts
those strings that end in 111
0

0

q0

1

qε
1

0
q1

1

q00
...
Smaller DFA
• Yes, we can do it with 4 states:
0
q0

1
0

1

q1

q2

1

q3

1

0
0

– The state remembers the number of co...
Even smaller DFA?
• Suppose we had a 3 state DFA M for L
• We do not know what this M looks like
… but let’s imagine what ...
Pigeonhole principle
Suppose you are tossing m balls into n bins, and
m > n. Then two balls end up in the same bin.
• Here...
A smaller DFA?
inputs:

M

ε, 1, 11, 111

• What goes wrong if…
– M ends up in same state on input 1 and input 111?
– M en...
A smaller DFA
• Suppose M ends up in the same state after reading
inputs x = 1i and y = 1j, where 0 ≤ i < j ≤ 3
inputs:

M...
No smaller DFA!
• Conclusion
There is no DFA with 3 states for L
• So, this DFA is minimal
0
q0

1
0

1

q1

q2

1

0
0

–...
DFA minimization
• There is an algorithm to start with any DFA and
reduce it to the smallest possible DFA
• The algorithm ...
Examples of equivalent states
0
q0

1
1
0, 1

q0, q1 equivalent

0
q0

0, 1

q1

0

q2

q3

0, 1

q2

1
q1

0, 1

q3

0, 1...
Equivalent and distinguishable states
• Two states q, q’ are equivalent if
For every string w, the states ^ w) and ^ w)
δ(...
Examples of distinguishable states
0
q0

q3

q3 distinguishable from q0, q1, q2
q3 is accepting, others are rejecting

(q0...
DFA minimization algorithm
• Find all pairs of distinguishable states as follows:
 For any pair of states q, q’:
If q is ...
Example of DFA minimization
0
0
0

q0

1

0
q1

q00

1

qε
1

q0

q01
1

q10

q1

1
0
0
0

q11

1

q00
q01
q10
q11

1

qε ...
Example of DFA minimization
0
0
0

q0

1

0
q1

q00

1

qε
1

q0

q01
1

q10

q1

1
0
0
0

q11

1

q00
q01
q10
q11

1

x x...
Example of DFA minimization
0
0
0

q0

1

0
q1

q00

1

qε
1

q0

q01
1

q10

q1

1
0
0
0

q11

1

q00

x

q01
q10
q11

1
...
Example of DFA minimization
0
0
0

q0

1

0
q1

q00

1

qε
1

q0

q01
1

q10

q1

1
0
0
0

q11

1

x

q00
q01

x x

q10

x...
Example of DFA minimization
0
0

A

0

q0

1

B

0
q1

q00

1

qε

q0

q01
1

q10

0
0
0

1

C

q1

1

q11

1

q00
q01
q10...
Example of DFA minimization
0
0

A

0

q0

1

B

0
q1

q00

1

qε

q0

q01
1

q10

q1

1
0

q01

1

0

1

q00

0

q10

q11...
Why does DFA minimization work?
• We need to convince ourselves of three properties:
M

M’

•
 Consistency
– The new DFA ...
Main claim
Any two states q and q’ in M are distinguishable
if and only if the algorithm marks them as
distinguishable
• P...
Proof of part 1
• First, suppose q, q’ are marked as distinguishable
• Could it be that they are equivalent?
– No, because...
Proof idea for part 2
• Now suppose q, q’ are not marked as distinguishable
• Could it be that they are distinguishable? S...
Proof of part 2
q

q’

w1

w1

…

wk-1

wk

w1

w1

…

wk-1

wk

 For any pair of states q, q’:
If q is accepting and q’ ...
Proof of part 2
q

x
q’

w1
w1

w2
x

w2

…
…

wk-1
wk-1

wk
x

wk

x

 Repeat until nothing is marked:
For any pair of s...
Why does DFA minimization work?
• We need to convince ourselves of three properties:
M

M’

•
 Consistency
– The new DFA ...
Consistency of transitions
0
0

A

0

q0

1

qε
1

B

0
q1

q00
q01
1

q10

1
0
0
0

1

1

q11

C

1

• Why are the transi...
Proof of consistency
B
q5

a
A
q1

q2

q3
q7

C

a
q6

• Suppose there were two inconsistent transitions in
M’ labeled a o...
Proof of consistency
B
q5

a
A
q1

q2

q3
q7

a
q6

• States q5 and q6 must be distinguishable because they
were not merge...
Proof of consistency
B
a
A
q1

q2

q3
q7

w

q5

C

a
q6

w

• Then, q3 and q7 must also be distinguishable, and this is
i...
Consistency of states
0
0

A

0

q0

1

qε
1

B

0
q1

q00
q01
1

q10

1
0
0
0

1

1

q11

C

1

• Why are the merged stat...
Consistency of states
0
0

A

0

q0

1

qε
1

B

0
q1

q00
q01
1

q10

1
0
0
0

1

1

q11

C

1

• Because merged states a...
Correctness
0
0

A

0

q0

1

qε
1

B

0
q1

q00
q01
1

q10

1
0
0
0

1

1

q11

C

1

• Each state of M’ corresponds to a...
Proof of correctness
• Claim: After reading input w,
M’ is in state qA if and only if M is in one of the
states represente...
Proof of minimality
All pairs of states in M’ are distinguishable
• Now suppose there is some smaller M’’ for L
• By the p...
Proof of minimality
All pairs of states in M’ are distinguishable
• Since q and q’ are distinguishable, for some w…
• But ...
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Automata Theory (Automata6)

155 views
133 views

Published on

MELJUN CORTES Automata Theory (Automata6)

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
155
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Automata Theory (Automata6)

  1. 1. MELJUN P. CORTES, MBA,MPA,BSCS,ACS CSC 3130: Automata theory and formal languages DFA minimization algorithm MELJUN CORTES
  2. 2. Example • Construct a DFA over alphabet {0, 1} that accepts those strings that end in 111 0 0 q0 1 qε 1 0 q1 1 q00 1 q01 … q10 q11 1 … 1 1 q001 … 0 q000 q101 … 0 q111 • This is big, isn’t there a smaller DFA for this? 1
  3. 3. Smaller DFA • Yes, we can do it with 4 states: 0 q0 1 0 1 q1 q2 1 q3 1 0 0 – The state remembers the number of consecutive 1s at the end of the string (up to 3) • Can we do it with 3 states?
  4. 4. Even smaller DFA? • Suppose we had a 3 state DFA M for L • We do not know what this M looks like … but let’s imagine what happens when: inputs: M ε, 1, 11, 111 • By the pigeonhole principle, on two of these inputs M ends in the same state
  5. 5. Pigeonhole principle Suppose you are tossing m balls into n bins, and m > n. Then two balls end up in the same bin. • Here, balls are inputs, bins are states: If you have a DFA with n states and you run it on m inputs, and m > n, then two inputs end up in same state.
  6. 6. A smaller DFA? inputs: M ε, 1, 11, 111 • What goes wrong if… – M ends up in same state on input 1 and input 111? – M ends up in same state on input ε and input 11?
  7. 7. A smaller DFA • Suppose M ends up in the same state after reading inputs x = 1i and y = 1j, where 0 ≤ i < j ≤ 3 inputs: M ε, 1, 11, 111 1 i, 1 j • Then after reading the continuation z = 13 – j – The state of yz should be accepting (yz = 111) – The state of xz should be rejecting (xz = ε, 1, or 11) … but they are both in the same state!
  8. 8. No smaller DFA! • Conclusion There is no DFA with 3 states for L • So, this DFA is minimal 0 q0 1 0 1 q1 q2 1 0 0 – In fact, it is the unique minimal DFA for L q3 1
  9. 9. DFA minimization • There is an algorithm to start with any DFA and reduce it to the smallest possible DFA • The algorithm attempts to identify classes of equivalent states • These are states that can be merged together without affecting the answer of the computation
  10. 10. Examples of equivalent states 0 q0 1 1 0, 1 q0, q1 equivalent 0 q0 0, 1 q1 0 q2 q3 0, 1 q2 1 q1 0, 1 q3 0, 1 q0, q1 equivalent q2, q3 also equivalent
  11. 11. Equivalent and distinguishable states • Two states q, q’ are equivalent if For every string w, the states ^ w) and ^ w) δ(q, δ(q’, are either both accepting or both rejecting ^ – Here, δ(q, w) is the state that the machine is in if it starts at q and reads the string w • q, q’ are distinguishable if they are not equivalent: For some string w, one of the states δ(q, w), δ(q’, w) is accepting and the other is rejecting
  12. 12. Examples of distinguishable states 0 q0 q3 q3 distinguishable from q0, q1, q2 q3 is accepting, others are rejecting (q0, q2) and (q1, q2) distinguishable they behave differently on input 0 q0, q1 equivalent 0 q01 1 0, 1 q2 0, 1 1 1 0, 1 0, 1 q1 0 q2 q3
  13. 13. DFA minimization algorithm • Find all pairs of distinguishable states as follows:  For any pair of states q, q’: If q is accepting and q’ is rejecting Mark (q, q’) as distinguishable  Repeat until nothing is marked: For any pair of states (q, q’): For every alphabet symbol a: If (δ(q, a), δ(q’, a)) are marked as distinguishable Mark (q, q’) as distinguishable  For any pair of states (q, q’): If (q, q’) is not marked as distinguishable Merge q and q’ into a single state
  14. 14. Example of DFA minimization 0 0 0 q0 1 0 q1 q00 1 qε 1 q0 q01 1 q10 q1 1 0 0 0 q11 1 q00 q01 q10 q11 1 qε q0 q1 q00 q01 q10
  15. 15. Example of DFA minimization 0 0 0 q0 1 0 q1 q00 1 qε 1 q0 q01 1 q10 q1 1 0 0 0 q11 1 q00 q01 q10 q11 1 x x x x x x qε q0 q1 q00 q01 q10  q11 is distinguishable from all other states
  16. 16. Example of DFA minimization 0 0 0 q0 1 0 q1 q00 1 qε 1 q0 q01 1 q10 q1 1 0 0 0 q11 1 q00 x q01 q10 q11 1 x x x x x x x x x qε q0 q1 q00 q01 q10  q1 is distinguishable from qε, q0, q00, q10 On transition 1, they go to distinguishable states
  17. 17. Example of DFA minimization 0 0 0 q0 1 0 q1 q00 1 qε 1 q0 q01 1 q10 q1 1 0 0 0 q11 1 x q00 q01 x x q10 x x x x x x x x q11 1 x x x qε q0 q1 q00 q01 q10  q01 is distinguishable from qε, q0, q00, q10 On transition 1, they go to distinguishable states
  18. 18. Example of DFA minimization 0 0 A 0 q0 1 B 0 q1 q00 1 qε q0 q01 1 q10 0 0 0 1 C q1 1 q11 1 q00 q01 q10 q11 1 A x A x A x x A x A x x B x x A x x x x x qε q0 q1 q00 q01 q10  Merge states not marked distinguishable qε, q0, q00, q10 are equivalent → group A q1, q01 are equivalent → group B q11 cannot be merged → group C
  19. 19. Example of DFA minimization 0 0 A 0 q0 1 B 0 q1 q00 1 qε q0 q01 1 q10 q1 1 0 q01 1 0 1 q00 0 q10 q11 C q11 A x A x A x x A x A x x B x x A x x x x x qε q0 q1 q00 q01 q10 1 0 minimized DFA: qΑ 1 0 qΒ 0 1 qC 1
  20. 20. Why does DFA minimization work? • We need to convince ourselves of three properties: M M’ •  Consistency – The new DFA M’ is well-defined  Correctness • – The new DFA M’ is equivalent to the original DFA  Minimality • – The new DFA M’ is the smallest DFA possible for L
  21. 21. Main claim Any two states q and q’ in M are distinguishable if and only if the algorithm marks them as distinguishable • Proof outline: – First, we assume q, q’ are marked distinguishable, and show they must be distinguishable – Then, we assume q, q’ are not marked distinguishable, and show they must be equivalent
  22. 22. Proof of part 1 • First, suppose q, q’ are marked as distinguishable • Could it be that they are equivalent? – No, because recall how the algorithm works: q x q’ w1 w1 w2 x w2 … … wk-1 wk-1 wk x wk x
  23. 23. Proof idea for part 2 • Now suppose q, q’ are not marked as distinguishable • Could it be that they are distinguishable? Suppose so ^ ^ • Then for some string w, δ(q, w) accepts, but δ(q’, w) rejects • Working backwards, the algorithm will mark q and q’ as distinguishable at some point
  24. 24. Proof of part 2 q q’ w1 w1 … wk-1 wk w1 w1 … wk-1 wk  For any pair of states q, q’: If q is accepting and q’ is rejecting Mark (q, q’) as distinguishable
  25. 25. Proof of part 2 q x q’ w1 w1 w2 x w2 … … wk-1 wk-1 wk x wk x  Repeat until nothing is marked: For any pair of states (q, q’): For every alphabet symbol a: If (δ(q, a), δ(q’, a)) are marked as distinguishable Mark (q, q’) as distinguishable
  26. 26. Why does DFA minimization work? • We need to convince ourselves of three properties: M M’ •  Consistency – The new DFA M’ is well-defined  Correctness • – The new DFA M’ is equivalent to the original DFA  Minimality • – The new DFA M’ is the smallest DFA possible for L
  27. 27. Consistency of transitions 0 0 A 0 q0 1 qε 1 B 0 q1 q00 q01 1 q10 1 0 0 0 1 1 q11 C 1 • Why are the transitions between the merged states consistent?
  28. 28. Proof of consistency B q5 a A q1 q2 q3 q7 C a q6 • Suppose there were two inconsistent transitions in M’ labeled a out of merged state qA
  29. 29. Proof of consistency B q5 a A q1 q2 q3 q7 a q6 • States q5 and q6 must be distinguishable because they were not merged together
  30. 30. Proof of consistency B a A q1 q2 q3 q7 w q5 C a q6 w • Then, q3 and q7 must also be distinguishable, and this is impossible
  31. 31. Consistency of states 0 0 A 0 q0 1 qε 1 B 0 q1 q00 q01 1 q10 1 0 0 0 1 1 q11 C 1 • Why are the merged states either all accepting or all rejecting?
  32. 32. Consistency of states 0 0 A 0 q0 1 qε 1 B 0 q1 q00 q01 1 q10 1 0 0 0 1 1 q11 C 1 • Because merged states are not distinguishable, so they are mutually equivalent
  33. 33. Correctness 0 0 A 0 q0 1 qε 1 B 0 q1 q00 q01 1 q10 1 0 0 0 1 1 q11 C 1 • Each state of M’ corresponds to a class of mutually equivalent states in M
  34. 34. Proof of correctness • Claim: After reading input w, M’ is in state qA if and only if M is in one of the states represented by A • Proof: True in start state, and stays true after, because transitions are consistent • At the end, M accepts w in state qi if and only if M’ on input w lands in the state qA that represents qi – qA must be accepting by consistency of states
  35. 35. Proof of minimality All pairs of states in M’ are distinguishable • Now suppose there is some smaller M’’ for L • By the pigeonhole principle, there is a state q’’ of M’’ such that v q v’ q’ M’ M’’ q’’ v, v’
  36. 36. Proof of minimality All pairs of states in M’ are distinguishable • Since q and q’ are distinguishable, for some w… • But in M’’, δ(q’’, w) cannot both accept and reject! • So, M’’ cannot be smaller than M’ v v’ q q’ w w M’ M’’ q’’ v, v’ w ?

×