PowerPoint プレゼンテーション

Download Report

Transcript PowerPoint プレゼンテーション

2.2 Nondeterministic Finite Accepters
Definition 1
A nondeterministic finite accepter or nfa is defined by the quintuple
M  (Q,,  , q0 , F )
where Q,  , q 0 , F are definedas for deterministic finiteaccepters,but
 : Q  {  {}}  2Q.
• A string is accepted by an nfa if there is some sequence of
possible moves that will put the machine in a final state at the
end of the string.
• A string is rejected by an nfa only if there is no possible
sequence of moves by which a final state can be reached.
Example 2.8
q0
a
q1
a
q4
a
a
q2
a
Nondeterministic
q5
a
q3
0
q0
Example 2.8
0,1
q1
1

nondeterministic
It accepts  ,1010,101010,but not 110,10100.
q2
 ( q 2 ,0)  
 (q2 ,1)  
 (q2 ,  )  {q2 }
 (q1 ,  )  {q1}
 ( q 0 ,0)  
Note for 10 there are two alternative walks.
T he transition functioncan be extendedso as its secondargumentis a string.
If  * (qi , w)  Q j thenQ j is theset of all possible statesthenfa may be in,
havingstartedin stateqi and havingread w.
Example 2.8
q0
 * (q1, a)  {q0 , q1, q2}
 * (q2 ,  )  {q0 , q2}
 * (q2 , aa)  {q0 , q1, q2}
a
q1


q2
 (q0 ,  )  {q0 }
 (q1 , a)  
 (q2 , a)  
Definition2.6
T helanguage L accepted by an nfa M  (Q,Σ, , q0 ,F ) is defined formally
as follows:
L(M)  {w  Σ* : δ *(q0 ,w)  F   }.
In words, thelanguage consist sof all strings w for whichthereis a walk
labeled w from theinitial vert exof the tansit iongraph tosome final vert ex.
Example 2.10
Does this nfa accept string
Find the language accepted by
w=110?
the following nfa.
Dead Configuration!!!
0
q0
1
q1
Solution
L  {(10)n : n  0}
0,1

q2
 (q2 ,0)  
 (q2 ,1)  
 ( q 0 ,0 )  
 ( q2 ,  )  q2
 (q1 ,  )  q1
• Why Nondeterminism? (Digital computers are completely
deterministic)
1. Nondeterministic machines is sometime helpful in solving
problems easily.
Example Search-and-backtrack algorithms
2. A nfa is an effective mechanism for describing some
complicated languages concisely.
Example
S  aSb|λ
3. There is no essential difference between these two types of
automata.
Given any dfa M, we can find an nfa M’ such that L(M)=L(M’).
Also given any nfa M, we can find a dfa M’ such that L(M)=L(M’).
Example
The following nfa and dfa are equivelant.
0
q0
q1
1
0
q0
1
0,1

1
q1
0
q2
q2
0,1
nfa
dfa
Procedure to convert nfa to dfa
1. Creat a graph G(D) with vertex {q0}. Identify this vertex as the initial vertex.
2. Repeat the following steps until no more edges are missing.
take any vertex{qi,qj,…,qk} of G(D) that has no outgoing edge for some
a  . Compute  N* (qi , a), N* (q j , a),..., N* (qk , a).
If  N* (qi , a)  N* (q j , a) ... N* (qk , a)  {ql, qm,...,qn},
create a vertex for G(D) labeled {ql,qm,…,qn} if it is not already exist. Add
to G(D) an edge from {qi,qj,…,qk} to {ql,qm,…,qn} and label it with a.
3. Every state of D(D) whose label contains any q f  FN is identified as a final
vertex.
4. If M N accepts  , the vertex {q0} in G(D) is also made a final vertex.
Convert nfa to dfa
Example
a
a
q0
q1

q2
b
 ({q0}, a}  {q1, q2 }
 ({q0}, b}  
 ({q1, q2}, a}  {q1, q2}
 ({q1, q2}, a}  {q0}
 ( , b}  
 ( , b}  
