0% found this document useful (0 votes)
9 views271 pages

Chapter 1 To 10

The document provides an overview of Boolean logic, detailing various operators such as NOT, AND, OR, IMPLIES, and EQUIVALENCE, along with their truth tables. It also discusses concepts like satisfiability, tautologies, contradictions, and logical equivalence, illustrating these with examples and truth tables. Additionally, it introduces advanced operators like XOR, NAND, and NOR, emphasizing their significance in logical expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views271 pages

Chapter 1 To 10

The document provides an overview of Boolean logic, detailing various operators such as NOT, AND, OR, IMPLIES, and EQUIVALENCE, along with their truth tables. It also discusses concepts like satisfiability, tautologies, contradictions, and logical equivalence, illustrating these with examples and truth tables. Additionally, it introduces advanced operators like XOR, NAND, and NOR, emphasizing their significance in logical expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 271

Boolean Logic

Chapter 1
Computing Is About Boolean Logic

The rules of the logic tell us how to manipulate inputs


and produce outputs.

We define the rules so that we get answers that are


useful to us.
Boolean Operators

NOT

P 
True
False
Boolean Operators

NOT

P 
True False
False True
Boolean Operators

AND

P Q PQ
True True
True False
False True
False False
Boolean Operators

AND

P Q PQ
True True True
True False False
False True False
False False False
Boolean Operators

OR

P Q PQ
True True
True False
False True
False False
Boolean Operators

OR

P Q PQ
True True True
True False True
False True True
False False False
Boolean Operators

IMPLIES

P Q PQ
True True
True False
False True
False False
Boolean Operators

IMPLIES

P Q PQ
True True True
True False False
False True True
False False True
Boolean Operators

EQUIVALENCE

P Q PQ
True True
True False
False True
False False
Boolean Operators

EQUIVALENCE

P Q PQ
True True True
True False False
False True False
False False True
Boolean Logic

P Q P PQ PQ PQ PQ


True True False True True True True
True False False True False False False
False True True True False True False
False False True False False True True
Using Boolean Logic

P Q P
True True

True False

False True

False False

P  ((P  Q) Q)
Using Boolean Logic

P Q P PQ
True True False

True False False

False True True

False False True

P  ((P  Q) Q)
Using Boolean Logic

P Q P P  Q (P  Q)  Q
True True False True

True False False True

False True True True

False False True False

P  ((P  Q) Q)
Using Boolean Logic

P Q P P  Q (P  Q)  Q
True True False True True

True False False True False

False True True True True

False False True False True

P  ((P  Q) Q)
Using Boolean Logic

P Q P P  Q (P  Q) Q P  ((P  Q) Q)


True True False True True

True False False True False

False True True True True

False False True False True

P  ((P  Q) Q)
Using Boolean Logic

P Q P P  Q (P  Q) Q P  ((P  Q) Q)


True True False True True False

True False False True False False

False True True True True True

False False True False True True

P  ((P  Q) Q)
Another Example
E H N S EH ((E  H)  N  S

True True True True True


True True True True True
True False True True
True False True True
True True True True
True True True True
True False True
True False True




Another Example
E H N S EH ((E  H)  N  S

True True True True True


True True True True True
True False True True
True False True True
True True True True
True True True True
True False True
True False True



((Exhausted  HidingPlaceNearby)  Nightime)  StopToSleep


Boolean Logic

P Q P PQ PQ PQ PQ


True True False True True True True
True False False True False False False
False True True True False True False
False False True False False True True

Let’s practice.
Boolean Identities

This notation:
• Multiply for AND
• Add for OR
Proving These Things
Prove the first of deMorgan’s laws:

(A  B)  A  B

A B AB (A  B)
True True True False
True False False True
False True False True
False False False True

A B A B A  B
True True False False False
True False False True True
False True True False True
False False True True True
Proving These Things
Prove the first of deMorgan’s laws:

(A  B)  A  B

A B AB (A  B)
True True True False
True False False True
False True False True
False False False True

A B A B A  B
True True False False False
True False False True True
False True True False True
False False True True True
Satisfiability

A Boolean formula is satisfiable if and only if there is


some row of the truth table that is T.

P Q P P  Q P  Q (P  Q) Q P  ((P  Q) Q)


True True False True True True False

True False False True False False False

False True True True True True True

False False True False True True True

The job of a SAT solver is to determine satisfiability.


Using Boolean Expressions

(W  C D)  (W  A  D)

Is this expression satisfiable?


Using Boolean Expressions

(Wounded  CanRun Daylight) 

(Wounded  InAmbulance  Daylight)

Is this expression satisfiable?


Binary Boolean Operators

P Q    
T T T T T T T T T T F F F F F F F F
T F T T T T F F F F T T T T F F F F
F T T T F F T T F F T T F F T T F F
F F T F T F T F T F T F T F T F T F

What about the other 12 columns?


Boolean Operators

Exclusive Or XOR

P Q PQ
True True
True False
False True
False False

Chips OR Fries
Boolean Operators

Exclusive Or XOR

P Q PQ
True True False
True False True
False True True
False False False

Chips OR Fries
Boolean Operators

Not And NAND

P Q  NAND
True True True
True False False
False True False
False False False
Boolean Operators

Not And NAND

P Q  NAND
True True True False
True False False True
False True False True
False False False True
Boolean Operators

Not Or NOR

P Q  NOR
True True True
True False True
False True True
False False False
Boolean Operators

Not Or NOR

P Q  NOR
True True True False
True False True False
False True True False
False False False True
Binary Boolean Operators

    
NA NOR
P Q ND

T T T T T T T T T T F F F F F F F F
T F T T T T F F F F T T T T F F F F
F T T T F F T T F F T T F F T T F F
F F T F T F T F T F T F T F T F T F
Satisfiability

Recall: A Boolean formula is satisfiable if and only if there


is some row of the truth table that is T.

P Q P P  Q P  Q (P  Q) Q P  ((P  Q) Q)


True True False True True True False

True False False True False False False

False True True True True True True

False False True False True True True

The job of a SAT solver is to determine satisfiability.


Tautologies, contradictions,
contingencies
DEF: A compound proposition is called a tautology if
no matter what truth values its atomic propositions
have, its own truth value is T.
EG: p  ¬p (Law of excluded middle)
The opposite to a tautology, is a compound proposition
that’s always false –a contradiction.
EG: p  ¬p
On the other hand, a compound proposition whose truth
value isn’t constant is called a contingency.
EG: p  ¬p

L3 69
Tautologies and contradictions
The easiest way to see if a compound
proposition is a tautology/contradiction is
to use a truth table.

p p p p p p p p
F T T F T F
T F T T F F

L3 70
Tautology example (1.2.8.a)
Part 1
Demonstrate that
[¬p (p q )]q
is a tautology in two ways:
1. Using a truth table – show that [¬p
(p q )]q is always true
2. Using a proof (will get to this later).

L3 71
Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q


T T

T F

F T

F F

L3 72
Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q


T T F

T F F

F T T

F F T

L3 73
Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q


T T F T

T F F T

F T T T

F F T F

L3 74
Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q


T T F T F

T F F T F

F T T T T

F F T F F

L3 75
Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q


T T F T F T

T F F T F T

F T T T T T

F F T F F T

L3 76
Logical Equivalence of Conditional
and Contrapositive
The easiest way to check for logical
equivalence is to see if the truth tables of both
variants have identical last columns:

p q p q p q ¬q ¬p ¬q¬p

Q: why does this work given definition of  ?


L3 77
Logical Equivalence of Conditional
and Contrapositive
The easiest way to check for logical
equivalence is to see if the truth tables of both
variants have identical last columns:

p q p q p q ¬q ¬p ¬q¬p
T T T
T F F
F T T
F F T

Q: why does this work given definition of  ?


L3 78
Logical Equivalence of Conditional
and Contrapositive
The easiest way to check for logical
equivalence is to see if the truth tables of both
variants have identical last columns:

p q p q p q ¬q ¬p ¬q¬p
T T T T T
T F F T F
F T T F T
F F T F F

Q: why does this work given definition of  ?


L3 79
Logical Equivalence of Conditional
and Contrapositive
The easiest way to check for logical
equivalence is to see if the truth tables of both
variants have identical last columns:

p q p q p q ¬q ¬p ¬q¬p
T T T T T F
T F F T F T
F T T F T F
F F T F F T

Q: why does this work given definition of  ?


L3 80
Logical Equivalence of Conditional
and Contrapositive
The easiest way to check for logical
equivalence is to see if the truth tables of both
variants have identical last columns:

p q p q p q ¬q ¬p ¬q¬p
T T T T T F F
T F F T F T F
F T T F T F T
F F T F F T T

Q: why does this work given definition of  ?


L3 81
Logical Equivalence of Conditional
and Contrapositive
The easiest way to check for logical
equivalence is to see if the truth tables of both
variants have identical last columns:

p q p q p q ¬q ¬p ¬q¬p
T T T T T F F T
T F F T F T F F
F T T F T F T T
F F T F F T T T

Q: why does this work given definition of  ?


L3 82
Logical Equivalences
A: p q by definition means that p  q is a
tautology. Furthermore, the biconditional
is true exactly when the truth values of p
and of q are identical. So if the last
column of truth tables of p and of q is
identical, the biconditional join of both is a
tautology.

L3 83
Logical Non-Equivalence of
Conditional and Converse
The converse of a logical implication is the reversal of the
implication. I.e. the converse of p q is q p.
EG: The converse of “If Donald is a duck then Donald is a bird.” is
“If Donald is a bird then Donald is a duck.”
As we’ll see next: p q and q p are not logically equivalent.

L3 84
Logical Non-Equivalence of
Conditional and Converse
p q p q q p (p q)  (q p)

L3 85
Logical Non-Equivalence of
Conditional and Converse
p q p q q p (p q)  (q p)
T T
T F
F T
F F

L3 86
Logical Non-Equivalence of
Conditional and Converse
p q p q q p (p q)  (q p)
T T T
T F F
F T T
F F T

L3 87
Logical Non-Equivalence of
Conditional and Converse
p q p q q p (p q)  (q p)
T T T T
T F F T
F T T F
F F T T

L3 88
Logical Non-Equivalence of
Conditional and Converse
p q p q q p (p q)  (q p)
T T T T T
T F F T F
F T T F F
F F T T T

L3 89
Derivational Proof Techniques
When compound propositions involve more and
more atomic components, the size of the truth
table for the compound propositions increases
Q1: How many rows are required to construct the
truth-table of:
( (q(pr ))  ((sr)t) )  (qr )
Q2: How many rows are required to construct the
truth-table of a proposition involving n atomic
components?

L3 90
Derivational Proof Techniques
A1: 32 rows, each additional variable doubles the
number of rows
A2: In general, 2n rows
Therefore, as compound propositions grow in
complexity, truth tables become more and more
unwieldy. Checking for tautologies/logical
equivalences of complex propositions can
become a chore, especially if the problem is
obvious.

L3 91
Derivational Proof Techniques
EG: consider the compound proposition
(p p )  ((sr)t) )  (qr )

Q: Why is this a tautology?

L3 92
Derivational Proof Techniques
A: Part of it is a tautology (p p ) and the
disjunction of True with any other
compound proposition is still True:
(p p )  ((sr)t ))  (qr )
 T  ((sr)t ))  (qr )
 T
