Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

CS5113

Advanced Theory of Computation


DR. AAMER NADEEM
WEEK 02 – MAR. 16, 2024

CAPITAL UNIVERSITY OF SCIENCE AND TECHNOLOGY


FSA – Examples
• Example 5
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑜𝑑𝑑 − 𝑙𝑒𝑛𝑔𝑡ℎ 𝑠𝑡𝑟𝑖𝑛𝑔𝑠

𝑅. 𝐸. = 𝑎+𝑏 𝑎+𝑏 (𝑎 + 𝑏)

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 2


FSA – Examples
• Example 6
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑒𝑣𝑒𝑛 − 𝑙𝑒𝑛𝑔𝑡ℎ 𝑠𝑡𝑟𝑖𝑛𝑔𝑠
𝑅. 𝐸. = ( 𝑎 + 𝑏 𝑎 + 𝑏 )∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 3


FSA – Examples
• Example 7
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑡ℎ𝑎𝑡 𝑏𝑒𝑔𝑖𝑛 𝑤𝑖𝑡ℎ 𝑎𝑏
𝑅. 𝐸. = 𝑎𝑏(𝑎 + 𝑏)∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 4


FSA – Examples
• Example 8
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑡ℎ𝑎𝑡 𝑐𝑜𝑛𝑡𝑎𝑖𝑛 𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔 𝑎𝑏
𝑅. 𝐸. =
𝑏 ∗ 𝑎𝑎∗ 𝑏(𝑎 + 𝑏)∗
(𝑎 + 𝑏)∗ 𝑎𝑏(𝑎 + 𝑏)∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 5


FSA – Examples
• Example 9
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑡ℎ𝑎𝑡 𝑒𝑛𝑑 𝑤𝑖𝑡ℎ 𝑎𝑏
𝑅. 𝐸. =
(𝑎 + 𝑏)∗ 𝑎𝑏

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 6


FSA – Examples
• Example 10
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑡ℎ𝑎𝑡𝑏𝑒𝑔𝑖𝑛 𝑤𝑖𝑡ℎ 𝑎 𝑎𝑛𝑑 𝑒𝑛𝑑 𝑤𝑖𝑡ℎ 𝑏
𝑅. 𝐸. = 𝑎(𝑎 + 𝑏)∗ 𝑏

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 7


FSA – Examples
• Example 11
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑡ℎ𝑎𝑡 𝑐𝑜𝑛𝑡𝑎𝑖𝑛 𝑎𝑎 𝑜𝑟 𝑏𝑏
𝑅. 𝐸. = 𝑎 + 𝑏 ∗ (𝑎𝑎 + 𝑏𝑏)(𝑎 + 𝑏)∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 8


FSA – Examples
• Example 12
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑡ℎ𝑎𝑡 𝑏𝑒𝑔𝑖𝑛 𝑤𝑖𝑡ℎ 𝑎𝑎 𝑜𝑟 𝑏𝑏
𝑅. 𝐸. = (𝑎𝑎 + 𝑏𝑏)(𝑎 + 𝑏)∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 9


FSA – Examples
• Example 13
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔 𝑡ℎ𝑎𝑡 𝑒𝑛𝑑 𝑤𝑖𝑡ℎ 𝑎𝑎 𝑜𝑟 𝑏𝑏
𝑅. 𝐸. = 𝑎 + 𝑏 ∗ (𝑎𝑎 + 𝑏𝑏)

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 10


FSA – Examples
• Example 14
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑖𝑛𝑔 𝑒𝑥𝑎𝑐𝑡𝑙𝑦 𝑡𝑤𝑜 𝑎′ 𝑠
𝑅. 𝐸. = 𝑏 ∗ 𝑎𝑏 ∗ 𝑎𝑏 ∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 11


FSA – Examples
• Example 15
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑎𝑙𝑙 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑖𝑛𝑔 𝑎𝑡 𝑙𝑒𝑎𝑠𝑡 𝑡𝑤𝑜 𝑎′ 𝑠
𝑅. 𝐸. = 𝑏 ∗ 𝑎𝑏 ∗ 𝑎(𝑎 + 𝑏)∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 12


FSA – Examples
• Example 16
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑜𝑓 𝑠𝑡𝑟𝑖𝑛𝑔𝑠 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑖𝑛𝑔 𝑎𝑡 𝑚𝑜𝑠𝑡 𝑡𝑤𝑜 𝑎′ 𝑠
𝑅. 𝐸. = 𝑏 ∗ (𝑎 + 𝜀)𝑏 ∗ (𝑎 + 𝜀)𝑏 ∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 13