a
b
{q0 }
a
{q1 , q2 }
dfa
b

a, b
nfa
0
Example
q0
q1
1
 ({q0 },0}  
 ({q0 },1}  {q1}
 ({q1},0}  {q0, q2 }
0,1

q2
 ({q1},1}  {q2 }
 ({q0 , q2 },0}  
 ({q0 , q2 },1}  {q1}
 ({q2 },0}  
 ({q2 },1}  
1
{q0 }
1
0
{q2 }
0,1
0,1

0
1
{q1}
0
{q0 , q2}
dfa
nfa
Chapter 3 Regular Languages and Regular Grammars
• We have known that a regular language can be described by some
dfa. In this chapter we look at other ways of representing regular
languages.
3.1 Regular Expressions
• Formal Definition of a Regular Expression
Definition3.1
Let Σ be a given alphabet .T hen
1. ,  and a  Σ are all regular expressions. T heseare called primit ive
regular expressons.
2. If r1 and r2 are regular expressions, so are r1  r2 , r1  r2 , r1* and (r1 ).
3. A stringis a regular expressionif and onlyif it can derived from
theprimit iveregular expressions by a finitenumber of applications
of therules in (2).

Example 3.1 For   {a, b, c}, thestring(a  b  c)  (c  ) is a regular
expression, but (a  b  ) is not a regular expression.
• Language Associated with Regular Expressions
Definition3.2
T helanguage L(r) denotedby any regular expressionr is defined
by thefollowingrules.
1. is a reguar expressiondenotingtheemptyset,
2. λ is a regular expressiondenoting{λ},
Termination
3. for everya  Σ , a is a regular expressiondenoting{a}. condition
If r1 and r2 are regular expressions, then
4. L(r1  r2 )  L(r1 )  L(r2 ),
5. L(r1  r2 )  L(r1 )L(r2 ),
Reduce L(r ) to simpler components.
6. L((r1 ))  L(r1 ),
7. L(r1* )  ( L(r1 ))* .
Example 3.2 Exhibit the language L(a*・(a+b)) in set notation.
Solution
L(a*  (a  b))  L(a* ) L(a  b)  ( L(a))* ( L(a)  L(b))
 {λ, a, aa, aaa,...}{a, b}  {a, aa, aaa,...,b, ab, aab,...}
Ambiguity:
L(a  b  c)  {ab, c} or L(a  b  c)  {ab, bc} ?
Precedence rules: star-closure, concatenation, union
low
high
Example 3.3
For Σ  {a,b}, theexpressionr  (a  b)*(a  bb)
is regular.It denotesthelanguage
L(r )  L((a  b)* ) L(a  bb)  ( L(a  b))* L(a  bb)
 ( L(a)  L(b))* ( L(a)  ( L(b) L(b)))  {a, b}*{a, bb}
 { , a, b, ab, ba,...} {a, bb, aa, abb, ba, bbb,...}
Example 3.4
T heexpressionr  (aa)*(bb)* b is regular.
It denotesthelanguage L(r)  {a 2 nb 2 m1;n  0,m  0 }.
Example 3.5
For   {0,1}, give a regular expressionr such that
L(r)  {w  Σ * : w has at least one pair of consequtive zeros}
Solution: r  (0  1)* 00(0  1)*
Example 3.6
Find a regular expressionfor thelanguage
L  {w {0,1} : w has no pair of consevutive zeros}.
*
Solution: r  (1  01) (0   )
3.2 Connection Between Regular Expressions and
Regular Languages
•
Given regular expression r, we have defined a language L(r ).
•
A language is said to be regular, if it is accepted by a dfa (or by
an nfa since dfa and nfa are equivalent).
What is the relation between regular expressions and regular language?
Regular languages
Regular expressions
Theorem 3.1 Let r be a regular expression. Then there exists some
nfa that accepts L(r ). Consequently, L(r ) is a regular language.
Theorem 3.2 Let L be a regular language. Then there exists a
regular expression r such that L=L(r ).
Theorem 3.1 Let r be a regular expression. Then there exists some
nfa that accepts L(r ). Consequently, L(r ) is a regular language.
Proof: Construct an nfa that accepts L(r ) as follows.
a
λ
q
q
q1
q1
q0
q1
0
0
(1) nfa accepts .
(2) nfa accepts{}.
M(r )
Let M(r) be an nfa acceptsr.

