You are on page 1of 33

Instructor: Mr.

Advincula, MAT

SETS
Intuitively, a set is simply a collection of objects. More formally, we have the
following definition and notation:

Definition. A set is a collection of unordered, well-defined and distinct


objects. The objects that belong to a given set are called elements.

Notation:
sets uppercase letters A, B, C, . . ., X, Y, Z
elements lowercase letters a, b, c, . . . , x, y, z
set groupings braces { …}

Thus to indicate that the letters a, e, i, o, and u make up a set called A, we


write A = {a, e, i, o, u}; that is the elements are displayed between braces.

Based on our definition of a set, note that the order in which we present the
elements of a set is not important. Thus, the set A = {a, e, i, o, u} and the set B =
{u, i, o, a, e} actually describe the same set. Secondly, duplication of elements is
not allowed. For example, the set {1,3,4,4,7} can be rewritten as {1,3,4,7}.

When an element x belongs to a set A, we can denote this by “x  A”. If x is


not an element of the set A, then we write “x  A”.

Example: Let B = {a,b,{c}}. Note that


a) a  B
b) b  B
c) c  B
d) {c}  (that is, {c} is a set itself)

1
Instructor: Mr. Advincula, MAT

Other Basic Terms on Sets


Definition. The universal set, denoted by U, is the totality of all elements
under consideration.

The elements of the universal set will differ from application to application.
So if we speak of a set A = {a, e, i, o, u} then we may define U to be the set of all
letters of the English Alphabet. If we define A instead to be the set of students
enrolled in CS33 (Automata & Language Theory), the universal set with respect to
this set may be the entire set of students in the BS Computer Science curriculum in
CKC or it could be also be the set of students in CKC.

Definition. The Cardinality of a set A, denoted by | A |, is the number of


elements in the set A.

Example: Let
S = {set of students enrolled in CS 33}
B = {0,1,3,5,8}
Z = {x | x is an integer}
A = {a}

The cardinality of the above sets would be


|S| = 110 (or so)
|B| = 5
|Z| = ∞ (here we have an infinitive set)
|A| = 1 (this means that A is a singleton).

Definition. Two sets A and B are equal, denoted by A = B, if and only if


a) whenever x  A then x  B; and
b) whenever x  B then x  A.
In other words, two sets are equal if they have exactly the same elements. Another
formal way to express equality of two sets A and B is
(A = B) ↔ (x  A → x  B) ^ (x  B → x  A)

2
Instructor: Mr. Advincula, MAT

Example: Which of the following sets are equal?


A = {x | x2 + x – 6}
B = {2,-3)
C = {-3,2)
D = {3,-2,2)

Sets B and C are equal in particular since the order in which the elements are
written does not matter. The set A is equal to both B and C since they have exactly
the same elements even if they described differently.

Definition. A set A is said to be a subset of another set B, denoted by A 


B, if and only if every element of set A is also an element of set B. when A  B, we
also say that A is contained in B.

A more formal way to define subsets is

(A  B) ↔ (x)(x  A→ x  B)

Definition. A set A is said to be a proper subset of another set B, denoted


by A  B, if and only if A is a subset of B and there is at least one element of set B
that is not an element of set A. when A  B, we also say that A is strictly contained
in B.
A more formal way to define proper subsets are:
(A  B) ↔ (x)(x  B → x  A)

(A  B) ↔ (A  B)  (A  B)
Example: Let A = {A x  Z and x  10)
B = {x x  Z and x  10)
C = {1,2,3,4,5,6,7,8,9,10}
Note that
a) B  A, C  A, B  C and C  B.
b) A  A, B  B, and C  C
c) B  A, and C  A

3
Instructor: Mr. Advincula, MAT

d) But B  C and C  B since B = C.


e) A  A, ….

Remark: We can also define equality of two sets based on the concept of subsets:
(A = B) ↔ (A  B)  (B ) A)

===================================================
Exercise:
Prove the following theorem:

Theorem: [(A  B)  (B  C)] → (A  C)]

Example. Let A = {1,3,4,5}, B = {1,2,3,4,5,6}, and C = {1,2,3,4,5,6}

We can easily see that A  B and that B  C and therefore A  C. Note that
actually A  B (although B  C) which still implies that A  C.

Definition. A null or empty set, denoted by  or {}, is the unique set that
does not contain any element.
Note that = 0 and that   {}. In the latter, {} actually describes a
set that contains the empty set. Thus {} is not empty.
Example. The set A = {x x2-6x+9 = 0 and x < 0) is an empty set.
Definition. The power set of a set A, denoted by P(A), is the set of all
subsets of A or P(A) = {x x is a set and x A}.

Examples:
P(1,2,3) = {, {1}, {2}, {3}, {1,2}, {1,3},{2,3},{1,2,3}}
P() = {}

