Lecture 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 57

09/01/2022

THEORY OF COMPUTING 1
1
Theory of Computing
(CPS422)

- UNIVERSITY OF THE GAMBIA -UTG-

09/01/2022
THEORY OF COMPUTING 2
2 Jabbi
Preview

Review
Mathematical Notions and Terminology
Alphabet, Strings & Languages

09/01/2022 THEORY OF COMPUTING 3 Jabbi


Review
This is the first “pure” course in theoretical computer science. We will
be concerned with the following basic questions in computer science:
1. What is a computation?
2. Is there a universal model of computation?
3. Can everything be computed?
4. Is there a relation between computations and mathematics?
5. Can we identify problems that are not computable?
6. What resources are needed to perform a certain computation?
7. Can we identify computationally hard problems?
09/01/2022 THEORY OF COMPUTING 4 Jabbi
Review
Can we mathematically model the computation?
oAutomata Theory
 Regular languages, Context-free languages, Turing machines

Can the computer solve all problems?


oComputability Theory

How efficient the solution is?


oComplexity Theory
 Time and space complexity (Big O notation)

09/01/2022 THEORY OF COMPUTING 5


Mathematical Notions and
Terminology

THEORY OF COMPUTING 6
6 Jabbi
Preview

Sets
Functions
Relations
Graphs
Proof Techniques

09/01/2022 THEORY OF COMPUTING 7 Jabbi


Sets
A set is a collection of objects
oOrder and repetition is irrelevant
 X = {1, 2, 3}
 X = {n | 1  n  3, n  N }
 Y = { } =   Y is an empty set
oThe  symbol signifies membership (belong to):
 x  X indicates that x is a member or element of the set X
 x  X indicates that x is not a member or element of the set X
◦ Assume X = {1, 2, 3}, then
◦ 1  X, but 4  X

09/01/2022 THEORY OF COMPUTING 8


Sets
Set Representations

oC = { a, b, c, d, e, f, g, h, i, j, k }

finite set
oC = { a, b, …, k }

infinite set

oS = { 2, 4, 6, … }

oS = { j : j > 0, and j = 2k for some k>0 }

oS = { j : j is nonnegative and even }


09/01/2022 THEORY OF COMPUTING 9
Sets
Universal Set U
A = { 1, 2, 3, 4, 5 }
6 A
2 3 8
1
7 4 5
9
10
Universal Set: all possible elements

 U = { 1 , … , 10 }

09/01/2022 THEORY OF COMPUTING 10


Sets
Set Operations
Assume X = {1,2,3} and Y = {2,4}

oUnion
 X  Y = {z | z  X or z Y} = {1,2,3,4}

oIntersection
 X  Y = {z | z  X and z Y} = {2}

oDifference
 X - Y = {z | z  X and z  Y} = {1,3}
09/01/2022 THEORY OF COMPUTING 11
Sets
Set Operations

oComplement
 Let X be a subset of Y
 The complement of X ( X ) with respect to Y is the set of elements in Y but not in X
 X = { z | z  Y and z  X } = Y - X
oAssume X = {1,2,3} is a subset of Y = {1,2,3,4}
o X = {4}

09/01/2022 THEORY OF COMPUTING 12


Sets

DeMorgan’s Laws

o (X  Y) = X  Y

o (X  Y) = X  Y

09/01/2022 THEORY OF COMPUTING 13


Sets
Empty, Null Set: 
={}

S=  = Universal Set


S=S
S - =S
 - S=
09/01/2022 THEORY OF COMPUTING 14
Sets
Power Set
The power set of X is the set of all subsets of X
o(X) = 2X = { Y | Y  X}
o|(X)| = 2|X|
Example X = {1, 2, 3}
o2X = (X)={{},{1},{2},{3},{1,2},{1,3},{2,3}, {1,2,3}}
o{1,2}  X is a subset of X
o{1,2,3} is a superset of {1, 2}

09/01/2022 THEORY OF COMPUTING 15


Sets
Cardinality

For finite sets:


oA = { 2, 5, 7 }

o|A| = 3

(set size)

09/01/2022 THEORY OF COMPUTING 16


Sets
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|

09/01/2022 THEORY OF COMPUTING 17


Functions
domain range
Let f be a function defined on a
set A and taking values to a set 4 A B
B. Set A is called the domain of f(1) = a a
f; set B is called the range of f. 1
2 b
3 c
5
oIf A = domain
o then f is a total function f : A -> B
o otherwise f is a partial function

