Toa All Merge - 3
Toa All Merge - 3
Toa All Merge - 3
CS09305
Before the start of the Formal lecture
Look at the statement give below
2
The Course
4
Grading
• Following is the division of marks:
• Mid-Term Exam 25
• Assignments 10
• Quizzes 15
• Final Exams. 50
5
INTRODUCTION
Introduction
11
SETS
• Definition: A set is well defined collection of objects,
which are
• unordered
• distinct
• same type
• with common properties
• Sets are written with curly braces {}, and the elements
in the set are written within the curly braces.
• The set {a, b, c} has elements a, b, and c.
• The sets {a, b, c} and {b, c, b, a, a} are the same since
order does not matter in a set and since redundancy
does not count.
• The set {a} has element a. Note that {a} and a are
different things; {a} is a set with one element a.
• The set {xn : n = 1, 2, 3, . . .} consists of x,
xx, xxx, . . ..
• The set of positive even numbers is {2, 4,
6, 8, 10, 12, . . .} = {2n : n =1, 2, 3, . . .}.
• The set of odd numbers is {1, 3, 5, 7, 9, 11,
13, . . .} = {2n + 1 : n = 0, 1, 2, . . .}.
Set Representations
C = { a, b, c, d, e, f, g, h, i, j, k }
C = { a, b, …, k } finite set
S = { 2, 4, 6, … } infinite set
U = { 1 , … , 10 }
15
Set Operations
A = { 1, 2, 3 } B = { 2, 3, 4, 5}
A B
• Union
2 4
1
A U B = { 1, 2, 3, 4, 5 } 3 5
• Intersection
A B = { 2, 3 }
U
2
3
• Difference
A-B={1}
1
B - A = { 4, 5 }
Venn diagrams
Courtesy Costas Busch - RPI 16
• Complement
Universal set = {1, …, 7}
A = { 1, 2, 3 } A = { 4, 5, 6, 7}
4
A
A 3 6
1
2
5 7
A=A
17
{ even integers } = { odd integers }
Integers
1 odd
even
6 5
2
0
4
3 7
18
DeMorgan’s Laws
AUB=A B
U
A B=AUB
U
19
Empty, Null Set:
={}
SU =S
S =
U
= Universal Set
S- =S
-S=
20
Subset
A = { 1, 2,3,5} B = { 1, 2, 3, 4, 5 }
A B
U
Proper Subset: A B
U
B
A
21
Disjoint Sets
A = { 1, 2, 3 } B = { 5, 6}
A B=
U
A B
22
Set Cardinality
• For finite sets
A = { 2, 5, 7, 1, 10 }
|A| = 5
(set size)
23
Powersets
A powerset is a set of sets
S = { a, b, c }
2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }
Observation: | 2S | = 2|S| ( 8 = 23 )
24
Cartesian Product
A = { 2, 4 } B = { 2, 3, 5 } C={ 7,8}
A X B = { (2, 2), (2, 3), (2, 5), ( 4, 2), (4, 3), (4, 5) }
|A X B| = |A| |B|
AXBX…XZ
25
Theory of Automata
Handout no 2
26
Courtesy Costas Busch - RPI 27
Languages
28
Language
• Informal language
• Incoherent strings are also
understandable
• Like idiom
• Raise ambiguity
– Interpretation varies with region
– Same words have multiple meanings.
• Like, light, base, etc.
Informal languages
– List of words
• Set of all valid words of a given language, e.g., a
language English_Words that contains all valid
words of English would have a = {all entries of
the dictionary + punctuation marks and blank
space….}
• Denoted by
• Strings: A string a finite sequence of symbols
chosen from alphabet. For example
0111100 , 123045, abbbcdeg etc.
• String Variable: A letter used for
denoting a string. The author uses w, x,
y and z as string variable. For example
w = 0111100 , x = 123045, z = abbbcdeg
• Length of String: The number of
positions for symbols in the string. For
simplicity we can say that it is the
number of symbols in the string. For
example
|w| = 7 , |x| = ? , |z| = ?
Finite vs. Infinite Languages
• Finite Languages
– Countable set of words
– Can be defined by rigorously listing the
words in
– E.g. English-Words
• Infinite Languages
– Infinite set of valid words
– Cant be listed completely
– E.g. English_Sentences
Infinite Languages
• Most of the languages are infinite
• How can u check whether a word belongs to
a language if it is
– Finite
• Checking its entry in
– Infinite
• Validating against rules
Defining Languages
• Define alphabet set
• Define rules for forming valid words and
sequences of words from
– Called grammar
– Can be descriptive
– Can be mathematical
• Can also define supporting functions e.g., length(X),
reverse(x)
Defining languages
• Example ={a,b,…z}
– L = {all words formed only of odd number of xs}
– L = {xn | n is odd}
– L = {all words of length less than or equal to 4}
– PALINDROME ={Λ, all strings x such that
reverse (x) = x}
Kleene Closure
• Set closure
• Kleene Closure (applied to )
– A set of all the strings (finite) that can be
formed by the elements of where the
elements may be repeated any number of
times.
– Denoted by *
– Also called Kleene star.
• ∑* : The set of all strings over an
alphabet ∑ and called Kleene Star
Closure of alphabet. So we have
∑* = ∑0 U ∑1 U ∑2 U ∑3 U……………
• ∑+ : The set of all strings over an
alphabet ∑ excluding empty string, ε,
and called plus operation. So we have
∑+ = ∑1 U ∑2 U ∑3 U……………
Some observations
• Λ represents an empty string (not alphabet
thus not a part of )
• ε also represents the same
• ε is not equivalent to
• If = then
– * = {Λ}
• Is S* == (S*)* and so on
Alphabets and Strings
We will use small alphabets: a, b
Strings
a
ab u ab
abba v bbbaaa
baba w abba
aaabbbaabab
47
String Operations
Concatenation
48
w a1a2 an ababaaabbb
Reverse
w an a2 a1
R
bbbaaababa
49
String Length
w a1a2 an
Length: w n
Examples: abba 4
aa 2
a 1
50
Length of Concatenation
uv u v
Example: u aab, u 3
v abaab, v 5
uv aababaab 8
uv u v 3 5 8
51
Empty String
A string with no letters: ,
Observations: 0
w w w
String Substring
abbab ab
abbab abba
abbab b
abbab bbab
53
Prefix and Suffix
abbab
Prefixes Suffixes
abbab w uv
a bbab
prefix
ab bab
suffix
abb ab
abba b
abbab
54
Another Operation
w ww
n
w
n
Definition: w
0
abba
0
55
The * Operation
* : the set of all possible strings from
alphabet
a, b
* , a, b, aa, ab, ba, bb, aaa, aab,
56
The + Operation
: the set of all possible strings from
alphabet except
a, b
* , a, b, aa, ab, ba, bb, aaa, aab,
*
a, b, aa, ab, ba, bb, aaa, aab,
57
Languages
A language is any subset of *
Example: a, b
* , a, b, aa, ab, ba, bb, aaa,
Languages:
a, aa, aab
{ , abba, baba, aa, ab, aaaaaa}
58
Note that:
Sets { } {}
Set size {} 0
An infinite language L {a b : n 0}
n n
ab
L abb L
aabb
aaaaabbbbb
60
Operations on Languages
The usual set operations
Definition: L {w : w L}
R R
L {a b : n 0}
n n
L {b a : n 0}
R n n
62
Concatenation
63
Another Operation
Definition: L
n
LL L
n
Definition: L* L L L
0 1 2
Example:
,
a, bb,
a, bb*
aa , abb , bba , bbbb,
aaa, aabb, abba, abbbb,
65
Positive Closure
Definition:
L L L
1 2
L *
a, bb,
a, bb aa, abb, bba, bbbb,
aaa, aabb, abba, abbbb,
66
• Sec-b end s on slide 31
• Sec-C ends on slide 35
• Sec- A ends on slide 35
Recursive Definitions
Recursive Language Definition
A recursive definition is characteristically a
three-step process:
1. First, we specify some basic objects in the set. The
number of basic objects specified must be finite.
2. Second, we give a finite number of rules for
constructing more objects in the set from the ones we
already know.
3. Third, we declare that no objects except those
constructed in this way are allowed in the set.
Example:
75 Theory Of Automata
Arithmetic Expression AE
a*= ^, a, aa,aaa,aaaa,aaaaa…………………
(ab)*= ^, ab, abab, ababab,……………
81
Given the alphabet = {a, b}, suppose we wish to define the
language L that contains all words of the form one a followed by
some number of b’s (maybe no b’s at all); that is
L = {a, ab, abb, abbb, abbbb, …}
82
We can apply the Kleene star to the whole
string ab if we want:
(ab)* = Λ or ab or abab or ababab…
Observe that
(ab)* ≠ a*b*
because the language defined by the
expression on the left contains the word
abab, whereas the language defined by
83
the expression on the right does not.
If we want to define the language L1 = {x; xx; xxx; …}
using the language-defining symbol, we can write
L1 = language(xx*)
which means that each word of L1 must start with an x
followed by some (or no) x’s.
84
Plus Sign
87
Example
88
Regular Expressions
Given = {a,b}
a* = {Λ, a,aa,aaa,aaa,aaaa,aaaaa, …}
ab* = {a, ab,abb,abbb,abbbb, …}
a+b = {a,b}
(ab)* = {Λ, ab, abab, ababab, …}
(a+b)* = {Λ, any string of as and bs}
Formal Definition of Regular
Expressions
The set of regular expression is defined by
following rules
1. Every letter of and Λ is a regular
expression
2. If r1 and r2 are regular expressions, then so
are
(r1)
r1r2
r1+r2
r1*
93
[Section 1.3]
Regular expressions
95
Regular Expression
Identities
Regular Expression Identities
1. u = u = u
2. * =
3. u+v=v+u
4. u+u=u
5. u* = (u*)*
6. u (v + w) = uv + uw
7. (u + v) w = uw + vw
96
Languages Associated
with Regular
Expressions
Definition
language(r1r2) = L1L2
98
Definition contd.
Rule 2 (cont.):
Proof
The language of all words that have at least two a’s can
be defined by the expression:
(a + b)*a(a + b)*a(a + b)*
transition
Finite Automaton
• Visual representations
– States represented by circles labeled to
identify each distinctly
1
• Initial (- sign) and Final states (+ sign )
– Transitions
• Directed edges labeled with the characters of
Finite Automata
• Example
– Language of all words that end at b
a
b
b
-1 +2
a
Characteristics of FA
• FA = (Q, ,q0,F,δ)
– Q = {q0,q1,q2,..qn where n is finite}
– = set of input alphabets
– q0 is the start states
– F Q is the set of final states F may be
– δ is the transition function
• δ (qi,xj) = qk
• Tabular representation of an FA
– Table representing states and transitions
a
b a b
b
-1 +2 -1 1 2
+2 1 2
a
Languages of FA
a 2 a
a, b
-1 b a +4
b
b
3
Language of FA
a a
b
-+1 2
b b
3
a
a
b
b
-+1 2
a
Examples
-+1 2
b
a a a a
b
3 4
b
Applications of FA
• Lexical Analyzer of Compiler
• Search mechanism in word processor
c a t
1- 2 3 4+
(Examples)
Examples ={a, b}
• All words that contain even number of
letters
• All words ending with ab
• All words that start with a
• All words of length >=3
• All words that don’t end at ba
• All words that contain a triple letter either
aaa or bbb
Mathematical Representations of FA
• FA = (Q, ,q0,F,δ)
– Q = {q0,q1,q2,..qn where n is finite}
– = set of input alphabets
– q0 is the start states
– F Q is the set of final states F may be
– δ is the transition function
• δ (qi,xj) = qk
• Tabular representation of an FA
– Table representing states and transitions
a
b a b
b
-1 +2 -1 1 2
+2 1 2
a
Languages of FA
a 2 a
a, b
-1 b a +4
b
b
3
Language of FA
a a
b
-+1 2
b b
3
a
a
b
b
-+1 2
a
Examples
-+1 2
b
a a a a
b
3 4
b
Applications of FA
• Lexical Analyzer of Compiler
• Search mechanism in word processor
c a t
1- 2 3 4+
Chapter 6
Deterministic FA (DFA)
• No missing edges
Nondeterministic Finite
Automata(NFA)
• The FA where a state can have more than
one transition for the same character. This
puts the machine in an indecisive state for
which transition to follow
– Has duplicate transitions
– Null transition
NFA
• Reduces number of states and transitions
• Costly execution
– Needs concurrent processing to find a successful path
• Acceptance
– If at least one successful path exists
• Rejection
– Either machine crashes on input or ]
– No successful path exists
NFA
• Examples
• ε- Transitions
(discussed next)
NFA
Alphabet = {a}
q1 a q2
a
Start
q0
a
q3
Example: Accepting
a a
q1 a q2
a
Start
q0
a
q3
Example: Accepting
a a
First choice q1 a q2
a
Start
q0
a
Parallel Processing
Second choice q3
Example: Accepting
a a
q1 a q2
a
Start
q0
a
q3 No transition so
leave it
Example: Accepting
a a
a a a
q1 a q2
a
Start
q0
a
q3
Example: Rejecting
a a a
q1 a q2
a
Start
q0
a
q3
Example: Rejecting
a a a
q1 a q2
a
Start
q0
a
q3 No transition
Example: Rejecting
a a a
q1 a q2 No transition so leave
a
the state
Start
q0 Since there is no current state or
a accepting state therefore, NFA
will “Reject” the input string.
q3
Language of the NFA
L {aa}
q1 a q2
a
Start
q0
a
q3
DFA NFA
1 Exactly one start state Exactly one start state
• Examples
– All words that start and end with a double letter
a,b
aa,bb
-1 2
aa,bb
+3
DFA NFA TG
1 Exactly one start state Exactly one start state Can have more the one start state
2 One or more or may be no One or more or may be no final One or more or may be no final
final state state state
3 Only single character read Only single character read on Can read multiple character on
on making transition making transition making transition
6 No Null transition Can have null transition Can have null transition
Examples
• The arc from state 1 to state 2 is labeled with the
string aa, which is not a single letter.
• There are two arcs leaving state 2 labeled with b.
• There is no arc leaving state 2 labeled with a.
• There is an arc from state 1 to state 3 labeled
with , which is not a letter from .
• There is no arc leaving state 3 labeled with b.
Transition Graphs
• Examples
– All words that have al least one double letter in
them
– All words that begin and end with different
letters
– All words in which a occurs only in even
clumps and that end in three or more bs
– All words that have even number of letters
Generalized Transition Graphs
• A variation of TG
• A generalized transition graph is a
collection of three things
– A finite set of states, of which at least one is a
start state and some (may be none) are final
states
– An alphabet of input letters
– Directed edges connecting some pairs of
states each labeled with a regular expression
Generalized Transition Graphs
a* a*
(ba +a)* (b + Λ)
-1 2 3
• Examples
– All words having even number of as and bs
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
b a
a
a
2
Step1
a 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 1 {1,2,3,4,5} {4, 5}
a
a
a 2 b 2 {3} {5}
a,b
1 5 {2}
3 ∅
a,b a 4 {5} {4}
4
b
5 ∅ ∅
Subset Construction Method
Transition from state q with Transition from state q
Fig1. NFA without λ-transitions input a Fig2. Transition
with inputtable
b
3 q δ(q,a) δ(q,b)
b a Starts here {1,2,3,4,5} {4,5}
a 1
a
a 2 b 2 {3} {5}
a,b
1 5 {2}
3 ∅
a,b a 4 {5} {4}
4
b
5 ∅ ∅
Subset Construction Method
5 ∅ ∅
Fig2. Transition table Fig3. Subset Construction table
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}
3 {2} {4,5}
∅
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}
3 {2} {4,5}
∅
4 {5} {4}
5 ∅ ∅
Step3
Repeat this process(step2) until no
more new states are reachable.
Fig2. Transition table Fig3. Subset Construction table
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
∅
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
Fig4. Resulting FA after applying
Fig3. Subset Construction table Subset Construction to fig1
a
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5} 12345
b 245 a
{1,2,3,4,5} {1,2,3,4,5} {2,4,5} 35
{4,5} 5 4 a
a,b a
{2,4,5} {3,5} {4,5} b
a b
1 ∅
3
5 ∅ ∅ a,b b
b a
4 5 4
2
{3,5} 2 a
∅ 45 5 b
∅ ∅ ∅ b 4 a
2 3 5
b
3 ∅ 2