Note that P(A) = 2A. So if A = 3, then P(A) = 23 = 8. and in


particular since  = 0, then P() = 20 = 1.

4
Instructor: Mr. Advincula, MAT

OPERATIONS ON SETS
Definition. The union of two sets A and B, denoted by A  B, is made up of
those elements that belong to both A and B.

 A  B = {x (x  A)  (x  B)}

Definition. The intersection of two sets A and B, denoted by A  B, is


made up of those elements that belong to both A and B.

 A  B = {x (x  A)  (x  B)}

Definition. The symmetric difference of two sets A and B, denoted by A 


B, is made up of those elements that belong to A only or to B only.

 A  B = {x (x  A)  (x  B)  (x  (A  B))}

Definition. The relative complement of set B with respect to set A,


denoted by A-B, is made up of those elements that are in set A but are not in set B.

 A - B = {x (x  A)  (x  B)}

Definition. The complement of a set A, denoted by A’, is made up of those


elements in the universal set U that are not in A.

 A’ = U - A = {x (x  U)  (x  A)}

Example: Let A = {1,3,4,5) and B = {3,4,6,8}. Also define U =


{1,3,4,5,6,8,10} then
a) A  B = {1,3,4,5,6,8}
b) A  B = {3,4}
c) A - B = {1,5}
d) B - A = {6,8}
e) A  B = {1,5,6,8}
f) B’ = {1,5,10}
g) (A  B)’ = {1,5,6,8,10}
h) A  B’ = {1,3,4,5,10}
i) A   = {1,3,4,5}

5
Instructor: Mr. Advincula, MAT

Exercise:
Using the example above, what is the set define by:
1. A’  B
2. A’  B
3. (A  B)’

Definition. Two sets A and B are said to disjoint if and only if A  B = .

In other words, two sets are disjoint if they do not have any element in
common. An example of this would be the sets A = {set of negative integers} and
B = {set of positive integers}. Likewise if we consider another set C = {0}, this is
also disjoint with respect to both sets A and B.

LAWS OF SET THEORY


1. (A’)’ = A Double Negation (Involution Law)
2. (A  B)’ = A’  B’ De Morgan’s Laws
(A  B)’ = A’  B’
3. A  B = B  A Commutative Laws
AB=BA

4. A  (B  C) = (A  B)  C Associative Laws
A  (B  C) = (A  B)  C

5. A  (B  C) = (A  B)  (A  C) Distributive Laws
A  (B  C) = (A  B)  (A  C)
6. A  A = A Idempotency Laws
AA=A
7. A   = A Identity Laws
AU=A

8. A  A’ = U Inverse Laws (Complement Laws)


A  A’ = 

6
Instructor: Mr. Advincula, MAT

9. A  U = U Domination Laws (Bound Laws)


A=

10. A  (A  B) = A Absorption Laws


A  (A  B) = A

Let A, B  U

Theorem: A - B = A  B’
Theorem: A  B = (A  B) – (A  B)
Theorem: A  B = A B’
Theorem: (A – B)  A  (A  B)

Theorem: The following statements are equivalent to each other:


a) A  B
b) A  B = B
c) A  B = A
d) B’  A’

===================================================
Exercise:
Prove the following theorem:

Therefore: If A, B  U then A and B are disjoint if and only if A  B = A  B

RELATIONS
Before we formally define what a relation is, the following definition will be
necessary:

7
Instructor: Mr. Advincula, MAT

Definition. Let A and B be non-empty sets. The Cartesian product (or


cross-product) of sets A and B is a set of ordered pairs defined as A x B = {(x, y)
x  A  y  B}.
Thus the Cartesian product of two sets A and B is simply a set of ordered
pairs (x, y) where the elements of set A (the set named first) are sued to fill in the
first coordinates, x, and those of set B (the set listed next) are sued to fill in the
second coordinates, y. when obtaining the Cartesian product of sets A and B, we
therefore match or pair-off every element in set A with every element in set B.
Example: Let A = {a, b} and B = {1,2,3}. Then the Cartesian product of A
and B is A x B = {(a,1), (a,2), (a,3), (b,1), (b,2), (b,3)}
Remark. Note that A x B  B x A, unless A = B.
In general, if A1, A2,…, An are non-empty sets, then the n-fold Cartesian
product of the sets is the set of all possible ordered defined as:

A1 x A2 …. x An = {(x1, x2, ….xn) x1  A1, …., Xn  An}.


Now we define a relation for two and for two more sets.

Definition. A relation R from a set A to a set B is any subset of the


Cartesian product A x B. if an ordered pairs (x, y)  R , x is said to related to y by
R, also denoted by xRy.

Definition. In general, any subset Rn of the n-fold Cartesian product A1 x A2


x … x An is called an n-ary relation. An element Rn is called an ordered n-tuple.