M (r1 )

(3) nfa acceptsa.
M (r1 )

M (r2 )

(5) nfa acceptsL(r1r2 ).

M (r2 )


(4) nfa acceptsL(r1  r2 ).

M (r )


(6) nfa acceptsL(r * ).

Example 3.7
Find an nfa which acceptsL(r), where
r  (a  bb)*(ba*  λ ).
Solution
L(r )  {a, bb}* {ba* , λ}.
a
a
b
λ
b
b
λ
λ
λ
a
a
b
b
λ
λ
λ
λ
b
λ
λ
Theorem 3.2 Let L be a regular language. Then there exists a
regular expression r such that L=L(r ).
Proof: Remove one vertex after the other between the initial one and
e
final one.
ce*b
ae*d
1
d
a 2
c
b
3
1
ce*d
ae*b
3
Finally, we reach the following situation:
r4
r1
q0
r3
r2
q2
It accepts thelanguage represented
by regular expression
r1*  r1*r2 r4 * (r1*r2 r4*r3r1*)*.
r4
r1
q0
r3
r2
q2
It acceptsthelanguage represented by
regular expressionr1*r2 r4*  r1*r2 r4* (r3r1*r2 r4* )*.
Regular Expressions for Describing Simple Patterns
T heset of all acceptablePascalintegersis defined
by theregular expressionsdd* , where s stands
for thesign, with possible value from{-,λ}, and
d standsforthedigits 0 to 9.
T heeditor ed in theUNIXoperat ingsyst em
recognaizes thecommad /aba* c/ as to find
thest ringaba* c. It means that theUNIXeditor
can recognaizeregular expressions.
Given a regular expression, we can find an nfa for recognizing it.
This nfa can be reduced to a dfa.
Example 3.8 Given the following nfa, find the regular expression
it accepts.
*
ab
b