Derivational techniques formalize the
intuition of this example.

L3 93
Tables of Logical Equivalences

• Identity laws
Like adding 0
• Domination laws
Like multiplying by 0
• Idempotent laws
Delete redundancies
• Double negation
“I don’t like you, not”
• Commutativity
Like “x+y = y+x”
• Associativity
Like “(x+y)+z = y+(x+z)”
• Distributivity
Like “(x+y)z = xz+yz”
L3 94
• De Morgan
Tables of Logical Equivalences

• Excluded middle
• Negating creates opposite
• Definition of implication in
terms of Not and Or

L3 95
DeMorgan Identities
DeMorgan’s identities allow for simplification of
negations of complex expressions
• Conjunctional negation:
(p1p2…pn)  (p1p2…pn)
“It’s not the case that all are true iff one is false.”
• Disjunctional negation:
(p1p2…pn)  (p1p2…pn)
“It’s not the case that one is true iff all are false.”

L3 96
Tautology example (1.2.8.a) Part 2
Demonstrate that
[¬p (p q )]q
is a tautology in two ways:
1. Using a truth table (did above)
2. Using a proof relying on Tables 5 and 6
of Rosen, section 1.2 to derive True
through a series of logical equivalences

L3 97
Tautology by proof
[¬p (p q )]q

L3 98
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive

L3 99
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE

L3 100
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity

L3 101
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE

L3 102
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE
 [¬(¬p) ¬q ]  q DeMorgan

L3 103
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE
 [¬(¬p) ¬q ]  q DeMorgan
 [p  ¬q ]  q Double Negation

L3 104
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE
 [¬(¬p) ¬q ]  q DeMorgan
 [p  ¬q ]  q Double Negation
 p  [¬q q ] Associative

L3 105
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE
 [¬(¬p) ¬q ]  q DeMorgan
 [p  ¬q ]  q Double Negation
 p  [¬q q ] Associative
 p  [q ¬q ] Commutative

L3 106
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE
 [¬(¬p) ¬q ]  q DeMorgan
 [p  ¬q ]  q Double Negation
 p  [¬q q ] Associative
 p  [q ¬q ] Commutative
pT ULE

L3 107
Tautology by proof
[¬p (p q )]q
 [(¬p p)(¬p q)]q Distributive
 [ F  (¬p q)]q ULE
 [¬p q ]q Identity
 ¬ [¬p q ]  q ULE
 [¬(¬p) ¬q ]  q DeMorgan
 [p  ¬q ]  q Double Negation
 p  [¬q q ] Associative
 p  [q ¬q ] Commutative
pT ULE
T Domination

L3 108
CSC211
Discrete Me t h o d s
Lecture 1
Languages and Grammars
Fall 2016
CSC211 - DISCRETE METHODS Spring 2015

Languages and Grammars

1.1 Languages and Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


2. Formal Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3. Formal Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Languages Generat ed by Grammars . . . . . . . . . . . . . . . . . . . . . . 15

1.0 O UTLINE 2
CSC211 - DISCRETE METHODS Spring 2015

1.1 Languages and Grammars


T he grammar of English t ells us whet her a combinat ion of words is a
valid sent ence.
• T he dog runs fast - is a valid sent ence
• dog fast jump - is not a valid sent ence
Unlike nat ural languages, a formal language is specified by a well-defined
set of rules of synt ax. T he valid sent ences of a formal language can be
described by a grammar.
Grammars help us answer two classes of problems:
(1) How can we det ermine whet her a combinat ion of words is a valid
sent ence in a formal language?
(2) How can we generat e t he valid sent ences of a formal language?

1.1 L ANGUAGES AND G RAMMARS 3


CSC211 - DISCRETE METHODS Spring 2015

Example 1.1. T he following grammar generat es a subset of t he English


language:

< sentence> → < noun phrase> < verb phrase>


< noun phrase> → < article> < noun>
< verb phrase> → < verb> < adverb> | < verb>
< article> → < a> | < t he>
< noun> → < cat > | < dog>
< verb> → < runs> | < walks>
< adverb> → < quickly> | < wildly>

We can form a valid sent ence using a sequence of t hese replacement


rules, st opping when no more rules can be applied. T he replacement
rules of a grammar are called product ions.

1.1 L ANGUAGES AND G RAMMARS 4


CSC211 - DISCRETE METHODS Spring 2015

For inst ance, we can perform t he following sequence of replacement s:

< sentence>
→ < noun phrase> < verb phrase>
→ < article> < noun> < verb phrase>
→ < article> < noun> < verb> < adverb>
→ < t he> < noun> < verb> < adverb>
→ < t he> < cat > < verb> < adverb>
→ < t he> < cat > < walks> < adverb>
→ < t he> < cat > < walks> < quickly>

Exercise 1.1. Form all ot her valid sent ences generat ed by t his grammar.

1.1 L ANGUAGES AND G RAMMARS 5


CSC211 - DISCRETE METHODS Spring 2015

2. Formal Languages
• A formal language is a set of finite-lengt h words (or “st rings”)
over some finit e alphabet
• A typical alphabet would be { a, b} , a typical st ring over t hat
alphabet would be ababba, and a typical language over t hat
alphabet cont aining t hat st ring would be t he set of all st rings
which cont ain t he same number of a’s as b’s.
• T he empty word is allowed and is usually denot ed by λ .
• Not e t hat while t he alphabet is a finit e set and every st ring has
finit e lengt h, a language may cont ain an infinit e number of st rings.

1.2 F ORMAL L ANGUAGES 6


CSC211 - DISCRETE METHODS Spring 2015

Some examples of formal languages include


• T he set of all words over { a, b} .
• T he set { an : n is a prime number} .
• T he set of synt act ically correct programs in Java.
• T he set of input s upon which a cert ain Turing machine halt s.

1.2 F ORMAL L ANGUAGES 7


CSC211 - DISCRETE METHODS Spring 2015

Definition 1.1.
• An alphabet (or vocabulary) V is a finit e, non-empty set of
element s called symbols
• A st ring (or word) over an alphabet V is a concat enat ion of a
finit e number of element s of V
• T he empty st ring or null st ring, denot ed by λ , is t he st ring
cont aining no symbols (not e t hat { λ } =I ∅)
• T he set of all words over an alphabet V is denot ed by V ∗
• A language over an alphabet V is a subset of V ∗
Languages can be specified in various ways –
(1) List all t he words in t he language: L = { 0, 00, 000, 0000, 00000} .
(2) Give some crit eria t hat a word must sat isfy: L = { 0n |n ∈ N} .
(3) Using a formal grammar.

1.2 F ORMAL L ANGUAGES 8


CSC211 - DISCRETE METHODS Spring 2015

1.3 Formal Grammars


Formal grammars describe formal languages.
Definition 1.2. A phrase-st ruct ure or Type-0 grammar G = (V, T, S, P)
consist s of
(1) A finit e alphabet V
(2) A subset T ⊆ V of t erminal symbols
(3) A st art symbol S ∈ V \ T
(4) A finit e set P of product ion rules, which t ransform st rings over V
int o st rings over V.

1.3 F ORMAL G RAMMARS 9


CSC211 - DISCRETE METHODS Spring 2015

• P hrase st ruct ure grammars are also called unrest rict ed grammars.
• T he element s of V \ T are called non-t erminal symbols.
• Every product ion in P must cont ain at least one non-t erminal
symbol on it s left side.
• A derivat ion is a sequence of rule applicat ions, st art ing wit h S.
• T he t erminal symbols T correspond t o t he alphabet of t he
associat ed formal language.

1.3 F ORMAL G RAMMARS 10


CSC211 - DISCRETE METHODS Spring 2015

Example 1.2. Consider t he phrase st ruct ure grammar G = (V, T, S, P)


defined by
V = { S, a, b}
T = { a, b}
P = { S → aSb, S → ab}
By applying t he first product ion n − 1 t imes, t hen applying t he second
product ion once

S → aSb → aaSbb → . . . → an− 1 Sbn− 1 → an bn

T he only st rings in t he language generat ed by t his grammar are an bn ,


where n ≥ 1.
Exercise 1.2. Describe t he typical st rings of t he language generat ed by
adding t he product ion rule S → SS t o t he set of product ions P in t he

1.3 F ORMAL G RAMMARS 11


CSC211 - DISCRETE METHODS Spring 2015

above example.

1.3 F ORMAL G RAMMARS 12


CSC211 - DISCRETE METHODS Spring 2015

Definition 1.3.Let G = (V, T, S, P) be a phrase st ruct ure grammar. Let


w0 = xy0 z (i.e. t he concat enat ion of x, y0 and z) and w1 = xy1 z be
st rings (words) over V.
(1) If y0 → y1 is a product ion of G, we say t hat w1 is direct ly derivable
from w0 , and we writ e w0 ⇒ w1 .
(2) If w0 , w1 , . . . , wn are st rings over V such t hat

w0 ⇒ w1 , w1 ⇒ w2 , . . . , wn− 1 ⇒ wn (n ≥ 0)

we say t hat wn is derivable from w0 , and we writ e w ∗0 ⇒ wn .


(3) T he sequence of st eps used t o obt ain wn from w0 is called a
derivat ion.

1.3 F ORMAL G RAMMARS 13


CSC211 - DISCRETE METHODS Spring 2015

Example 1.3. Consider t he phrase st ruct ure grammar G = (V, T, S, P)


defined by
V = { S, a, b}
T = { a, b}
P = { S → aSb, S → ab}
• T he st ring aaSbb is direct ly derivable from aSb, since S → aSb
is a product ion in t he grammar.
• T he st ring aaaabbbb is derivable from aaSbb since
aaSbb ⇒ aaaSbbb ⇒ aaaabbbb, using t he product ions S → aSb
and S → ab in succession.

1.3 F ORMAL G RAMMARS 14


CSC211 - DISCRETE METHODS Spring 2015

1.4 Languages Generated by Grammars


T he language generat ed by a formal grammar G, denot ed by L(G), is
t he set of all st rings over V t hat can be generat ed, st art ing wit h t he
st art symbol, by applying product ion rules unt il no more non-t erminal
symbols are present in t he st ring.

Definition 1.4. Let G = { V, T, S, P} be a phrase st ruct ure grammar.


T he language generat ed by G, denot ed by L(G), is t he set of all st rings
of t erminal symbols t hat are derivable from t he st art symbol S,

L(G) = { w ∈ T ∗ : S∗ ⇒ w}

In t he following two examples we will find t he language generat ed by a


phrase st ruct ure grammar.

1.4 L ANGUAGES G ENERATED BY G RAMMARS 15


CSC211 - DISCRETE METHODS Spring 2015

Example 1.4. Describe t he language generat ed by G = { V, T, S, P}


where
V = { S, A, a, b}
T = { a, b}
P = { S → aA, S → b, A → aa}
Solut ion:
• From t he st art symbol S we derive aA using t he product ion
S → aA.
• We can also use t he product ion S → b t o derive b.
• From aA, t he product ion A → aa can be used t o derive aaa.
• No addit ional words can be derived so we conclude t hat
L(G) = { aaa, b}

1.4 L ANGUAGES G ENERATED BY G RAMMARS 16


CSC211 - DISCRETE METHODS Spring 2015

Example 1.5. Describe t he language generat ed by G = { V, T, S, P}