Exercises
• Write Regular Expressions and construct FSA for each of the
following languages over Σ = {𝑎 𝑏}
• Language of all those strings which begin with aba
• Language of all those strings which end with aba
• Language of all those strings which contain odd number of b’s
• Language of all those strings whose length is greater than 3
• Language of all those strings whose length is a multiple of 3
• Language of all those strings whose length is even and last symbol is b
• Language of all those strings in which second last symbol is b
• Language of all those strings whose length is odd and second last symbol is b
• Language of all those strings which begin and end with different symbols
• Language of all those strings in which number of a’s odd and number of b’s is
even
• Language of all those strings in which every third symbol is b

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 14


Non-deterministic FSA
Non-deterministic Finite State Automata
• A non-deterministic finite-state automaton (NFA) does not need to
have a unique outgoing transition for each alphabet symbol from
each state
• There can be more than one outgoing transitions from same state having same
label
• There may be no outgoing transition for a symbol from a given state

• Transitions may have 𝜀 as transition label


• There may be more than one start states

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 16


Possible NFA transitions

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 17


Language of an NFA
• When we run a string on an NFA
• There can be multiple paths that can be followed
• The string may get accepted on some paths
• On some paths, the string may get rejected because a non-final state is reached
• On some paths, the string may get rejected because NFA crashes

• A string is considered as accepted on an NFA if it is accepted on at


least one path
• A string is considered as rejected on an NFA if it is rejected on all
paths
• Language of an NFA is the set of all strings accepted on the NFA

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 18


NFA – Examples
• Example 1
Σ = {𝑎 𝑏}
𝑅. 𝐸. = 𝑏 ∗ 𝑎∗

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 19


NFA – Examples
• Example 2
Σ = {𝑎 𝑏}
𝑅. 𝐸. = 𝑎 + 𝑏 ∗ (𝑎𝑎 + 𝑏𝑏)

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 20


NFA – Examples
• Example 3
Σ = {𝑎 𝑏}
𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒?

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 21


Regular Languages
Equivalence of DFA, NFA and RE
• DFA, NFA and Regular Expressions are equivalent
• In other words, any language that can be defined by one of these
methods, can also be defined by other two
• Every NFA can be converted to a DFA
• Every DFA can be converted to a regular expression
• Every regular expression can be converted to an NFA

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 23


Regular Languages
• A language that can be defined by any of these methods:
• Regular expression
• DFA
• NFA

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 24


Regular Languages
Theorem: All finite languages are regular
Proof: Let L be a finite language having n words as follows:
𝑤1 , 𝑤2 , 𝑤3 , … … , 𝑤𝑛
Then it is possible to write a regular expression for L as:
𝑤1 + 𝑤2 + 𝑤3 + … … + 𝑤𝑛
Therefore L is regular

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 25


Closure Properties of
Regular Languages
Operations on Languages
• Basic operations on languages are:
• Union
• Intersection
• Difference
• Product
• Complement
• Kleene Closure

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 27


Operations on Languages
• Example
Σ = {𝑎 𝑏}
𝐿1 = {𝑎𝑏 𝑏𝑏 𝑏𝑎𝑎}
𝐿2 = {𝑏𝑎 𝑏𝑏 𝑎𝑏𝑎}
𝐿1 ∪ 𝐿2 = {𝑎𝑏 𝑏𝑎 𝑏𝑏 𝑎𝑏𝑎 𝑏𝑎𝑎}
𝐿1 ∩ 𝐿2 = {𝑏𝑏}
𝐿1 − 𝐿2 = {𝑎𝑏 𝑏𝑎𝑎}
𝐿1 𝐿2 = {𝑎𝑏𝑏𝑎 𝑎𝑏𝑏𝑏 𝑏𝑏𝑏𝑎 𝑏𝑏𝑏𝑏 𝑎𝑏𝑎𝑏𝑎 𝑏𝑎𝑎𝑏𝑎 𝑏𝑎𝑎𝑏𝑏 𝑏𝑏𝑎𝑏𝑎 𝑏𝑎𝑎𝑎𝑏𝑎}
𝐿′1 = {𝜀 𝑎 𝑏 𝑎𝑎 𝑏𝑎 𝑎𝑎𝑎 𝑎𝑎𝑏 𝑎𝑏𝑎 𝑎𝑏𝑏 … … }
𝐿∗1 = {𝜀 𝑎𝑏 𝑏𝑏 𝑏𝑎𝑎 𝑎𝑏𝑎𝑏 𝑎𝑏𝑏𝑏 𝑏𝑏𝑎𝑏 𝑏𝑏𝑏𝑏 𝑎𝑏𝑏𝑎𝑎 𝑏𝑎𝑎𝑎𝑏 𝑏𝑎𝑎𝑏𝑏 … … }

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 28