09/01/2022 THEORY OF COMPUTING 18


Relations
Let A & B be sets. A binary relation “R” from A to B
R = {(x1, y1), (x2, y2), (x3, y3), …}
Where Xi  A and Yi  B
R⊆AxB

xi R yi to denote (a,b)  R

e. g. if R = ‘>’: 2 > 1, 3 > 2, 3 > 1


09/01/2022 THEORY OF COMPUTING 19
Relations
Equivalence Relations
•Reflexive: xRx
• Symmetric: x R y yRx
• Transitive: x R y and y R z xRz
oExample: R = ‘=‘
x=x
x=y y=x
 x = y and y = z x=z

09/01/2022 THEORY OF COMPUTING 20


Graphs

oVertices and edges


oDirected Graphs and labelled graphs
oWalking(traversing) a graph
oPath and cycles
oEuler tour and Hamilton cycles

09/01/2022 THEORY OF COMPUTING 21


Trees

oRoot, parent, child/leaf


oTree height
oLevels in a tree

09/01/2022 THEORY OF COMPUTING 22


Proof Techniques

•Proof by induction
• Proof by contradiction
•Construction
•Contradiction
•If-And-Only-If Proofs

09/01/2022 THEORY OF COMPUTING 23


Proof by induction
We have statements P1, P2, P3, …
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
09/01/2022 THEORY OF COMPUTING 24
Proof by induction
oInductive basis
Find P1, P2, …, Pb which are true
o Inductive hypothesis
Let’s assume P1, P2, …, Pk are true,
for any k >= b
o Inductive step
Show that Pk+1 is true
09/01/2022 THEORY OF COMPUTING 25
Proof by induction
Example:
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
◦ We want to show: L(i) <= 2i

09/01/2022 THEORY OF COMPUTING 26


Proof by induction

• 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

09/01/2022 THEORY OF COMPUTING 27


Proof by induction
Induction Step

09/01/2022 THEORY OF COMPUTING 28


Proof by induction
Induction Step

L(k) <= 2k

L(k+1) <= 2 * L(k) <= 2 * 2k = 2k+1


(we add at most two nodes for every leaf of level k)
09/01/2022 THEORY OF COMPUTING 29
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

09/01/2022 THEORY OF COMPUTING 30


Proof by Contradiction
Theorem: 2 is not rational

Proof:
Assume by contradiction that it is rational
 2 = n/m
 n and m have no common factors
We will show that this is impossible

09/01/2022 THEORY OF COMPUTING 31


Proof by Contradiction
2 = n/m 2 m2 = n2

n is even
Therefore, n is even
2

n=2k

m is even
2 m2 = 4k2 m2 = 2k2
m=2p

Thus, m and n have common factor 2


Contradiction!
09/01/2022 THEORY OF COMPUTING 32
Proof by Construction

Many theorems state that a particular type of object exists.


One way to prove such a theorem is by demonstrating how
to construct the object. This technique is a proof by
construction.
See reference book, section 0.4

09/01/2022 THEORY OF COMPUTING 33


If-and-Only-if proofs
Often, a statement we need to prove is of the form
o“X if and only if Y”

We are then required to do two things:


oProve the if-part: Assume Y and prove X
oProve the only-if-part: Assume X, prove Y

Remember:
oThe if and only-if parts are converses of each other

One part, say “if X then Y” says nothing about whether Y is true when
X is false.
An equivalent form to “if X then Y” is “if not Y then not X”; the latter
is the contra-positive of the former.
09/01/2022 THEORY OF COMPUTING 34
Alphabet, Strings & Languages

THEORY OF COMPUTING 35
35 Jabbi
Alphabet, Strings & Languages
A SYMBOL is an abstract entity.
oLetters (a-z) and digits (0-9) are examples of frequently used symbols.
Alphabet: Finite, nonempty set of symbols
oExamples:
 1 = {0, 1}: binary alphabet
 2 = {a, b, c, …, z}: the set of all lower case letters
 3 = {0, 1, x , y , z }.
 The set of all ASCII characters

String: Finite sequence of symbols from an alphabet 


oExamples:
 01101 where  = {0, 1}
 abracadabra where  = {a, b, c, …, z}