ab
a
b
b
a,b
ab*b q
2
a
q0
q1 b q2
a
Therefore,theregular expressionit acceptsis (b  ab*a)* ab*b(a  b)*
q0
3.3 Regular Grammars
• The third way of describing regular languages is by means of
certain simple grammars. (what are the other two ways?)
Right- and Left-Linear Grammars
A grammarG  (V,T,S,P)is said to be right - linear if all product ions
are of theform
A  xB,
A x,
where A,B  V , and x  T * . A grammaris said to be left - linear
if all product ions are of theform
A  Bx,
A x.
A regular grammais one thatis eitherright - linear or left - linear.
Example3.9
T hegrammarG1  ({S }, {a, b}, S , P1 ), wit h P1 given as
S  abS|a
is right linear.
T hegrammarG 2  ({S , S1 , S 2 }, {a, b}, S , P2 ), wit h P2 given as
S  S1ab, S1  S1ab|S2 , S2  a is left linear.
Bot h G1 and G2 are regular grammars.
L (G1 )  L (( ab )* a )
L (G2 )  L ( aab ( ab )* )
Example3.10
T hegrammarG1  ({S , A, B}, {a, b}, S , P ), with P given as
S  A, A aB|, B  Ab is not regular.
Right-linear Grammars Generate Regular Languages
•Right-linear grammar
Regular language
•left-linear grammar
Regular language
Theorem 3.3 Let G=(V,T,S,P) be a right-linear grammar. Then
L(G) is a regular language.
Policy of Proof: Construct an nfa which accepts L(G).
V
a1
a2
ak
V
V’
a1
a2
ak V
f
(2) nfa forV  a1a2 ak
(1) nfa forV  a1a2 akV '
Example3.11 Constructa finiteautomatonthatacceptsthelanguage
generatedby thegrammar
V a
V b V
V0  aV1,
V1  abV0|b.
0
b
1
a
f
Theorem 3.4 If L is a regular language on an alphabet , then
there exists a right-linear grammar G=(V, ,S,P) such that L=L(G).
Policy of Proof: Let M be the dfa that accepts L. Construct the
right-linear grammar G from M such that G generates L.
Example3.12 Constructa right - linear grammarfor L(aab* a).
Solution
A dfa which acceptsL(aab* a).
b
q0 a
q1
a
q2
a
A gram m arwhich generatesL(aab* a) :
G  (V , , S , P)
where V  {q0 ,q1,q2 ,q f }, Σ  {a,b}, S  q0 and P is as follows:
q 0  aq1 , q1  aq2 , q 2  bq2 , q 2  aq f , q f   .
qf
Course Project 1: Design An Automatic Selling Machine
Problem: There is a machine selling 3 types of drinks d1, d2, d3, where the price for d1 is 70
cents, for d2 is 90 cents and for d3 is 1 dollar. The buyers can use coins of 10 cents, 25
cents, 50 cents and 1 dollar. The number of drinks which a buyer can buy once from
the machine is up to 3. Suppose that in the machine there is a place for inserting coins,
a button for selecting the type of drinks and a button for selecting the number of drinks.
To buy drinks, the buyer first insert money, then select the type of drinks, finally select
the number of the drinking.
(1) Suppose that   {c1, c2, c3, c4, d1, d 2, d 3, n1, n2, n3} , where c1=10cents, c2=25cents,
c3=50cents, c4=1 dollars, n1=1, n2=2, n3=3. Write regular expression/language the
machine accepts.
(2) Design a DFA for the language in (1).
(3) Design a regular grammar to generate the language in (1)
(4) Write a program (using C++ or C#) to implement the DFA in (2)
(5) (Optional) Change the program in (4) so that it gives the change to users.
Requirement for the project:
(1) A report with a detailed description for the requirements of the machine and for (1), (2), (3)
and a program for (4). The program must have a using interface for a user to give an input and get
an output (tell user if the buying is successful and give back the change and drinks).
(2) Give a power point presentation.
Course Project 2: Design A Simple Calculator
+
-
×
/
0
1
2
3
Problem: Design a simple calculator (see the figure).
4
5
6
7
Suppose that   {,,, /,0,1,2,3,4,5,6,7,8,9,., (, ), }
8
9
(
)
(1) Design a regular expression r for the calculator.
=
.
(2) Design a DFA for accepting the regular language L(r).
(3) Design a regular grammar to generate the regular language L(r).
(4) Write a program (using C++ or C#) to implement the DFA.
Requirement for the project:
(1) A report with a detailed description for (1), (2), (3) and a program for (4). The program
must have a using interface for a user to give an input and get an output.
(2) Give a power point presentation.
Chapter 4 Properties of Regular Language
4.2 Elementary questions about regular languages
T heorem5 Given a standardrepresentation of any regular
language L on Σ and any w  Σ * , thereexistsan algorithm
for determingwhetheror not w is in L.
Proof: We represent the language by some dfa, then test w
to see if it is accepted by this dfa.
Theorem 6 There exists an algorithm for determining whether a
regular language, given in standard representation, is empty,
finite,or infinite.
Proof: (1) represent the language by some dfa. If there is a simple path
from the initial vertex to any final vertex, then the language is not
empty. (2) Find all the vertices that are the base of some cycle. If any
of these are on a path from an initial to a final vertex, the language is
infinite. Otherwise, it is finite.
T heorem7 Given a standardrepresentation of two regular
languages L1 and L2 , thereexistsan algorithmto determine
whetheror not L1  L2 .
P roof: Define thelanguage L  (L1  L2 )  ( L1  L2 ).
L is regular and we can find a dfa M thatacceptsL.
We use theorem6 to deteminif L3 is empty. Wecan
provethat L3   if and onlyif L1  L2 .