Outline Nondeterminism Regular expressions Elementary - - PowerPoint PPT Presentation

outline
SMART_READER_LITE
LIVE PREVIEW

Outline Nondeterminism Regular expressions Elementary - - PowerPoint PPT Presentation

Outline Nondeterminism Regular expressions Elementary reductions Computation, Computers, and Programs Nondetermistic Finite Automata 1 http://www.cs.caltech.edu/~cs20/a October 8, 2002 Determistic Finite Automata A finite


slide-1
SLIDE 1

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

1

Outline

  • Nondeterminism
  • Regular expressions
  • Elementary reductions
slide-2
SLIDE 2

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

2

Determistic Finite Automata

  • A finite automaton is a 5-tuple (Q, Σ, δ, q0, F)

– Q is a finite set of states – Σ is an alphabet – δ : Q × Σ → Q is a transition function – q0 ∈ Q is the initial state – F ⊆ Q is a set of final or accepting states

slide-3
SLIDE 3

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

3

Transition diagrams

Q = {q0, q1, q2, q3} Σ = {0, 1} δ = State Symbol Goto q0 q2 q0 1 q1 q1 q3 q1 1 q0 q2 q0 q2 1 q3 q3 q1 q3 1 q2 q0 = q0 F = {q0}

1 1 1 1 q0 q1 q2 q3

slide-4
SLIDE 4

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

4

A finite automaton

1 1 1 1

Finite Control Read-only tape Tape head

  • 1. In state q
  • a. read a symbol c
  • b. move the tape head right
  • b. goto state delta(q, c)
  • 2. Accept iff the FA is in a final

state after reading the last symbol

slide-5
SLIDE 5

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

5

Definition of regular expressions

  • Let Σ be an alphabet. The regular expressions are

defined inductively as follows: – ∅ is a regular expression denoting {}, – ǫ is a regular expression denoting {ǫ}, – for each a ∈ Σ, a is a regular expression de- noting {a}, – Assume r and s are regular expressions de- noting sets R and S, then ∗ rs denotes RS, ∗ r + s denoted R ∪ S, ∗ r ∗ denotes R∗

slide-6
SLIDE 6

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

6

Examples of regular expressions

  • abc denotes {abc},
  • a∗b denotes {b, ab, aab, aaab, . . .},
  • (a+b)∗aa(a+b)∗: all strings containing at least

two consecutive a's.

  • (0∗(101∗01)∗)∗: all binary numbers that are a

multiple of 3.

slide-7
SLIDE 7

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

7

Nondeterminism

  • For determistic finite automata, the transition

function delta is a function

– delta : state * symbol -> state – For each state, and each symbol, there is a unique next state

  • What if we relax this?
  • In a nondeterministic finite automaton, delta is a

transition relation

slide-8
SLIDE 8

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

8

Definition of an NFA

  • A nondeterministic finite automaton is a 5-tuple

(Q, Σ, δ, s, F) – Q is a finite set of states – Σ is an alphabet – δ : Q × Σ → 2Q is a transition relation – s ∈ Q is the initial state – F ⊆ Q is a set of final or accepting states

slide-9
SLIDE 9

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

9

The delta relation

  • Note, the following definitions are equivalent:

– δ : Q × Σ → 2Q is a transition relation – δ ⊆ Q × Σ × Q is a transition relation

  • The transition diagram is often drawn without empty

edges: – if δ(q, c) = {}, then the machine rejects and we do not draw the edge

slide-10
SLIDE 10

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

10

Example NFA

0,1 0,1 0,1 1 1 q0 q1 q2 q4 q3

slide-11
SLIDE 11

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

11

Executions (for DFAs)

  • An execution of a deterministic automaton M =

(Q, Σ, δ, q0, F) is a sequence (q0, c0)(q1, c1) . . . (qn−1, cn−1)qn ∈ (Q × Σ)∗ such that: – q0 is the start state – δ(qi, ci) = qi+1

  • The execution string s is c0c1 . . . cn−1
  • If there exists an execution for string s with qn ∈ F

the automaton accepts and s ∈ L(M); otherwise it rejects

slide-12
SLIDE 12

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

12

Executions (for NFAs)

  • An execution of a nondeterministic automaton M =