09/01/2022 THEORY OF COMPUTING 36


Alphabet, Strings & Languages
Empty String. The string that contains no symbols is denoted by . In some books it is also
denoted by λ. Notice that the length of this string is 0.
Length of String: Number of symbols in the string.
o The length of a string w is usually written |w|
o |1010| = 4
o|  | = 0
o |uv| = |u| + |v|
o 01011 is a string of length 5 over Σ = {0, 1}
o is a string of length 0 over Σ = {0, 1}.
o 10xx is a string of length 4 over  = {0, 1, x , y }.
2
o abpqrsxy is a string of length 8 over  = {a, b, · · · , z }.
3

Reverse : wR
o If w = abc, wR = cba
09/01/2022 THEORY OF COMPUTING 37
Strings and Languages
Substring: any string of consecutive characters in some string w
◦ If w = abc
◦ e, a, ab, abc, c, bc are substrings of w

Prefix and suffix:


◦ if w = vu
◦ v is a prefix of w
◦ u is a suffix of w
◦ Example
◦ If w = abc
◦ a, ab , abc are prefixes of w
◦ c, bc, abc are suffixes of w

THEORY OF COMPUTING 38
Strings and Languages

Suppose: S is the string banana


◦ Prefix : ban, banana
◦ Suffix : ana, banana
◦ Substring : nan, ban, ana, banana

THEORY OF COMPUTING 39
Alphabet, Strings & Languages
Concatenation: if x and y are strings, then xy is the string obtained by placing a copy of y
immediately after a copy of x
o x = a1a2 …ai,
o y = b1b2 …bj
o xy = a1a2 …aib1b2 …bj

o Example:
 x = 01101,
 y = 110,
 xy = 01101110

Note that |xy | = |x | + |y |. Hence the length of the concatenation of two strings is the sum of their
individual lengths.
The empty string is the identity for the concatenation operator. That is:
◦ w = w = w.
09/01/2022 THEORY OF COMPUTING 40
Alphabet, Strings & Languages
Concatenation:
oLet x be a string. We let xk denote the concatenation of x with itself k times.
◦ 010113 = 010110101101011
◦ 5 = 
◦ 110 = 1111111111
◦ Abc4 = abcabcabcabc.
oNote that |xk| = k |x |.

09/01/2022 THEORY OF COMPUTING 41


Alphabet, Strings & Languages
Cartesian Product:
oLet A and B be two sets of strings. We define
AB = {xy : x ∈ A, y ∈ B}.

oA = {00, 11} and B = {x , y }


AB = {00x , 00y , 11x , 11y }
oA = {10, 01} and B = {xx , y , xy }
AB = {10xx , 10y , 10xy , 01xx , 01y , 01xy }.

Note that |AB| = |A| × |B|.

09/01/2022 THEORY OF COMPUTING 42


Alphabet, Strings & Languages
Power of an Alphabet: k = the set of strings of length k with symbols from S
o Example:  = {0, 1}
o S1 = S = {0, 1}
o S2 = {00, 01, 10, 11}
o S0 = {e}

Question: How many strings are there in S3?

Let Σ be an alphabet. Then Σk is the set of all strings of length k over the alphabet Σ.
If Σ = {a, b, c , . . . , z } then Σ7 is the set of all possible strings of length 7, one can make out
of these characters. There are 267 of them. That is a lot of strings!

09/01/2022 THEORY OF COMPUTING 43


The * Operation
* : the set of all possible strings over alphabet 
S* = S0  S1  S2  S3  …

S = {a, b}
S* = ?

THEORY OF COMPUTING 44 44
The + Operation
+ the set of all possible strings from alphabet  except e
S+ = S1  S2  S3  …

S = {a, b}
S* = S+  {e}
S+ = S* - {e}
S+ = ?
09/01/2022 THEORY OF COMPUTING 45
Strings and Languages
Language: set of strings chosen from some alphabet
A language is a subset of *
◦ Example of languages:
◦ The set of valid Arabic words
◦ The set of strings consisting of n 0’s followed by n 1’s
◦ {e, 01, 0011, 000111, …}
◦ The set of strings with equal number of 0’s and 1’s
◦ {e, 01, 10, 0011, 0101, 1010, 1001, 1100, …}