Definition. The domain of a relation R, denoted by D(R), is the set of all


first coordinates in R.

Definition. The range of a relations R, denoted by R(R), is the set of all


second coordinates in R.

Definition. The field of a relations R, denoted by F(R), is the union of its


domain and range.

8
Instructor: Mr. Advincula, MAT

A relation therefore simply matches an element in set A with some element


(either one or more) in another set B.

Examples: Let A = B = {0,1,2,3}.


a) Define R1 = {(x,y)  x > y} = {(1,0). (2,0), (2,1), (3,0), (3,1), (3,2)}
Here, D(R1) = {1,2,3} and R(R1) = {0,1,2}.

b) Define R2 = {(x,y) x + y= 3} = {(0,3), (3,0), (1,2), (2,1)}


Here, D(R2) = R(R2) = {0,1,2,3}

c) Define R3 = {(x,y) x + y  6} = {(0,0), (0,1), … (3,2), (3,3)}


Here, D(R2) = R(R2) = {0,1,2,3} and note that R3 = A x b.

d) Define R4 = {(x,y) x - y = -4} = . This is an example of an empty relation or


-relation.

e) Define R5 = {(x,y) x = y } = {(0,0), ... , (3,3,)}. This is an example of an

identity relation, denoted by .

Exercise:

Let A = {0,1,2,3} and B = {2,3,4,5). What are sets described by the following:
1. R1 = {(x, y)  x > y }
2. R2 = {(x, y)  x + y = 3}
3. R3 = {(x, y)  x + y  6}
4. R4 = {(x, y)  x – y = -4 }
5. R5 = {(x, y)  x = y }

PROPERTIES OF RELATIONS

Therefore a few special types of relations depending on how the relation is


defined. However, it would be easier to determine if a given relation can be
classified as such by identifying its properties.

9
Instructor: Mr. Advincula, MAT

Definition. A relation R is reflexive if and only if (x)(x  F(R) → (x, x) 


R). in other words, a relation R is reflexive if it relates every element in its field to
itself.

Example: For this example and for the rest regarding properties of relations,
define F(R) = {a,b,c}.

Then R1 = {(a,b), (a,a), (c,c,), (c,b)} is not reflexive.


And R2 = {(a,b), (a,a), (b,b), (b,c), (c,c)} is reflexive.

Definition. A relation R is irreflexive if and only if (x)(x  F(R) → (x, x) 


R). A relation R is irreflexive if it does not relate any element in its field to itself.

Then R1 in the previous example is not irreflexive and neither is it reflexive.


But R3 = {(a,b), (a,c), (b,c)} is irreflexive.

