Fundmentls of Computer Science Chpter 3: NFA nd DFA equivlence Regulr expressions Henrik Björklund Umeå University Jnury 23, 2014
NFA nd DFA equivlence As we shll see, it turns out tht NFA nd DFA re equivlent, i.e., they hve the sme expressive power. This cn lso e sid s follows: For every NFA A there is DFA B such tht L(A) = L(B). In yet other words, the clss of NFAs nd the clss of DFAs define the sme fmily of lnguges.
NFA nd DFA equivlence As we shll see, it turns out tht NFA nd DFA re equivlent, i.e., they hve the sme expressive power. This cn lso e sid s follows: For every NFA A there is DFA B such tht L(A) = L(B). In yet other words, the clss of NFAs nd the clss of DFAs define the sme fmily of lnguges. This lso gives us n lterntive defintion of the regulr lnguges: A forml lnguge L is regulr if nd only if there is n NFA A such tht L = L(A).
DFA NFA This direction is trivil: Given DFA A = (Q, Σ, δ, q 0, F), define n NFA A = (Q, Σ, δ, q 0, F), where, for ll q Q nd Σ, we define δ (q, ) = {δ(q, )}. This mens tht if δ(q, ) = p, then δ (q, ) = {p}.
DFA NFA This direction is trivil: Given DFA A = (Q, Σ, δ, q 0, F), define n NFA A = (Q, Σ, δ, q 0, F), where, for ll q Q nd Σ, we define δ (q, ) = {δ(q, )}. This mens tht if δ(q, ) = p, then δ (q, ) = {p}. Slogn: Every DFA is n NFA.
NFA DFA It is less ovious tht for every NFA there is lnguge-equivlent DFA.
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 0
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 0
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 0
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0 p 02
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Exmple,,, q 0 q 1 q 2 q 3 p 01 p 012 p 0123 p 0 p 02 p 013 p 03 p 023
NFA DFA: Generl procedure Given: n NFA A = (Q, Σ, δ, q 0, F). 1. Crete n initil stte q 0. 2. Repet the follwoing until no trnsitions re missing: Tke ny stte {q i, q j,..., q k } tht does not hve trnsition for some Σ. Compute p = δ (q i, ) δ (q j, ) δ (q k, ). If there is no stte with lel p yet, crete such stte. Add trnsition from the stte with lel {qi, q j,..., q k } to the stte with lel p for the symol. 3. Mrk every stte whose lel contins ny q F s ccepting. 4. If the NFA ccepts ε, mrk the stte {q 0 } s ccepting.
Regulr expressions Perhps the most widely known wy of defining regulr lnguges re through the regulr expressions.
Regulr expressions Perhps the most widely known wy of defining regulr lnguges re through the regulr expressions. While finite utomt emphsizes the computtionl spects of regulr lnguges, regulr expressions re more purely descriptive.
Regulr expressions Perhps the most widely known wy of defining regulr lnguges re through the regulr expressions. While finite utomt emphsizes the computtionl spects of regulr lnguges, regulr expressions re more purely descriptive. Regulr expressions descrie lnguges y repeted use of union, conctention, nd Kleene closure.
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ.
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ. 2. If Σ, then is regulr expression over Σ.
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ. 2. If Σ, then is regulr expression over Σ. 3. If r 1 nd r 2 re regulr expressions over Σ, then so re r1 + r 2,
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ. 2. If Σ, then is regulr expression over Σ. 3. If r 1 nd r 2 re regulr expressions over Σ, then so re r1 + r 2, r1 r 2,
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ. 2. If Σ, then is regulr expression over Σ. 3. If r 1 nd r 2 re regulr expressions over Σ, then so re r1 + r 2, r1 r 2, (r1 ), nd
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ. 2. If Σ, then is regulr expression over Σ. 3. If r 1 nd r 2 re regulr expressions over Σ, then so re r1 + r 2, r1 r 2, (r1 ), nd r1.
RE definition Let Σ e n lphet. 1. ε nd re regulr expressions over Σ. 2. If Σ, then is regulr expression over Σ. 3. If r 1 nd r 2 re regulr expressions over Σ, then so re r1 + r 2, r1 r 2, (r1 ), nd r1. Exmple: ( + c) ( + ε) is regulr expression over {,, c}.
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) =
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) = If r = ε, then L(r) = {ε}
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) = If r = ε, then L(r) = {ε} If r =, then L(r) = {}
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) = If r = ε, then L(r) = {ε} If r =, then L(r) = {} If r = r 1 + r 2, then L(r) = L(r 1 ) L(r 2 )
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) = If r = ε, then L(r) = {ε} If r =, then L(r) = {} If r = r 1 + r 2, then L(r) = L(r 1 ) L(r 2 ) If r = r 1 r 2, then L(r) = L(r 1 ) L(r 2 )
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) = If r = ε, then L(r) = {ε} If r =, then L(r) = {} If r = r 1 + r 2, then L(r) = L(r 1 ) L(r 2 ) If r = r 1 r 2, then L(r) = L(r 1 ) L(r 2 ) If r = (r 1 ), then L(r) = L(r 1 )
RE lnguges If r is regulr expression, we write L(r) for the lnguge ssocited with r. We define L(r) y the following rules: If r =, then L(r) = If r = ε, then L(r) = {ε} If r =, then L(r) = {} If r = r 1 + r 2, then L(r) = L(r 1 ) L(r 2 ) If r = r 1 r 2, then L(r) = L(r 1 ) L(r 2 ) If r = (r 1 ), then L(r) = L(r 1 ) If r = (r 1 ), then L(r) = L(r 1 )
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε))
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε)
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε) = L( + c) (L( ) L(ε))
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε) = L( + c) (L( ) L(ε)) = ({} {c}) (L() L( ) {ε})
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε) = L( + c) (L( ) L(ε)) = ({} {c}) (L() L( ) {ε}) = {, c} (L() L() {ε})
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε) = L( + c) (L( ) L(ε)) = ({} {c}) (L() L( ) {ε}) = {, c} (L() L() {ε}) = {, c} ({} {ε,,,... } {ε})
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε) = L( + c) (L( ) L(ε)) = ({} {c}) (L() L( ) {ε}) = {, c} (L() L() {ε}) = {, c} ({} {ε,,,... } {ε}) = {, c} {ε,,,... }
Exmple Rememer the regulr expression ( + c) ( + ε). Wht lnguge does it define? L(( + c) ( + ε)) = L(( + c) ) L(( + ε)) = L(( + c)) L( + ε) = L( + c) (L( ) L(ε)) = ({} {c}) (L() L( ) {ε}) = {, c} (L() L() {ε}) = {, c} ({} {ε,,,... } {ε}) = {, c} {ε,,,... } = {ε,, c,, c, c,... } {ε,,,... }
Precedence Kleene str inds hrder thn conctention nd union Conctention inds hrder thn union
Exmple Construct regulr expression over Σ = {,, c} tht defines the lnguge of ll words over Σ tht do not hve two consecutive :s.
Exmple Construct regulr expression over Σ = {,, c} tht defines the lnguge of ll words over Σ tht do not hve two consecutive :s. ( + ε)( )
REs nd regulr lnguges Regulr expressions re expressively equivlent to DFA nd NFA. In other words, every lnguge tht cn e defined y finite utomton cn e defined y regulr expression nd vice vers. This mens tht the regulr expressions, just like the DFA nd the NFA, define exctly the regulr lnguges. We will prove one direction of this clim y showing tht for every regulr expression r, there is n NFA A such tht L(r) = L(A).
q 0 An NFA for L( )
q 0 An NFA for L(ε)
q 0 q 1 An NFA for L()
An NFA for L(r 1 + r 2 ) A(r 1 ) ε ε q 0 q 1 ε A(r 2 ) ε
An NFA for L(r 1 r 2 ) A(r 1 ) A(r 2 ) ε ε ε q 0 q 1
An NFA for L(r 1 ) A(r 1 ) ε ε q 0 q 1