where
V = { S, 0, 1}
T = { 0, 1}
P = { S → 11S, S → 0}
Solut ion:
• From S we can derive eit her 0 using S → 0 or 11S using
S → 11S.
• From 11S we can derive eit her 110 or 1111S, et c.
• At any st age of derivat ion we can eit her add two 1s at t he end of
t he st ring, or t erminat e by adding a 0 at t he end of t he st ring.
• Hence L(G) = { 12n 0 : n ≥ 0} = { 0, 110, 11110, 1111110, . . .}

1.4 L ANGUAGES G ENERATED BY G RAMMARS 17


CSC211 - DISCRETE METHODS Spring 2015

Example 1.6. Consider t he grammar G = (V, T, S, P) wit h


V = { S, B , a, b, c}
T = { a, b, c}
P = { S → aB Sc, S → abc, B a → aB , B b → bb}

1.4 L ANGUAGES G ENERATED BY G RAMMARS 18


CSC211 - DISCRETE METHODS Spring 2015

Example 1.6. Consider t he grammar G = (V, T, S, P) wit h


V = { S, B , a, b, c}
T = { a, b, c}
P = { S → aB Sc, S → abc, B a → aB , B b → bb}
Some examples of t he derivat ion of st rings in L(G) are:
• S → abc
• S → aB Sc → aB abcc → aaB bcc → aabbcc
• S → aB Sc → aB aB Scc → aB aB abccc → aaB B abccc
→ aaB aB bccc → aaaB B bccc → aaaB bbccc → aaabbbccc
T his grammar defines t he language L(G) = { an bn c n |n > 0} where an
denot es a st ring of n consecut ive a’s.

1.4 L ANGUAGES G ENERATED BY G RAMMARS 19


CSC211 - DISCRETE METHODS Spring 2015

Example 1.7. Find two phrase st ruct ure grammars t hat generat e t he
language L = { 0m 1n : m, n ≥ 0}
Solut ion:
One answer is G1 = { V, T, S, P} where
V = { S, 0, 1}
T = { 0, 1}
P = { S → 0S, S → S1, S → λ }
T he st ring 0m 1n is obt ained by applying t he first product ion m t imes and
t he second product ion n t imes.

1.4 L ANGUAGES G ENERATED BY G RAMMARS 20


CSC211 - DISCRETE METHODS Spring 2015

Anot her answer is G2 = { V, T, S, P} where


V = { S, A, 0, 1}
T = { 0, 1}
P = { S → 0S, S → S1A, S → λ , A → λ }

Exercise 1.3. Check t hat t he grammars G2 does indeed generat e L.

1.4 L ANGUAGES G ENERATED BY G RAMMARS 21


CSC211
Discrete Method
Lecture 2
Classification of Formal Grammars
Fall 2016
CSC211 - DISCRETE METHOD SPRING 2015

Classification of Formal Grammars


2.1 T he Chomsky Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Cont ext -sensit ive grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Cont ext -free grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Regular grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.0 O UTLINE 2
CSC211 - DISCRETE METHOD SPRING 2015

2.1 The Chomsky Hierarchy


Formal grammars can be classified according t o t he types of product ions
t hat are allowed. One such classification scheme is t he Chomsky
hierarchy, first described by Noam Chomsky in 1956.

P hrase st ructure grammars (Type-0)


Context -sensit ive grammars (Type-1)
Cont ext-free grammars (Type-2)
Regular grammars (Type-3)

Fig. 2.1: T he Chomsky hierarchy

2.1 T HE C HOMSKY H IERARCHY 3


CSC211 - DISCRETE METHOD SPRING 2015

As illust rat ed in Figure 2.1,


(1) Every regular grammar is a cont ext -free grammar.
(2) Every cont ext -free grammar is a cont ext -sensit ive grammar.
(3) Every cont ext -sensitive grammar is a phrase st ruct ure grammar.
As we shall see, t he classes of grammar defined in t his scheme
correspond t o classes of languages t hat can be recognised by different
types of aut omat a (machines).

Grammars Languages Automata Production rules


Type-0 Recursively Turing machines Unrestricted
Type-1 enumerable Context- Linear-bounded automata aAb → abc
Type-2 sensitive Context-free Non-deterministic pushdown automata A→c
Type-3 Regular Finite state automata A → aB, A → a

2.1 T HE C HOMSKY H IERARCHY 4


CSC211 - DISCRETE METHOD SPRING 2015

2.2 Context-sensitive grammars


Let |x| be t he number of symbols in t he st ring x. T his is t he lengt h of x.

Definition 2.1.
(1) A formal grammar G = (V, T, S, P) is cont ext -sensit ive or Type-1
if all product ion rules in P are of t he form x → y where x and y
are any two st rings over V such t hat |x| ≤ |y|.
(2) A formal language is cont ext -sensitive if t here is a
cont ext -sensit ive grammar which generat es it .

2.2 C ONTEXT- SENSITIVE GRAMMARS 5


CSC211 - DISCRETE METHOD SPRING 2015

Example 2.1. T he language { 0n 1n 2n : n ≥ 0} is generat ed by t he


grammar G = (V, T, S, P) where
V = { S, A, B , 0, 1, 2}
T = { 0, 1, 2}
P = { S → 0SAB , S → λ , B A → AB ,
0A → 01, 1A → 11, 1B → 12, 2B → 22}
Each of t he product ions of G has at least as many symbols on t he right
as on t he left, so G is cont ext -sensit ive.

2.2 C ONTEXT- SENSITIVE GRAMMARS 6


CSC211 - DISCRETE METHOD SPRING 2015

T he language L(G) cont ains t he word 0n 1n 2n because


S → 0SAB applied n t imes gives S → 0n S(AB ) n
S → λ applied once gives S → 0n (AB ) n
B A → AB applied n t imes gives S → 0n An B n
0A → 01 applied once gives S → 0n 1An− 1 B n 1A
→ 11 applied n − 1 t imes gives S → 0n 1n B n 1B
→ 12 applied once gives S → 0n 1n 2B n− 1 2B
→ 22 applied n − 1 t imes gives S → 0n 1n 2n

2.2 C ONTEXT- SENSITIVE GRAMMARS 7


CSC211 - DISCRETE METHOD SPRING 2015

2.3 Context-free grammars


Definition 2.2.
(1) A formal grammar G = (V, T, S, P) is cont ext -free or Type-2 if
t he left hand side of each product ion consist s of a single
nont erminal symbol., i.e. all product ion rules in P are of t he form
A → x, where A is a single non-t erminal symbol and x is any
st ring consist ing of t erminal and non-t erminal symbols.
(2) A formal language is cont ext -free if t here is a cont ext -free
grammar which generat es it .

2.3 C ONTEXT- FREE GRAMMARS 8


CSC211 - DISCRETE METHOD SPRING 2015

• T he t erm cont ext -free derives from t he fact t hat A can always be
replaced by x, regardless of cont ext in which it occurs.
• Cont ext -free languages are t he t heoret ical basis for t he synt ax of
most programming languages.

Example 2.2. T he language { an bn : n ≥ 0} is generat ed by t he


cont ext -free grammar G = (V, T, S, P) where
V = { S, a, b}
T = { a, b}
P = { S → aSb, S → λ }

Exercise 2.1. Why is t he language L(G) = { 0n 1n 2n : n ≥ 0} of


Example 2.1 not cont ext free?

2.3 C ONTEXT- FREE GRAMMARS 9


CSC211 - DISCRETE METHOD SPRING 2015

Example 2.3. In Example 2.2, if we t ake a and b t o be t he symbols ‘(’


and ‘)’ respect ively, we obt ain t he language of well-nest ed bracket s. T his
is t he language generat ed by t he grammar G1 = (V, T, S, P) where
V = { S, (, )}
T = { (, )}
P = { S → (S), S → λ }
A st ring in L(G1 ) cont ains any number of open bracket s followed by t he
same number of closed bracket s.

2.3 C ONTEXT- FREE GRAMMARS 10


CSC211 - DISCRETE METHOD SPRING 2015

If we add t he product ion rule S → SS t o G1 , we obt ain t he language of


well-formed bracket s. T his is t he language generat ed by t he grammar
G2 = (V, T, S, P) where
V = { S, (, )}
T = { (, )}
P = { S → SS, S → (S), S → λ }
A st ring in L(G2 ) cont ains an equal number of open and closed bracket s
and up t o any point along t he st ring, t here are no more closed bracket s
t han open bracket s.

2.3 C ONTEXT- FREE GRAMMARS 11


CSC211 - DISCRETE METHOD SPRING 2015

2.4 Regular grammars


Definition 2.3. Let G = (V, T, S, P) be a formal grammar, let A, B ∈ V
be non-t erminal symbols and let w ∈ T ∗ be any (possibly empty) st ring
of t erminal symbols.
(1) If all product ion rules in P are of t he form A → wB or A → w,
t hen G is a right linear grammar.
(2) If all product ion rules in P are of t he form A → B w or A → w,
t hen G is a left linear grammar.

2.4 R EGULAR GRAMMARS 12


CSC211 - DISCRETE METHOD SPRING 2015

Example 2.4. T he language L = { 0(10) n : n = 0, 1, 2, . . .} is generat ed


by
• T he right linear grammar G1 = (V, T, S, P) where
V = { S, A, 0, 1}
T = { 0, 1}
P = { S → 0A, A → 10A, A → λ }
• T he left linear grammar G2 = (V, T, S, P) where
V = { S, 0, 1}
T = { 0, 1}
P = { S → S10, S → 0}

2.4 R EGULAR GRAMMARS 13


CSC211 - DISCRETE METHOD SPRING 2015

Definition 2.4.
(1) If G is eit her right or left linear, t hen G is called a regular or
Type-3 grammar.
( 2 ) T he language L(G) generat ed by a regular grammar G is called a
regular language.

Exercise 2.2. Why is t he language L(G) = { an bn : n ≥ 0} of


Example 2.2 not regular?

2.4 R EGULAR GRAMMARS 14


CSC211
Discrete Me t h o d s
Lecture 3
Regular Languages
Fall 2016
CSC211 - DISCRETE METHOD SPRING 2015

Regular Languages

3.1 Kleene closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


3.2 Regular languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 Regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 T he pumping lemma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.0 O UTLINE 2
CSCS211 D ISCRETE M ATHEMATICS II SPRING 2015

3.1 Kleene closure


Definition 3.1. Let V be an alphabet and let L 1 and L 2 be subset s of
V∗. T he concat enat ion of L 1 and L 2 , denot ed by L 1 L 2 , is t he set of all
st rings of t he form xy where x ∈ L 1 and y ∈L 2 .

Example 3.1. Find t he set s L 1 L 2 and L 2 L 1 where L 1 = { 0, 11} and


L 2 = { 1, 10, 110} .
Solut ion:

Not e t hat L 1 L 2 /=
L 2L 1.
3.1 K LEENE CLOSURE 3
CSC211 - DISCRETE METHOD SPRING 2015

3.1 Kleene closure


Definition 3.1. Let V be an alphabet and let L 1 and L 2 be subset s of
V ∗. T he concat enat ion of L 1 and L 2 , denot ed by L 1 L 2 , is t he set of all
st rings of t he form xy where x ∈L 1 and y ∈L 2 .

