CS242 Module 1
CS242 Module 1
26/12/2021
Theory of Computing
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
• Sets, relations and functions
What is a set?
• A set is a group of “objects”
• Students in a class: { Ali, Idris, Fazal, Ravi}
• Courses offered by a department: { CS101, CS211, … }
• Colors: { red, orange, yellow, green, blue, purple }
• Although a set can contain (almost) anything, we will
most often use sets of numbers
• All positive numbers less than or equal to 7: {1, 2, 3, 4, 5, 6, 7}
• The set of all natural numbers: {1, 2, 3, 4, …}
7
Set properties
8
Specifying a set
9
Special sets
• U is the universal set – the set of all of elements (or the “universe”)
from which given any set is drawn
• For the set {-2, 0.4, 2}, U would be the real numbers
• For the set {0, 1, 2}, U could be the natural numbers (zero and up), the
integers, the rational numbers, or the real numbers, depending on the
context
• If a set has zero elements, it is called the empty (or null) set
• Written using the symbol
• Thus, = { }
• As the empty set is a set, it can be a element of other sets
• { , 1, 2, 3, x } is a valid set
10
Set equality
• Two sets are equal if they have the same elements
• {1, 2, 3, 4, 5} = {5, 4, 3, 2, 1}
• Remember that order does not matter!
• {1, 2, 3, 2, 4, 3, 2, 1} = {4, 3, 2, 1}
• Remember that duplicate elements do not matter!
• Two sets are not equal if they do not have the same
elements
• {1, 2, 3, 4, 5} ≠ {1, 2, 3, 4}
11
Subsets
• If all the elements of a set S are also elements of a set T, then S
is a subset of T
• For example, if S = {2, 4, 6} and T = {1, 2, 3, 4, 5, 6, 7}, then
S is a subset of T
• This is specified by S T
• Or by {2, 4, 6} {1, 2, 3, 4, 5, 6, 7}
• If S is not a subset of T, it is written as such:
• ST
• For example, {1, 2, 8} {1, 2, 3, 4, 5, 6, 7}
• Note that any set is a subset of itself!
• Given set S = {2, 4, 6}, since all the elements of S are
elements of S, S is a subset of itself
• This is kind of like saying 5 is less than or equal to 5
• Thus, for any set S, S S
12
Proper Subsets
• If S is a subset of T, and S is not equal to T, then S is a
proper subset of T
• Let T = {0, 1, 2, 3, 4, 5}
• If S = {1, 2, 3}, S is not equal to T, and S is a subset of T
• A proper subset is written as S T
• Let R = {0, 1, 2, 3, 4, 5}. R is equal to T, and thus is a subset
(but not a proper subset) or T
• Can be written as: R T and R T (or just R = T)
• Let Q = {4, 5, 6}. Q is neither a subset or T nor a proper
subset of T
13
Set cardinality
• The cardinality of a set is the number of elements in a
set
• Written as |A|
• Examples
• Let R = {1, 2, 3, 4, 5}. Then |R| = 5
• || = 0
• Let S = {, {a}, {b}, {a, b}}. Then |S| = 4
• This is the same notation used for vector length in
geometry
• A set with one element is sometimes called a singleton
set
14
Power sets
• Given the set S = {0, 1}. What are all the possible subsets of S?
• They are: (as it is a subset of all sets), {0}, {1}, and {0, 1}
• The power set of S (written as P(S)) is the set of all the subsets of S
• P(S) = { , {0}, {1}, {0,1} }
• Note that |S| = 2 and |P(S)| = 4
• Let T = {0, 1, 2}. The P(T) = { , {0}, {1}, {2}, {0,1}, {0,2}, {1,2},
{0,1,2} }
• Note that |T| = 3 and |P(T)| = 8
• P() = { }
• Note that || = 0 and |P()| = 1
15
Cartesian products
• A Cartesian product is a set of all ordered 2-tuples where
each “part” is from a given set
• Denoted by A x B, and uses parenthesis (not curly brackets)
• For example, 2-D Cartesian coordinates are the set of all ordered
pairs Z x Z
• Recall Z is the set of all integers
• This is all the possible coordinates in 2-D space
• Example: Given A = { a, b } and B = { 0, 1 }, what is their Cartesian
product?
• C = A x B = { (a,0), (a,1), (b,0), (b,1) }
• Note that Cartesian products have only 2 parts in these examples
(later examples have more parts)
• Formal definition of a Cartesian product:
• A x B = { (a,b) | a A and b B }
16
Set operations: Union
• Formal definition for the union of two sets:
A U B = { x | x A or x B }
• Further examples
• {1, 2, 3} U {3, 4, 5} = {1, 2, 3, 4, 5}
• {New York, Washington} U {3, 4} = {New York, Washington, 3, 4}
• {1, 2} U = {1, 2}
• Properties of the union operation
• AU=A Identity law
• AUU=U Domination law
• AUA=A Idempotent law
• AUB=BUA Commutative law
• A U (B U C) = (A U B) U C Associative law
17
Set operations: Intersection
• Formal definition for the intersection of two sets: A ∩ B = { x | x A
and x B }
• Further examples
• {1, 2, 3} ∩ {3, 4, 5} = {3}
• {New York, Washington} ∩ {3, 4} =
• No elements in common
• {1, 2} ∩ =
• Any set intersection with the empty set yields the empty set
• Properties of the intersection operation
• A∩U=A Identity law
• A∩= Domination law
• A∩A=A Idempotent law
• A∩B=B∩A Commutative law
• A ∩ (B ∩ C) = (A ∩ B) ∩ C Associative law
18
Disjoint sets
• Formal definition for disjoint sets: two sets are disjoint if their
intersection is the empty set
• Further examples
• {1, 2, 3} and {3, 4, 5} are not disjoint
• {New York, Washington} and {3, 4} are disjoint
• {1, 2} and are disjoint
• Their intersection is the empty set
• and are disjoint!
• Their intersection is the empty set
19
Set operations: Difference
20
Complement sets
• Formal definition for the complement of a set: A = { x | x A }
• Or U – A, where U is the universal set
• Further examples (assuming U = Z)
• {1, 2, 3} = { …, -2, -1, 0, 4, 5, 6, … }
• Properties of complement sets
21
Set identities: DeMorgan’s Law
A B = A B
A B = A B
22
Relations and Functions
23
Definitions
• A relation between two variables x and y is a set of
ordered pairs.
• An ordered pair consists of an x and y-coordinate
◼ A relation may be viewed as ordered pairs, mapping
design, table, equation, or written in sentences.
• x-values are called input, independent variable, or
domain.
• y-values are called output, dependent variable, or
range.
24
Example 1
Is this a relation?
Input 4 –5 0 9 –1
Output –2 7
• Domain
{4, -5, 0, 9, -1}
• Range
{-2, 7}
25
Is relation a function?
• What is a function?
• A function is a relation in which every input has
exactly one output”.
• Focus on the x-coordinates, when given a relation
• If the set of ordered pairs has different x-coordinates,
it is a function.
26
RELATIONS
xi R yi
Example: R = ‘=‘
•x=x
•x=y y=x
• x = y and y = z x=z
Equivalence Classes
For equivalence relation R equivalence class of x = {y : x R y}
Example:
R = { (1, 1), (2, 2), (1, 2), (2, 1), (3, 3), (4, 4), (3, 4), (4, 3) }
Equivalence class of 1 = {1, 2}
Equivalence class of 3 = {3, 4}
Example 2
• Is this a relation?
◼ Yes
• Is this a function?
◼ Yes
30
Example 3
{(–1, −7), (1, 0), (2, −3), (0, −8), (0, 5), (–2, −1)}
• Is this a function?
◼ No
31
Example 4
Which relation mapping represents a function?
One Two
–1 2 2
3
1 2 –1 3
0 3 3 –2
0
Ans. One
32
Example 5
Which relation mapping represents a function?
A. B. B.
Ans. B
33
Vertical Line Test
• Vertical Line Test: A relation is a function if a
vertical line drawn through its graph, passes
through only one point.
• The Pencil Test: Take a pencil and move it from
left to right (–x to x); if it crosses more than one
point, it is not a function
34
Vertical Line Test
35
Vertical Line Test
36
Discrete or Continuous Functions (1)
A Discrete function
▪ Domain: {-7, 1, 5, 7, 8, 10}
▪ Range: {1, 0, -7, 5, 2, 8}
37
Discrete or Continuous Functions (2)
A Continuous function
▪ Domain: [-8, 8]
▪ Range: [-6, 6]
38
Domain and Range in Real Life
• Johnny bought at most 10 tickets to a concert for him
and his friends. The cost of each ticket was $12.50.
• Complete the table below to list the possible domain
and range.
1 2 3 4 5 6 7 8 9 10
12.50 25.00 37.50 50 62.50 75 87.50 100 112.50 125
39
• Graphs and trees
What is a graph?
• Graphs represent the relationships among data items
• A graph G consists of
• a set V of nodes (vertices)
• a set E of edges: each edge connects two nodes
• Each node represents an item
• Each edge represents the relationship between two items
node
edge
41
Formal Definition of graph
• The set of nodes is denoted as V
• For any nodes u and v, if u and v are connected by an edge,
such edge is denoted as (u, v)
• The set of edges is denoted as E
• A graph G is defined as a pair (V, E)
v
(u, v)
42
Path and simple path
• A path from v1 to vk is a sequence of nodes v1, v2, …, vk that are
connected by edges (v1, v2), (v2, v3), …, (vk-1, vk)
• A path is called a simple path if every node appears at most once.
v2 v3
v1
v4 v5
43
Cycle and simple cycle
v2
v1 v3
v4 v5
44
Connected graph
v2 v3
v1
v4 v5
45
Directed graph (digraph)
• All previous graphs are undirected graph
• If each edge in E has a direction, it is called a directed edge
• A directed graph is a graph where every edges is a directed
edge
Madinah Makkah
500
Directed edge
1400
1000
Riyadh
46
Property of graph
• A undirected graph that is connected and has no cycle is a tree.
• A tree with n nodes have exactly n-1 edges.
• A connected undirected graph with n nodes must have at least n-1 edges.
47
What is a tree?
node
edge
48
Terminology I
• For any two nodes u and v, if there is an edge pointing
from u to v, u is called the parent of v while v is called
the child of u. Such edge is denoted as (u, v).
• In a tree, there is exactly one node without parent,
which is called the root. The nodes without children
are called leaves.
root
u
u: parent of v
v: child of u
v
leaves 49
Terminology II
internal nodes
leaves
50
Terminology III
• If two nodes have the same parent, they are siblings.
• A node u is an ancestor of v if u is parent of v or
parent of parent of v or …
• A node v is a descendent of u if v is child of v or child
of child of v or …
T
A subtree of T
v v
52
Terminology V
• Level of a node n: number of nodes on the path from root to
node n
• Height of a tree: maximum level among all of its node
Level 1
Level 2
height=4
n Level 3
Level 4
53
Binary Tree
• Binary Tree: Tree in which every node has at most 2 children
• Left child of u: the child on the left of u
• Right child of u: the child on the right of u
x: left child of u
u v y: right child of u
w: right child of v
z: left child of w
x y w
z
54
Property of binary tree (I)
• A full binary tree of height h has 2h-1 nodes
No. of nodes = 20 + 21 + … + 2(h-1)
= 2h – 1
Level 1: 20 nodes
Level 2: 21 nodes
Level 3: 22 nodes
57
Strings
A string or word is a finite sequence of symbols
taken from ∑.
• The empty string is (epsilon).
58
Powers of an alphabet
Let ∑ be an alphabet.
• ∑* = ∑0 U ∑1 U ∑2 U …
• ∑+ = ∑1 U ∑2 U ∑3 U …
59
Languages
L is a said to be a language over alphabet ∑, only if L ∑*
▪ This is because ∑* is the set of all strings (of all possible length
including 0) over the given alphabet ∑.
Examples:
1. Let L be the language of all strings consisting of n 0’s followed
by n 1’s:
L = {, 01, 0011, 000111,…}
1. Let L be the language of all strings of equal number of 0’s and
1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}
60
The Membership Problem
Given a string w ∑*and a language L over ∑, decide whether
or not w L.
Example:
Let w = 100011
Q) Is w the language of strings with equal number of 0s
and 1s?
A) Yes
61
Finite Automata
• Some Applications
• Software for designing and checking the behavior of
digital circuits
• Lexical analyzer of a typical compiler
• Software for scanning large bodies of text (e.g., web
pages) for pattern finding
• Software for verifying systems of all types that have a
finite number of states (e.g., stock market transaction,
communication/network protocol)
62
Finite Automata: Examples
• On/Off switch action
state
63
Structural expressions
• Grammars
• Regular expressions
• E.g., Unix style to capture city names such as “Palo Alto CA”:
• [A-Z][a-z]*([ ][A-Z][a-z]*)*[ ][A-Z][A-Z]
64
• Principle of Induction
Induction
This method consists of three basic steps:
Step 1: Prove P(n) for n = 0 or 1. This is called the proof for the basis.
Step 2: Assume the result/properties for P(n). This is called the
induction hypothesis.
Step 3: Prove P(n + 1) using the induction hypothesis.
If we have statements P1, P2, P3, …
If we know:
for some k that P1, P2, …, Pk are true, for any n >= k tha P1, P2, …, Pn
imply Pn+1, then every Pi is true.
Proof by Induction (modified)
1. Inductive basis:
▪ Find P1, P2, …, Pk which are true
2. Inductive hypothesis:
▪ Let us assume P1, P2, …, Pn are true, for any n >= k
3. Inductive step:
▪ Show that Pn+1 is true
Example
Theorem: A binary tree of height n has at most 2n leaves.
Proof:
let l(i) be the number of leaves at level i
l(0) = 0
l(1) = 2
l(2) = 4
l(3) = 8
Induction Step
Level
n hypothesis: l(n) <= 2n
n+1
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, …, n
• Induction step
•we need to show that l(n + 1) <= 2n+1
Induction Step
Level
n hypothesis: l(n) <= 2n
n+1
2 = n/m
n and m have no common factors
We will show that this is impossible
Example (2)
2 = n/m 2 m2 = n2
n is even
Therefore, n2 is even
n=2k
m is even
2 m2 = 4k2 m2 = 2k2
m=2p
Therefore, m and n have common factor 2
Contradiction!
Deductive Proofs
“implies” or “follows”
77
Main Reference
1. Sets, set operations, and terminologies
2. Relations, functions and their kinds
3. Graphs and Trees
4. Strings and Languages
5. Boolean Logic
Fundamental Proof Techniques (KLP Mishra, N. Chandrasekaran
(2008). Theory of Computer Science: Automata, Languages and
Computation. (3rd Edition). PHI.)
Additional References
https://www.inf.ed.ac.uk/teaching/courses/dmmr/slides/13-14/Ch2.pdf
https://ncert.nic.in/ncerts/l/kemh104.pdf
https://www.cs.virginia.edu/~asb/teaching/cs202-spring07/slides/07-sets.ppt
https://www.paulding.k12.ga.us/cms/lib/GA01903603/Centricity/Domain/3809/Relations%20and
%20Functions%20Power%20Point.ppt
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
Thank You