Empty language:  = { }
The language {e} consisting of the empty string
Note:   {e}
THEORY OF COMPUTING 46
Strings and Languages
We Can concatenate languages
◦ L1L2 = {xy | x  L1, y  L2}
◦ Ln = L concatenated with itself n times
◦ L0 = {e}; L1 = L
Star-closure
◦ L* = L0  L1  L2  ...
◦ L+ = L* - L0
Positive-closure
◦ L+ = L* - L0
Languages can be finite or infinite
◦ L = {a, aba, bba}
◦ L = {an | n > 0}

THEORY OF COMPUTING 47
Strings and Languages
OPERATION DEFINITION
union of L and M L  M = {s | s is in L or s is in M}
written L  M
concatenation of L LM = {st | s is in L and t is in M}
and M written LM
Kleene closure of L L* =  Li , i=0,.., 
written L*
L* denotes “zero or more concatenations of “ L
positive closure of L +
written L + L =  L i
, i=1,.., 
L+ denotes “one or more concatenations of “ L
L+ = LL*

THEORY OF COMPUTING 48
Strings and Languages
Summary L = {A, B, …, Z, a, b, …z} D = {1, 2, …, 9}
L  D = the set of letters and digits
LD = all strings consisting of a letter followed by a digit
L2 = the set of all two-letter strings
L4 = L2 L2 = the set of all four-letter strings
L* = { All possible strings of L plus  }, L+ = L* - 
D+ = set of strings of one or more digits
L (L  D ) = set of all strings consisting of a letter followed
by a letter or a digit
L (L  D )* = set of all strings consisting of letters and
digits beginning with a letter

THEORY OF COMPUTING 49
Strings and Languages
Example:
The language L consists of strings over {a,b} in which each string begins with an a
should have an even length

◦ aa, ab  L
◦ aaaa,aaab,aaba,aabb,abaa,abab,abba,abbb  L

◦ baa  L
◦ aL

THEORY OF COMPUTING 50
Strings and Languages
Example:
The language L consists of strings over {a,b} in which each occurring of b is
immediately preceded by an a

◦eL
◦aL
◦ abaab  L

◦ bb  L
◦ bab  L
◦ abb  L

THEORY OF COMPUTING 51
Strings and Languages
Example:

Let X = {a,b,c} and Y = {abb, ba}. Then


◦ XY = {aabb, babb, cabb, aba, bba, cba}
◦ X0 = {e}
◦ X1 = X = {a,b,c}
◦ X2 = XX = {aa,ab,ac,ba,bb,bc,ca,cb,cc}
◦ X3 = XXX =
{aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,bbc,bca,bcb,bcc,caa,cab,cac,cba,
cbb,cbc,cca,ccb,ccc}

THEORY OF COMPUTING 52
Strings and Languages
Example:
The language L = {a,b}*{bb}{a,b}* = *{bb}*
◦ consists of the strings over {a,b} that contain the substring bb
◦ bb  L
◦ abb  L
◦ bbb  L
◦ aabb  L
◦ bbaaa  L
◦ bbabba  L
◦ abab  L
◦ bab  L
◦bL

THEORY OF COMPUTING 53
Strings and Languages
Let L be the language that consists of all strings that begin
with aa or end with bb
◦ L1 = {aa}{a,b}*
◦ L2 = {a,b}*{bb}
◦ L = L1  L2 = {aa}{a,b}*  {a,b}*{bb}
◦ bb  L
◦ abb  L
◦ bbb  L
◦ aabb  L
◦ bbaaa  L
◦ bbabba  L
◦ abab  L
◦ bab  L
◦ ba  L

THEORY OF COMPUTING 54
Strings and Languages
Example:

Let L1 = {bb} and L2 = {e, bb, bbbb} over b

The languages L1* and L2* both contain precisely the strings consisting of an even
number of b’s.

e , with length zero, is an element of L1* and L2*

THEORY OF COMPUTING 55
Strings and Languages
??????
What is the language of all even-length strings over {a,b}
◦ L = {aa, bb, ab, ba}* = (aa|bb|ab|ba)*

What is the language of all odd-length strings over {a,b}


◦ L = {a,b}* - {aa, bb, ab, ba}* or
◦ L = {a,b}{aa, bb, ab, ba}* or
◦ L = {aa, bb, ab, ba}* {a,b}

THEORY OF COMPUTING 56
Any Question?

09/01/2022 THEORY OF COMPUTING 57 Jabbi

You might also like