Introduction to Languages and the Theory of Computation

Download Report

Transcript Introduction to Languages and the Theory of Computation

Theory of Computing
Topics
• Formal languages
• automata
• computability and related matters
Purposes
• To know the foundations and principles of computer science
• To learn the material that is useful in subsequent course
Prerequisites
• Fundamentals of data structures and algorithms
• Discrete mathematics that includes set, functions, relations,
logic, and mathematical reasoning
Chapter 1 Introduction to The Theory of Computation
1.1.1 Sets
Definition 1. A set is a group of objects.The objects in a set
are called the elements, or members, of the set.
Example 1
The set of positive integers less than 100 can be denoted as 1,2,3,...,99.
Example 2
A set can also consists of seemingly unrelated elements: a,2, Fred, New Jersey.
Definition 2. Two sets are equal if and only if they have the
same elements.
Example3
Set {1,3,3,3,5,5,5,5}is thesame as set {1,3,5}.
• A set can be described by using a set builder notation.
Example3
O  {x | x is an odd positiveintegerless than10}
Example4
N  {x | naturalnumbers} {0,1,2,3,...}
Z  {x | integers} {...,-2,-1,0,1,...}
Z  {x | positiveintegers} {1,2,3,...}
R  {x | real numbers}
• A set can be described by using a Venn diagram.
Example 5
Draw a Venn diagram that presents V, the set of vowels in English alphabet.
U
a,e,i,o,u
V
• The set that has no elements is called empty set, denoted by .
Definition 3. The set A is said to be a subset of B if and
only if every element of A is also an element of B. We use
the notation A  B to indicate that A is a subset of the set B.
 A  B if and onlyif x(x  A  x  B)
 For any set P,   P and P  P.
 If A  B and B  A, then A  B.
Example6
{ ,{a},{b}, {a, b}}  {x| x is a subset of theset {a, b}}.
 If a set A is a subset of set B but thatA  B, A is called to be a propersubset of B,
denotedas A  B.
U
A
B
Definition 4. The power set of a set S is the set of all subsets of S,
denoted by P(S) or 2 S.
Example7
Whatis thepower set of theset S  {0,1,2}?
Solution: P({0,1,2}) (or 2S )  { ,{0}, {1}, | {2}, {0,1}, {0,2}, {1,2}, {0,1,2}}.
• Set Operations:
ComplementA  {x | x U and x  A}, where set U is called" universal"
thatcont ainsall theelement swe might ever consider}
Union
A  B  {x | x  A or x  B}
Intersection A  B  {x | x  A and x  B}
Difference A  B  {x | x  A and x  B}
Example8
{1,3,5}  {1,2,3}  {1,2,3,5}.
{1,3,5}  {1,2,3}  {1,3}.
{1,3,5}- {1,2,3} {5}.
U
A
U
U
B
A
B
B
Example9
Let A  {a,e,i,o,u} and theuniversalset is theset of thelettersof theEnglish
alphabet.Then A  {b, c, d , f , g , h, j, k , l , m, n, p, q, r , s, t , v, w, x, y, z}.
U
A
A
• Set Identities
Table 1
Identity
A   A
Set Identities
A U  A
A U  U
A   
A A  A
A A  A
( A)  A
A B  B  A
A B  B  A
A  ( B  C )  ( A  B)  C
A  ( B  C )  ( A  B)  C
A  ( B  C )  ( A  B)  ( A  C )
A  ( B  C )  ( A  B)  ( A  C )
A B  A B
A B  A B
Name
Identity laws
Domination laws
Idempotent laws
Complementation laws
Commutative laws
Associative laws
Distributive laws
De Morgan’s law
 Cartisianproduct of A and B, denotedby A  B, is theset of all orderedparis(a,b),