Example 3.1. Find t he set s L 1 L 2 and L 2 L 1 where L 1 = { 0, 11} and


L 2 = { 1, 10, 110} .
Solut ion:
(1) L 1 L 2 = { 01, 010, 0110, 111, 1110, 11110}

Not e t hat L 1 L 2 /=
L 2L 1.
3.1 K LEENE CLOSURE 3
CSC211 - DISCRETE METHOD SPRING 2015

3.1 Kleene closure


Definition 3.1. Let V be an alphabet and let L 1 and L 2 be subset s of
V ∗. T he concat enat ion of L 1 and L 2 , denot ed by L 1 L 2 , is t he set of all
st rings of t he form xy where x ∈L 1 and y ∈L 2 .

Example 3.1. Find t he set s L 1 L 2 and L 2 L 1 where L 1 = { 0, 11} and


L 2 = { 1, 10, 110} .
Solut ion:
(1) L 1 L 2 = { 01, 010, 0110, 111, 1110, 11110}
(2) L 2 L 1 = { 10, 111, 100, 1011, 1100, 11011}

/ L 2L 1.
Not e t hat L 1 L 2 =

3.1 K LEENE CLOSURE 3


CSC211 - DISCRETE METHOD SPRING 2015

Definition 3.2. T he set L n is defined recursively for n = 0, 1, 2, . . . by

L0 = { λ } and L n+ 1 = for n = 1, 2, . . .
L nL

Definition 3.3. Let V be an alphabet and let L be a subset of V ∗. T he


Kleene closure of L, denot ed by L ∗ is defined t o be

𝐿𝐿∗ = � 𝐿𝐿𝑛𝑛
𝑛𝑛=0

T he Kleene closure L ∗ is t he set of all st rings t hat can be const ruct ed by


concat enat ing an arbit rary number of st rings from L.

3.1 K LEENE CLOSURE 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.2. Let V = { 0, 1} . What are t he Kleene closures of t he


subset s L 1 = { 0} , L 2 = { 0, 1} and L 3 = { 11} of t he set V ∗?
Solut ion:
(1) Every st ring in L ∗1 is t he concat enat ion of t he st ring 0 wit h it self
an arbit rary number of t imes, so L ∗1 = { 0n : n = 0, 1, 2, . . .}
(2) Every st ring in L ∗2 is t he concat enat ion of an arbit rary number of
st rings, where each st ring is eit her 0 or 1. T his is t he set of all
st rings over alphabet V = { 0, 1} so L ∗2 = V ∗
(3) Every st ring in L ∗3 is t he concat enat ion of t he st ring 11 wit h it self
an arbit rary number of t imes, so L ∗3 = { 12n : n = 0, 1, 2, . . .}

3.1 K LEENE CLOSURE 5


CSC211 - DISCRETE METHOD SPRING 2015

2. Regular languages
A regular language L sat isfies t he following equivalent propert ies:
• L can be generat ed by a regular grammar.
• L can be described by a regular expression.
• L can be accept ed by a finit e st at e machine.
• L can be accept ed by a read-only Turing machine.

3.2 R EGULAR LANGUAGES 6


CSC211 - DISCRETE METHOD SPRING 2015

T he collect ion of regular languages over an alphabet V can be defined


recursively as follows:
• T he empty language ∅ is a regular language.
• T he empty st ring language { λ } is a regular language.
• For each a ∈V, t he singlet on language { a} is a regular language.
• If L 1 and L 2 are regular languages, t hen L 1 ∪L 2 , L 1 L 2 and L1∗
are
regular languages.
• No ot her languages over V are regular.

3.2 R EGULAR LANGUAGES 7


CSC211 - DISCRETE METHOD SPRING 2015

3.3 Regular expressions


Definition 3.4. T he regular expressions over a set V are defined
recursively by t he following:
( 1 ) T he symbol ∅is a regular expression.
( 2 ) T he symbol λ is a regular expression.
( 3 ) T he symbol a is a regular expression whenever a ∈V.
(4) T he symbols L 1 ∪L 2 , L 1 L 2 and L1∗ are regular expressions
whenever L 1 and L 2 are regular
expressions.
Definition 3.5. A regular set is a set of st rings described by a regular
expression.

3.3 R EGULAR EXPRESSIONS 8


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.3.
Regular expression Descript ion
a∗ a repeat ed any number of t imes
a∗b∗ Any number of as followed by any number of bs
(abc)d ∗(ca) ∗ abc followed by d any number of t imes fol-
lowed by ca any number of t imes
(a ∪b) ∗(aa) ∗ Any st ring consist ing of as or bs followed by
any even number of as

Every regular expression can be assembled from t he t erminal symbols


and t he t hree connect ives
(1) ‘or’ (2) ‘followed by’ (3) ‘any number of t imes’.
Not e t hat ‘any number of t imes’ includes possibly zero t imes

3.3 R EGULAR EXPRESSIONS 9


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.4. Describe a typical st ring in t he regular set s specified by


t he following regular expressions
Solut ion:
(a) 10∗
(b) (10) ∗
(c) 0 ∪(01)
(d) 0(0 ∪1) ∗
(e) (0∗1) ∗

3.3 R EGULAR EXPRESSIONS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.4. Describe a typical st ring in t he regular set s specified by


t he following regular expressions
Solut ion:
(a) 10∗ A 1 followed by zero or more 0s
(b) (10)∗
(c) 0 ∪ (01)
(d) 0(0 ∪ 1)∗
(e) (0∗ 1)∗

3.3 R EGULAR EXPRESSIONS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.4. Describe a typical st ring in t he regular set s specified by


t he following regular expressions
Solut ion:
(a) 10∗ A 1 followed by zero or more 0s
(b) (10)∗ Any number of copies of 10 (including the null string)
(c) 0 ∪ (01)
(d) 0(0 ∪ 1)∗
(e) (0∗ 1)∗

3.3 R EGULAR EXPRESSIONS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.4. Describe a typical st ring in t he regular set s specified by


t he following regular expressions
Solut ion:
(a) 10∗ A 1 followed by zero or more 0s
(b) (10)∗ Any number of copies of 10 (including the null string)
(c) 0 ∪ (01) The string 0 or the string 01
(d) 0(0 ∪ 1)∗
(e) (0∗ 1)∗

3.3 R EGULAR EXPRESSIONS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.4. Describe a typical st ring in t he regular set s specified by


t he following regular expressions
Solut ion:
(a) 10∗ A 1 followed by zero or more 0s
(b) (10)∗ Any number of copies of 10 (including the null string)
(c) 0 ∪ (01) The string 0 or the string 01
(d) 0(0 ∪ 1)∗ Any string beginning with 0
(e) (0∗ 1)∗

3.3 R EGULAR EXPRESSIONS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.4. Describe a typical st ring in t he regular set s specified by


t he following regular expressions
Solut ion:
(a) 10∗ A 1 followed by zero or more 0s
(b) (10)∗ Any number of copies of 10 (including the null string)
(c) 0 ∪ (01) The string 0 or the string 01
(d) 0(0 ∪ 1)∗ Any string beginning with 0
(e) (0∗ 1)∗ Any string not ending with 0

3.3 R EGULAR EXPRESSIONS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.5. Const ruct a regular expressions t o represent each of t he


following set s of of binary st rings.
( 1 ) T he number of 1s in each st ring is divisible by t hree.
(2) All 1s in each st ring appear in blocks of at least t hree.
(3) For all n ≥ 0, t he symbols at posit ions 3n and 3n + 2 in each
st ring are bot h 0s.
Solut ion:

3.3 R EGULAR EXPRESSIONS 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.5. Const ruct a regular expressions t o represent each of t he


following set s of of binary st rings.
( 1 ) T he number of 1s in each st ring is divisible by t hree.
(2) All 1s in each st ring appear in blocks of at least t hree.
(3) For all n ≥ 0, t he symbols at posit ions 3n and 3n + 2 in each
st ring are bot h 0s.
Solut ion:
(1) (0∗10∗10∗1) ∗0∗

3.3 R EGULAR EXPRESSIONS 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.5. Const ruct a regular expressions t o represent each of t he


following set s of of binary st rings.
( 1 ) T he number of 1s in each st ring is divisible by t hree.
(2) All 1s in each st ring appear in blocks of at least t hree.
(3) For all n ≥ 0, t he symbols at posit ions 3n and 3n + 2 in each
st ring are bot h 0s.
Solut ion:
(1) (0∗10∗10∗1) ∗0∗
(2) 0∗ ∪0∗1111∗ ∪(0∗1111∗0) ∗

3.3 R EGULAR EXPRESSIONS 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.5. Const ruct a regular expressions t o represent each of t he


following set s of of binary st rings.
( 1 ) T he number of 1s in each st ring is divisible by t hree.
(2) All 1s in each st ring appear in blocks of at least t hree.
(3) For all n ≥ 0, t he symbols at posit ions 3n and 3n + 2 in each
st ring are bot h 0s.
Solut ion:
(1) (0∗10∗10∗1) ∗0∗
(2) 0∗ ∪0∗1111∗ ∪(0∗1111∗0) ∗
(3) ((0 ∪1)00) ∗(λ ∪(0 ∪1) ∪(0 ∪1)0)

3.3 R EGULAR EXPRESSIONS 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.6. Const ruct a regular expression t o describe t he language


L(G) generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → 0S, S → 1A, A → 1A, A → 0S, S → 0, S → λ }

3.3 R EGULAR EXPRESSIONS 12


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.6. Const ruct a regular expression t o describe t he language


L(G) generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → 0S, S → 1A, A → 1A, A → 0S, S → 0, S → λ }
Solut ion: St art ing wit h S, we can eit her
(a) use t he first rule t o generat e t he st ring 0S,

3.3 R EGULAR EXPRESSIONS 12


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.6. Const ruct a regular expression t o describe t he language


L(G) generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → 0S, S → 1A, A → 1A, A → 0S, S → 0, S → λ }
Solut ion: St art ing wit h S, we can eit her
(a) use t he first rule t o generat e t he st ring 0S,
(b) use t he second rule, followed by t he t hird rule any number of
t imes, followed by t he fourt h rule t o generat e 11∗0S, or

3.3 R EGULAR EXPRESSIONS 12


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.6. Const ruct a regular expression t o describe t he language


L(G) generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → 0S, S → 1A, A → 1A, A → 0S, S → 0, S → λ }
Solut ion: St art ing wit h S, we can eit her
(a) use t he first rule t o generat e t he st ring 0S,
(b) use t he second rule, followed by t he t hird rule any number of
t imes, followed by t he fourt h rule t o generat e 11∗0S, or
( c ) t erminat e using t he one of t he last two rules.
Because S appears at t he end of t he st rings produced in (a) and (b), we
can apply t hese any number of t imes (in any order) before applying (c)
t o t erminat e. L(G) can t herefore be represent ed by (0 ∪11∗0) ∗(λ ∪0).

3.3 R EGULAR EXPRESSIONS 12


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.7. Const ruct a regular expression t o describe t he language


generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, B , 0, 1} , T = { a, b} and
P = { S → bA, A → bB , B → bB , B → aS, B → a}

3.3 R EGULAR EXPRESSIONS 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.7. Const ruct a regular expression t o describe t he language


generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, B , 0, 1} , T = { a, b} and
P = { S → bA, A → bB , B → bB , B → aS, B → a}
Solut ion:
• St art ing wit h S, we must use t he first rule followed by t he second
rule, which generat es t he st ring bbB .

3.3 R EGULAR EXPRESSIONS 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.7. Const ruct a regular expression t o describe t he language


generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, B , 0, 1} , T = { a, b} and
P = { S → bA, A → bB , B → bB , B → aS, B → a}
Solut ion:
• St art ing wit h S, we must use t he first rule followed by t he second
rule, which generat es t he st ring bbB .
• Next we can use t he t hird rule any number of t imes, which yields
bbb∗B .

3.3 R EGULAR EXPRESSIONS 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.7. Const ruct a regular expression t o describe t he language


generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, B , 0, 1} , T = { a, b} and
P = { S → bA, A → bB , B → bB , B → aS, B → a}
Solut ion:
• St art ing wit h S, we must use t he first rule followed by t he second
rule, which generat es t he st ring bbB .
• Next we can use t he t hird rule any number of t imes, which yields
bbb∗B .
• T hen we can eit her t erminat e using t he final rule, which gives
bbb∗a, or use t he fourt h rule t o yield bbb∗aS t hen it erat e t he first
three rules (as before) any number of t imes.

3.3 R EGULAR EXPRESSIONS 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.7. Const ruct a regular expression t o describe t he language


generat ed by t he grammar G = (V, T, S, P) where
V = { S, A, B , 0, 1} , T = { a, b} and
P = { S → bA, A → bB , B → bB , B → aS, B → a}
Solut ion:
• St art ing wit h S, we must use t he first rule followed by t he second
rule, which generat es t he st ring bbB .
• Next we can use t he t hird rule any number of t imes, which yields
bbb∗B .
• T hen we can eit her t erminat e using t he final rule, which gives
bbb∗a, or use t he fourt h rule t o yield bbb∗aS t hen it erat e t he first
three rules (as before) any number of t imes.
• L(G) can t herefore be represent ed by bbb∗a(bbb∗a) ∗

3.3 R EGULAR EXPRESSIONS 13


CSC211 - DISCRETE METHOD SPRING 2015

4. The pumping lemma


• Every regular language is cont ext -free, but t he converse is not
true.
• T he language L = { an bn : n ∈N} is cont ext -free. How can we
show t hat it ’s not regular?
• A pumping lemma for a given class of languages is a st at ement
that any sufficiently long st ring in t he language can be broken int o
pieces t hat can be repeat ed (pumped) t o produce an even longer
st ring in t he language.
• To prove t hat a given language is not regular we use t he pumping
lemma for regular languages.

3.4 T HE PUMPING LEMMA 14


CSC211 - DISCRETE METHOD SPRING 2015

Lemma 3.1 (The pumping lemma for regular languages). If L is a


regular language, t hen t here exist s a number N > 0 such t hat every
st ring x ∈L wit h |x| ≥ N can be writ t en in t he form x = uvw where
|uv| ≤ N, |v| ≥ 1 and uv k w ∈L for every k ≥ 0.

3.4 T HE PUMPING LEMMA 15


CSC211 - DISCRETE METHOD SPRING 2015

Example 3.8. Show t hat L = { an bn : n = 0, 1, . . .} is not regular.


Solut ion: We use t he met hod of proof by cont radict ion. First we
suppose t hat L is regular, t hen obt ain a cont radict ion t o show t hat t his
assumpt ion must be false.
• Let N be t he number assert ed t o exist by t he pumping lemma and
let n > 2N.
• By t he pumping lemma, for any x ∈L with |x | = n, we may write
x = uv w where |uv | ≤ N, |v | ≥ 1 and uv k w ∈L for all k ≥ 0. In
part icular, uv 2 w ∈L.
• Since |x | > 2N and |uv | ≤ N, the string uv contains only a’s. In
part icular, the st ring v cont ains only a’s. T hus, because |v | ≥ 1,
t he st ring uv 2 w cont ains more a’s t han b’s so uv 2 w ∈
/ L.
• T his is a cont radict ion, so our original assumpt ion was false and
we conclude t hat L is not regular

3.4 T HE PUMPING LEMMA 16


CSC211 - DISCRETE METHOD SPRING 2015

3.4.1 The pumping lemma for context-free languages


Lemma 3.2 (The pumping lemma for context-free languages). If L
is a cont ext -free language, t hen t here exist s a number N > 0 such t hat
every st ring x ∈L wit h |x| ≥ N can be writ t en in t he form x = uvwyz
where |vwy| ≤ N, |vw| ≥ 1 and uv k wy k z ∈L for every k ≥ 0.

Exercise 3.1. Show t hat t he language L = { an bn c n : n > 0} is not


cont ext -free.

3.4 T HE PUMPING LEMMA 17


CSC211
Discrete Me t h o d s
Lecture 5
Finite State Automata I
Fall 2016
CSC211 - DISCRETE METHOD SPRING 2015

Finite State Automata I


5.1 Finit e St at e Aut omat a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.2 Vending machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.3 Binary adder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.4 Coding errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5.0 O UTLINE 2
CSC211 - DISCRETE METHOD SPRING 2015

5.1 Finite State Automata


A finit e-st ate machine (FSM) or finit e-stat e aut omat on (FSA) is an
abst ract machine having
(1) A finit e set of st at es. T hese carry no furt her st ruct ure and provide
a simple form of memory.
(2) A st art st at e and a set of final st at es.
(3) A finit e set of input symbols (alphabet )
(4) A finit e set of t ransit ion rules which specify how t he machine,
when in a part icular st at e, responds t o a part icular input symbol.
T he response may be t o change st at e and/ or produce an out put
(act ion).

5.1 F INITE S TATE AUTOMATA 3


CSC211 - DISCRETE METHOD SPRING 2015

• A FSA processes an input st ring over it s alphabet . Each symbol is


processed in t urn.
• T he FSA st art s in it s init ial st at e and uses t he t ransit ion rules t o
det ermine t he next st at e and out put (if any) from it s current
st at e and t he symbol just read.
• If t here is no rule defined for t he current st at e and input symbol,
t he machine halt s.
• If t he ent ire st ring has been processed and t he machine is in a
final st at e, t he FSA is said t o accept t he st ring.
• T he set of st rings accept ed by a FSA is t he language recognised
by t he FSA.

5.1 F INITE S TATE AUTOMATA 4


CSC211 - DISCRETE METHOD SPRING 2015

T here are several types of finit e st at e aut omat a:


(1) Accept ors produce a “yes” or “no” answer (t hey eit her accept t he
input or do not ).
(2) Recognizers cat egorise t he input .
(3) Transducers are used t o generat e an out put from a given input .
• Mealy machines have out put s (act ions) associat ed wit h
t ransit ions.
• Moore machines have out put s (act ions) associat ed wit h
st at es.

5.1 F INITE S TATE AUTOMATA 5


CSC211 - DISCRETE METHOD SPRING 2015

Finit e st at e machines can be represent ed by direct ed graphs called st at e


diagrams.
• St at es are represent ed by t he nodes of t he graph. Final st at es (if
any) are represent ed by double circles.
• Transit ions are represent ed by t he edges of t he graph. Each edge
is labelled wit h t he associat ed input symbol.
• Out put s (if any) are marked on t he edges (Mealy machines) or
t he nodes (Moore machines).

5.1 F INITE S TATE AUTOMATA 6


CSC211 - DISCRETE METHOD SPRING 2015

Definition 5.1. A finit e-st ate aut omat on or finit e-state machine
M = (S, I, f , s0 , F ; O, g) consist s of
(1) A finit e set of st at es S ,
(2) A finit e input alphabet I,
(3) A t ransit ion funct ion f : S × I → S which assigns a new st at e t o
(st at e, input ) pairs,
(4) An init ial st at e s0 ∈S ,
(5) A set of final st at es F ⊂ S ,
(6) A finit e out put alphabet O,
(7) An out put funct ion g : S × I → O which assigns an out put t o
(st at e, input ) pairs.

Not e t hat f and g are part ial funct ions – t hey need not be defined for

5.1 F INITE S TATE AUTOMATA 7


CSC211 - DISCRETE METHOD SPRING 2015

every (st at e, input ) pair.

5.1 F INITE S TATE AUTOMATA 8


CSC211 - DISCRETE METHOD SPRING 2015

5.2 Vending machine


Example 5.1. A vending machine accept s 10p and 20p coins. When a
t ot al of 30p has been deposit ed, t he machine immediat ely ret urns t he
amount in excess of 30p. When 30p has been deposit ed and any excess
has been refunded, t he cust omer can push a red but t on t o receive a
Mars bar or a blue but t on t o receive a Twix.

We can describe how t he machine works by specifying


( 1 ) t he st at es of t he machine
(2) how t he machine changes st at e when input is received
( 3 ) t he out put t hat is produced for every combinat ion of input and
current st at e

5.2 V ENDING MACHINE 9


CSC211 - DISCRETE METHOD SPRING 2015

To complet ely describe t he machine, we specify t he st at e change and


out put t hat result s from each combinat ion of st at e and input .
• T he machine can be in any of four different st ates si (i= 0,1,2,3)
where si is t he st at e where t he machine has collect ed i × 10p.
• T he machine st art s in st at e s0 wit h 0p received.
• T he possible input s are 10p, 20p, t he red but t on (R) and t he blue
but t on (B ).
• T he possible out put s are 10p, 20p, a Mars bar (M) and a Twix
(T ).

5.2 V ENDING MACHINE 10


CSC211 - DISCRETE METHOD SPRING 2015

T he vending machine can be described by it s t ransit ion and out put


funct ions:
Table of next states Table of outputs
current Input current Input
state state
10 20 R B 10 20 R B
s0 s1 s2 s0 s0 s0
s1 s2 s3 s1 s1 s1
s2 s3 s3 s2 s2 s2 10
s3 s3 s3 s0 s0
s3 10 20 M T

T he machine can also be described using a st at e diagram

5.2 V ENDING MACHINE 11


CSC211 - DISCRETE METHOD SPRING 2015

s1

st art s0 s3

s2

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

s1
(10, )

st art s0 s3

(20, )
s2

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

s1
(R, ) (10, )

st art s0 s3

(B , ) (20, )
s2

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

s1
(R, ) (10, )

st art s0 s3
(10, )

(B , ) (20, )
s2

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

s1
(R, ) (10, )
(20, )

st art s0 s3
(10, )

(B , ) (10, )
(20, )
s2 (20, 10)

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

(R, )
(B , )
s1
(R, ) (10, )
(20, )

st art s0 s3
(10, )

(B , ) (10, )
(20, )
s2 (20, 10)

(R, )
(B , )

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

(R, )
(B , )
s1
(R, ) (10, )
(20, )

st art s0 s3 (10, 10)


(10, )
(20, 20)

(B , ) (10, )
(20, )
s2 (20, 10)

(R, )
(B , )

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

(R, )
(B , )
s1 (R,M)
(R, ) (10, )
(20, )

st art s0 s3 (10, 10)


(10, )
(20, 20)

(B , ) (10, )
(20, )
s2 (20, 10)
(B ,T )
(R, )
(B , )

5.2 V ENDING MACHINE 12


CSC211 - DISCRETE METHOD SPRING 2015

5.3 Binary adder