Definition. A relation R is symmetric if and only if (x)(y)[x,y  F(R) →


((x,y  R → (y,x)  R]. A relation R is symmetric if for all ordered pairs (x,y)  R,
(y,x) should also be in R.

Example:
R4 = {(a,b), (b,c), (b,b), (b,a), (c,b)} is symmetric.
R5 = {(a,a), (c,c))} is also symmetric.
But R6 = {(a,b), (b,c), (c,b)} is not symmetric.

Definition. A relation R is asymmetric if and only if (x)(y)[x,y  F(R) →


((x,y  R → (y,x)  R]. A relation R is asymmetric if for all ordered pair (x,y)  R,
the ordered pair (y,x) should not appear in R.

Example:
R7 = {(a,b), (b,c)} is asymmetric.
R8 = {(a,b), (b,b)} is not asymmetric (nor symmetric).

10
Instructor: Mr. Advincula, MAT

Definition. A relation R is antisymmetric if and only if (x)(y)[x,y  F(R)


→ [((x,y)  (y,x)  R) → (y = x)]}. A relation R is antisymmetric if the only
symmetric pair in R is of the form (x,x).

Example:
R9 = {(a,a), (b,c)} is antisymmetric but not symmetric nor asymmetric.
R10 = {(a,b), (b,b), (b,a)} is not antisymmetric but is symmetric.
R11 = {(a,b), (a,c)} is not antisymmetric but is asymmetric.

Definition. A relation R is transitive if and only if (x)(y)(z) [x,y,z 


F(R) → [((x,y)  (y,z)  R) → (x,z)  R]}.

Example:
R12 = {(a,a), (a,b), (b,c), (a,c)} is transitive.
R13 = {(a,b), (b,c), (b,b)} is not transitive since it does not contain the
ordered pair (a,c) .
R14 = {(a,b), (a,c)} is transitive.
R15 = {(a,a), (c,c)} is also transitive.

Definition. A relation R is intransitive if and only if (x)(y)(z) [x,y,z 


F(R) → [((x,y)  (y,x)  R) → (x,z)  R]}.

Example:
R16 = {(a,b), (b,c), (c,a)} is intransitive.
R17 = {(a,b), (b,c), (b,b)} is not intransitive nor transitive. Note that R17
becomes intransitive if remove the pair (b,b).

Definition. A relation R is an equivalence relation if and only if R is


symmetric, reflexive and transitive.

Example: Let F(R) = {1,2,3,4}. Give examples of relations R having the


following properties:
1. Reflexive, symmetric and not transitive.
2. Reflexive, not symmetric and not transitive.

11
Instructor: Mr. Advincula, MAT

3. Reflexive, antisymmetric and not transitive.


4. Not reflexive, not antisymmetric but transitive and symmetric.
5. Not reflexive, not symmetric but transitive.

Solutions:
1. Reflexive, symmetric and not transitive.
R1 = {(1,1), (2,2), (3,3), (4,4), (1,2), (2,1), (2,3), (3,2), (3,4)}

2. Reflexive, not symmetric and not transitive.


R2 = {(1,1), (2,2), (3,3), (4,4), (1,2), (2,3), ((3,4)}

3. Reflexive, antisymmetric and not transitive.


R3 = {(1,1,), (2,2), (3,3), (4,4), (1,2), (2,3), (3,4)}.

4. Not reflexive, not antisymmetric but transitive and symmetric.


R4 = {(1,1), (2,2), (3,3), (1,2), (2,1), (2,3), (1,3), (3,2), (3,1), (1,4)}

5. Not reflexive, not symmetric but transitive.


R4 = {(1,1), (2,2), (3,3), (4,4),(1,2), (2,3), (3,4), (1,4)}

===================================================
Exercises:

Given F(R) = {a, b, c}. Give examples of relations R having the following
properties:
1. Reflexive, symmetric and not transitive.
2. Reflexive, not symmetric and not transitive.
3. Reflexive, antisymmetric and not transitive.
4. Not reflexive, not antisymmetric but transitive and symmetric.
5. Not reflexive, not symmetric but transitive.

Theorem. Let R be a relation. If R is reflexive or symmetric, then D(R) =


R(R).

12
Instructor: Mr. Advincula, MAT

Theorem. Let R be a relation. If R is symmetric and transitive, then R is also


reflexive.
Example. It is easy to verify the second theorem by checking back at a few
examples given earlier.
As for the first theorem, consider the relation below:
R = {(a,a), (b,c), (c,b), b,b), (c,c)}.
Note that F(R) = {a,b,c} and that D(R) = R(R) = {a,b,c}.

FUNCTIONS
Definition: Let A and B be non-empty sets. A function or mapping f from A
to B, is a relation from A to B in which every element of A appears exactly once as
the first coordinate of an ordered pair in the relation. If f is a function from set A to
set B, then we write f: A → B.
Note that from the definition, it can be understood that for a relation to be a
function, all the elements in the set A must appear as first coordinates in the
ordered pairs in the relation. In addition to this, each element in A must appear no
more than once as a first coordinate.

Example. We may visualize the relation {(1,c), (2,a), (3,a)} in Figure 1.1.

a
1
b
2 c
d
3
e

A B
Figure 1.1. The relation {(1,c), (2,a), (3,a)}.

Examples. Let A = {1,2,3,4} and B = {5,6,7}

13
Instructor: Mr. Advincula, MAT

1) The relation f = {(1,5), (3,4), (2,6), (4,5)} is a function.


2) The relation g = {(1,5), (2,6), (4,5)} is not a function since not all elements
of the set A are mapped. In this case, the element 3 is not mapped.
3) The relation h = {(1,5), (2,6), (2,7), (3,5), 4,6)} is not a function either
since an element of A (the number 2) appears twice as a first coordinate.

Exercises:
Let A = {1,2,3,4,5} and B = {a,b,c,d,e}. Give two examples of:

1. Relations that are functions


2. Relations that are not functions

Sometimes, to define a function more clearly, we use the notation given


below. Notice this notation clearly shows the relationship between an element of set
A and its corresponding element in the set B.
We also often define the function f as f(x) = y where (x,y)  f and x  A and
y  B.
 Y is called the image of the element x under the function f.
 X is called the pre-image.
For variety, sometimes the letter g and h are used in place of f.

Example. As an example of the notation given above, we may define the


function f on the set of integers as follows: f(x) = x + 2.

Example. In the example given earlier, i.e., f = {(1,5), (3,4), (2,6), (4,5)},
the image of the element 1 (of the set A) under the function f is 5. conversely, the
element 1 is the pre-image of 5.

Definition. Let f: A → B is a function from A to B.


 Set A is called the domain of f.
 Set B is called the codomain of f.

14
Instructor: Mr. Advincula, MAT

 Those elements that actually appear as second coordinates in f make


up the range of f, denoted by f(A). f(A) is also sometimes referred to
as the set of images of the elements of A.