wherea  A and b  B. Hence, A  B  {(a,b)| a A  b  B}.
Example10
Let A  { 1,2 } and B  {a,b,c}.
A  B  {( 1,a),(1,b),(1,c),(2,a),(2,b),(2,c)}
B  A  {(a,1 ),(a,2 ),(b,1 ),(b,2 ),(c,1 ),(c,2 )}
A B  B  A
1.1.2 Functions and Relations
Definition 1. Let A and B be sets. A function f from A to B is an assignment of
exactly one element of B to each element of A. We write f(a)=b if b is the
unique element of B assigned by the function f to the element a of A. If f is a
function from A to B, we write f : A B.
x
x
y
z
A function
Not a function
Example 1
Let set A={Adams, Chou, Goodfriend, Rodriguez, Stevens} and B={A,B,C,D,F}.
Let G be the function that assigns a grade to a student in our theory of computation.
G
Adames
A
Chou
B
Goodfriend
C
Rodriguez
D
Stevens
F
The domain of G is the set A={Adams,
Chou, Goodfriend, Rodriguez, Stevens},
and the range of G is the set {A,B,C,F}.
Considering the function whose domain and range are in the set
of integers. We are often interested only in the behavior of these
functions as their arguments become very large.
Definition2
Let f(n) and g(n)be functionswhose domain is a subset of thepositiveintegers.
(1) If thereexistsa positiveconstantc such thatfor all n
if |f(n)| c|g(n)|, we say that f has order at most g , denotedas f(n)  O(g(n)),
if |f(n)| c|g(n)|,then f has order at least g , denotedas f(n)  Ω(g(n)
(2) If thereexist constantsc1and c2 , such thatc1|g (n) || f (n) | c2 | g (n) |, f and g
have thesame orderof magnitude,denotedas f(n)  Θ(g(n)).
Example 2
Let f (n)  2n 2  3n, g (n)  n3 , h(n)  10n 2  100
T hen f(n) O(g(n)), g(n)  Ω(h(n)), f(n)  Θ(n)
Definition 3
Let A and B be the sets. A relation R from A to B is a subset of A  B .
From thedefinition, relationR is a set of pairs.
If (a, b)  R, we say a has a relationR with b, denotedas aRb.
• Functions can be consider as relations, but relations are
more general than functions.
Example 3
Let A={1,2,3,4}. R={(1,2), (2,3),(3,1),(4,4)} is a relation from A to A,
and it is also a function from A to A.
Example 4
R={(1,2), (2,3),(1,3),(1,4)} is a relation but not a function.
Example 5
Let A be the set of students in the TSU. Let B the set of
courses. The set R that consists of those pairs (a,b), where
a is a student enrolled in course b, is a relation from A to B.
Definition 4
R is an equivalence relation if for any pair (x,y) of R
xRx for all x
(reflexivity)
If xRy then yRx
(symmetry)
If xRy and yRz, then xRz.
(transitivity)
We usually use

to denote equivalence relation.
Example6
Let I be theintegerset and let R be a relationfromI to I,
where (x,y)  R if and onlyif x mod3  y mod3.
T hen2 R5, 12R0, and 0 R36.
It is an equivalence relation,as it satisfiesreflexity,symmetry,and transitivity.
Example 7
‘=’ on the set of integers is an equivalence relation.
Definition 4
R is an equivalence relation if for any pair (x,y) of R
xRx for all x
(reflexivity)
If xRy then yRx
(symmetry)
If xRy and yRz, then xRz.
(transitivity)
We usually use

to denote equivalence relation.
Example6
Let I be theintegerset and let R be a relationfromI to I,
where (x,y)  R if and onlyif x mod3  y mod3.
T hen2 R5, 12R0, and 0 R36.
It is an equivalence relation,as it satisfiesreflexity,symmetry,and transitivity.
Example 7
‘=’ on the set of integers is an equivalence relation.
1.1.3 Graphs and Trees
Definition1
A graph is a constructconsistingof two sets, denotedas G  (V,E),
whereV  {v1,v2 ,...,vn } is a set of verticesand E  {e1,e2 ,...,em } is a set of edges.
Each edge is a pair from V .
Example 2
Example 1
e1
v1 e4
v2 e3
v7
e10
v5 e5
v3
e7
e1
e8
e6
v8
v9 e2 v4 e9
A directed graph
(digraph)
v1 e4
v2 e3
v7
e10
v5 e5
v3
e7
e8
e6
v8
v9 e2 v4 e9
An undirected graph
Definition2
(1) A sequence of edges (vi ,v j ),(vj ,vk ),...,(vm ,vn ) is a walk.
(2) T helengthof a walk is the total number of edges traversed
in going from theintial vertex tothefinalone.
(3) A walk with no repeatedis said to be a path.
(4) A pathis sim ple if no vertexis repeated.
(5) A walk fromvi to itself with no repeatededges is called as a cycle with base vi .
(6) An edge froma vertex toitself is called a loop.
Example 3
e1
v1 e4
v2 e3
v7
e10
v5 e5
v3
e7
e8
e6
v8
v9 e2 v4 e9
walk : (v1,v2 ),(v2,v9 ),(v9,v7 ),(v7 ,v2 ),(v2,v9 )
path: (v1,v2 ),(v2,v9 ),(v9,v7 ),(v7 ,v2 )
cycle: (v2,v9 ),(v9,v7 ),(v7 ,v2 )
loop: (v3,v3 )
Definition 3
A tree is a directed graph that has no cycles. There is a one distinct
vertex in tree, called the root.
v1
Level 0
Level 1
Height=3
Level 2
Level 3
v2
root
v3 Parent of v6
v4
v8
v5
v7
v6
v9
Child of v3
1.1.4 A Proof Technique—Mathematical Induction
Definition1
Mathematic
al inductionis a mathodwe use to prove a sequence of statements
P1,P2 ,... to be true.
(i) Basis : P roving thatfor some k  1, P1 is true.
(ii) InductiveAssumption: Sopposingfor any n  k, Pn is true.
(iii) InductiveStep :
P rovingthat Pn 1 is true.
Example1
A binary tree (no parenthas morethen twochidren)of height n has at most 2n leaves.
Proof: We use l(n) to denotethemaximumnumber of leavesin a binary tree of height n.
Basis : Clealy l( 0 )  1  20.
InductiveAssumption: l(i)  2i , for i  0,1,...,n.
InductiveStep : T o get a binary tree of height n  1 fromone of height n, we can create,at most,
two leavesin placeof each previousone.
T herefore,l(n  1 )  2l(n).
Using theinductiveassumption, we get l(n  1 )  2  l(n)  2 n 1 .
Example2
n
Show thatSn   i 
i 0
n(n  1)
.
2
Proof:
1
Basis :
S1   i  1 
i 0
1( 1  1 )
2
n
InductiveAssumption: Assuming thatS n   i 
i 0
n 1
InductiveStep : S n 1   i  S n  n  1 
i 0
n(n  1 )
for all n  1.
2
n(n  1 )
(n  1)(n  2)
 n 1 
.
2
2