Example 5.2. Const ruct a finite-state machine t hat adds two int egers
using t heir binary represent at ions.
Solut ion: Let x = (xn xn− 1 . . . x1 x0 ) and y = (yn yn− 1 . . . y1 y0 )
represent two binary numbers where xi , yi ∈{ 0, 1} . We proceed as
follows:
(1) Add t he bit s x0 and y0 , producing a sum bit z0 and a carry bit c0 .
(2) Add t he bit s x1 , y1 along wit h t he carry bit c0 , producing a sum
bit z1 and a carry bit c1 .
(3) Repeat t his procedure unt il t he nt h st ep, where we add xn , yn and
t he carry bit cn− 1 t o produce t he sum bit zn and t he carry bit cn .

5.3 B INARY ADDER 13


CSC211 - DISCRETE METHOD SPRING 2015

For simplicity, we assume t hat xn and yn are bot h 0 (so t hat we can
avoid making special arrangement s concerning t he sum bit zn+ 1 ).
A finit e st at e machine t o perform t his addit ion can t hen be const ruct ed
using only two st at es.
( 1 ) T he st art st at e s0 is used t o remember t hat t he carry bit is 0.
( 2 ) T he ot her st at e s1 is used t o remember t hat t he carry bit is 1.

Since t he input s t o t he machine are pairs of bit s, t here are four possible
input s. We represent t hese by
(1) 00 (when bot h bit s are 0),
(2) 01 (when t he first bit is 0 and t he second bit is 1),
(3) 10 (when t he first bit is 1 and t he second bit is 0),
(4) 11 (when both bits are 1).

5.3 B INARY ADDER 14


CSC211 - DISCRETE METHOD SPRING 2015

s0 s1

st art

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(11, 0)

s0 s1

st art

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(11, 0)

s0 s1

(00, 1)
st art

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(01, 1)
(11, 0)

s0 s1

(00, 1)
st art

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(01, 1)
(11, 0)

(00, 0) s0 s1

(00, 1)
st art

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(01, 1)
(11, 0)

(00, 0) s0 s1

(00, 1)
st art (10, 1)

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(01, 1) (01, 0)
(11, 0)

(00, 0) s0 s1

(00, 1)
st art (10, 1)

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(01, 1) (01, 0)
(11, 0)

(00, 0) s0 s1 (11, 1)

(00, 1)
st art (10, 1)

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

(01, 1) (01, 0)
(11, 0)

(00, 0) s0 s1 (11, 1)

(00, 1)
st art (10, 1) (10, 0)

Fig. 5.2: FSM for adding two binary numbers

5.3 B INARY ADDER 15


CSC211 - DISCRETE METHOD SPRING 2015

5.4 Coding errors


Example 5.3. In cert ain coding scheme, when t hree consecut ive 1s
appear in a message, t he receiver of t he message knows t hat t here has
been a t ransmission error. Const ruct a finite-state machine t hat out put s
a 1 if and only if t he previous t hree input s are all 1s.

Solut ion: We need t hree st at es:


( 1 ) T he st art st at e s0 remembers t hat t he previous input value (if it
exist s) was not a 1.
( 2 ) T he st at es s1 remembers t hat t he previous input was a 1, but t he
input before t he previous input (if it exist s) was not a 1.
( 3 ) T he st at e s2 remembers t hat t he previous two input s were 1s.

5.4 C ODING ERRORS 16


CSC211 - DISCRETE METHOD SPRING 2015

st art s0 s1 s2

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211 - DISCRETE METHOD SPRING 2015

(0,0)

st art s0 s1 s2

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211 - DISCRETE METHOD SPRING 2015

(0,0)

(1,0)
st art s0 s1 s2

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211 - DISCRETE METHOD SPRING 2015

(0,0)

(1,0)
st art s0 s1 s2
(0,0)

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211 - DISCRETE METHOD SPRING 2015

(0,0)

(1,0)
st art (1,0)
s0 s1 s2
(0,0)

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211 - DISCRETE METHOD SPRING 2015

(0,0)

(1,0)
st art (1,0)
s0 s1 s2
(0,0)

(0,0)

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211 - DISCRETE METHOD SPRING 2015

(0,0)

(1,0)
st art (1,0)
s0 s1 s2 (1,1)
(0,0)

(0,0)

Fig. 5.3: A FSA t o det ect coding errors

• T his machine is an example of a language recogniser.

5.4 C ODING ERRORS 17


CSC211
Discrete Me t h o d s
Lecture 6
Finite State Automata II
Fall 2016
CSC211 - DISCRETE METHOD SPRING 2015

Finite State Automata II


6.1 Language Recognit ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
6.2 Nondet erminist ic Finit e St at e Aut omat a . . . . . . . . . . . . . . . . . 10

6.0 O UTLINE 2
CSC211 - DISCRETE METHOD SPRING 2015

1. Language Recognition
• One of t he most import ant applicat ions of finite st at e aut omat a is
in language recognit ion.
• T his applicat ion plays a fundament al role in t he design and
const ruct ion of compilers for programming languages.
• Language recognit ion is a (binary) decision problem.
• Inst ead of producing out put , t he machines have a set of final
st at es.
• A st ring is accept ed or recognised by t he machine if and only if it
takes t he st art ing st at e t o one of it s final st at es.

6.1 L ANGUAGE R ECOGNITION 3


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1
s1 s0 s2
st art
s2 s0 s0 s0 s3
s3 s2 s1
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0
s1 s0 s2
st art
s2 s0 s0 s0 s3
s3 s2 s1
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0 1
s1 s0 s2
st art
s2 s0 s0 s0 s3
s3 s2 s1
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0 1
s1 s0 s2 0
st art
s2 s0 s0 s0 s3
s3 s2 s1
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0 1
s1 s0 s2 0
st art
s2 s0 s0 s0 1 s3
s3 s2 s1
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0 1
s1 s0 s2 0
st art
s2 s0 s0 s0 1 s3
s3 s2 s1
0, 1
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0 1
s1 s0 s2 0
st art
s2 s0 s0 s0 1 s3
s3 s2 s1
0, 1 0
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.1. Const ruct t he st at e diagram for t he finit e-state


aut omat on M = (S, I, f , s0 , F ), where
S = { s0 , s1 , s2 , s3 } , I = { 0, 1} , F = { s0 , s 1 } (6.1)
and f is defined in Table 6.1.
Solut ion:
State Input 0 Input 1 s1
s0 s0 s1 0 1 1
s1 s0 s2 0
st art
s2 s0 s0 s0 1 s3
s3 s2 s1
0, 1 0
s2
Table 6.1: Transit ion funct ion f

6.1 L ANGUAGE R ECOGNITION 4


CSC211 - DISCRETE METHOD SPRING 2015

T he t ransit ion funct ion f can be ext ended from single input symbols t o
st rings of input symbols:
• Let x = x1 x2 x3 . . . xk be a st ring in I ∗.
• Let f (s1 , x) denot e t he st at e obt ained by using each successive
symbol of x (from left t o right ) as input , st art ing wit h st at e s1 .
• From s1 we go t o st at e s2 = f (s1 , x1 ), t hen t o st at e s3 = f (s2 , x2 )
and so on, unt il we reach f (s1 , x) = f (sk , xk ).

6.1 L ANGUAGE R ECOGNITION 5


CSC211 - DISCRETE METHOD SPRING 2015

Definition 6.1. Let M = (S, I, f , s0 , F ) be a (det erminist ic) finite-state


aut omat on.
(1) A st ring x is said t o be recognised (or accept ed) by M if it t akes
t he init ial st at e s0 t o a final st at e, i.e. f (s0 , x) is a st at e in F .
( 2 ) T he set of all st rings t hat are recognised by M is called t he
language recognised or accept ed by M, and is denot ed by L(M).
( 3 ) Two finit e-stat e aut omat a are called equivalent if t hey recognise
t he same language.

6.1 L ANGUAGE R ECOGNITION 6


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.2. Ident ify t he language recognised by t he finit e-state


aut omat a M1 , M2 and M3 shown below.
1 0, 1

st art 0
s0 s1

Fig. 6.1: Aut omat on M1

Solut ion:

6.1 L ANGUAGE R ECOGNITION 7


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.2. Ident ify t he language recognised by t he finit e-state


aut omat a M1 , M2 and M3 shown below.
1 0, 1

st art 0
s0 s1

Fig. 6.1: Aut omat on M1

Solut ion:
• T he only final st at e of M1 is s0 .

6.1 L ANGUAGE R ECOGNITION 7


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.2. Ident ify t he language recognised by t he finit e-state


aut omat a M1 , M2 and M3 shown below.
1 0, 1

st art 0
s0 s1

Fig. 6.1: Aut omat on M1

Solut ion:
• T he only final st at e of M1 is s0 .
• T he st rings t hat t ake s0 t o it self are t hose consist ing of zero or
more consecut ive ones.

6.1 L ANGUAGE R ECOGNITION 7


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.2. Ident ify t he language recognised by t he finit e-state


aut omat a M1 , M2 and M3 shown below.
1 0, 1

st art 0
s0 s1

Fig. 6.1: Aut omat on M1

Solut ion:
• T he only final st at e of M1 is s0 .
• T he st rings t hat t ake s0 t o it self are t hose consist ing of zero or
more consecut ive ones.
• Hence L(M1 ) = { 1n : n = 0, 1, 2, . . .} = 1∗

6.1 L ANGUAGE R ECOGNITION 7


CSC211 - DISCRETE METHOD SPRING 2015

s1
0 0
st art
s0 1 s3 0, 1
1
0, 1
s2

Fig. 6.2: Aut omat on M2

Solut ion:

6.1 L ANGUAGE R ECOGNITION 8


CSC211 - DISCRETE METHOD SPRING 2015

s1
0 0
st art
s0 1 s3 0, 1
1
0, 1
s2

Fig. 6.2: Aut omat on M2

Solut ion:
• T he only final st at e of M2 is s2 .

6.1 L ANGUAGE R ECOGNITION 8


CSC211 - DISCRETE METHOD SPRING 2015

s1
0 0
st art
s0 1 s3 0, 1
1
0, 1
s2

Fig. 6.2: Aut omat on M2

Solut ion:
• T he only final st at e of M2 is s2 .
• T he st rings t hat t ake s0 t o s2 are 1 and 01.

6.1 L ANGUAGE R ECOGNITION 8


CSC211 - DISCRETE METHOD SPRING 2015

s1
0 0
st art
s0 1 s3 0, 1
1
0, 1
s2

Fig. 6.2: Aut omat on M2

Solut ion:
• T he only final st at e of M2 is s2 .
• T he st rings t hat t ake s0 t o s2 are 1 and 01.
• Hence L(M2 ) = { 1, 01}

6.1 L ANGUAGE R ECOGNITION 8


CSC211 - DISCRETE METHOD SPRING 2015

0 s2 0, 1
1
st art 1
s0 s1
0
s3 0, 1

Fig. 6.3: Aut omat on M3

Solut ion:

6.1 L ANGUAGE R ECOGNITION 9


CSC211 - DISCRETE METHOD SPRING 2015

0 s2 0, 1
1
st art 1
s0 s1
0
s3 0, 1

Fig. 6.3: Aut omat on M3

Solut ion:
• T he only st rings t hat t ake s0 t o it self are λ , 0, 00, . . ., i.e. any
st ring of zero or more consecut ive 0s.

