Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
Formal Languages and Automata - Yonsei CS 2
Structure of Chapter 1
1.1 The main ideas from mathematics that will be required
1.2 Central concepts of languages
1.3 Simple applications of the concepts
Formal Languages and Automata - Yonsei CS 3
}2,1,0{=S
A set is a collection of elements• Specified by enclosing some description of its elements
in curly braces
SETS
},,,{ airplanebicyclebustrainB =
A∈1Bship∉
We write• 1 is an element of a set A
• ship is not in a set B
Formal Languages and Automata - Yonsei CS 4
Set Representations
C = { a, b, c, d, e, f, g, h, i, j, k }
C = { a, b, …, k }
S = { 2, 4, 6, … }
S = { i : i is nonnegative and even }
S = { i : i > 0, i= 2k for some k>0 }
finite set
infinite set
• When the meaning is clear
• More explicit notation
Formal Languages and Automata - Yonsei CS 5
A = { 1, 2, 3, 4, 5 }
Universal Set: all possible elements
U = { 1 , … , 10 }
1 2 34 5
A
U
6
78
910
Formal Languages and Automata - Yonsei CS 6
Set Operations
A = { 1, 2, 3 } B = { 2, 3, 4, 5}
• Union
A U B = { 1, 2, 3, 4, 5 }
• Intersection
A B = { 2, 3 }
• Difference
A - B = { 1 }
B - A = { 4, 5 }
U
A B23
14
5
23
1
Venn diagrams
Formal Languages and Automata - Yonsei CS 7
A
• Complement of A consists of all elements not in A
Universal set = {1, …, 7}
A = { 1, 2, 3 } A = { 4, 5, 6, 7}
12
3
4
5
6
7
A
A = A
Formal Languages and Automata - Yonsei CS 8
024
6
1
3
5
7
even
{ even integers } = { odd integers }
odd
Integers
• Another example of complementation
Formal Languages and Automata - Yonsei CS 10
Empty, Null Set:
= { }
S U = S
S =
S - = S
- S =
U
= Universal Set
• The set with no elements
• From the definition of a set, it is obvious
Formal Languages and Automata - Yonsei CS 11
Subset
A = { 1, 2, 3} B = { 1, 2, 3, 4, 5 }A B
U
Proper Subset:
A BU
A
B
If every element of S1 is also an element of S,a set S1 is a subset of S
•B contains an element not in A
Formal Languages and Automata - Yonsei CS 12
Disjoint Sets
A = { 1, 2, 3 } B = { 5, 6}
A B = U
A B
If S1 and S2 have no common element,
Formal Languages and Automata - Yonsei CS 13
Set CardinalityFor finite sets
A = { 2, 5, 7 }
|A| = 3
•The cardinality of a finite set: the number of elements in the set.
•We will say that the cardinality of an infinite set is infinity (written as ∞).
Formal Languages and Automata - Yonsei CS 14
PowersetsPowerset of S = the set of all the subsets of S
S = { a, b, c }2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }
Observation: | 2S | = 2|S| ( 8 = 23 )
Formal Languages and Automata - Yonsei CS 15
Cartesian Product
A = { 2, 4 } B = { 2, 3, 5 }
A X B = { (2, 2), (2, 3), (2, 5), ( 4, 2), (4, 3), (4, 5) }
|A X B| = |A| |B|
The set of all possible ordered pairs whose first component is a member of X and whose second component is a member of Y.
X × Y = { (x, y) | x ∈ X and y ∈ Y }
Generalizes to more than two sets:The collection of all ordered n-tuples that can be formed so that they contain one element of the first set, one element of the second,..., and one element of the n-th set. This collection can be seen as constituting an n-dimensional space in which each n-tupledesignates a cell. A X B X … X Z
Formal Languages and Automata - Yonsei CS 16
FUNCTIONSdomain
123
a
bc
f : A -> B
A Brange
If the domain of f is all of Athen f is a total functionotherwise f is a partial function
f(1) = a4
5
• Function is a rule to assign to elements of one setto a unique element of another set
Formal Languages and Automata - Yonsei CS 17
RELATIONS• For a function, each element of the domain has
exactly one associated element in the range.• In a relation, there can be several such elements
in the range.
R = {(x1, y1), (x2, y2), (x3, y3), …}
xi R yi
e. g. if R = ‘>’: 2 > 1, 3 > 2, 3 > 1
Formal Languages and Automata - Yonsei CS 18
GRAPHS (Relation)
A directed graph
• Nodes (Vertices)
V = { a, b, c}
• Edges
E = { (a,b), (b,c),(c,a), }
bnode
edge
a
c
A graph is a construct consisting of 2 finite sets
• If we associate a direction with each edge
Formal Languages and Automata - Yonsei CS 20
Walk
Walk is a sequence of adjacent edges
(e, d), (d, c), (c, a)
a
b
c
d
e
Formal Languages and Automata - Yonsei CS 21
Path and Cycle
Path is a walk where no edge is repeatedSimple path: no node is repeated
(a,c), (c,b)
Cycle: a walk from a node (base) to itself with norepeated edges
(a,c), (c,c), (c,a)Loop: egde from a node to itself
a b c
Formal Languages and Automata - Yonsei CS 22
Treesroot
leaf parent
childTree is a directed graph that has no cycles• root, leaf• parent, child: If there is an edge
from one node to another
Formal Languages and Automata - Yonsei CS 23
root
leaf
Level 0
Level 1
Level 2
Level 3
Height 3
• Level: number of edges in the path from root to the vertex
• Height: largest level
Formal Languages and Automata - Yonsei CS 24
PROOF TECHNIQUES
• Proof by induction
• Proof by contradiction
Two special proof techniques.
Formal Languages and Automata - Yonsei CS 25
Induction
We have a seq. of statements P1, P2, P3, …
If we know
• for some k that P1, P2, …, Pk are true
• for any n >= k that the truths of P1, P2, …, Pn
imply the truth of Pn+1
Then
Every Pi is true
Formal Languages and Automata - Yonsei CS 26
Proof by Induction• Inductive basis
Find P1, P2, …, Pk which are true
• Inductive hypothesisLet’s assume P1, P2, …, Pn are true, for any n >= k
• Inductive step (We will know that the patternis clear)
Show that Pn+1 is true
Formal Languages and Automata - Yonsei CS 27
ExampleTheorem: A binary tree* of height n
has at most 2n leaves.Proof by induction:
let L(i) be the maximum number of leaves of any subtree at height i
* Binary tree is a tree in which no parent can have more than twochildren
Formal Languages and Automata - Yonsei CS 28
We want to show: L(i) <= 2i
• Inductive basis
L(0) = 1 (the root node)
• Inductive hypothesis
Let’s assume L(i) <= 2i for all i = 0, 1, …, k
• Induction step
we need to show that L(k + 1) <= 2k+1
Formal Languages and Automata - Yonsei CS 29
Induction Step
From Inductive hypothesis: L(k) <= 2k
height
k
k+1
Formal Languages and Automata - Yonsei CS 30
L(k) <= 2k
L(k+1) <= 2 * L(k) <= 2 * 2k = 2k+1
Induction Step
height
k
k+1
(we add at most two nodes for every leaf of level k)
Formal Languages and Automata - Yonsei CS 31
Proof by Contradiction
We want to prove that a statement P is true
• we assume that P is false
• then we arrive at an incorrect conclusion
• therefore, statement P must be true
Formal Languages and Automata - Yonsei CS 32
Example
Theorem: is not rational
Proof:
Assume by contradiction that it is rational
= n/m
n and m have no common factors
We will show that this is impossible
2
2
* A rational number: expressed as the ration of two integers n and m so that n and m have no common factor.
Formal Languages and Automata - Yonsei CS 33
= n/m 2 m2 = n2
Therefore, n2 is evenn is even
n = 2 k
2 m2 = 4k2 m2 = 2k2m is even
m = 2 p
Thus, m and n have common factor 2
Contradiction!
2
Formal Languages and Automata - Yonsei CS 35
A language is a set of strings
String: A sequence of letters
Examples: “cat”, “dog”, “house”,…
Defined over an alphabet:
Three fundamental ideas: languages, grammars, and automata
Formal Languages and Automata - Yonsei CS 36
Alphabets and StringsWe will use a finite, nonempty set of
symbols:
Strings: finite sequences of symbols from the alphabet
abbawbbbaaavabu
===
{ }ba,=Σ
baaabbbaabababaabbaaba
Stringnames
Specificvalue
Formal Languages and Automata - Yonsei CS 37
String Operations
m
n
bbbvaaaw
L
L
21
21
==
bbbaaaabba
mn bbbaaawv LL 2121=
ConcatenationThe string obtained by appending the symbols of vto the right end of w.
abbabbbaaa
12aaaw nR L=
naaaw L21= ababaaabbbReverse
bbbaaababaObtained by writing the symbols in reverse order
Formal Languages and Automata - Yonsei CS 38
String Length
Length:
Examples:
naaaw L21=
nw =
12
4
=
=
=
aaaabba
Formal Languages and Automata - Yonsei CS 39
Empty StringA string with no letters:
Observations:
λ
abbaabbaabba
www
==
==
=
λλ
λλ
λ 0
Formal Languages and Automata - Yonsei CS 40
SubstringSubstring of string:
a subsequence of consecutive characters
Prefixes Suffixesabbab
abbababbaabbabaλ
λbabbabbbababbab
uvw =
a prefix
a suffix
Formal Languages and Automata - Yonsei CS 41
Another Operation
Example:
Definition for a special case:
43421 Ln
n wwww =
( ) abbaabbaabba =2
ww ∀= λ0
( ) λ=0abba
The string obtained by repeating w n times
Formal Languages and Automata - Yonsei CS 42
The *, + Operation: the set of all possible strings fromalphabet
: the set of all possible strings fromalphabet except
λ−Σ=Σ+ *
*ΣΣ
{ }{ }K,,,,,,,,,*
,aabaaabbbaabaaba
baλ=Σ
=Σ
{ }K,,,,,,,, aabaaabbbaabaaba=Σ+
+ΣΣ λ
Formal Languages and Automata - Yonsei CS 43
LanguagesA language is any subset of
Example:
These sets are languages on the alphabet :
*Σ
{ }{ }K,,,,,,,,*
,aaabbbaabaaba
baλ=Σ
=Σ
{ }{ }
},,,,,{,,
aaaaaaabaababaabbaaabaaa
λ
λ
Formal Languages and Automata - Yonsei CS 44
Another Example
An infinite language }0:{ ≥= nbaL nn
aaaaabbbbbaabbabλ
L∈ Labb∉
Formal Languages and Automata - Yonsei CS 45
Operations on LanguagesThe usual set operations since languages are
sets
Complement:
{ } { }{ } { }{ } { } { }aaaaaabbbaaaaaba
ababbbaaaaabaaaaabbabaabbbaaaaaba
,,,,}{,,,
},,,{,,,
=−==
I
U
LL −Σ= *
{ } { }K,,,,,,, aaabbabaabbaa λ=
Formal Languages and Automata - Yonsei CS 46
Reverse
Definition:
The concatenation of two languages L1 and L2 is the set of all strings obtained by concatenating any element of L1 with any element of L2
Definition:
}:{ LwwL RR ∈=
The reverse of a lang. is the set of all stringreversals
{ }2121 ,: LyLxxyLL ∈∈=
Formal Languages and Automata - Yonsei CS 47
Another OperationDefinition:
L concatenated with itself n times
Special case:
321Ln
n LLLL =
{ } { }{ }{ }{ }bbbbbababbaaabbabaaabaaa
babababa,,,,,,,
,,,, 3 ==
{ }{ } { }λ
λ=
=0
0
,, aaabbaaL
Formal Languages and Automata - Yonsei CS 48
Star-Closure of a Language
Definition:
Example:
LUU 210* LLLL =
{ }
⎪⎪⎭
⎪⎪⎬
⎫
⎪⎪⎩
⎪⎪⎨
⎧
=
K,,,,,,,,
,,,
*,
abbbbabbaaabbaaabbbbbbaabbaa
bbabba
λ
Formal Languages and Automata - Yonsei CS 49
Positive Closure
Definition:
{ }λ−==+
*
21
LLLL LUU
{ }⎪⎭
⎪⎬
⎫
⎪⎩
⎪⎨
⎧=+
K,,,,,,,,
,,,
abbbbabbaaabbaaabbbbbbaabbaa
bbabba