(Q, Σ, δ, q0, F) is a sequence (q0, c0)(q1, c1) . . . (qn−1, cn−1)qn ∈ (Q × Σ)∗ such that: – q0 is the start state – qi+1 ∈ δ(qi, ci)

  • The execution string s is c0c1 . . . cn−1
  • If there exists an execution for string s with qn ∈ F

the automaton accepts and s ∈ L(M); otherwise it rejects

slide-13
SLIDE 13

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

13

A nondeterministic finite automaton

1 1 1 1

Finite Control Read-only tape Tape head

  • 1. In state q
  • a. read a symbol c
  • b. move the tape head right
  • b. goto some state in delta(q, c)
  • 2. Accept iff the NFA is in a final

state after reading the last symbol

slide-14
SLIDE 14

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

14

Execution models

  • 1. Classical: For each state q and input sym-

bol c, the automaton makes a nontermistic choice among all the possible options δ(q, c); it accepts if it made all the right choices

  • 2. Another choice:

– At any point, the automaton is in a super- position of states Q1 – For each symbol c, it enters states Q2 where q2 ∈ Q2 iff ∃q1 ∈ Q1.q2 ∈ δ(q1, c)

slide-15
SLIDE 15

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

15

Extending the transition relation

  • Defining

δ: – δ(q, ǫ) = {q} – δ(q, wa) = {p | ∃r ∈ δ(q, w).p ∈ δ(r, a)}

  • A machine M = (Q, Σ, δ, q0, F) accepts string s iff
  • δ(q0, s) ∩ F ≠ {}
slide-16
SLIDE 16

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

16

Expressive power of NFAs vs. DFAs

  • Every DFA is an NFA
  • NFAs allow more transitions
  • Is the language more expressive?

– Is there a language L=L(M) for some NFA M, where L<>L(M’) for some DFA M’?

slide-17
SLIDE 17

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

17

NFA/DFA Equivalence

  • To prove equivalence of DFAs and NFAs we must

do two things:

– I: For each DFA, produce an NFA that accepts the same language – II: For each NFA, produce a DFA that accepts the same language

  • This is called a reduction—reduce one

construction to another equivalent one

– We’ll do part II; part I is immediate

slide-18
SLIDE 18

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

18

NFA->DFA construction

  • Consider an arbitrary NFA M = (Q, Σ, δ, s, F); con-

struct an equivalent DFA M′ = (Q′, Σ, δ′, s′, F′) – Let Q′ = 2Q – Let s′ = {s} – Let δ′({q1, . . . , qn}, c) = n

i=1 δ(qi, c)

– Let F′ = {t ∈ 2Q | t ∩ F ≠ {}}

slide-19
SLIDE 19

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

19

NFA->DFA construction: verification

Theorem δ(s, x) = δ′({s}, x) Corollary δ(s, x) ∩ F ≠ {} ⇔ δ′({s}, x) ∩ F ′ ≠ {}

slide-20
SLIDE 20

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

20

NFA->DFA construction: base case

Theorem δ(s, x) = δ′({s}, x) Base case δ(s, ǫ) = {s} = δ′({s}, ǫ)

slide-21
SLIDE 21

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

21

NFA->DFA construction: induction step

  • Assume δ(s, x) = δ′({s}, x) for some x
  • Prove δ(s, xa) = δ′({s}, xa) for any a

δ(s, xa) = by definition of δ δ(δ(s, x), a) = by definition of δ

  • p∈δ(s,x) δ(p, a)

= by induction

  • p∈δ′(s,x) δ(p, a)

= by definition of δ′

  • p∈δ′(s,x) δ′({p}, a)

= by definition of δ′

  • p∈δ′({s},x) δ′({p}, a)

= by definition of δ′ δ′(δ′({s}, x), a) = by definition of δ′ δ′({s}, xa)

slide-22
SLIDE 22

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

22

NFA with epsilon transitions

  • Suppose we add a new twist

– Allow transitions that require no input – Label them epsilon (no input) – The automaton may choose to take an epsilon transition without any input

  • Executions include (q_i, epsilon) (q_i+1, _) steps
  • A machine accepts if there exists an execution

that accepts

slide-23
SLIDE 23

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

23

Example e-NFA

q0 q1 q2 1 2 ε ε

slide-24
SLIDE 24

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

24

E-NFA definition

  • A nondeterministic finite automaton with ǫ transi-