6.1 L ANGUAGE R ECOGNITION 9


CSC211 - DISCRETE METHOD SPRING 2015

0 s2 0, 1
1
st art 1
s0 s1
0
s3 0, 1

Fig. 6.3: Aut omat on M3

Solut ion:
• T he only st rings t hat t ake s0 t o it self are λ , 0, 00, . . ., i.e. any
st ring of zero or more consecut ive 0s.
• T he only st rings t hat t ake s0 t o s3 are t hose consist ing of zero or
more consecut ive 0s, followed by 10, followed by any st ring.

6.1 L ANGUAGE R ECOGNITION 9


CSC211 - DISCRETE METHOD SPRING 2015

0 s2 0, 1
1
st art 1
s0 s1
0
s3 0, 1

Fig. 6.3: Aut omat on M3

Solut ion:
• T he only st rings t hat t ake s0 t o it self are λ , 0, 00, . . ., i.e. any
st ring of zero or more consecut ive 0s.
• T he only st rings t hat t ake s0 t o s3 are t hose consist ing of zero or
more consecut ive 0s, followed by 10, followed by any st ring.
• Hence L(M3 ) = 0∗ ∪0∗10(0 ∪1) ∗

6.1 L ANGUAGE R ECOGNITION 9


CSC211 - DISCRETE METHOD SPRING 2015

6.2 Nondeterministic Finite State Automata


Definition 6.2. (1) A det erminist ic finite-state aut omat on (DFSA) is
a FSA whose t ransit ion funct ion assigns a single st at e t o each
(st at e, input ) pair,
f :S×I→S
(2) A nondet erminist ic finit e-state aut omat on (NFSA) is a FSA whose
t ransit ion funct ion assigns a set of st at es t o (st at e, input ) pairs,

f : S × I → P (S )

where P (S ) is t he set of all subset s of S (i.e. t he power set of S ).

Not e: Nondet erminist ic FSA can also have t ransit ion rules of t he form
f (s1 , λ ) = { s3 } . T hese are called λ -t ransit ions.

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .

Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3
s1 s0 s1 , s3
st art
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1

Fig. 6.4: T he st at e t able s2

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .

Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3 0
0
s1 s0 s1 , s3
st art
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1

Fig. 6.4: T he st at e t able s2

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .

Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3 0
0
s1 s0 s1 , s3
st art 1
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1

Fig. 6.4: T he st at e t able s2

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .

Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3 0
0
s1 s0 s1 , s3
st art 0 1
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1

Fig. 6.4: T he st at e t able s2

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .
1
Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3 0
0
s1 s0 s1 , s3
st art 0 1 1
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1

Fig. 6.4: T he st at e t able s2

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .
1
Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3 0
0
s1 s0 s1 , s3
st art 0 1 1
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1
1
Fig. 6.4: T he st at e t able s2

1
6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11
CSC211 - DISCRETE METHOD SPRING 2015

Example 6.3. Find t he st at e diagram for t he NFSA whose st at e t able is


shown in Table 6.4. T he final st at es are s2 and s3 .
1
Solut ion:
State Input 0 Input 1
s1
s0 s0 , s1 s3 0 0, 1
0
s1 s0 s1 , s3
st art 0 1 1
s2 s0 , s2 s0 s3
s3 s0 , s1 , s2 s1 0
1 0
Fig. 6.4: T he st at e t able s2

1
6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 11
CSC211 - DISCRETE METHOD SPRING 2015

What does it mean for a nondet erminist ic finite-state aut omat on t o


recognise a st ring x = x0 x1 x2 . . . xk ?
(1) T he first input symbol x0 t akes t he st art ing st at e s0 t o a set of
st at es s 1∗.
(2) T he next input symbol x1 t akes each of t he st at es in s ∗1 t o a set of
st at es. Let s 2∗ be t he union of t hese set s.
(3) We cont inue t his process, including at each st age all st at es
obt ained (via t he t ransit ion funct ion) from a st at e obt ained at t he
previous st age and t he current input symbol.

Definition 6.3. A st ring x is said t o be recognised or accept ed by t he


NFSA M = (S, I, f , s0 , F ) if t he set of all st at es t hat can be obt ained
from s0 using x cont ains a final st at e. T he language recognised by a
NFSA is t he set of all st rings recognised by t his aut omat on.

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 12


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.4. Find t he language recognised by t he nondet erminist ic


finit e-st ate aut omat on M shown below.

s1
a a
st art
s0 λ s3
b b
s2

Solut ion:

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.4. Find t he language recognised by t he nondet erminist ic


finit e-st ate aut omat on M shown below.

s1
a a
st art
s0 λ s3
b b
s2

Solut ion:
• s3 can be reached by t he st rings aa and bb.

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.4. Find t he language recognised by t he nondet erminist ic


finit e-st ate aut omat on M shown below.

s1
a a
st art
s0 λ s3
b b
s2

Solut ion:
• s3 can be reached by t he st rings aa and bb.
• s3 can also be reached by t he st ring aλ b, i.e. ab.

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 6.4. Find t he language recognised by t he nondet erminist ic


finit e-st ate aut omat on M shown below.

s1
a a
st art
s0 λ s3
b b
s2

Solut ion:
• s3 can be reached by t he st rings aa and bb.
• s3 can also be reached by t he st ring aλ b, i.e. ab.
• Hence L(M) = { aa, bb, ab}

6.2 N ONDETERMINISTIC F INITE S TATE AUTOMATA 13


CSC211
Discrete Me t h o d s
Lecture 7
Kleene’s Theorem
Fall 2016
CSC211 - DISCRETE METHOD SPRING 2015

Kleene’s Theorem
7.1 Kleene’s T heorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
7.2 Regular Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
7.3 Kleene’s T heorem and t he P umping Lemma . . . . . . . . . . . . . . 13

7.0 O UTLINE 2
CSC211 - DISCRETE METHOD SPRING 2015

1. Kleene’s Theorem
• A st ring is accept ed or recognised by a FSA if and only if it t akes
t he st art ing st at e t o one of it s final st at es.
• Kleene’s t heorem says t hat finit e st at e aut omat a are precisely
t hose machines t hat recognise regular expressions.

Theorem 7.1 (Kleene’s theorem). A language is regular if and only if


it ’s recognised by a finit e st at e aut omat on.

We can use t he met hod of proof by induct ion t o show t hat every regular
language can be recognised by a finit e st at e aut omat on.

7.1 K LEENE ’ S T HEOREM 3


CSC211 - DISCRETE METHOD SPRING 2015

Recall t hat t he regular languages over an alphabet V are defined


induct ively by:
• ∅is a regular language.
• { λ } is a regular language.
• { a} is a regular language for every a ∈ V.
• If L1 and L2 are regular languages, t hen L1 ∪ L2 , L1 L2 and L∗1 are
regular languages.
• No ot her languages over V are regular.
T he next diagram shows t hat ∅, { λ } and { a} can all be recognised by
finit e st at e aut omat a.

7.1 K LEENE ’ S T HEOREM 4


CSC211 - DISCRETE METHOD SPRING 2015

st art
s0

st art λ
s0 s1

st art a
s0 s1

Let L1 and L2 be two regular languages, recognised by t he FSA


M1 = (S 1 , I, f1 , s1 , F1 ) and M2 = (S 2 , I, f2 , s2 , F2 ) respect ively (not e t hat
s1 is t he init ial st at e of M1 and s2 is t he init ial st at e of M2 ).
We suppose t hat S 1 ∩ S 2 = ∅(st at es can be re-labelled if necessary).

7.1 K LEENE ’ S T HEOREM 5


CSC211 - DISCRETE METHOD SPRING 2015

( 1 ) T he union L1 ∪ L2 is recognised by Mu = (S u , I, fu , su , Fu ) where


• S u = S 1 ∪ S 2 ∪ { su } and su is a new (init ial) st at e.
• fu = f1 ∪ f2 ∪ { f (su , λ ) = s1 } ∪ { f (su , λ ) = s2 }
• Fu = F1 ∪ F2

s1 s1j s1F M1
λ
st art
su

λ
s2 s2j s2F M2

7.1 K LEENE ’ S T HEOREM 6


CSC211 - DISCRETE METHOD SPRING 2015

( 2 ) T he concat enat ion L1 L2 is recognised by Mc = (S c , I, fc , sc , Fc )


where
• Sc = S1 ∪ S2
• sc = s1
• fc = f1 ∪ f2 ∪ { f (s1F , λ ) = s2 : s1F ∈ F1 }
• Fc = F2

s1F s2F
λ
st art
s1 s2

λ
s1G s2G
M1 M2

7.1 K LEENE ’ S T HEOREM 7


CSC211 - DISCRETE METHOD SPRING 2015

(3) T he Kleene closure L∗1 is recognised by Mk = (S k , I, fk , sk , Fk ) where


• S k = S 1 ∪ { sk } where sk is a new init ial st at e
• fk = f1 ∪ { f (sk , λ ) = s1 } ∪ { f (s1F , λ ) = sk : s1F ∈ F1 }
• F c = { sk }
λ

s1i s1F

st art
sk s1
λ

s1j s1G

7.1 K LEENE ’ S T HEOREM 8


CSC211 - DISCRETE METHOD SPRING 2015

7.2 Regular Grammars


T he language L(G) generat ed by a regular grammar G = (V, T, S, P ) can
be recognised by t he finite st at e aut omat on M = (S, I, f , s0 , F ) where:
• S cont ains a st at e sA for each nont erminal symbol A of G, and an
addit ional st at e sF which is a final st at e of M.
• I is equal t o t he set of t erminal symbols T of G..
• s0 is t he st at e corresponding t o t he st art symbol S of G.
• T he t ransit ion rules f are defined by
• f (sA , a) = sB if A → aB is a product ion of G.
• f (sA , a) = sF if A → a is a product ion of G.
• F cont ains sF and also cont ains s0 if S → λ is a product ion of G.
It is easy t o see t hat L(M) is equal t o L(G).

7.2 R EGULAR G RAMMARS 9


CSC211 - DISCRETE METHOD SPRING 2015

Example 7.1. Const ruct a finite st at e aut omat on t hat recognises t he


language generat ed by t he regular grammar G = (V, T, S, P ), where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → λ , S → 1A, S → 0, A → 0A, A → 1A, A → 1}

Solut ion:
sA

st art • s0 corresponds t o S
s0 • sA corresponds t o A
• sF is t he final st at e.
sF

7.2 R EGULAR G RAMMARS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 7.1. Const ruct a finite st at e aut omat on t hat recognises t he


language generat ed by t he regular grammar G = (V, T, S, P ), where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → λ , S → 1A, S → 0, A → 0A, A → 1A, A → 1}

Solut ion:
sA
1
st art • s0 corresponds t o S
s0 • sA corresponds t o A
• sF is t he final st at e.
sF

7.2 R EGULAR G RAMMARS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 7.1. Const ruct a finite st at e aut omat on t hat recognises t he


language generat ed by t he regular grammar G = (V, T, S, P ), where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → λ , S → 1A, S → 0, A → 0A, A → 1A, A → 1}

Solut ion:
sA
1
st art • s0 corresponds t o S
s0 • sA corresponds t o A
0 • sF is t he final st at e.
sF

