IST Studies - UH
George Pasparakis
Formal Systems
Introduction to Formal Systems
IST STudies - UH
George Pasparakis
Languages
The terms language and word are
used in a strict technical sense in
this course:
A language is a set of words.
A word is a sequence (or string) of
symbols.
 (or ) denotes the empty word, the
sequence of zero symbols.
IST STudies - UH
George Pasparakis
Symbols and Alphabets
• What is a symbol, then?
• Anything, but it has to come from
an alphabet which is a finite set.
• A common (and important)
instance is = {0, 1}.
•  , the empty word, is never an
symbol of an alphabet.
IST STudies - UH
George Pasparakis
Computation
CPU memory
IST STudies - UH
George Pasparakis
CPU
input memory
output memory
Program memory
temporary memory
IST STudies - UH
George Pasparakis
CPU
input memory
output memory
Program memory
temporary memory
3
)
( x
x
f 
compute x
x
compute x
x 
2
Example:
IST STudies - UH
George Pasparakis
CPU
input memory
output memory
Program memory
temporary memory
3
)
( x
x
f 
compute x
x
compute x
x 
2
2

x
IST STudies - UH
George Pasparakis
CPU
input memory
output memory
Program memory
temporary memory
3
)
( x
x
f 
compute x
x
compute x
x 
2
2

x
4
2
*
2 

z
8
2
*
)
( 
 z
x
f
IST STudies - UH
George Pasparakis
CPU
input memory
output memory
Program memory
temporary memory
3
)
( x
x
f 
compute x
x
compute x
x 
2
2

x
4
2
*
2 

z
8
2
*
)
( 
 z
x
f
8
)
( 
x
f
IST STudies - UH
George Pasparakis
Automaton
CPU
input memory
output memory
Program memory
temporary memory
Automaton
IST STudies - UH
George Pasparakis
Different Kinds of Automata
Automata are distinguished by the temporary memory
• Finite Automata: no temporary memory
• Pushdown Automata: stack
• Turing Machines: random access memory
IST STudies - UH
George Pasparakis
input memory
output memory
temporary memory
Finite
Automaton
Finite Automaton
Example: Vending Machines
(small computing power)
IST STudies - UH
George Pasparakis
input memory
output memory
Stack
Pushdown
Automaton
Pushdown Automaton
Example: Compilers for Programming Languages
(medium computing power)
Push, Pop
IST STudies - UH
George Pasparakis
input memory
output memory
Random Access Memory
Turing
Machine
Turing Machine
Examples: Any Algorithm
(highest computing power)
IST STudies - UH
George Pasparakis
Finite
Automata
Pushdown
Automata
Turing
Machine
Power of Automata
Less power More power
Solve more
computational problems
IST STudies - UH
George Pasparakis
Mathematical Preliminaries
IST STudies - UH
George Pasparakis
Mathematical Preliminaries
• Sets
• Functions
• Relations
• Graphs
• Proof Techniques
IST STudies - UH
George Pasparakis
}
3
,
2
,
1
{

A
A set is a collection of elements
SETS
}
,
,
,
{ airplane
bicycle
bus
train
B 
We write
A

1
B
ship
IST STudies - UH
George Pasparakis
Set Representations
C = { a, b, c, d, e, f, g, h, i, j, k }
C = { a, b, …, k }
S = { 2, 4, 6, … }
S = { j : j > 0, and j = 2k for some k>0 }
S = { j : j is nonnegative and even }
finite set
infinite set
IST STudies - UH
George Pasparakis
A = { 1, 2, 3, 4, 5 }
Universal Set: all possible elements
U = { 1 , … , 10 }
1 2 3
4 5
A
U
6
7
8
9
10
IST STudies - UH
George Pasparakis
Set Operations
A = { 1, 2, 3 } B = { 2, 3, 4, 5}
• Union
A U B = { 1, 2, 3, 4, 5 }
• Intersection
A B = { 2, 3 }
• Difference
A - B = { 1 }
B - A = { 4, 5 }
U
A B
A-B
IST STudies - UH
George Pasparakis
A
• Complement
Universal set = {1, …, 7}
A = { 1, 2, 3 } A = { 4, 5, 6, 7}
1
2
3
4
5
6
7
A
A = A
IST STudies - UH
George Pasparakis
0
2
4
6
1
3
5
7
even
{ even integers } = { odd integers }
odd
Integers
IST STudies - UH
George Pasparakis
DeMorgan’s Laws
A U B = A B
U
A B = A U B
U
IST STudies - UH
George Pasparakis
Empty, Null Set:
= { }
S U = S
S =
S - = S
- S =
U
= Universal Set
IST STudies - UH
George Pasparakis
Subset
A = { 1, 2, 3} B = { 1, 2, 3, 4, 5 }
A B
U
Proper Subset: A B
U
A
B
IST STudies - UH
George Pasparakis
Disjoint Sets
A = { 1, 2, 3 } B = { 5, 6}
A B =
U
A B
IST STudies - UH
George Pasparakis
Set Cardinality
• For finite sets
A = { 2, 5, 7 }
|A| = 3
IST STudies - UH
George Pasparakis
Powersets
A powerset is a set of sets
Powerset of S = the set of all the subsets of S
S = { a, b, c }
2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }
Observation: | 2S | = 2|S| ( 8 = 23 )
IST STudies - UH
George Pasparakis
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|
Generalizes to more than two sets
A X B X … X Z
IST STudies - UH
George Pasparakis
FUNCTIONS
domain
1
2
3
a
b
c
range
f : A -> B
A B
If A = domain
then f is a total function
otherwise f is a partial function
f(1) = a
IST STudies - UH
George Pasparakis
RELATIONS
R = {(x1, y1), (x2, y2), (x3, y3), …}
xi R yi
e. g. if R = ‘>’: 2 > 1, 3 > 2, 3 > 1
In relations xi can be repeated
IST STudies - UH
George Pasparakis
Equivalence Relations
• Reflexive: x R x
• Symmetric: x R y y R x
• Transitive: x R y and y R z x R z
Example: R = ‘=‘
• x = x
• x = y y = x
• x = y and y = z x = z
IST STudies - UH
George Pasparakis
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}
IST STudies - UH
George Pasparakis
GRAPHS
A directed graph
• 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) }
node
a
b
c
d
e
IST STudies - UH
George Pasparakis
Labeled Graph
a
b
c
d
e
1 3
5
6
2
6
2
IST STudies - UH
George Pasparakis
Walk
a
b
c
d
e
Walk is a sequence of adjacent edges
(e, d), (d, c), (c, a)
IST STudies - UH
George Pasparakis
Path
a
b
c
d
e
Path is a walk where no edge is repeated
Simple path: no node is repeated
IST STudies - UH
George Pasparakis
Cycle
a
b
c
d
e
1
2
3
Cycle: a walk from a node (base) to itself
Simple cycle: only the base node is repeated
base
IST STudies - UH
George Pasparakis
Euler Tour
a
b
c
d
e
1
2
3
4
5
6
7
8 base
A cycle that contains each edge once
IST STudies - UH
George Pasparakis
Hamiltonian Cycle
a
b
c
d
e
1
2
3
4
5 base
A simple cycle that contains all nodes
IST STudies - UH
George Pasparakis
Trees
root
leaf
parent
child
Trees have no cycles
IST STudies - UH
George Pasparakis
root
leaf
Level 0
Level 1
Level 2
Level 3
Height 3
IST STudies - UH
George Pasparakis
Binary Trees
IST STudies - UH
George Pasparakis
PROOF TECHNIQUES
• Proof by induction
• Proof by contradiction
IST STudies - UH
George Pasparakis
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
IST STudies - UH
George Pasparakis
Proof by Induction
• Inductive basis
Find P1, P2, …, Pb which are true
• Inductive hypothesis
Let’s assume P1, P2, …, Pk are true,
for any k >= b
• Inductive step
Show that Pk+1 is true
IST STudies - UH
George Pasparakis
Example
Theorem: A binary tree of height n
has at most 2n leaves.
Proof by induction:
let L(i) be the number of leaves at level i
L(0) = 1
L(1) = 2
L(2) = 4
L(3) = 8
IST STudies - UH
George Pasparakis
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, …, k
• Induction step
we need to show that L(k + 1) <= 2k+1
IST STudies - UH
George Pasparakis
Induction Step
From Inductive hypothesis: L(k) <= 2k
Level
k
k+1
IST STudies - UH
George Pasparakis
L(k) <= 2k
Level
k
k+1
L(k+1) <= 2 * L(k) <= 2 * 2k = 2k+1
Induction Step
IST STudies - UH
George Pasparakis
Remark
Recursion is another thing
Example of recursive function:
f(n) = f(n-1) + f(n-2)
f(0) = 1, f(1) = 1
IST STudies - UH
George Pasparakis
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
IST STudies - UH
George Pasparakis
Example
Theorem: is not rational
Proof:
Assume by contradiction that it is rational
= n/m
n and m have no common factors
We will show that this is impossible
2
2
IST STudies - UH
George Pasparakis
= n/m 2 m2 = n2
Therefore, n2 is even
n is even
n = 2 k
2 m2 = 4k2 m2 = 2k2
m is even
m = 2 p
Thus, m and n have common factor 2
Contradiction!
2