Closure w.r.t. Union Operation
Theorem: Let 𝐿1 and 𝐿2 be two regular languages, then so is 𝐿1 ∪ 𝐿2
Proof:
Let 𝐿1 and 𝐿2 be defined by regular expressions 𝑅1 and 𝑅2
respectively
Then 𝐿1 ∪ 𝐿2 can be defined by regular expression 𝑅1 + 𝑅2
Therefore 𝐿1 ∪ 𝐿2 is regular

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 29


Closure w.r.t. Product Operation
Theorem: Let 𝐿1 and 𝐿2 be two regular languages, then so is 𝐿1 𝐿2
Proof:
Let 𝐿1 and 𝐿2 be defined by regular expressions 𝑅1 and 𝑅2
respectively
Then 𝐿1 𝐿2 can be defined by regular expression 𝑅1 𝑅2
Therefore 𝐿1 𝐿2 is regular

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 30


Closure w.r.t. Kleene Closure Operation
Theorem: Let 𝐿 be a regular language, then so is 𝐿∗
Proof:
Let 𝐿 be defined by regular expression 𝑅
Then regular expression 𝑅∗ defines the language 𝐿∗
Therefore 𝐿∗ is regular

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 31


Closure w.r.t. Complement Operation
Theorem: Let 𝐿 be a regular language, then so is 𝐿′
Proof:
Let 𝐿 be defined by FSA 𝐹
Then FSA 𝐹 ′ obtained by making all final states non-final and all
non-final states final will accept the language 𝐿′
Therefore 𝐿′ is regular

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 32


Closure w.r.t. Intersection Operation
Theorem: Let 𝐿1 and 𝐿2 be two regular languages, then so is 𝐿1 ∩ 𝐿2
Proof:
Let 𝐿1 and 𝐿2 be regular languages
Then 𝐿′1 and 𝐿′2 both are regular languages
 𝐿′1 ∪ 𝐿′2 is a regular language
 (𝐿′1 ∪ 𝐿′2 )′ is a regular language
 𝐿1 ∩ 𝐿2 is a regular language [By DeMorgan’s Law]

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 33


Closure w.r.t. Difference Operation
Theorem: Let 𝐿1 and 𝐿2 be two regular languages, then so is 𝐿1 − 𝐿2
Proof:
Let 𝐿1 and 𝐿2 be regular languages
Then 𝐿1 and 𝐿′2 both are regular languages
 𝐿1 ∩ 𝐿′2 is a regular language
 𝐿1 − 𝐿2 is a regular language [By Set Difference Law]

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 34


Non-Regular Languages
Non-regular Languages
• Languages which cannot be defined by DFA, NFA or regular
expressions
• Example
𝐿 = 𝑎𝑛 𝑏 𝑛 𝑛 ∈ 𝑁}
• Why this language is non-regular?
• How can we define this language?

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 36


Push Down Automata
Push Down Automata
• A Push Down Automaton (PDA) consists of
• A state machine
• A stack

• Each transition has 3 labels written as (𝑥, 𝑦/𝑧)


• 𝑥 is the input symbol; 𝑥 ∈ Σ where Σ is the alphabet set
• 𝑦 is the stack symbol (popped); 𝑦 ∈ Γ where Γ is the stack alphabet
• 𝑧 is the string pushed into stack; 𝑧 ∈ Γ ∗

• Two outgoing transitions from a state cannot have same 𝑥 𝑎𝑛𝑑 𝑦


• If 𝑥 is 𝜀, there can be no non-𝜀 outgoing transition from that state
with same stack symbol 𝑦
• The stack has a special symbol 𝑍 at the bottom
CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 38
PDA Example 1
𝐿 = 𝑎𝑛 𝑏 𝑛 𝑛 ∈ 𝑁}

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 39


PDA Example 2
• 𝐿 = 𝑎𝑛 𝑏 𝑛+2 𝑛 ∈ 𝑁}

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 40


PDA Exercise 1
• 𝐿 = 𝑎𝑛+2 𝑏 𝑛 𝑛 ∈ 𝑁}

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 41


PDA Example 3
• 𝐿 = 𝑎𝑛 𝑏 2𝑛 𝑛 ∈ 𝑁}

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 42


PDA Example 4
• 𝐿 = 𝑎2𝑛 𝑏 𝑛 𝑛 ∈ 𝑁}

CS5113 Advanced Theory of Computation – by Dr. Aamer Nadeem Spring 2022 43


Thank you!

You might also like