7.2 R EGULAR G RAMMARS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 7.1. Const ruct a finite st at e aut omat on t hat recognises t he


language generat ed by t he regular grammar G = (V, T, S, P ), where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → λ , S → 1A, S → 0, A → 0A, A → 1A, A → 1}

Solut ion:
sA (0,1)
1
st art • s0 corresponds t o S
s0 • sA corresponds t o A
0 • sF is t he final st at e.
sF

7.2 R EGULAR G RAMMARS 10


CSC211 - DISCRETE METHOD SPRING 2015

Example 7.1. Const ruct a finite st at e aut omat on t hat recognises t he


language generat ed by t he regular grammar G = (V, T, S, P ), where
V = { S, A, 0, 1} , T = { 0, 1} and
P = { S → λ , S → 1A, S → 0, A → 0A, A → 1A, A → 1}

Solut ion:
sA (0,1)
1
st art • s0 corresponds t o S
s0 1 • sA corresponds t o A
0 • sF is t he final st at e.
sF

7.2 R EGULAR G RAMMARS 10


CSC211 - DISCRETE METHOD SPRING 2015

T he language L(M) recognised by a FSA M = (S, I, f , s0 , F ) (where s0 is


never t he next st at e of any t ransit ion) can be generat ed by t he regular
grammar G = (V, T, S, P ) where
• T he nont erminal symbols As ∈ V correspond t o t he st at es s ∈ S .
• T he t erminal symbols a ∈ T correspond t o t he input s a ∈ I.
• T he st art symbol S corresponds t o t he st art st at e s0 .
• T he set of product ions P is defined by
• As → aAt is a product ion of G if f (s, a) = t
• As → a is a product ion of G if f (s, a) ∈ F
• S → λ is a product ion of G if λ ∈ L(M)
It is easy t o see t hat L(G) is equal t o L(M).

7.2 R EGULAR G RAMMARS 11


CSC211 - DISCRETE METHOD SPRING 2015

s1 0
Example 7.2. Find a 1
regular grammar t hat st art
generates the regular set s0 0 1
recognised by this finite 0
st at e aut omat on. s2 1

7.2 R EGULAR G RAMMARS 12


CSC211 - DISCRETE METHOD SPRING 2015

s1 0
Example 7.2. Find a 1
regular grammar t hat st art
generates the regular set s0 0 1
recognised by this finite 0
st at e aut omat on. s2 1

V = { S, A, B , 0, 1}

7.2 R EGULAR G RAMMARS 12


CSC211 - DISCRETE METHOD SPRING 2015

s1 0
Example 7.2. Find a 1
regular grammar t hat st art
generates the regular set s0 0 1
recognised by this finite 0
st at e aut omat on. s2 1

V = { S, A, B , 0, 1}
T = { 0, 1}

7.2 R EGULAR G RAMMARS 12


CSC211 - DISCRETE METHOD SPRING 2015

s1 0
Example 7.2. Find a 1
regular grammar t hat st art
generates the regular set s0 0 1
recognised by this finite 0
st at e aut omat on. s2 1

V = { S, A, B , 0, 1}
T = { 0, 1}
P = { S → λ , S → 1A, S → 0B , S → 0, A → 0A,
A → 1B , A → 1, B → 0A, B → 1B , B → 1}
where S, A, B correspond t o s0 , s1 , s2 respect ively.

7.2 R EGULAR G RAMMARS 12


CSC211 - DISCRETE METHOD SPRING 2015

3. Kleene’s Theorem and the Pumping Lemma


• A finit e st at e aut omat on has only a finit e amount of memory – a
FSA having N st at es can only remember N different t hings.
• T his limit at ion means t hat finit e st at e aut omat a cannot recognise
languages t hat are not regular.
• For example, in order t o recognise t he language { a n b n : n ≥ 0} a
FSA would have t o recognise every st ring in t he set , and t herefore
would need count an arbit rarily large number of as before st art ing
t o process t he bs.
• Kleene’s t heorem can be used t o demonst rat e t hat a language is
not regular.

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 13


CSC211 - DISCRETE METHOD SPRING 2015

Example 7.3. Use Kleene’s t heorem t o show t hat L = { a n b n : n ≥ 0} is


not regular.

We use t he met hod of proof by cont radict ion.


Suppose t hat L is regular. By Kleene’s t heorem, t his means t hat t here
exist s finite-state aut omat on M = (S, I, f , s0 , F ) t hat recognises L.
• Let N be t he number of st at es in M.
• Because M recognises t he set L it must recognise t he st ring
a N b N . As it processes t he st ring a N b N , it goes t hrough a sequence
of 2N st at es, st art ing at t he init ial st at e s0 .
• Let s0 , s1 , s2 , s3 , . . . , s2N be t his sequence of st at es (not e t hat s2N
is a final st at e):
s1 = f (s0 , a), . . . , sN = f (sN − 1 , a),
sN + 1 = f (sN , b), . . . , s2N = f (s2N − 1 , b)

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 14


CSC211 - DISCRETE METHOD SPRING 2015

• Because M has only N st at es, at least two of t he first N + 1 st at es


in t he sequence s0 , . . . , s2N (say si and sj ) must be t he same.
• If i < j, t here is a loop leading from st at e si back t o it self:
f (si , a t ) = sj where t = j−i> 0

• If M is in st at e si , a sequence of t consecut ive as will ret urn M t o


st at e si .
• For t > 0 t he st ring a n+ t b n should not be recognised by M.
However, because t he addit ional as in t he st ring t ake us around
t he loop from si back t o it self again, we again end up in st at e s2N ,
and t his is a final st at e.
• T his is a cont radict ion, so t here does not exist a FSA t hat
recognises t he language { a n b n : n ≥ 0} , so t he language is not
regular.

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 15


CSC211 - DISCRETE METHOD SPRING 2015

s0
st art

Fig. 7.1: Kleene’s T heorem and t he P umping Lemma

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 16


CSC211 - DISCRETE METHOD SPRING 2015

s0 a s1 a ... a si
st art

Fig. 7.1: Kleene’s T heorem and t he P umping Lemma

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 16


CSC211 - DISCRETE METHOD SPRING 2015

...
a a
si + 2 sj − 2
a a
si + 1 sj − 1
a a
s0 a s1 a ... a si
st art

Fig. 7.1: Kleene’s T heorem and t he P umping Lemma

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 16


CSC211 - DISCRETE METHOD SPRING 2015

...
a a
si + 2 sj − 2
a a
si + 1 sj − 1
a a
s0 a s1 a ... a si a ... a sN ... s2N
st art b b

Fig. 7.1: Kleene’s T heorem and t he P umping Lemma

7.3 K LEENE ’ S T HEOREM AND THE P UMPING L EMMA 16


CSC211
Discrete Me t h o d s
Lecture 8
NFA to DFA
Fall 2016
Non Deterministic Features of NFA

There are three main cases of non- determinism in NFAs:


1. Transition to a state without consuming any input.
2. Multiple transitions on the same input symbol.
3. No transition on an input symbol.

To convert NFAs to DFAs we need to get rid of non-determinism


from NFAs.
Subset Construction Method

Using Subset construction method to convert NFA to DFA


involves the following steps:
• For every state in the NFA, determine all reachable states for every input
symbol.
• The set of reachable states constitute a single state in the converted DFA
(Each state in the DFA corresponds to a subset of states in the NFA).
• Find reachable states for each new DFA state, until no more new states can
be found.
Subset Construction Method

Fig1. NFA without λ-transitions


Subset Construction Method

Fig1. NFA without λ-transitions

3
b a
a
a
a 2 b
a,b
1 5

a,b a
4

b
Subset Construction Method

Fig1. NFA without λ-transitions

3
a
a
a
b
Step1
a 2 b
Construct a transition table showing
a,b all reachable states for every state
1 5
for every input signal.
a,b a
4

b
Subset Construction Method

Fig1. NFA without λ-transitions Fig2. Transition table

3
b a
a
a
a 2 b
a,b
1 5

a,b a
4

b
Subset Construction Method

Fig1. NFA without λ-transitions Fig2. Transition table

3 q δ(q,a) δ(q,b)
b a
a 1 {1,2,3,4,5} {4,5}
a
a 2 b 2 {3} {5}
a,b
1 5 3 ∅ {2}

a,b a 4 {5} {4}


4

b
5 ∅ ∅
Subset Construction Method

Transition from Fig2.


state qTransition
with table from state q
Transition
Fig1. NFA without λ-transitions input a with input b

3 q δ(q,a) δ(q,b)
b a
a Starts here 1 {1,2,3,4,5} {4,5}
a
a 2 b 2 {3} {5}
a,b
1 5 3 ∅ {2}

a,b a 4 {5} {4}


4

b
5 ∅ ∅
Subset Construction Method

Fig2. Transition table

q δ(q,a) δ(q,b) Step2


1 {1,2,3,4,5} {4,5}
The set of states resulting from every
2 {3} {5} transition function constitutes a new
state. Calculate all reachable states
3 ∅ {2} for every such state for every input
signal.
4 {5} {4}

5 ∅ ∅
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) Starts with


Initial state
q δ(q,a) δ(q,b)
{1,2,3,4,5} 1 {1,2,3,4,5} {4,5}
1 {4,5}

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) Starts with q δ(q,a) δ(q,b)


Initial state
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5}
2 {3} {5}
{4,5}
3 ∅ {2}

4 {5} {4}

5 ∅ ∅
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) Starts with q δ(q,a) δ(q,b)


Initial state
1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5}
2 {3} {5}
{4,5}
3 ∅ {2}

4 {5} {4}

5 ∅ ∅

Step3
Repeat this process(step2) until no
more new states are reachable.
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


{1,2,3,4,5} 1 {1,2,3,4,5} {4,5}
1 {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5}
3 ∅ {2} {2,4,5}
4 {5} {4}

5 ∅ ∅
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5}
4 {5} {4}
5
5 ∅ ∅ 4
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5
5 ∅ ∅ 4
{3,5}
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


{1,2,3,4,5} 1 {1,2,3,4,5} {4,5}
1 {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4} 5 ∅ ∅
5 ∅ ∅ 4
{3,5}


Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5}
We already got 4 and 5.
So we don’t add them again.
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2


2
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2

∅ ∅ ∅
2
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2

∅ ∅ ∅
2 3 5
3
Fig2. Transition table Fig3. Subset Construction table

q δ(q,a) δ(q,b) q δ(q,a) δ(q,b)


1 {1,2,3,4,5} {4,5} 1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} 5 4
3 ∅ {2}
{2,4,5} {3,5} {4,5}
4 {5} {4}
5 ∅ ∅
5 ∅ ∅ 4 5 4
{3,5} ∅ 2

∅ ∅ ∅
Stops here as there are 2 3 5
no more reachable states
3 ∅ 2
Fig3. Subset Construction table Fig4. Resulting FA after applying
Subset Construction to fig1
a
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5} 1,2,3,4,5
b 2,4,5 a
{1,2,3,4,5} {1,2,3,4,5} {2,4,5} 3,5

{4,5} 5 4 a
a,b a
{2,4,5} {3,5} {4,5} b
b
1 ∅ a
∅ ∅
3
5 b
b a,b
a
4 5 4
2
{3,5} ∅ 2
4,5
a
5 b
∅ ∅ ∅ b 4 a

2 3 5
b
3 ∅ 2

You might also like