Example: Let A = {1,2,3,4} and B = {5,6,7} and f = {(1,5), (2,7), (3,7),


(4,7)}. The domain of f is A = {1,2,3,4}; its codomain is B = {5,6,7} while its
range is {5,7}. Note that in this case (as it is in most cases), the range and the
codomain of f are not equal.
Notice that in our definition of a function, we specify that each element of the
domain a are mapped exactly once but we do not care how often each element of
the codomain B are mapped. We may define special types of functions based on
how each element of the codomain are mapped.

Definition. A function f: A → B is a 1-1 Function or Injection if and only if


each element of B appears at most once as a second coordinate in f.
A 1-1 function may be visualized as in Figure 1.2

A B
Figure 1.2. 1-1 function

Note that if sets A and B are finite sets, A  B must hold in order for
some function f: A → B to be a 1-1 function.

Example. Let A = {1,2,3} and B = {5,6,7,8}


1) f = {(1,5), (2,6), (3,8)} is a 1-1 function.
2) g = {(1,7), (2,5), (3,8), (1,6)} is not a 1-1 function; in fact, it is not a
function at all.

15
Instructor: Mr. Advincula, MAT

3) h = {(1,6), (1,5), (3,5)} is not a 1-1 function since the element 5 in the
set B appears twice as a second coordinate.

===================================================
Exercises:
Let A = {1,2,3} and B = {a,b,c,d}. Give two examples of:
1) 1-1 functions
2) Not 1-1 functions

Definition. A function f: A → B is an Onto function or Surjection if and


only if every element in B is mapped to at least one element in A.
An example of an Onto function is shown in Figure 1.3.

A B
Figure 1.3 An onto function.
Note that if the sets A and B are finite sets, A  B must hold in order for
some function f: A → B to be an onto function.
Examples. Let A = {1,2,3,4} and B = {5,6,7}

1) f = {(1,5), (2,7), (3,5), (4,6)} is an onto function.


2) h = {(1,2), (2,7), 3,7), (4,5)} is not an onto function since the element 6
in the set B is not matched to any element of set A.

===============================================
Exercises:
Let A = {1,2,3} and B = {a,b,c,d}. Give two examples of:
1) onto functions

16
Instructor: Mr. Advincula, MAT

2) not onto functions

Definition. A function f: A → B is an One-to-one correspondence or


Bijection if and only if f is both 1-1 and onto.
An example of one-to-one correspondence is given in Figure 1.4.

A B
Figure 1.4 A one-to-one correspondence.
Note that if the sets A and B are finite sets, A = B must hold in order for
some function f: A → B to be a one-to-one correspondence.

Example. Let A = {1,2,3,4} and B = {2,4,6,8}. The function f = {(x,y) y =


2x, x  A and y  B} is a bijection.
Exercises:

Let A = {1,2,3,4} and B = {a,b,c,d}. Give two examples of:


1) bijections
2) not bijections

17
Instructor: Mr. Advincula, MAT

ALPHABETS and LANGUAGES

Definition. A finite nonempty set of symbols is called an alphabet. The


alphabet is usually denoted by.

For example, the English alphabet is the of 52 symbols {a,b,c,...z, A, B, …,


Z}. The set of {0,1} is the binary alphabet used commonly in the theory of
computations. In programming languages like Pascal, for example, the alphabet
consist of the legal Pascal symbols such as Pascal identifiers, key and reserved
words, special characters and so on.

Definition. A string over an alphabet is a finite sequence of symbols from


the alphabet.

For example, computation is a string in the alphabet  = {a,b,c, …, Z} and


01010101 is a string in the alphabet  = {0,1}. A string may be composed of just
one symbol of the alphabet in which case we simply write the symbol, i.e., a is a
string in the alphabet  = {a, b, c, …, z}. also, it is possible to have a string with no
symbol and this is called the empty string and is denoted by .

We denote by exponentiation those parts of the string that repeats. For


example,

w3 = aaa if w = a
w3 = ababab if w = ab
hence, the string anbn denotes the string with n a’s followed by n b’s.

Definition. The set of all string, including the empty string, over the
alphabet  is denoted by *.

Definition. The length of a string is the number of symbols in the string.

18
Instructor: Mr. Advincula, MAT

For example, the length of computation is 11 and the length of 010101 is 6. we


denote the length of string w by w. thus abacus = 6 and  = 0.

Two strings over the same alphabet can be combined to form a third string
using the operation concatenation. The concatenation of the string x and y,
written as xy, is the string x followed by the string y. concatenation is obviously
associative, i.e., (wx)y = w(xy) for nay string w, x, and y.
A string v is a substring of string w if and only if there are strings (possibly
empty) x and y such that w = xvy. Clearly, x and y could both є, os every string is
a substring of itself. Taking x = w and v = y = є, we conclude that є is a substring
of every string.

If w = xv for some x where x is possibly empty, then v is a suffix of w; if w


