Professional Documents
Culture Documents
tyho@cs.nthu.edu.tw
http://theta.cs.nthu.edu.tw
Department of Computer Science
National Tsing Hua University
Hsinchu, Taiwan
Sets
Functions
Relations
Graphs
Proof Techniques
We write
1 A 1 is an element of the set A
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 }
7 Ch 1 Introduction to Formal Language (Spring 2017)
Set Operations
A = { 1, 2, 3 } B = { 2, 3, 4, 5 }
A B
Union (U)
2 4
1
A U B = { 1, 2, 3, 4, 5 } 3 5
Intersection
(U)
U
A B = { 2, 3 } 2
3
Difference (-)
A-B={1}
1
B - A = { 4, 5 }
Venn diagrams
8 Ch 1 Introduction to Formal Language (Spring 2017)
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
9 Ch 1 Introduction to Formal Language (Spring 2017)
{ even integers } = { odd integers }
Integers
1 odd
even
6 5
2
0
4
3 7
U
AUB=A B
U
A B=AUB
SU =S
U
S = = Universal Set
S - =S
-S=
A B
U
Subset: If every element of A is also an element of B
Proper Subset: If A B, but B contains an element not in A
U
B
A B
U
U
A B=
A B
S = { a, b, c }
Example 1.1
2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }
|A X B| = |A| |B|
Note that the order in which the elements of a pair are written matters
domain range
4 A B
f(1) = a a
1
2 b
3 c
5
f:AB
If A = domain
then f is a total function
otherwise f is a partial function
19 Ch 1 Introduction to Formal Language (Spring 2017)
Asymptotic Analysis
20 Ch 1
O(n) + O(n) = 2O(n) ? Introduction to Formal Language (Spring 2017)
Asymptotic Analysis
Example 1.3
f(n) = 2n2 + 3n,
g(n) = n3,
h(n) = 10n2 + 100.
f(n) = O(g(n)),
g(n) = (h(n))
f(n) = (h(n))
22 Ch 1 Introduction to Formal Language (Spring 2017)
RELATIONS
Relations are more general than functions:
In a function, each element of the domains has exactly one associated
element in the range;
In a relation, there may be several such elements in the range.
xi R yi
Example: R =
x=x
x=y y=x
x = y and y = z x=z
c
Nodes (Vertices)
V = { a, b, c, d, e }
Edges
E = { (a,b), (b,c), (b,e),(c,a), (c,e), (d,c), (e,b), (e,d) }
e
b
a d
e
b
a d
base e
b
3
a 1 d
2
c
e
b
a d
c
origin
e
b
a d
c
origin
(c, a)
(c, e)
e
b
a d
(c, a) c
origin
(c, a), (a, b)
(c, e)
(c, e), (e, b)
(c, e), (e, d)
36 Ch 1 Introduction to Formal Language (Spring 2017)
Step 3
e
b
a d
(c, a) c
origin
(c, a), (a, b)
(c, a), (a, b), (b, e)
(c, e)
(c, e), (e, b)
37
(c,Che),
1
(e, d) Introduction to Formal Language (Spring 2017)
Step 4
e
b
a d
(c, a)
(c, a), (a, b) c
origin
(c, a), (a, b), (b, e)
(c, a), (a, b), (b, e), (e,d)
(c, e)
(c, e), (e, b)
(c, e), (e, d) Introduction to Formal Language (Spring 2017)
38 Ch 1
Trees
root
parent
leaf
child
Level 1
leaf Height 3
Level 2
Level 3
Direct/Constructive Proof
Proof by Induction
Proof by Contradiction
If X, then Y
Assume X is true, show directly that Y is true.
(e.g. X = it rains, Y = sidewalk will wet)
If we know
for some b that P1, P2, , Pb are true
for any k >= b that
P1, P2, , Pk imply Pk+1
Then
Every Pi is true
Inductive hypothesis
Lets assume P1, P2, , Pk are true,
for any k >= b
Inductive step
Show that Pk+1 is true
45 Ch 1 Introduction to Formal Language (Spring 2017)
Example 1.5
Theorem: 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
Inductive basis
L(0) = 1 (the root node)
Inductive hypothesis
Lets assume L(i) <= 2i for all i = 0, 1, , k
Induction step
we need to show that L(k + 1) <= 2k+1
height
k
k+1
height
k L(k) <= 2k
k+1
f(0) = 1, f(1) = 1
l1 l2
l3
Solve it
recursively!!
Proof:
Assume by contradiction that it is rational
2 = n/m
n and m have no common factors
n is even
Therefore, n2 is even
n=2k
m is even
2 m2 = 4k2 m2 = 2k2
m=2p
3 Some Applications
Languages
Grammars
Automata (will discuss in Chap. 2)
S = {a, b}
a
ab u = ab
abba v = bbbaaa
baba w = abba
aaabbbaabab
59 Ch 1 Introduction to Formal Language (Spring 2017)
String Operations
w = a1a2 !an abba
v = b1b2 !bm bbbaaa
Concatenation
Reverse
R
w = an !a2a1 bbbaaababa
Examples: abba = 4
aa = 2
a =1
62 Ch 1 Introduction to Formal Language (Spring 2017)
Length of Concatenation
uv = u + v
Example: u = aab, u = 3
v = abaab, v = 5
uv = aababaab = 8
uv = u + v = 3 + 5 = 8
63 Ch 1 Introduction to Formal Language (Spring 2017)
Empty String
A string with no letters: l
Observations: l =0
lw = wl = w
String Substring
abbab ab
abbab abba
abbab b
abbab bbab
65 Ch 1 Introduction to Formal Language (Spring 2017)
Prefix and Suffix
abbab
Prefixes Suffixes
l abbab w = uv
a bbab prefix
ab bab suffix
abb ab
abba b
abbab l
66 Ch 1 Introduction to Formal Language (Spring 2017)
Another Operation
n
w = ww
$
!#%!
"w
n
0
Definition: w =l
(abba ) = l
0
S = {a, b}
S* = {l , a, b, aa, ab, ba, bb, aaa, aab,!}
S = {a, b}
S* = {l , a, b, aa, ab, ba, bb, aaa, aab,!}
+
S = S * -l
+
S = {a, b, aa, ab, ba, bb, aaa, aab,!}
69 Ch 1 Introduction to Formal Language (Spring 2017)
Languages
A language is any subset of S*
Example: S = {a, b}
S* = {l , a, b, aa, ab, ba, bb, aaa,!}
Languages: {l }
(Finite)
{a, aa, aab}
70
{l , abba, baba, aa, ab, aaaaaa}
Introduction to Formal Language (Spring 2017)
Ch 1
Note that:
Sets = { } {l}
Set size {} = = 0
l
ab
L abb L
aabb
aaaaabbbbb
72 Ch 1 Introduction to Formal Language (Spring 2017)
Operations on Languages
The usual set operations
{a, ab, aaaa} " {bb, ab} = {a, ab, bb, aaaa}
{a, ab, aaaa} ! {bb, ab} = {ab}
{a, ab, aaaa} - {bb, ab} = {a, aaaa}
Complement: L = S * -L
n n
L = {a b : n 0}
R n n
L = {b a : n 0}
74 Ch 1 Introduction to Formal Language (Spring 2017)
Concatenation
0
{a , bba , aaa } = {l}
76 Ch 1 Introduction to Formal Language (Spring 2017)
More Examples
n n
L = {a b : n 0}
2 n n m m
L = {a b a b : n, m 0}
2
aabbaaabbb L
Note that n and m in the above are unrelated
77 Ch 1 Introduction to Formal Language (Spring 2017)
Star-Closure (Kleene *)
0 1 2
Definition: L* = L " L " L !
Example:
l ,
a, bb,
{a, bb}* =
aa , abb, bba , bbbb ,
aaa, aabb, abba, abbbb,!
78 Ch 1 Introduction to Formal Language (Spring 2017)
Positive Closure
+ 1 2
Definition: L = L " L "!
= L * -{l}
a, bb,
+
{a, bb} = aa, abb, bba, bbbb,
aaa, aabb, abba, abbbb,!
79 Ch 1 Introduction to Formal Language (Spring 2017)
Grammars
Wikipedia says:
Languages can be described as a system
of symbols and the grammars (rules) by
which the symbols are manipulated
Grammar is the study of rules governing
the use of language.
Example 1.14
G1 = ({S}, {a, b}, S, P1) with P1:
S aSb, S
G2 = ({S, A}, {a, b}, S, P2) with P2:
S aAb| , A aAb|
1.1
20, 29, 42
1.2
14, 19, 22