Formal systems introduction

  • 1.
    IST Studies -UH George Pasparakis Formal Systems Introduction to Formal Systems
  • 2.
    IST STudies -UH George Pasparakis Languages The terms language and word are used in a strict technical sense in this course: A language is a set of words. A word is a sequence (or string) of symbols.  (or ) denotes the empty word, the sequence of zero symbols.
  • 3.
    IST STudies -UH George Pasparakis Symbols and Alphabets • What is a symbol, then? • Anything, but it has to come from an alphabet which is a finite set. • A common (and important) instance is = {0, 1}. •  , the empty word, is never an symbol of an alphabet.
  • 4.
    IST STudies -UH George Pasparakis Computation CPU memory
  • 5.
    IST STudies -UH George Pasparakis CPU input memory output memory Program memory temporary memory
  • 6.
    IST STudies -UH George Pasparakis CPU input memory output memory Program memory temporary memory 3 ) ( x x f  compute x x compute x x  2 Example:
  • 7.
    IST STudies -UH George Pasparakis CPU input memory output memory Program memory temporary memory 3 ) ( x x f  compute x x compute x x  2 2  x
  • 8.
    IST STudies -UH George Pasparakis CPU input memory output memory Program memory temporary memory 3 ) ( x x f  compute x x compute x x  2 2  x 4 2 * 2   z 8 2 * ) (   z x f
  • 9.
    IST STudies -UH George Pasparakis CPU input memory output memory Program memory temporary memory 3 ) ( x x f  compute x x compute x x  2 2  x 4 2 * 2   z 8 2 * ) (   z x f 8 ) (  x f
  • 10.
    IST STudies -UH George Pasparakis Automaton CPU input memory output memory Program memory temporary memory Automaton
  • 11.
    IST STudies -UH George Pasparakis Different Kinds of Automata Automata are distinguished by the temporary memory • Finite Automata: no temporary memory • Pushdown Automata: stack • Turing Machines: random access memory
  • 12.
    IST STudies -UH George Pasparakis input memory output memory temporary memory Finite Automaton Finite Automaton Example: Vending Machines (small computing power)
  • 13.
    IST STudies -UH George Pasparakis input memory output memory Stack Pushdown Automaton Pushdown Automaton Example: Compilers for Programming Languages (medium computing power) Push, Pop
  • 14.
    IST STudies -UH George Pasparakis input memory output memory Random Access Memory Turing Machine Turing Machine Examples: Any Algorithm (highest computing power)
  • 15.
    IST STudies -UH George Pasparakis Finite Automata Pushdown Automata Turing Machine Power of Automata Less power More power Solve more computational problems
  • 16.
    IST STudies -UH George Pasparakis Mathematical Preliminaries
  • 17.
    IST STudies -UH George Pasparakis Mathematical Preliminaries • Sets • Functions • Relations • Graphs • Proof Techniques
  • 18.
    IST STudies -UH George Pasparakis } 3 , 2 , 1 {  A A set is a collection of elements SETS } , , , { airplane bicycle bus train B  We write A  1 B ship
  • 19.
    IST STudies -UH George Pasparakis Set Representations C = { a, b, c, d, e, f, g, h, i, j, k } C = { a, b, …, k } S = { 2, 4, 6, … } S = { j : j > 0, and j = 2k for some k>0 } S = { j : j is nonnegative and even } finite set infinite set
  • 20.
    IST STudies -UH George Pasparakis A = { 1, 2, 3, 4, 5 } Universal Set: all possible elements U = { 1 , … , 10 } 1 2 3 4 5 A U 6 7 8 9 10
  • 21.
    IST STudies -UH George Pasparakis Set Operations A = { 1, 2, 3 } B = { 2, 3, 4, 5} • Union A U B = { 1, 2, 3, 4, 5 } • Intersection A B = { 2, 3 } • Difference A - B = { 1 } B - A = { 4, 5 } U A B A-B
  • 22.
    IST STudies -UH George Pasparakis A • Complement Universal set = {1, …, 7} A = { 1, 2, 3 } A = { 4, 5, 6, 7} 1 2 3 4 5 6 7 A A = A
  • 23.
    IST STudies -UH George Pasparakis 0 2 4 6 1 3 5 7 even { even integers } = { odd integers } odd Integers
  • 24.
    IST STudies -UH George Pasparakis DeMorgan’s Laws A U B = A B U A B = A U B U
  • 25.
    IST STudies -UH George Pasparakis Empty, Null Set: = { } S U = S S = S - = S - S = U = Universal Set
  • 26.
    IST STudies -UH George Pasparakis Subset A = { 1, 2, 3} B = { 1, 2, 3, 4, 5 } A B U Proper Subset: A B U A B
  • 27.
    IST STudies -UH George Pasparakis Disjoint Sets A = { 1, 2, 3 } B = { 5, 6} A B = U A B
  • 28.
    IST STudies -UH George Pasparakis Set Cardinality • For finite sets A = { 2, 5, 7 } |A| = 3
  • 29.
    IST STudies -UH George Pasparakis Powersets A powerset is a set of sets Powerset of S = the set of all the subsets of S S = { a, b, c } 2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} } Observation: | 2S | = 2|S| ( 8 = 23 )
  • 30.
    IST STudies -UH George Pasparakis 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| Generalizes to more than two sets A X B X … X Z
  • 31.
    IST STudies -UH George Pasparakis FUNCTIONS domain 1 2 3 a b c range f : A -> B A B If A = domain then f is a total function otherwise f is a partial function f(1) = a
  • 32.
    IST STudies -UH George Pasparakis RELATIONS R = {(x1, y1), (x2, y2), (x3, y3), …} xi R yi e. g. if R = ‘>’: 2 > 1, 3 > 2, 3 > 1 In relations xi can be repeated
  • 33.
    IST STudies -UH George Pasparakis Equivalence Relations • Reflexive: x R x • Symmetric: x R y y R x • Transitive: x R y and y R z x R z Example: R = ‘=‘ • x = x • x = y y = x • x = y and y = z x = z
  • 34.
    IST STudies -UH George Pasparakis 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}
  • 35.
    IST STudies -UH George Pasparakis GRAPHS A directed graph • 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) } node a b c d e
  • 36.
    IST STudies -UH George Pasparakis Labeled Graph a b c d e 1 3 5 6 2 6 2
  • 37.
    IST STudies -UH George Pasparakis Walk a b c d e Walk is a sequence of adjacent edges (e, d), (d, c), (c, a)
  • 38.
    IST STudies -UH George Pasparakis Path a b c d e Path is a walk where no edge is repeated Simple path: no node is repeated
  • 39.
    IST STudies -UH George Pasparakis Cycle a b c d e 1 2 3 Cycle: a walk from a node (base) to itself Simple cycle: only the base node is repeated base
  • 40.
    IST STudies -UH George Pasparakis Euler Tour a b c d e 1 2 3 4 5 6 7 8 base A cycle that contains each edge once
  • 41.
    IST STudies -UH George Pasparakis Hamiltonian Cycle a b c d e 1 2 3 4 5 base A simple cycle that contains all nodes
  • 42.
    IST STudies -UH George Pasparakis Trees root leaf parent child Trees have no cycles
  • 43.
    IST STudies -UH George Pasparakis root leaf Level 0 Level 1 Level 2 Level 3 Height 3
  • 44.
    IST STudies -UH George Pasparakis Binary Trees
  • 45.
    IST STudies -UH George Pasparakis PROOF TECHNIQUES • Proof by induction • Proof by contradiction
  • 46.
    IST STudies -UH George Pasparakis 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
  • 47.
    IST STudies -UH George Pasparakis Proof by Induction • Inductive basis Find P1, P2, …, Pb which are true • Inductive hypothesis Let’s assume P1, P2, …, Pk are true, for any k >= b • Inductive step Show that Pk+1 is true
  • 48.
    IST STudies -UH George Pasparakis Example Theorem: A binary tree of height n has at most 2n leaves. Proof by induction: let L(i) be the number of leaves at level i L(0) = 1 L(1) = 2 L(2) = 4 L(3) = 8
  • 49.
    IST STudies -UH George Pasparakis 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, …, k • Induction step we need to show that L(k + 1) <= 2k+1
  • 50.
    IST STudies -UH George Pasparakis Induction Step From Inductive hypothesis: L(k) <= 2k Level k k+1
  • 51.
    IST STudies -UH George Pasparakis L(k) <= 2k Level k k+1 L(k+1) <= 2 * L(k) <= 2 * 2k = 2k+1 Induction Step
  • 52.
    IST STudies -UH George Pasparakis Remark Recursion is another thing Example of recursive function: f(n) = f(n-1) + f(n-2) f(0) = 1, f(1) = 1
  • 53.
    IST STudies -UH George Pasparakis 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
  • 54.
    IST STudies -UH George Pasparakis Example Theorem: is not rational Proof: Assume by contradiction that it is rational = n/m n and m have no common factors We will show that this is impossible 2 2
  • 55.
    IST STudies -UH George Pasparakis = n/m 2 m2 = n2 Therefore, n2 is even n is even n = 2 k 2 m2 = 4k2 m2 = 2k2 m is even m = 2 p Thus, m and n have common factor 2 Contradiction! 2