kleene's theorem - cornell university · 2014-11-07 · kleene's theorem a language is...

48
Kleene's Theorem CS 2800: Discrete Structures, Fall 2014 Sid Chaudhuri

Upload: others

Post on 26-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene's Theorem

CS 2800: Discrete Structures, Fall 2014

Sid Chaudhuri

Page 2: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene's Theorem

● A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a fnite automaton

Page 3: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene's Theorem

● A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a fnite automaton– Regex has FA

● Relatively simple construction

Page 4: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene's Theorem

● A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a fnite automaton– Regex has FA

● Relatively simple construction

– FA has regex● Tricky to prove

Page 5: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Regex → FA

● For every regular expression, there is a fnite automaton that recognizes the same language

Page 6: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Regex → FA

● For every regular expression, there is a fnite automaton that recognizes the same language

● We will construct an ε-NFA

Page 7: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Regex → FA

● For every regular expression, there is a fnite automaton that recognizes the same language

● We will construct an ε-NFA– … which can be converted to an NFA

Page 8: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Regex → FA

● For every regular expression, there is a fnite automaton that recognizes the same language

● We will construct an ε-NFA– … which can be converted to an NFA– … which can be converted to a DFA

Page 9: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Recap: NFAs with epsilon transitions

● Just like ordinary NFAs, but...– Can “instantaneously” change state without reading an input symbol

– Valid transitions of this type are shown by arcs labeled 'ε'

– Note that ε does not suddenly become a member of the alphabet. Instead, we assume ε does not belong to any alphabet – it's a special symbol.

q0

q2

q1

a ε

εb

Page 10: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Why ε-NFAs?● Suitable for representing “or” relations● E.g. L = { an | n ∈ N is divisible by 2 or 3 }

● … but they're equivalent to NFAs and DFAs

p

r0

q0

q1

r1 r

2

ε

ε

a

aa a

a

Page 11: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● The ε-closure of a state q is the set of states that can be reached from q following only ε-transitions

Page 12: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● The ε-closure of a state q is the set of states that can be reached from q following only ε-transitions– The set includes q itself

Page 13: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● The ε-closure of a state q is the set of states that can be reached from q following only ε-transitions– The set includes q itself– We'll denote the set ECLOSE(q)

Page 14: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● The ε-closure of a state q is the set of states that can be reached from q following only ε-transitions– The set includes q itself– We'll denote the set ECLOSE(q)

ECLOSE(p) = ?

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

Page 15: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● The ε-closure of a state q is the set of states that can be reached from q following only ε-transitions– The set includes q itself– We'll denote the set ECLOSE(q)

ECLOSE(p) = { p, q0, r

0 }

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

Page 16: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

x

x x

xA) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 2, 3, 4 }

Page 17: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

x

x x

xA) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 2, 3, 4 }

Page 18: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

εx x

xA) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 2, 3, 4 }

Page 19: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

εx x

xA) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 2, 3, 4 }

Page 20: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

εε ε

εA) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 21: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

εε ε

εA) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 22: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

x

ε εx

A) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 23: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

x

ε εx

A) { 2, 4 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 24: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

x

x

ε

x

ε εx

A) { 1 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 25: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

x

x

ε

x

ε εx

A) { 1 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 26: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

x

ε x

xA) { 1, 2, 3 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

Page 27: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ECLOSE(1)?

1

4

2 3

5 6

ε

ε

ε

x

ε x

xA) { 1, 2, 3 }B) { 1, 2, 4 }C) { 1, 2, 3, 4 }D) { 1, 2, 3, 4, 5, 6 }

E) { 1, 2, 3, 4, 5 }

Page 28: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● Converting ε-NFA Nε to ordinary NFA N (short-circuiting ε paths)

Page 29: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● Converting ε-NFA Nε to ordinary NFA N (short-circuiting ε paths)

1. Make p an accepting state of N if ECLOSE(p) contains an accepting state of Nε

Page 30: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● Converting ε-NFA Nε to ordinary NFA N (short-circuiting ε paths)

1. Make p an accepting state of N if ECLOSE(p) contains an accepting state of Nε

2. Add an arc labeled a from p to q if Nε has an arc labeled a from some state in ECLOSE(p) to q

Page 31: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

ε-NFA to ordinary NFA

● Converting ε-NFA Nε to ordinary NFA N (short-circuiting ε paths)

1. Make p an accepting state of N if ECLOSE(p) contains an accepting state of Nε

2. Add an arc labeled a from p to q if Nε has an arc labeled a from some state in ECLOSE(p) to q

3. Delete all arcs labeled ε

Page 32: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

Page 33: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

Make p an accepting state of N if

ECLOSE(p) contains an accepting state of Nε

Page 34: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

Make p an accepting state of N if

ECLOSE(p) contains an accepting state of Nε

Add an arc labeled a from p to q if Nε has an arc labeled a from

some state in ECLOSE(p) to q

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

a

a

Page 35: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

p

r0

q0

q1

r1 r

2

a

a

a a

a

a

a

Make p an accepting state of N if

ECLOSE(p) contains an accepting state of Nε

Add an arc labeled a from p to q if Nε has an arc labeled a from

some state in ECLOSE(p) to q

Delete all arcs labeled ε

p

r0

q0

q1

r1 r

2

ε

ε

a

a

a a

a

a

a

Page 36: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Regular expression to ε-NFA

● Structural induction on regex– Construct simple automata for base cases– For every higher-order construction, construct

equivalent ε-NFA from smaller ε-NFAs

Page 37: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Empty set

q0

Regex: ∅

Page 38: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Empty string

q0

Regex: ε

Page 39: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Literal character

q1

Regex: a

q0

a

Page 40: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Concatenation

Regex: AB

Page 41: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Concatenation

Regex: AB p'

p0

...p"

ⵗNFA for A

q0

...NFA for B

Page 42: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Concatenation

Regex: AB p'

p0

...p"

ⵗNFA for A

q0

...NFA for B

p'

p0

...p"

q0

...

NFA for AB

ε

ε

Page 43: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Alternation

Regex: A|B

Page 44: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Alternation

Regex: A|B

q0

...NFA for B

p0

...NFA for A

Page 45: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Alternation

Regex: A|B

q0

...NFA for B

s

NFA for A|B

p0

...NFA for A

p0

...

q0

...

ε

ε

Page 46: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene star

Regex: A*

Page 47: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene star

Regex: A*

p'p0

...

ⵗNFA for A

Page 48: Kleene's Theorem - Cornell University · 2014-11-07 · Kleene's Theorem A language is regular, i.e. it can be defned by a regular expression, if and only if it is recognized by a

Kleene star

Regex: A*

s

NFA for A*

p'p0

...

ⵗNFA for A p'p

0...

fε ε

ε

ε