= vy for some y which is possibly empty, then v is a prefix of w. For example,
basket is prefix of basketball and ball is the suffix of basketball.

The reversal of a string w, denoted by wR, is the string w spelled backwards.


For example, the reverse of bookshop is pohskooB.

The collection of symbols forms a string, while the collection of strings forms
a language. A language my be finite or infinite

Formal Grammars
Definition. A formal grammar is a four-tuple G = (V, T, P, S) where V is the
finite set of nonterminals, T is the set of termin0als, P is the set productions and S
is the start symbol (S  V). Each production in P is of the form УAΨ → УwΨ in which
У, Ψ and w are possibly empty strings in (V  T)* and A is a nonterminal.

A sentential form in the grammar is any string in (V  T)*.

If α → β is a production of G and w = УαΨ and w’ = УβΨ are sentential forms,


we say that w’ is immediately derived from w in G, and we indicate this by writing
w  w’.

19
Instructor: Mr. Advincula, MAT

If w1, w2 …,wn is a sequence of sentential forms such that w1  w2  …. 


wn, we say that wn is derivable from w1 and can be written as w1 * wn.

The sequence w1, w2, …,wn is called the derivation of wn from w1 according to
G.

Definition. The language L(G) generated by a formal grammar G is the set


of terminal strings derivable from S, i.e., L(G) = {w  T* S * w}.

Example: Consider the grammar G = (V, T, P, S) for the language L =


{akbkck  n ≥ 1}, where V = {A, B, C, S}, T = {a, b, c,} and the set of productions
{S → A,
A → aABC,
A → abC,
CB → BC,
bB → bb,
bC → bc,
cC → cc}.

A derivation for the string aabbcc is:

S Start Symbol
A S→A
 aABC A → aABC
 aabCBC A → abC
 aabBCC CB → BC
 aabbCC bB → bb
 aabbcC bC → bc
 aabbcc cC → cc

Note that the same language can be generated another grammar G = (V, T, P, S),
where V = {S, B}, T = {a, b, c} and the set of productions
{S → aBSc,

20
Instructor: Mr. Advincula, MAT

S → abc,
Ba → aB,
Bb → bb}.

A derivation for the string aabbcc is:

S Start Symbol
 aBSc S → aBSc
 aBabcc S → abc
 aaBbcc Ba → aB
 aabbcc Bb → bb

The example illustrate that for a specific language, it is possible to formulate


several different grammars for recognizing or generating the language. This is the
case because grammars are viewed as a model of computation. And we know that
for a particular computing problem, there are many possible algorithms for solving
that computing problem. Hence we can view grammars as algorithms for computing
the problem of generating or recognizing all the strings in a language.

Classes of Grammars
There are four types of grammars. These are:
1. Unrestricted Grammar (Type 0) – the definition of unrestricted grammar is
exactly similar to the definition of forma grammars.

The production rules in an unrestricted grammar may contain contracting and


noncontracting rules.

A contracting rule is one that produces a new sentential form wi+1 shorter in
length that w1. For example the rule bC → b is a contracting rule. A grammar
containing at least one contracting rule is a contracting grammar.

A noncontracting rule is a production rule α → β satisfying α  β.

21
Instructor: Mr. Advincula, MAT

2. Context-Sensitive Grammar (Type 1) – A context-sensitive grammar G = (V, T,


P, S) is a formal grammar in which all productions are the form: УAΨ → УwΨ, w ≠
. The grammar may contain the production S → . If G is a context-sensitive
grammar, then L(G) is a context-sensitive language.

Each production in a context-sensitive grammar is required to be


noncontracting. As a consequence, sentenial forms in any context-sensitive
derivation w1  w2 …  wn are nondecreasing in length, i.e.,  w1   w2  … 
 wn . It follows therefore that if G is a context-sensitive grammar,   L(G) iff
G contains the production S → .

3. Context-Free Grammar (Type 2) – A context-free grammar G = (V, T, P, S) is a


formal grammar in which all productions are of the form: A → w, where A  V and w
 (V  T)* - {}. Again   L(G) iff S →  is a production in the context-free
grammar.

4. Regular Grammar (Type 3) – A regular grammar is a formal grammar in which


the right-hand side of the production may contain at most a single nonterminal
symbol. If G is a regular grammar, then L(G) is a regular language.

22
Instructor: Mr. Advincula, MAT

REGULAR EXPRESSIONS, FINITE


AUTOMATA AND REGULAR LANGUAGE

Regular Expression

Let  be an alphabet. The regular expression over  and the sets that they
denote are defined recursively as follows:
1. ф is a regular expression and denotes the empty set;
2.  is a regular expression and denotes the set of {};
3. for each a in , a is a regular expression and denotes the set {a};
4. If r and s are regular expressions denoting the language R and S,
respectively, then (r+s), rs, r* are regular expressions denoting the sets R 
S, RS and R*, respectively.