tions is a 5-tuple (Q, Σ, δ, s, F) – Q is a finite set of states – Σ is an alphabet – δ : Q × (Σ ∪ {ǫ}) → 2Q is a transition relation – s ∈ Q is the initial state – F ⊆ Q is a set of final or accepting states

slide-25
SLIDE 25

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

25

E-NFA->NFA reduction

  • Consider the superposition model

– If the e-NFA could be in states {q1, q2, … qn}, add the states that could be reached by e-transitions from any state qi – This is called the e-closure

slide-26
SLIDE 26

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

26

E-closure example q0 q1 q2 1 2 ε ε ε-closure(q0) = { q0, q1, q2 } ε-closure(q1) = { q1, q2 } ε-closure(q2) = { q2 }

slide-27
SLIDE 27

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

27

Formal definition

  • Define ǫ-closure(P) =
  • p∈P ǫ-closure(p)

– Let δ(q, ǫ) = ǫ-closure(q) – Let δ(q, xa) = ǫ-closure(P), where P = {p | ∃r ∈ δ(q, w).p ∈ δ(r, a)} – Define δ(R, a) =

  • r∈R δ(r, a)

– Define δ(R, a) =

  • r∈R

δ(r, a)

  • M = (Q, Σ, δ, s, F) accepts string x iff

δ(s, x)∩F ≠ {}

slide-28
SLIDE 28

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

28

Reduction

  • Let M = (Q, Σ, δ, s, F) be an NFA with ǫ moves.
  • Construct M′ = (Q, Σ, δ′, s, F′) an NFA without ǫ

moves. – Let δ′(q, a) = δ(q, a) – Let F′ =

  • F ∪ {s}

if ǫ-closure(s) ∩ F ≠ {} F

  • therwise
slide-29
SLIDE 29

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

29

Proof

Prove δ′(s, x) = δ(s, x) by induction on the length of x Note that δ′(s, ǫ) = δ(s, ǫ) may not be true. Oh well, start the induction at |x| = 1. Base If |x| = 1 then x = a and δ′(s, a) = δ(s, a) by definition

slide-30
SLIDE 30

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

30

Induction step

Step Suppose x = wa δ′(s, wa) = by definition δ′(δ′(s, w), a) = by induction δ′( δ(s, w), a) = by definition

  • q∈

δ(s,w) δ′(q, a)

= by definition of δ′

  • q∈

δ(s,w)

δ(q, a) = by definition of δ

  • δ(s, wa)
slide-31
SLIDE 31

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

31

Summary (so far)

  • Proofs so far

– Proved NFA->DFA – Proved e-NFA->NFA

  • Next, establish total equivalence

– Prove regex->e-NFA – Prove DFA->regex

  • If so, all four are equivalent
slide-32
SLIDE 32

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

32

Definition of regular expressions

  • Let Σ be an alphabet. The regular expressions are

defined inductively as follows: – ∅ is a regular expression denoting {}, – ǫ is a regular expression denoting {ǫ}, – for each a ∈ Σ, a is a regular expression de- noting {a}, – Assume r and s are regular expressions de- noting sets R and S, then ∗ rs denotes RS, ∗ r + s denoted R ∪ S, ∗ r ∗ denotes R∗

slide-33
SLIDE 33

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

33

Regex->eNFA

  • Prove by structural induction: induction on the

size of the regular expression

  • Base cases:

– Show the empty RE has an e-NFA – Show the RE has an e-NFA – Show the a (symbol) RE has an e-NFA

  • Induction:

– Show (xy) has an e-NFA – Show (x + y) has an e-NFA – Show x* has an e-NFA

slide-34
SLIDE 34

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

34

Base cases

a r = ∅ r = a r = ε

slide-35
SLIDE 35

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

35

Choice

ε ε ε ε Machine 1 Machine 2 Start state Any (originally) final state

slide-36
SLIDE 36

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

36

Concatenation Machine 1 Machine 2 ε

slide-37
SLIDE 37

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

37

Kleene closure

Machine 1 ε ε ε ε

slide-38
SLIDE 38

Computation, Computers, and Programs Nondetermistic Finite Automata http://www.cs.caltech.edu/~cs20/a October 8, 2002

38

Final proof: DFA->regex

  • Think about it