Professional Documents
Culture Documents
Notes1031_2324_4up (1)
Notes1031_2324_4up (1)
Module CSC1031:
Fundamentals of Computing
1. Set Theory
2. Relations and Functions
Part 1: Modelling, Computation 3. Proof Techniques
and Reasoning 4. Propositional Logic
5. Predicate Logic
Jason Steggles
Jason.Steggles@ncl.ac.uk
Discrete Mathematics
CSC1013: Foundation Maths for Computing Science Slide 9 CSC1013: Foundation Maths for Computing Science Slide 10
{2,4,6,8,10,…}
{3,2,…,-2}
{1,2,4,…,22}
(Person, Age) e.g. (Jim, 29) • Refer to AB as the Cartesian product of A and B.
• Example: Let A={1,2,3} and B={a,b}. |A|*|B|=3*2=6
• Idea is given two sets A and B, we can have a pair (x,y)
where x A and y B . AB = {(1,a),(1,b),(2,a),(2,b),(3,a),(3,b)}
(tea,1.75)
A={tea,coffee,soup} BA =
B=R (coffee,2.5)
BB =
• Important: in a pair order is important, i.e. (x,y) is not
equal to (y,x). (tea,1.75) ≠ (1.75,tea) • The cardinality |AB| is always equal to |A|*|B|
• Can consider having sets of pairs, e.g. • Remember, in pairs order is important so in general AB is
{(tea,1.75),(soup,2.95),(tea,2.15)} not equal to BA . (a,1)≠(1,a)
{a,c,e} {d,a,f,c} =
B Jim
Sue {1,2,3} {} = A {} = {} A = A
Paul
N {-2,-1} =
B Jim
Sue • Sets A are B are called disjoint if they have no elements in
Paul common, i.e. AB={}
Joe
{a,f,c,d}\{d,f,c} =
B Jim
Sue {1,2,3}\{} =
Paul
Joe Z\N = A \{} = A
2) Intersection: A B A B C
C
Consider (A B)\C
A B
A B
3) Difference: A\B
A B
C C
CSC1031: Fundamentals of Computing Slide 43 CSC1031: Fundamentals of Computing Slide 44
Venn Diagram Example Simplifying using Venn Diagrams
• Draw a Venn diagram to illustrate the set defined by the • Venn diagrams can be used to simplify set expressions.
following set expression: ((A B) C) B
• Example: To simplify((A B) C) B we find a simpler
set expression with same Venn diagram:
A B A B A B
(((A B) C) B)
C
A B C C
A B A B
+
C C C
CSC1031: Fundamentals of Computing Slide 45 CSC1031: Fundamentals of Computing Slide 46
Pros/Cons?
• Precise and concise
• Can be difficult to write
CSC1031: Fundamentals of Computing Slide 61 CSC1031: Fundamentals of Computing Slide 62
1) Every element of
set A is a node.
2) For each pair (x,y)R
Recursive Set Definition add an edge
x Y
1) Base Case: (0,1)less
2) Recursive Case: if (x,y)less then
• Asking if R(a,c) holds is the same as asking “is there an
(x,y+1)less and (x+1,y+1)less edge going from a to c”.
2) Symmetric
3) Transitive
2) Symmetric 2) Symmetric
• Examples: Are these relations symmetric?
Definition: Relation R AA is symmetric if whenever
R(x,y) holds then R(y,x) also holds (for any x,y A). 1) Let A={a,b,c} and let R1 AA be defined by
R1={(b,a),(c,b),(a,a),(a,b)}
• Example: Let A={1,2,3} and R AA then if R is
symmetric and we know R(1,3) and R(3,2) holds then
R(3,1) and R(2,3) must also hold.
• What does this mean visually?
2) Let A={2,4,6} and let R2 AA be defined by
R2={(2,4),(2,2),(6,2),(2,6),(4,2)}
R(x,y) and R(y,z) hold then R(x,z) also holds (for 1) Let A={a,b,c} and let R1 AA be defined by
any x,y,z A). R1={(b,c),(c,a),(b,b),(b,a),(c,c),(c,b)}
g◦f(x)= g◦g◦f:NR
g◦g◦f(x)=
Can't do composition!
for any function f:A B we have • Not every function has an inverse.
• Given a function f:A B denote its inverse (if it exists) by
f ◦ IA = f
IB ◦ f = f f-1 :B A
• In the recursive case the function is defined by calling the 2) Recursive Case: For x N, x>0 define
function itself, i.e. looping round. sumErr(x) = sumErr(x) + x
• The base case is very important as it stops the looping and sumErr(3) sumErr(3) + 3 sumErr(3) + 3 + 3 …
allows a result to be output.
Will never terminate on any value x>0.
CSC1031: Fundamentals of Computing Slide 101 CSC1031: Fundamentals of Computing Slide 102
CSC1031: Fundamentals of Computing Slide 103 CSC1031: Fundamentals of Computing Slide 104
Another Example Example: Addition
• Give a trace of how rem(3) will be evaluated using the • Suppose you can only add one to a number. Define using
previous recursive definition: recursion the function add:N NN which given two
natural numbers n and m returns n+m.
rem(3) Do some examples:
rem(2) - 7 by recursive case (x=3)
add(0,4) = 4 add(3,4) = 1+1+1+4
(rem(1) - 7) - 7 by recursive case (x=2)
add(1,4) = 1+4
(rem(0) - 7) - 7) - 7 by recursive case (x=1)
add(2,4) = 1+1+4
(250 - 7) - 7) - 7 by base case
229 by arithmetic
add(x,m) =
CSC1031: Fundamentals of Computing Slide 105 CSC1031: Fundamentals of Computing Slide 106
1 + (1 + 6) by base case
8 by arithmetic
CSC1031: Fundamentals of Computing Slide 107 CSC1031: Fundamentals of Computing Slide 108
Recursive Functions and Programming
• Recursive functions can be used instead of using looping
constructs like for, do, while loops.
• They often simplify problems by allowing you to define the 2.7 Different types of function
result in terms of the problem itself.
• Main drawbacks:
Can be inefficient and result in more computations being
required.
CSC1031: Fundamentals of Computing Slide 109 CSC1031: Fundamentals of Computing Slide 110
CSC1031: Fundamentals of Computing Slide 111 CSC1031: Fundamentals of Computing Slide 112
2) Injective Function (One to one) Some Helpful Hints:
• Functions that map each input element to a different • A function cannot be surjective if the cardinality of its
result value are called an injective function. codomain is greater than the cardinality of its domain.
|dom f| < |cod f|
• A function is injective if its domain and range are same f
size. Cannot be
surjective
Example: Let P be a set of people on a flight and S be a
set of seats. Define the function seat:PS by
Domain Codomain
seat(x) = seat that passenger x is sitting in • A function cannot be injective if the cardinality of its
domain is greater than the cardinality of its codomain.
Want to ensure every seat only has one person sitting |dom f| > |cod f|
in it, i.e. every result must be unique . f
So we want seat to be an injective function. Cannot be
|dom seat| = |rng seat| injective
CSC1031: Fundamentals of Computing Slide 115 CSC1031: Fundamentals of Computing Slide 116
Partial Functions Partial Functions continued…
• So far we’ve only considered functions that map every • Partial functions can be useful when modelling computing
element of their domain to some result. systems, e.g. a piece of code that goes into an infinite loop
for some inputs.
• Refer to these functions as total functions. (Slide 84)
• Often useful to allow functions to be only partially defined, • Can think of partial functions simply as functions which have
too large a domain.
i.e. the result of a function can be undefined for some
inputs, e.g. 4/0 = ? Undefined! • Most of what can be done with total functions can be done
with partial ones.
• Refer to these functions as partial functions and denote
them by f:XY .
Drop property 1) from Slide 84 • Throughout the rest of this course when we talk about a
“function” we will mean a total function!
• Example: f:{a,b,c}{1,2,3}
f(a)=3, f(c)=1
CSC1031: Fundamentals of Computing Slide 117 CSC1031: Fundamentals of Computing Slide 118
CSC1031: Fundamentals of Computing Slide 119 CSC1031: Fundamentals of Computing Slide 120
Motivation for using Proof What is a Theorem?
• Suppose you’ve written a program for a safety critical • Given some initial facts (assumptions) we often want to
application. show that some new fact (conclusion) holds, e.g.
• How can you be sure it works correctly? If you have two sets A and B which have nothing
• You might test your program but testing is limited – you in common then you know A\B=A
cannot test all possible scenarios. • We call such a statement a theorem.
"Testing can show presence of errors
but not their absence" Theorem 1:
E. W. Dijkstra 1969 Let A and B be sets such that AB={}.
Then we have A\B=A.
• Solution: use proof techniques to show mathematically that
your program is correct.
CSC1031: Fundamentals of Computing Slide 121 CSC1031: Fundamentals of Computing Slide 122
CSC1031: Fundamentals of Computing Slide 123 CSC1031: Fundamentals of Computing Slide 124
Proving a Theorem is True Proving a Theorem is True
• How can we show that a theorem is true?
2) Test values: A={1,2,3}, B={}
• What about using testing?
AB={} Assumptions are true
• Example:
A\B={1,2,3}=A Conclusion is true
Theorem 1:
3) Test values: A={a,b}, B={c,d,e}
Let A and B be sets such that AB={}. AB={} Assumptions are true
Then we have A\B=A. A\B={a,b}=A Conclusion is true
CSC1031: Fundamentals of Computing Slide 125 CSC1031: Fundamentals of Computing Slide 126
4) Proof by Induction.
CSC1031: Fundamentals of Computing Slide 127 CSC1031: Fundamentals of Computing Slide 128
Direct Proof
• A direct proof is a sequence of deduction steps.
• These link together to show directly why the conclusion
3.2 Direct proofs stated in the theorem is true from the given assumptions.
• Each deduction step can make use of the assumptions
given and any facts or definitions already known.
CSC1031: Fundamentals of Computing Slide 129 CSC1031: Fundamentals of Computing Slide 130
CSC1031: Fundamentals of Computing Slide 131 CSC1031: Fundamentals of Computing Slide 132
Direct Proof Example 3: Using Equations
• Can chain equations together to prove a theorem.
Theorem: For any x,yN we have
3.3 Proof by cases
20 * (x + y) = (y + x).
Proof:
1. By definition of exponential
20 * (x + y) = 1 * (x + y)
2. By definition of multiplication
1 * (x + y) = (x + y)
3. By commutativity of addition
(x + y) = (y + x)
CSC1031: Fundamentals of Computing Slide 133 CSC1031: Fundamentals of Computing Slide 134
CSC1031: Fundamentals of Computing Slide 135 CSC1031: Fundamentals of Computing Slide 136
Proof by Cases: Example 1 Proof by Cases: Example 2
• Consider the following min and max functions:
Proof: We have two cases to consider:
y, if x>y x, if x>y
Case 1: Assume (x<0) min(x,y)= max(x,y)=
x, otherwise y, otherwise
1. By assumption and definition of if know y=10
2. By 1 know y is 10 so must be greater than or equal to 0.
• Then we have the following theorem:
Case 2: Assume (x>=0)
Theorem: Let x, y and z be integer variables and
1. By assumption and definition of if know y=x
consider the following code:
2. By assumption and 1 we know y must be greater than or
z = max(x,y) + min(x,y)
equal to 0.
Then z is always equal to x+y.
CSC1031: Fundamentals of Computing Slide 137 CSC1031: Fundamentals of Computing Slide 138
CSC1031: Fundamentals of Computing Slide 139 CSC1031: Fundamentals of Computing Slide 140
Counter Examples
• Can state theorems that are false, so have no proof, e.g.
3.4 Using counter examples Theorem: For any sets A and B such that |A|>|B|
we have |AB| = |A|+|B|.
CSC1031: Fundamentals of Computing Slide 143 CSC1031: Fundamentals of Computing Slide 144
Proof by Induction
• Proof by Induction is a powerful proof technique that can
be used to prove theorems of the form:
3.5 Proof by induction
Theorem: For all natural numbers nN
we have property P(n) is true.
CSC1031: Fundamentals of Computing Slide 145 CSC1031: Fundamentals of Computing Slide 146
0 1 2 3 4 ... n
by 1) k=0 k=1 k=2
• How can we prove that the last domino in a row will always Domino Domino Domino Domino
fall no matter how many dominoes it contains? 0 falls 1 falls 2 falls 3 falls
• It turns out we only need to prove two things:
• Use 1) to start then simply keep applying 2) as many times
as we need to.
1) The first domino will fall.
• So once we have proved that facts 1) and 2) hold, then we
2) For any kN, if domino k falls then the next know the property holds for any number of dominoes.
domino k+1 will fall. • This is the idea behind proof by induction.
CSC1031: Fundamentals of Computing Slide 147 CSC1031: Fundamentals of Computing Slide 148
Using Proof by Induction Proof by Induction: Example 1
• Let P(n) represent that domino n will fall. • Consider proving the following theorem:
• Then to prove P(n) holds for all natural numbers nN we
just need to prove the following two cases:
Theorem: For all n N we have 2n+3 = 2n * 23
1) Induction Base:
Show P(0) holds (first domino falls).
• This theorem has the right form for using Proof by
Induction.
Give direct proof
• What is the property P(n) here that we want to prove for
2) Induction Step: kN all natural numbers?
Assume P(k) holds (i.e. domino k falls)
P(n) is the equality “2n+3 = 2n * 23”
Show P(k+1) holds (i.e. domino k+1 falls).
Give direct proof which uses induction hypothesis
CSC1031: Fundamentals of Computing Slide 149 CSC1031: Fundamentals of Computing Slide 150
20+3 23 20 * 23
2(k+1)+3 2(k+1) * 23
CSC1031: Fundamentals of Computing Slide 153 CSC1031: Fundamentals of Computing Slide 154
CSC1031: Fundamentals of Computing Slide 155 CSC1031: Fundamentals of Computing Slide 156
Proof by Induction: Example 2 Proof by Induction: Example 2
• Write out the Induction Step case by answering the 2) Induction Step: Let kN.
following questions: Assume double(k) = 2*k holds (Induction Hypothesis)
What is the induction hypothesis here? kN Show double(k+1) = 2*(k+1) holds.
Assume double(k) = 2*k holds (i.e. P(k) holds)
1. By definition of double know
What is the property we are trying to prove here? double(k+1) = 2 + double(k)
Show double(k+1) = 2*(k+1) (i.e. show P(k+1) holds)
2. By induction hypothesis know
• Can now write out and prove the Induction Step. 2 + double(k) = 2 + (2*k)
double(k+1) 2*(k+1)
CSC1031: Fundamentals of Computing Slide 157 CSC1031: Fundamentals of Computing Slide 158
CSC1031: Fundamentals of Computing Slide 159 CSC1031: Fundamentals of Computing Slide 160
Proof by Induction: Example 3 Proof by Induction: Example 4
2) Induction Step: Let kN, k>0. • Consider the recursive function add:NNN defined by
Assume P(k) holds (Induction Hypothesis), 1) Base Case: for any m N define
add(0,m)= m
Show P(k+1)holds, i.e. “Takes k matches for k+1 teams”
2) Recursive Case: for any x,m N, x>0 define
1. After playing one match the k+1 teams becomes k teams add(n,m)= 1 + add(n-1,m)
(i.e. one team is eliminated).
CSC1031: Fundamentals of Computing Slide 163 CSC1031: Fundamentals of Computing Slide 164
Topic 4: Propositional Logic
Introduce propositional logic for modelling 4.1 Introduction to Propositional Logic
and reasoning about logical statements:
4.1 Introduction to Propositional Logic.
4.2 Truth tables.
4.3 Logical equivalence and simplifying formulas.
4.4 Implication.
4.5 Formalising sentences in Propositional Logic.
CSC1031: Fundamentals of Computing Slide 165 CSC1031: Fundamentals of Computing Slide 166
CSC1031: Fundamentals of Computing Slide 169 CSC1031: Fundamentals of Computing Slide 170
Propositional Formulas
• Can capture interesting properties by writing down
propositional formulas, e.g.
p (q p) 4.2 Truth Tables
(p q) (p q)
(p q) ((p r) (r p))
• We evaluate the behaviour of such formulas by assigning
Boolean values to the propositional variables:
• Example: for formula p (q p) assign p=T, q=F
T (F T) T (F F)
TF
T
CSC1031: Fundamentals of Computing Slide 171 CSC1031: Fundamentals of Computing Slide 172
Truth Tables Truth Table Example
• We use a truth table, to consider all the possible behaviors • Consider formula (p r) (q r)
of a propositional formula. • Three propositional variables so 2n = 8 rows in truth table.
• Here’s the truth table for the formula: p (q p)
p q r (p r) (q r)
p q p (q p)
Satisfiable Satisfiable
• Interesting to consider if it is possible to make a • Checking satisfiability of a formula is important as it can be
propositional formula true: used to solve interesting problems:
CSC1031: Fundamentals of Computing Slide 175 CSC1031: Fundamentals of Computing Slide 176
Tautology Contradiction
• Possible for a propositional formula to always evaluate to • Possible for a propositional formula to always evaluate to
true and we call such a formula a tautology: false and we call such a formula a contradiction:
CSC1031: Fundamentals of Computing Slide 177 CSC1031: Fundamentals of Computing Slide 178
Logical Equivalence
• Consider the following two propositional formulas:
CSC1031: Fundamentals of Computing Slide 179 CSC1031: Fundamentals of Computing Slide 180
Logical Equivalence continued… Logical Equivalence Rules
1) Commutative: Swap arguments round
Two propositional formula are logically equivalent
if they have the same truth tables. x y y x, xy y x
2) Associative: Shift brackets
• Logical equivalence turns out to be very important as it x (y z) (x y) z
allows us to replace complex expressions by simpler ones.
x (y z) (x y) z
• Example: A system designer has come up with the
following propositional expression: 3) Distributive: Expand brackets
q (p (p q)) x (y z) (x y) (x z)
However, we can show that this formula is logically
x (y z) (x y) (x z)
equivalent to (p q) which we can use instead of 4) Idempotent: Cancelling
the above complex formula.
x x x, xx x
CSC1031: Fundamentals of Computing Slide 181 CSC1031: Fundamentals of Computing Slide 182
(p q) p
• Very useful for simplifying expression, e.g. consider code:
“if not(k<5 or k>10) then …”
p (p q) by commutative rule
Can simplify guard expression using de Morgan’s laws:
(p p) (p q) by distributive rule (k<5 k>10)
p (p q) by idempotent rule (k<5) (k>10) by de Morgan's laws
(k5) (k10) by defn of < , >
CSC1031: Fundamentals of Computing Slide 183 CSC1031: Fundamentals of Computing Slide 184
Simplifying Propositional Formulas
• Can use these logical equivalence rules to simplify
expressions while preserving their meaning.
• Example: Simplify the following propositional formula: 4.4 Implication
p (p q)
p (p q)
p (p q) by de Morgan's Laws
CSC1031: Fundamentals of Computing Slide 185 CSC1031: Fundamentals of Computing Slide 186
Implication Implication
• Consider the following proposition: • This type of “if p then q” proposition is called an
implication, and we say that “p implies q”.
“if it is raining then Jim will stay at home”
• In propositional logic we use the implies connective:
• Tells you that if it is raining then Jim will stay at home.
• What does it tell you if it’s not raining? pq
Nothing! It is conditional information.
"If p is true then q has to be true"
• So when is it false?
Only when it is raining but Jim is not at home. • Implies is a logical connective like and
• When is it true?
1) It is raining and Jim is at home.
2) If it is NOT raining. (by default)
CSC1031: Fundamentals of Computing Slide 187 CSC1031: Fundamentals of Computing Slide 188
Implication Properties of Implication
• Important things to remember about implication: • Note that (F q) is always true no matter what value we
give to q because in this case the implies isn’t applicable.
• The implication p q can only be false when p is true
but q is false. q (F q)
• If p is false then p q doesn’t tell us anything about q F
T
T
T
and the whole implication becomes true by default.
• Truth table for implication: • Also (p T) is always true no matter what value we give
to p as the truth table shows:
p q (p q) p (p T)
F F F T
T T
F T
T F • Thus we can show that (p q) is logically equivalent
T F to (p q) (check bycomparing their truth tables).
CSC1031: Fundamentals of Computing Slide 189 CSC1031: Fundamentals of Computing Slide 190
S h h bi ii l i
CSC1031: Fundamentals of Computing Slide 193 CSC1031: Fundamentals of Computing Slide 194
CSC1031: Fundamentals of Computing Slide 197 CSC1031: Fundamentals of Computing Slide 198
CSC1031: Fundamentals of Computing Slide 201 CSC1031: Fundamentals of Computing Slide 202
Combining Predicates
• If we instantiate a variable in a predicate logic formula then
we instantiate all (free) occurrences of it with same value.
• Example: 5.2 The universal quantifier
odd(x) (prime(x) odd(y))
CSC1031: Fundamentals of Computing Slide 203 CSC1031: Fundamentals of Computing Slide 204
Universal Quantifier Universal Quantifier Examples
• Given a predicate logic formula P(x)representing 1) “Every number is odd or even”
something like “x 0” we would like to say things like:
xN & (odd(x) even(x))
“For every natural number x we have P(x) holds”
• In predicate logic we use the Universal quantifier . 2) “Every city with a metro is a large city”
• To say “for every natural number x” we use
xCity & (metro(x) large(x))
xN Set of values
Universal Quantifier
variable 3) xPeople & (young(x) old(x))
• To complete the formula we need to add the predicate "Every person is young or old"
we’re interested in, e.g.
Predicate Logic 4) xN & (prime(x) odd(x))
xN & P(x)
formula "Every natural number which is prime is also odd"
CSC1031: Fundamentals of Computing Slide 205 CSC1031: Fundamentals of Computing Slide 206
CSC1031: Fundamentals of Computing Slide 207 CSC1031: Fundamentals of Computing Slide 208
Universal Quantifier Examples Universal Quantifier on Infinite Sets
• Are following universally quantified formulas true or false? • How do we handle a predicate formula like:
x=12: (12<6 even(12)) = T • Note to show it is false still just need one witness value.
CSC1031: Fundamentals of Computing Slide 209 CSC1031: Fundamentals of Computing Slide 210
CSC1031: Fundamentals of Computing Slide 211 CSC1031: Fundamentals of Computing Slide 212
Existential Quantifier Existential Quantifier Examples
• Another common type of sentence is: 1) “Exists at least one city which is not large”
“There exists at least one natural number x such that P(x) holds”
xCity & ¬large(x)
• To formalise such sentences, predicate logic provides the
existential quantifier . 2) “There is a number which is odd and prime”
• So to say “There exists at least one natural number x” we use
xN & (odd(x) prime(x))
Existential xN Set of values
Quantifier 3) xPeople & old(x)
Variable
• To complete the formula we need to add the predicate "There exists at least one person who is old"
we’re interested in, e.g.
4) xN & (x<5 odd(x))
Predicate Logic
xN & P(x) formula "There exists a natural number which is less than 5 and odd"
CSC1031: Fundamentals of Computing Slide 213 CSC1031: Fundamentals of Computing Slide 214
CSC1031: Fundamentals of Computing Slide 215 CSC1031: Fundamentals of Computing Slide 216
Existential Quantifier Examples Existential Quantifier Examples
• Are following existentially quantified formulas true or false?
x=8: odd(8) = F
CSC1031: Fundamentals of Computing Slide 221 CSC1031: Fundamentals of Computing Slide 222
CSC1031: Fundamentals of Computing Slide 223 CSC1031: Fundamentals of Computing Slide 224
Formalizing Sentences in Predicate Logic
• Can use Predicate Logic to formalise English statements.
• Example: Consider following sentence:
5.5 Formalizing sentences
“Everybody likes walking or owns a car”
using quantifiers
walk(x)= "x likes walking"
CSC1031: Fundamentals of Computing Slide 225 CSC1031: Fundamentals of Computing Slide 226
CSC1031: Fundamentals of Computing Slide 227 CSC1031: Fundamentals of Computing Slide 228