When writing regular expressions, parenthesis may be omitted. The


precedence that should be followed when several operators are present is * having
the highest precedence followed by concatenation, then +. For example,
((0(1*))+0) may be written as 01* + 0.

Examples of regular expressions:

(0+1)* - set of all string of 0’s and 1’s


(0+1)*0000 – set of all string of 0’1 and 1’s with al least two consecutive 0’s
(1+10)* - set of all strings 0’s and 1’s beginning with 1 and not having two
consecutives 0’s
(0+1)*011 – set of all strings 0’s and 1’s ending in 011
a*b*c – set of strings with any number of a’s followed by any number of b’s
and followed by any number of c’s
aa*bb*cc* - subset of a*b*c* with at least one each of a,b,c. May be written
as a+b+c+

23
Instructor: Mr. Advincula, MAT

The set of all strings of 0’1 and 1’s (denoted by (0+1)*) is a regular expression
because
0 is a regular expression
1 is a regular expression
0+1 is a regular expression
(0+1)* is a regular expression

(0+1)* is a regular expression because 1 is a regular expression, 10 is a


regular expression, the same is true for 1+10 and finally (0+1)* which is a also a
regular expression.

The same arguments can be used for the other examples above.

Every regular expression represents a language. This is obvious from the


definition of regular expressions when we describe the set represented by a regular
expression. Later, we will know that this language described by regular expression
is called the regular language.

Example. What language is represented by (0*01+1)?


In terms of set notation, we can write this as:
{0}*{0}{1}  {1}

Note that {0}*{0}{1} is a set of strings that ends with a 1 but with one or more
0’s before it. On the other hand, {1} is a string with only 1 (but also with no 0‘s
before it). Hence, if we  the two sets, we end up with the set of strings that ends
in 1 but with zero or more 0’s before. That is,

L = {w {0, 1}* | w ends with a 1 but is prefixed by zero or more 0’s =


{0}*1.

24
Instructor: Mr. Advincula, MAT

Example. What language is represented by (0+1)*1?

Clearly, the string described by (0+1)* is the set of strings composed of any
number of 0’s and 1’s. then, the regular expression ends with a 1. This means that
the language represented by the regular expression is:

L = L = {w {0, 1}* | w ends with a 1}.

Alternatively, we can derived the language it represent by converting the


regular expression to set notation, i.e.,

(0+1)*1 can be written as ({0} ({1}*{1} = ({0,1})*{1} = {0,1}*{1}.

Example. What language is represented by the regular expression a*ab + b.

a*a describes the language composed of strings of a’s with at least one a. a*
ab is the set of strings composed at least one a ended by ab. a*ab is the set of
string composed any number of a’s followed by ab. Formally,

L = {w {a, b}* | w composed of any number of a followed by ab}

Example. Rewrite the regular expression a* + b* + (a+b)* as simpler


expression representing the same set.

Note that a* is a subset of set (a+b). The same is true with b*. Hence, a* +
b* + (a+b)* = (a+b)*

Example. Rewrite the regular expression (aa)*a + (aa)* as a simpler


expression representing the same set.

(aa)*a – contains odd number of a’s with the shortest containing one a.
(aa)* – contains even number of a’s with the shortest containing no a.

Hence, (aa)*a + (aa)* = a*.

25
Instructor: Mr. Advincula, MAT

Example. Write a regular expression for the language L = {w  {a,b}*  w


contains no more that 2 a’s.

The regular expression for L is b* + b*ab*+ b*ab*ab*.

Example. Write regular expression for L = {ai  i >0}.

The regular expression for L is aa*.

Example.Given  = (0,1), write regular expression for L = {w  *  each 1


appearing in w is immediately following by the substring 10}.

Solution: (110 + 0)

Finite Automata
A finite automaton (figure below) is a restricted model of an actual computer.
It has a processor, but has no memory. Its input is in the form of a string that is
delivered to it on an input tape. It delivers no output except an indication of
whether the input is accepted or not. Hence, a finite automaton is basically a
language recognizer.

Input Tape
a b a b b b a a a b b

q0 q1
q5 q2
q4 q3
Finite Control
Finite Automaton

26
Instructor: Mr. Advincula, MAT

Deterministic Finite Automata (DFA)


Formally, a deterministic finite automaton can be denoted by a quintuple M =
(Q, , δ, q0, F), where Q is a set of states,  is the finite input symbol alphabet, q0
is the initial state, F is the final states, and δ is the transition function mapping Q x
 to Q (i.e., δ(q,a) is the state for each state q and input symbol a).

A string w is said to be accepted by a finite automaton M = (Q, , δ, q0, F) if


δ(q0, w) = p for some p in F.

Before one can say that w is accepted or not, however, the string w need to
be read one symbol at a time and every time a symbol is read M changes state. The
intermediate states of M are what we call the sequence of configuration that
represents the status of the finite control (processor state), reading head and input
tape at successive moments. We represents a configuration by the pair (q,w) where
q is the current state of the DFA, w is the remaining string and the first symbol of w
is where the reading head is pointing.

The binary operation - will be defined to show the transition from one
configuration to another. Trhus if (q,w) and (q’,w’) are two configurations of M,
then (q,w) -(q’,w’) if and only if w = σ w’ for some σ   and δ(q, σ) = q’.

Example. Consider the DFA M = (Q, , δ, q0, F) where Q = {q1, q2, q3}, 
= {0,1}, F = {q0}. The transition function δ may be presented in tabular form
(transition table),
inputs
states 0 1
q0 q2 q1
q1 q3 q0
q2 q0 q3
q3 q1 q2

or in graphical form called the transition diagram below

27
Instructor: Mr. Advincula, MAT

q1
1 0

1 0
q0 q2
0 1

0 1
q3

Figure 2.2 A transition Diagram

Note that with the transition diagram, the five components of M may be
deduced. For example, given the transition diagram above, one can figure out that
Q = {q0, q1, q2, q3} and  = {0,1}. The final states are represented by states in
two consecutive circles and the initial states is always q0. From now, we shall use
the transition diagram to represent finite automaton.
Suppose M is given the input w = 110101. Does M accept w or not? This can
be answered using the notations for configuration of FA’s.
(q0, 110101
- (q1, 10101)
- (q0, 0101)
- (q2, 101)
- (q3, 01)
- (q1, 1)
- (q0, )

Since, (q0, 110101) -* (q0, ) and q0 is in F, 110101 is accepted b y M.


Example: Describe the language recognized by the DFA whose transition diagram is
given in Figure 2.3. a
q0 q1

b
Figure 2.3

28
Instructor: Mr. Advincula, MAT

Note that starting from q0, it can stop and accept immediately. Hence, the
empty string is part of the language. If the language is not empty, then it must
take an a and for it to be accepted by the DFA, it should take a b after a. Therefore,
every time an a is recognized it should be followed by a b to be accepted. Hence,
ab is in the language. Note that after ab it could be followed by another ab and
another and so on. Therefore, the language accepted by the DFA is

L = (ab)*
that is any number greater than or equal to 0 of ab’s.

Example. Describe the language recognized by the DFA whose transition


diagram is given in Figure 2.4.

a
q0 q1

d c

q2

Figure 2.4

Note that the difference from the previous example is the addition of another
state q2 that is connected to q0. Hence, the original language (ab)* is still being
recognized by this DFA. Similarly, the language (cd)* is also recognized by the DFA.
This time, however, the DFA can either go to q1 or q2 and repeat this as many as it
likes. Therefore, the language recognized by the DFA is

L = ((ab)* + (cd)*)*

29
Instructor: Mr. Advincula, MAT

Example. Describe the language recognized by the DFA whose transition diagram is
given in Figure 2.5.

a c
q0 q1 q2

b d
Figure 2.5

This time we extended the transition diagram from a non-final state. Again,
DFA recognized the language (ab)*. Note that after a it could go to c and d and
repeat this as many times as it likes. Therefore, the language recognized is

L = (a(ad)*b)*

Example. Design a DFA that accepts the language L = {w  {0,1}  w


containing exactly one 0.
The transition diagram for this DFA is given in Figure 2.6.

1 1

0
q0 q1

Figure 2.6 A transition diagram for L.

Formally, we can define a DFA M = (Q, , δ, q0, F) where Q = {q0, q1},  =


{0,1}, F = {q1}. The transition function δ may be presented in tabular form
(transition table),
inputs
states 0 1
q0 q1 q0
q1 - q1

30
Instructor: Mr. Advincula, MAT

An example computation for the string 111011 is :

(q0, 111011
- (q0, 11011)
- (q0, 1011)
- (q0, 011)
- (q1, 11)
- (q1, 1)
- (q1, )

Note that whatever the state goes to q1 for the string to be accepted it
should go back to q0. Hence, the set of strings described by the DFA above is

L = {w  {0,1}*  w contains an even number of 0’s}.

An example computation for the string 1101011 is:


(q0, 1101011
- (q0, 101011)
- (q0, 01011)
- (q1, 1011)
- (q0, 011)
- (q0, 11)
- (q0, 1)
- (q0, ).

Exercise:
Construct a DFA for each of the following Languages
1. 0*(1+0*)*
2. (0+)(1+)
3. 01*0
4. (00+1)*

31
Instructor: Mr. Advincula, MAT

↔→γγΨΨУąαβ≠ßδβQσ

^
q0

New # 09269689542

32
Instructor: Mr. Advincula, MAT

33

You might also like