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

Overview of Part 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

CSC1031: Fundamentals of Computing Slide 2

Module Organisation (Semester 1) Module Resources


• Delivered using a blended approach of on-campus • Lecture notes provided for the module (see Canvas) and
sessions and online resources. these form the basis for the short video lectures.
 These are partial notes and contain spaces for you to
Each week you have: add additional information found in the lecture videos.
 Introductory lecture at the start of the week  Remember it’s important you develop your own set of
 Work online using short study videos and tutorial lecture notes by adding your own additional notes.
exercises available on Canvas. • Study Videos introducing course material.
 A tutorial session that provides individual help and a • Tutorial exercises are integrated with the video lectures
(formative) online quiz (solutions are provided).
• Online quiz released as part of Tutorial Session.
• A reading list providing some recommended and
background texts is provided on Canvas.

CSC1031: Fundamentals of Computing Slide 3 CSC1031: Fundamentals of Computing Slide 4


On-Campus Sessions Assessment
Each week there will be the following on-campus sessions: • Part 1 (Semester 1) of this module is 100% assessed by a
• Introductory Lecture to Week: computer-based exam.
 Gives an overview of the material covered that week.  Forms 50% of your overall module mark.
 Motivates material covered and sets scene for work.  Takes place in the Semester 1 Exam period.
 Introduces key areas.  Is an open book exam.
• Tutorial Session: • You will be given full information about this assessment
 Provides individual help and support with the material during the revision sessions that will take place in
covered that week. Teaching Week 11.
 An online quiz will be released to do in this session. • You will be provided with a sample computer-based exam
 Important you attend these sessions. to help support you prepare for this assessment.
• The module has no summative coursework components.

CSC1031: Fundamentals of Computing Slide 5 CSC1031: Fundamentals of Computing Slide 6

Topic 1: Set Theory


Introduce sets and the basic set operations: 1.1 Introduction to sets
1.1 Introduction to sets.
1.2 Defining sets: listing and set comprehensions.
1.3 Subset and Powerset
1.4 Cartesian product
1.5 Basic operations: union, intersection, difference.
1.6 Venn Diagrams.
1.7 Recursive set definition.

CSC1031: Fundamentals of Computing Slide 7 CSC1031: Fundamentals of Computing Slide 8


What is a set? Properties of a Set
• A set is simply a collection of objects. • A set has two very important properties:
• Examples: consider the following three sets: 1) Order (position) of elements is unimportant, e.g.
following three sets are all the same:
9 Joe Rob Jane
0 cat Sue Sue Joe
3
2 5 dog Mike = Mike = Rob
1 fish Rob Joe Mike
7 Jane Jane Sue
Joe
set of numbers Sue set of pets 2) Duplicate elements are ignored, e.g.
Mike
Rob cat dog cat
Jane cat dog
fish cat
set of people fish dog fish

CSC1013: Foundation Maths for Computing Science Slide 9 CSC1013: Foundation Maths for Computing Science Slide 10

Notation for Sets Properties of Sets


• A set is written by listing out it’s elements between {…}, e.g. • Remember sets have two important properties:
{5,1,9,2,3,4} set of numbers  Order of elements is unimportant:
{cat, dog, fish} set of pets {1,2,3} = {3,1,2} = {2,3,1}
{red, blue} set of colours  Duplicate elements are ignored:
B = {T,F} set of Boolean values {1,2,3} = {1,2,2,3} = {1,2,2,2,3}
(T – true, F – false)
• The objects within a set are called the elements or • So lots of different ways of writing the same set, normally
members of the set, e.g. choose simplest and omit duplicate elements
Is 4 a member of set {1,4,3} ? Yes • When are two sets equal?
• Empty Set is the set that contains no elements and is
Two sets A and B are equal A = B if, and only if,
denoted by {} (or ).
• Normally use A,B,C,… to denote sets and x,y,z to denote
elements.
CSC1031: Fundamentals of Computing Slide 11 CSC1031: Fundamentals of Computing Slide 12
When are Sets Equal? Membership of a Set
Which of the following sets are equal to each other? Connect • Often want to know if an element is a member of a set, e.g.
equal sets by a line: Is 2 a member of set {1,2,3,4} ? Yes!
Is fish a member of set {dog,cat} ? No!
{4,2,8,4,8} {2,4,8,5,2} • Use x  A to denote that “x is a member of set A”, e.g.
Maths 2  {1,2,3,4} True
notation fish  {dog, cat} False
{5,2,4,2,2,8,4} {2,4,5,5,7,5}
• Note: Can be asked as a question or stated as a fact
5A
{2,8,4} • Also have x  A meaning “x is not a member of A”, e.g.
1  {2,3,4,5} True
sue  {john, jane, sue} False

CSC1031: Fundamentals of Computing Slide 13 CSC1031: Fundamentals of Computing Slide 14

Infinite Sets Why are sets important?


• Sets can contain an infinite number of elements, e.g. set of • Sets are fundamental in mathematics.
all positive whole numbers:
• Sets are used extensively in computing science, e.g. types
Set of natural in programs, modelling computing systems,…
{0,1,2,3,4,5,6,7,…}
numbers
• Useful when abstractly modelling a collection of elements
• “Infinite” means the set is endless, e.g. pick any number, where order and number of occurrences of elements is not
then can always add one to it to get another number! important.
• Refer to such sets as infinite sets. • Could you use sets to model the following?
• The following are some standard infinite sets:  Collection of students in a class.
N = {0,1,2,…} set of natural numbers
 Collection of all modules in a degree.
Z = {…-1,0,1,2,…} set of integers
 Travel directions.
Q = set of rational numbers, e.g. 2/3, -13/7
 Votes in an election.
R = set of real numbers, e.g. ,  2
CSC1031: Fundamentals of Computing Slide 15 CSC1031: Fundamentals of Computing Slide 16
Defining a Set: Listing
• Simplest way to define a set is to list it’s content:
{4,-2,8,10}
{{1,2},{4,2},{}}
1.2 Defining sets: listing and {a,b,c,d,e}
set comprehensions {john, jane, jim}

• Use “…” to mean “carry on pattern”, e.g.


{1,2,3,…,100}

{2,4,6,8,10,…}

{3,2,…,-2}

{1,2,4,…,22}

CSC1031: Fundamentals of Computing Slide 17 CSC1031: Fundamentals of Computing Slide 18

Defining a Set: Set Comprehensions Set Comprehension Examples


• Set Comprehensions allow us to define new sets from i) {x*2 | x  {1,…,10}, x is odd}
existing ones in a precise way, e.g. = {1*2,3*2,5*2,7*2,9*2}
x=1: 1 is odd? True
{x2 | x  {4,7,2,9,3}, x is even} x=2: 2 is odd? False = {2,6,10,14,18}
Pattern Generator Filter (condition) x=3: 3 is odd? True
...
for result x=4,7,2,9,3
x=10: 10 is odd? False
x=4: 4 is even? True, add 42=16 to result set ii) {x+y | x  {1,8,3}, y  {2,5}, x>y}
x=7: 7 is even? False = { 8+2,8+5,3+2 }
x=2: 2 is even? True, add 22=4 to result set = {10,13,5}
x=9: 9 is even? False x=1: x=8: x=3:
x=3: 3 is even? False y=2: 1>2 False y=2: 8>2 True y=2: 3>2 True
Result Set = { 16, 4 } y=5: 1>5 False y=5: 8>5 True y=5: 3>5 False

CSC1031: Fundamentals of Computing Slide 19 CSC1031: Fundamentals of Computing Slide 20


Subsets
• One set can be totally contained within another set, e.g.
A B
1.3 Subset and Powerset 2 3 4
5  5
1
4 subset 2

{2,5,4} is contained within {1,2,3,4,5}


• We say {2,5,4} is a subset of {1,2,3,4,5} and denote
this by
{2,5,4}  {1,2,3,4,5}

CSC1031: Fundamentals of Computing Slide 21 CSC1031: Fundamentals of Computing Slide 22

Subsets Some Important Related Symbols:

• Proper(strict) subset: A  B means A  B but AB


Definition: Let A and B be sets. Then A  B if, and
only if, every element of A is an element of B. {1,2}{1,2,3}? True
{1,2,3}{1,2,3}? False
• Some examples:
{John,Sue,John} {Jim,John,Sue,Tim} ? True • Not a subset: A  B means A is not a subset of B
{cat,dog} {fish,cat,rat} ? False {1,2}{1,2,3} ? False
{1,2}{1,4,3} ? True
{} {2,3,1} ? True {} A

{1,2,3} {2,3,1} ? True A A

CSC1031: Fundamentals of Computing Slide 23 CSC1031: Fundamentals of Computing Slide 24


Power Set Power Set
• Useful to be able to consider all subsets of a set. • Examples:
• Given any set A we define the power set of A, denoted  P({a})=
P(A), to be the set of all subsets of A.
P(A)  Let A={a,b} then P(A)=
{}
A {Jim} {Tim}
{Sue}  Let A={1,2,3} then P(A)=
Jim
Sue {Jim,Sue}
Tim {Jim, Tim}
{Sue,Tim}
{Jim,Sue,Tim}  Let A={1,2,3,4} then how many values will be in
P(A)?
• Note if a set A has N values in it then P(A) will have 2N
values in it.

CSC1031: Fundamentals of Computing Slide 25 CSC1031: Fundamentals of Computing Slide 26

Cardinality: The Size of a Set


• Often important to know the size of a set, i.e. the number of
elements it contains.
• We call the size of a set it’s cardinality.
• Given a set A we denote it’s cardinality by |A|
1.4 Cartesian product
• Examples:
|{2,4,6}| = 3
|{1,…,10}| = 10
|{}| = 0
|{-1,3,5,3,2,3}| = 4
|P({a,b})| = 2|{a,b}| = 22 = 4
|N| = Infinite!
• Remember that we don’t count duplicate elements.
CSC1031: Fundamentals of Computing Slide 27 CSC1031: Fundamentals of Computing Slide 28
Pairs of Values Cartesian Product
• Often want to have pairs of values, e.g. • Let AB denote the set of all pairs over A and B defined by
(Module, Lecturer) e.g. (CSC1031, Steggles) AB = {(x,y)| x  A, y  B }

(Person, Age) e.g. (Jim, 29) • Refer to AB 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 . AB = {(1,a),(1,b),(2,a),(2,b),(3,a),(3,b)}
(tea,1.75)
A={tea,coffee,soup} BA =
B=R (coffee,2.5)
BB =
• 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 |AB| is always equal to |A|*|B|
• Can consider having sets of pairs, e.g. • Remember, in pairs order is important so in general AB is
{(tea,1.75),(soup,2.95),(tea,2.15)} not equal to BA . (a,1)≠(1,a)

CSC1031: Fundamentals of Computing Slide 29 CSC1031: Fundamentals of Computing Slide 30

Tuples and Cartesian Products Modelling using Sets and Tuples


• Can generalise Cartesian product to n sets: A1A2…An • Sets and tuples can be used to abstractly model systems:
• Example: Let A={1,2}, B={a,b,c} and C={p,q}.  Tuples can be used to represent objects;
 Sets can represent collections of objects.
ABC = {(1,a,p),(1,a,q),(1,b,p),(1,b,q), • Example: Consider a stock system for an online book
3-Tuple selling company.
(1,c,p),(1,c,q),(2,a,p),(2,a,q),
Books = Set of all books
(2,b,p),(2,b,q),(2,c,p),(2,c,q)}
Stock Entry (Game of Thrones,20)

Current stock S(BooksN)


• Call (a1,a2,…,an) A1A2…An an n-tuple of values.
• What is the cardinality of Cartesian product A1A2…An ? e.g. S={(Game of Thrones,20),(Hobbit,3),…}

|A1| |A2| …|An|

CSC1031: Fundamentals of Computing Slide 31 CSC1031: Fundamentals of Computing Slide 32


Operations on Sets
• Should now know what a set is and how to define one.
• In particular, know the distinguishing properties of sets, i.e.
1.5 Basic operations: union,  Duplicate elements are ignored;
 Order of elements is unimportant.
intersection, difference
• What actions would we like to perform on sets?

• Have standard set operations to perform the above actions.

CSC1031: Fundamentals of Computing Slide 33 CSC1031: Fundamentals of Computing Slide 34

Operations on Sets: Union Operations on Sets: Union


• Can combine together two sets to produce a new one. • Given two sets A and B denote their union by A  B, e.g.
• Call this the union of two sets. {4,8,2}  {1,5,6} = {4,8,2,1,5,6}
• Example: Each sports club has a set of members. If two • Note we ignore duplicate elements as always with sets, e.g.
clubs merge what’s the set of new members?
{4,8,2}  {5,2,8} =
A A union B
Joe AB • Examples: A B = B  A
Sue
Mike
Jay {dog,cat}  {fish,cat} =

{a,c,e}  {d,a,f,c} =
B Jim
Sue {1,2,3}  {} = A {} = {} A = A
Paul
N  {-2,-1} =

CSC1031: Fundamentals of Computing Slide 35 CSC1031: Fundamentals of Computing Slide 36


Operations on Sets: Intersection Operations on Sets: Intersection
• Often interested in which elements two sets have in • Given two sets A and B denote their intersection by A  B.
common.
• Call this the intersection of two sets. • Examples:
• Example: Each person has a set of friends. How do we find {4,8,2}  {1,2,4,7} = {4,2} A B = B  A
the set of friends two people have in common?
A intersection B {a,c,e}  {d,a,f,c} =
A Jay
Sue AB {1,2,3}  {} =
Mike A {} = {} A = {}
Joe
N  {1,2,3} =

B Jim
Sue • Sets A are B are called disjoint if they have no elements in
Paul common, i.e. AB={}
Joe

CSC1031: Fundamentals of Computing Slide 37 CSC1031: Fundamentals of Computing Slide 38

Operations on Sets: Difference Operations on Sets: Difference cont…


• Useful to know what elements are in one set but not in • Given two sets A and B denote their difference by A\B.
another. • Remember that A\B stands for the set of all elements in A
• Call this the difference between two sets. which are not in B.
• Example: How can we find the set of friends that one • So in general A\B is not equal to B\A.
person knows but another person doesn’t?
• Examples:
A A difference B
Joe {4,8,2}\{1,2,4,7} = {8}
Jay A\B
Mike
Sue {1,2,4,7}\{4,8,2} =

{a,f,c,d}\{d,f,c} =
B Jim
Sue {1,2,3}\{} =
Paul
Joe Z\N = A \{} = A

CSC1031: Fundamentals of Computing Slide 39 CSC1031: Fundamentals of Computing Slide 40


Evaluating Set Expressions

1) ({b,c,a}  {e,a,a}) {d,c,a}

1.6 Venn Diagrams


2) ({4,1,3,2}  {5,2,4,1})\{3,2,5}

3) ({2,5,1}  {7,3,2})  ({5,3} {8,4})

CSC1031: Fundamentals of Computing Slide 41 CSC1031: Fundamentals of Computing Slide 42

Venn Diagrams Venn Diagrams


Can use Venn diagrams to visualise set operations: • Can also visualise more complex set expressions:
1) Union: A B A  Consider (A  B)  C A B
B
A B

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

Recursive Set Definitions


• Let us start with an example:
"Define what a Queue of People (QoP) is"
1.7 Recursive Set Definition
Base Case: 1) A single person is a QoP
Recursion
Recursive 2) If you have a QoP then adding a person
Case: to its end gives you a QoP

CSC1031: Fundamentals of Computing Slide 47 CSC1031: Fundamentals of Computing Slide 48


Recursive Set Definitions Recursive Definitions continued…
• How can we define a very large or infinite set? • Part 1) is called the base case and defines the initial values
• Can’t simply list the elements of the set (too many!) and in the set.
using “…” can lead to confusion.
• One solution is to use a recursive definition, i.e. a • Part 2) is the recursive case which uses values already in
definition which is defined in terms of itself. the set to define new elements in the set.
• Example: Here is a recursive definition for the set N of
natural numbers:
• For example, we can show that 4 is a natural number:
1) Know that 0  N. 1) 2) 2) 2) 2)
2) If we already know x  N then we
know(x+1) N. recursion 0 N 1 N 2 N 3 N 4 N
3) Nothing else is in set N.
• Using this approach can show that any positive number n is
a natural number.
CSC1031: Fundamentals of Computing Slide 49 CSC1031: Fundamentals of Computing Slide 50

Recursive Definitions Recursive Definitions


• So recursive definitions of sets always have following • What set R does the following recursive set definition
template form: define?
1) Base Case: Newcastle R;
1) Base Case: Initial values in set; 2) Recursive Case: If city x R and there is a
2) Recursive Case: Given a value (or values) bus route from x to city y then y R.
already in the set then define new value in set.

• Remember only the elements given by 1) and 2) are in set


being defined (this is clause 3 which we normally omit).

CSC1031: Fundamentals of Computing Slide 51 CSC1031: Fundamentals of Computing Slide 52


Recursive Definition Examples Recursive Definition Examples
1) List the first five values from the set X defined recursively 2) Consider the set Y which is defined recursively as follows:
as follows:
1) Base Case: Know that 2  Y;
1) Base Case: Know that 5  X; 2) Recursive Case: If x  Y then know (x*2) Y
and (x*3) Y.
2) Recursive Case: If x  X then know (x+2) X.
State whether 24Y or not, and explain your answer.

CSC1031: Fundamentals of Computing Slide 53 CSC1031: Fundamentals of Computing Slide 54

Advice: What to do at the end of a topic?


• We have now come to the end of our first topic. Topic 2: Relations and Functions
• Important you go back over the topic in your own time. Introduce the basic theory for relations and
• Make a summary of all the notation introduced. functions:
• Make sure you have completed all the tutorial exercises. 2.1 Introduction to relations.
• Do the on-line quiz for the topic. 2.2 Binary relations.
2.3 Reflexive, symmetric and transitive relations.
2.4 Introduction to functions.
2.5 Function composition.
2.6 Defining functions using recursion.
2.7 Different types of functions.

CSC1031: Fundamentals of Computing Slide 55 CSC1031: Fundamentals of Computing Slide 56


Relations
• Often some elements in a set have a distinguishing
property, e.g. odd numbers, lower case letters, …

2.1 Introduction to relations • Represent such properties by defining a relation R on set.


• Example: Define relation even over N as follows:
evenN even = {0,2,4,...}
even(x)= true only if x is even.
even(2) = true
even(3) = false

• A relation R over a set A is simply a subset R  A.


• So R(x) is the same as the question x  R?
• Relations that take one parameter called Unary Relations.
CSC1031: Fundamentals of Computing Slide 57 CSC1031: Fundamentals of Computing Slide 58

Examples of Unary Relations Defining Relations


• Let C be the set of all people in the class then the following Two ways to define a relation:
could be unary relations over C:
1) Property: English sentence
tallC
tall(x) = true only if x is greater than 6ft  State property in natural language to define when
relation is true.
leazesC
 Example: Let R  N and define R by
leazes(x) = true only if x staying in Castle Leazes
R(x) = true only when x is odd and less than 10
• Let M be the set of all modules offered on the Computing
Science degree.
R(1)=true R(2)=false R(11)=false
firstM
first(x) = true only if x is taken by first year students  Pros/Cons?
sem1M • Easy to read/write
sem1(x) = true only if x runs in Semester 1 • Can be ambiguous

CSC1031: Fundamentals of Computing Slide 59 CSC1031: Fundamentals of Computing Slide 60


Defining Relations continued…
2) Subset: Mathematical
 Define actual subset of elements relation is true for.
 Can use listing, set comprehension or recursive set 2.2 Binary relations
definition.
 Example: Let R  N and define R by
R = {1,3,5,7,9}

R(1)=true R(2)=false R(11)=false

 Pros/Cons?
• Precise and concise
• Can be difficult to write
CSC1031: Fundamentals of Computing Slide 61 CSC1031: Fundamentals of Computing Slide 62

Binary Relations Binary Relations continued…


• Relations can also represent properties between sets. • Again, can define by a property or by actually giving
• Relations taking two parameters called Binary Relations. subset of pairs, e.g. less(NN)
• Examples: 1) Property:
less(x,y)= true only if x is less than y. less(x,y)= true only if x is less than y
less(NN) less(1,3) = true,
2) Subset:
less(3,1) = false
less={(0,1),(0,2),…,(1,2),(1,3),…,(2,3),…}
older(x,y)= true only if person x is older than y years.
older(PN) older(Jason,25) = true, or ?
P = Set of all people older(Jason,70) = false
• A Binary relation R is simply a subset of a Cartesian
product RAB, for some sets A and B.
• So R(x,y) is the same as the question (x,y)  R?

CSC1031: Fundamentals of Computing Slide 63 CSC1031: Fundamentals of Computing Slide 64


Binary Relations continued… Binary Relations as Graphs
• Binary relations of the form R  AA for some set A can be
visualised as directed graphs. (If A is finite)
• Example: Let A={a,b,c,d} and define relation R  AA by
R={(a,b),(a,c),(b,d),(c,c)(d,a)}

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”.

CSC1031: Fundamentals of Computing Slide 65 CSC1031: Fundamentals of Computing Slide 66

Important Properties for Relations


• Let R  AA be a binary relation over a set A.
• The following are three important properties that may or
may not hold for R:
2.3 Reflexive, symmetric and
transitive relations
1) Reflexive

2) Symmetric

3) Transitive

CSC1031: Fundamentals of Computing Slide 67 CSC1031: Fundamentals of Computing Slide 68


1) Reflexive 1) Reflexive
• Examples: Are these relations reflexive?
Definition: Relation R  AA is reflexive if R(x,x)
holds for every x  A. 1) Let A={a,b,c} and let R1  AA be defined by
R1={(b,a),(a,a),(c,a),(c,c)}
• Example: Let A={1,2,3} and let R  AA be a relation.
Then R is reflexive only if the following hold:
R(1,1), R(2,2), R(3,3)

• What does this mean visually?


2) Let A={2,4,6} and let R2  AA be defined by
R2={(2,4),(2,2),(6,2),(6,6),(4,4)}

CSC1031: Fundamentals of Computing Slide 69 CSC1031: Fundamentals of Computing Slide 70

2) Symmetric 2) Symmetric
• Examples: Are these relations symmetric?
Definition: Relation R  AA 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  AA be defined by
R1={(b,a),(c,b),(a,a),(a,b)}
• Example: Let A={1,2,3} and R  AA 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  AA be defined by
R2={(2,4),(2,2),(6,2),(2,6),(4,2)}

CSC1031: Fundamentals of Computing Slide 71 CSC1031: Fundamentals of Computing Slide 72


3) Transitive 3) Transitive

Definition: Relation R  AA is transitive if whenever


• Examples: Are these relations transitive?

R(x,y) and R(y,z) hold then R(x,z) also holds (for 1) Let A={a,b,c} and let R1  AA be defined by
any x,y,z  A). R1={(b,c),(c,a),(b,b),(b,a),(c,c),(c,b)}

• Example: Let A={1,2,3} and R  AA. If R is transitive


and we know R(2,3)and R(3,1) hold then R(2,1)
must also hold.
• What does this mean visually: 2) Let A={2,4,6} and let R2  AA be defined by
R2={(2,4),(4,2),(2,2)}

CSC1031: Fundamentals of Computing Slide 73 CSC1031: Fundamentals of Computing Slide 74

Checking Relations Reasoning about Relations


Let R  NN be relation defined as follows: Let A={1,2,3} and R  AA. Suppose you know the
following about R:
R(x,y) = true only if x<y and both x and y are odd. i) R is symmetric and transitive.
ii) R(3,2) and R(3,1) holds.
R(3,9)= true, R(3,10)= false, R(7,5)= false
Does R(2,1) hold or not? Yes!
1) Reflexive? No! R(3,3) not true as 3>3 is false.
1) Know R(3,2) so by symmetry R(2,3) holds
2) Symmetric? No! R(3,5) is true but not R(5,3).
2) Know R(2,3) and R(3,1) so by transitivity R(2,1) holds
3) Transitive? Yes!
R(x,y) and R(y,z) are true then R(x,z) must be true Q: Is R reflexive?
as x>z has to be true and clearly x and z are odd.

CSC1031: Fundamentals of Computing Slide 75 CSC1031: Fundamentals of Computing Slide 76


What is a function?
• A function f can be thought of as a machine that takes an
input and then produces a single output, e.g.

2.4 Introduction to functions x function x*x


f
2 f 2*2=4
function f is a squaring machine.
• Write f(x) to denote the value returned by f when it is
applied to x, e.g.
f(x) = x*x

f(2) = 2*2 = 4 f(5) = 5*5 = 25


• Note that for any input can only be one possible output!
CSC1031: Fundamentals of Computing Slide 77 CSC1031: Fundamentals of Computing Slide 78

What is a function? continued… What is a function? continued…


Most activities can be viewed as a function, e.g. • Can think of functions as rules for mapping elements
between two sets.
• Finding the name of a student: • E.g. square function sqr(x)=x*x
Outputs
Inputs
student function name of 2 4
number x name student x -2
5 25
10 100
Domain Codomain
• E.g. function ord(c)=ASCII value for character c.
Set of all characters
• Finding the balance of a bank account: A 65
Z 90
account function balance of
number x balance account x * 42
Domain Codomain
CSC1031: Fundamentals of Computing Slide 79 CSC1031: Fundamentals of Computing Slide 80
What is a function? continued… Defining a Function
• To define a function you state it’s name, domain,
Definition: Let X and Y be sets. A (total) function f codomain and mapping rule.
from X to Y is a rule that assigns to each element of • Example: A function that calculates area of a square.
X exactly one element of Y.
sqr: Z Z (dom sqr)=
• Use f:XY to denote that f is a function from input set X sqr(x)= x*x (cod sqr)=
to output set Y. X Y
f sqr(2)=
Domain Codomain
• Set of input elements X is called the domain of f and is
• Some ways to define mapping rule:
denoted dom f. 1) Mathematical Formula;
2) Listing;
• Set of output elements Y is called the codomain of f and is 3) English Statement.
denoted cod f.

CSC1031: Fundamentals of Computing Slide 81 CSC1031: Fundamentals of Computing Slide 82

Defining Functions Key Properties of (Total) Functions


1) Mathematical Formula: (dom reduce) = R Two important properties are required to be a (total) function:
(cod reduce) = R
reduce:RR 1) Every element in a function's domain must be mapped to
reduce(8)= (8/100)*75 some element in its codomain.
reduce(x) = (x/100)*75
=6

2) Listing: Let X={a,b,c}, Y={1,…,10}


f:XY dom f = {a,b,c} cod f = {1,…,10}
Domain Codomain Domain Codomain
f(a) = 1, f(b) = 2, f(c) = 3. f(b)= 2 2) No input element can map to more than one element in the
codomain of a function.
3) English Statement: Let P= set of people dom age = P
cod age = N
age:PN
age(x) = age of person x age(Queen)= 93
Domain Codomain Domain Codomain
CSC1031: Fundamentals of Computing Slide 83 CSC1031: Fundamentals of Computing Slide 84
Key Properties of Functions cont… Other Properties of Functions
The following are not valid functions: • Note that two elements in domain of a function are allowed
to map to the same element in the codomain.
1) Define sqrt:ZR by sqrt(x)= square root of x.
sqrt(-1) = ? E.g. consider square function sqr(x) = x*x
sqrt(4) = 2 Ambiguous,
sqrt(4) = -2 breaks 2) No result, 2 Functions are
breaks 1) 4 allowed to do this
2) Define f:NN by f(x) = a number greater than x. -2
f(5) = 6
f(5) = 7
Ambiguous, • Also not every element of a functions codomain needs to be
breaks 2) used as a result in a function.
f(5) = 8
3) Let P be the set of all people and define sister:PP by E.g. codomain for sqr:Z Z is set Z but only natural
sister(x) = sister of person x. numbers are results under sqr.
• To take account of this define the range of a function.
rng sqr = {0,1,4,9,…} (subset of codomain Z)

CSC1031: Fundamentals of Computing Slide 85 CSC1031: Fundamentals of Computing Slide 86

Range of a Function Functions with Multiple Inputs


• Functions can take more than one input parameter.
Definition: The range of a function f:XY, denoted
rng f, is set of all results f(x) function f returns, i.e.
• Some examples:
dom avg = NN
rng f = {f(x) | x  X} avg:NNR
cod avg = R
avg(x,y) = (x+y)/2
• So range is the set of all result values used in the function.
• Example: Let X={a,b,c,d} and Y={1,2,3,4,5} and
define f:XY by f(a)=1, f(b)=2, f(c)=5, f(d)=2. first:NZRN dom first = NZR
Domain: dom f = first(x,y,z) = x cod first = N
Codomain: cod f =
Range: rng f = • Note really these functions just take a single argument
which is a tuple of values from a Cartesian product.
• Clearly rng f  cod f always holds for any function f.
CSC1031: Fundamentals of Computing Slide 87 CSC1031: Fundamentals of Computing Slide 88
Function Composition
• Given two functions f:XY and g:YZ we can join them
together to produce a new function: Calc f(x) then
2.5 Function composition use as input to g
function f(x) function
x g(f(x))
f g

• Refer to this as function composition and denote it by


g◦f : XZ
• So g◦f means first apply f then apply g to the answer, i.e.
g◦f(x) = g(f(x))
• Note to compose two functions f and g must ensure
codomain of f is equal to domain of g.

CSC1031: Fundamentals of Computing Slide 89 CSC1031: Fundamentals of Computing Slide 90

Function Composition Example Function Composition Example


f:NN g:NZ
f:NN g:NZ
x function x+3 x function x-1
f g
f(x) = x+3 g(x) = x-1

Check cod f = dom g ? N = N, Yes! Check cod f = dom g ?


Condition? Condition?
g◦f:NZ
g◦f g◦f(x) =

x function x+3 function (x + 3) - 1


f g
= x+2

CSC1031: Fundamentals of Computing Slide 91 CSC1031: Fundamentals of Computing Slide 92


More Composition Examples More Composition Examples
1) Define f:NR by f(x)=x/2 and g:RR by g(x)=x*5. iii) Consider g◦g◦f Check: cod f = dom g ?
i) Consider g◦f Check: cod f = dom g ?
cod g = dom g ?
g◦f:NR

g◦f(x)= g◦g◦f:NR

g◦g◦f(x)=

ii) Consider f◦g Check: cod g = dom f ?

Can't do composition!

CSC1031: Fundamentals of Computing Slide 93 CSC1031: Fundamentals of Computing Slide 94

Identity Function Inverse Function


• For any set A we define the identity function IA:A  A by • Interesting to consider whether the application of a function
can be undone.
IA(x) = x
• The identity function does nothing and simply returns
• Leads to the idea of an inverse function:
whatever input it is given.
Given two functions f:A  B and g:B  A we say
• Example: Identity function IN : N  N for natural numbers: that g is the inverse function of f if we have
IN (9) = 9 IN (101) = 101 g ◦ f = IA
• The identity function is useful mathematically, e.g. is the f ◦ g = IB
identity for function 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

CSC1031: Fundamentals of Computing Slide 95 CSC1031: Fundamentals of Computing Slide 96


Inverse Function Examples

f:{a,b,c}{2,4,6} f-1 :{2,4,6}{a,b,c}


f-1(2)=c, f-1(4)=a,
f(a)=4, f(b)=6, f(c)=2
f-1(6)=b 2.6 Defining functions using
g:Z  Z, g(x)=x-10 g-1:Z  Z, g-1(x)=x+10 recursion
h:{1,2,3}{a,b} No inverse! h-1(a)=?
h(1)=a, h(2)=b, h(3)=a
k:{1,2,3}{a,b,c,d} No inverse! k-1(c)=?
k(1)=a, k(2)=b, k(3)=d

A function has an inverse only if it is a bijection


(see Slide 116 for more information).

CSC1031: Fundamentals of Computing Slide 97 CSC1031: Fundamentals of Computing Slide 98

Defining Functions using Recursion Recursive Functions


• Defining functions over infinite sets can be problematic. • Recursive Definition: define function in terms of itself.
• Example: Suppose we want to define a function sum:NN • Following is a recursive definition for function sum:
such that sum(n) is the sum 0+1+2+…+n.
Problem is that n  N can be any number so need to list all 1) Base Case: For 0  N define
possibilities, e.g.
sum(3) = 0+1+2+3 sum(0) = 0
sum(0) = 0
2) Recursive Case: For any x  N, x>0 define
sum(1) = 0+1 sum(x) = sum(x-1) + x
sum(2) = 0+1+2
sum(x) = sum(x-1) + x • Note use of the recursive case where the function is
But this is impossible to do! defined by calling itself again.
• Answer is to use recursion to define the function sum. • Above definition based on recursive definition for the set N
of natural numbers (very similar form!).

CSC1031: Fundamentals of Computing Slide 99 CSC1031: Fundamentals of Computing Slide 100


Recursive Functions continued… Recursive Functions continued…
• Consider how the function works by evaluating sum(3) • This type of function referred to as a Recursive Function.
sum(3) ‘Recursive’ means the function calls
itself again to calculate its answer.
 sum(2) + 3 by rec. case (x=3)
 (sum(1) + 2) + 3 by rec. case (x=2) • Very important to ensure that a recursive function always
 (((sum(0) + 1) + 2) + 3 by rec. case (x=1) terminates and that it never enters an infinite loop.
 0 + 1 + 2 + 3 by base case • Example: Define function sumErr:NN by
 6 by arithmetic
1) Base Case: For 0  N define sumErr(0) = 0

• 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

Another Example Another Example


• Define a recursive function rem:NZ which calculates • Now formulate recursive definition:
rem(x) = 250 – (x*7)
• Start by doing some examples, e.g. 1) Base Case: rem(0) = 250

2) Recursive Case: For any x  N, x>0 define


rem(x) = rem(x-1) - 7

• Try to define result in terms of the function itself.

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  NN 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

Example: Addition Example: Addition


• Define add:N  NN using recursion as follows: • A trace of the sequence of function calls used to
calculate add(2,6).
1) Base Case: Let m  N. add(0,m) = m
add(2,6)
2) Recursive Case: For any m,x  N, x>0 define
 1 + add(1,6) by recursive case (x=2)
add(x,m) = 1 + add(x-1,m)
 1 + (1 + add(0,6)) by recursive case (x=1)

 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.

 Require extra memory to keep track of recursive calls.


• However, one major advantage is that we can reason about
recursively defined sets and functions using the principle of
induction (see later!).

CSC1031: Fundamentals of Computing Slide 109 CSC1031: Fundamentals of Computing Slide 110

Important kinds of functions 1) Surjective Function (Onto)


• So by now you should know what a function is and be able • Functions which use all elements of the codomain as results
to define one. are called surjective functions.
• Now consider three important kinds of functions: • A function is surjective if its codomain and range are the
same.
1) Surjective
Every result in codomain used. Example: Let S be a set of students and L be a set of
lecturers. Then define the function assign:SL by
2) Injective assign(x) = tutor of x
No two inputs map to same result.
 Want to ensure every lecturer is a tutor, i.e. every
3) Bijective. element of codomain must be used.
 So we want assign to be a surjective function.
Surjective and Injective.
cod assign = rng assign

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:PS 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

What if seat is surjective? Domain Codomain


CSC1031: Fundamentals of Computing Slide 113 CSC1031: Fundamentals of Computing Slide 114

Some Examples 3) Bijective Functions


f:XY X={a,b,c},Y={1,…,10} • Functions which are both surjective and injective are called
f(a) = 1, f(b) = 2, f(c) = 3. bijective functions or bijections.
Surjective? No! Injective? Yes! • If a function is a bijection then we know that the cardinality
4 not used as a result. No two results the same. (size) of its domain and codomain are the same.
But not vice
f a versa!!
g:XY X={a,b,c,d,e},Y={3,5,6,1}
g(a) = 5, g(b) = 3, g(c) = 6, g(d) = 3, g(e) = 1.
Surjective? Yes! Injective? No!
All of codomain used. g(b)=g(d) |dom f| = |rng f| = |cod f|
h:N  N • Important property because only bijective functions have
an inverse, i.e. can always be undone.
h(x) = x+1
Surjective? No! Injective? Yes! • Note that injections, surjections and bijections are
0 not used as a result. No two results the same. preserved by function composition.

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:XY .
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

Partial function as f(b) is undefined.

CSC1031: Fundamentals of Computing Slide 117 CSC1031: Fundamentals of Computing Slide 118

Topic 3: Proof Techniques


Introduce the idea of mathematical proof and
3.1 Introduction to theorems
the principle of proof by induction: and proof
3.1 Introduction to theorems and proof.
3.2 Direct proofs.
3.3 Proof by cases.
3.4 Using counter examples.
3.5 Proof by induction.

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 AB={}.
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

How do you use a Theorem? Examples of Theorems


• Once proved, a theorem can be used to derive new facts.
• To use a theorem need to make its assumptions true. Theorem (Pythagoras): Given a right angled triangle, the
• Examples: square of the hypotenuse equals the sum of the squares
of the other two sides.
1) Suppose we set A={4,2,7} and B={1,5,3}.
Can we apply Theorem 1?
Check assumptions hold: AB={}?
Theorem: For any set A we have |P(A)|=2|A|.

2) Suppose we set A={3,8,2} and B={1,5,3}.


Can we apply Theorem 1? Theorem: Given a circle of radius r then the area of the
Check assumptions hold: AB={}? circle is r2.

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?
AB={} 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 AB={}. AB={} Assumptions are true
Then we have A\B=A. A\B={a,b}=A Conclusion is true

1) Test values: A={1,2,3}, B={4,5} • What is the problem here?


AB={} Assumptions are true • To show the theorem is true for all possible values we
A\B={1,2,3}=A Conclusion is true have to construct a proof.

CSC1031: Fundamentals of Computing Slide 125 CSC1031: Fundamentals of Computing Slide 126

Introduction to Proof Introduction to Proofs


• A proof is a structured mathematical argument which • Proof is a powerful mathematical technique used in all
rigorously explains why a theorem is true. science/engineering disciplines including computing.
• A proof should show why the truth of the conclusion in a • Writing proofs is a skill that is developed by practice.
theorem follows from the given assumptions. • A range of different techniques exist to help us write proofs
• Proofs can make use of mathematical definitions and and we will consider just a few here:
previously proved theorems.
• A good proof should convince the reader that the theorem 1) Direct Proof.
in question is true beyond any doubt.
2) Proof by Cases.

Note: proofs cover all possible values and so do not


suffer from the same problems as testing. 3) Counter Examples.

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.

By [assumptions or known facts] infer that [new fact]

• Deduction Step Example: Assume AB={} is true.

By [assumption AB={} is true and so by definition of


intersection] can infer [A and B have no values in common]

CSC1031: Fundamentals of Computing Slide 129 CSC1031: Fundamentals of Computing Slide 130

Direct Proof: Example 1 Direct Proof Example 2


• Consider Theorem 1: Assumptions: A and B are sets, • Give a direct proof to show the following theorem is true.
AB={}.
Theorem: Let A and B be sets such that AB.
Conclusion: A\B=A. Then we have AB=B.
• Direct proof for Theorem 1: Proof:
1.By definition of \ know set A\B is set A with 1. By assumption AB and definition of subset know all
all members of B removed. elements of A are in set B.
2. Know AB is the set constructed by adding all elements
2.By assumption AB={} and definition of  in A to set B.
know that none of the elements of B are in A.
3. By 1 and 2, and since duplicates do not count in sets,
know AB must be same as set B.
3.Thus by 1 and 2 we know that no elements
are removed from A, i.e. A\B=A.

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,yN 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

Proof by Cases Proof by Cases: Example 1


• Sometimes a direct proof isn’t possible because we don’t • Consider the following theorem which is looking at a
have enough information. property that holds for a piece of code:
• Can often overcome the problem by splitting proof into a
number of cases, each with its own simpler proof. Theorem: Let x and y be integer variables. After
• Such a proof is called a Proof by Cases. executing the following code y always holds a value
greater than or equal to zero.
• Example: Suppose we want to show that for any
natural number xN we have x+x is even. if (x<0):
 By considering natural numbers we can split the proof y = 10
into two cases that cover all possibilities: else:
y=x
Case 1: x is even
Case 2: x is odd • How could you prove this theorem using Proof by Cases?
Important: Cases must cover all possible values of x

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

Proof by Cases: Example 2 Proof by Cases: Example 2


Proof: We have following cases to consider:
Case 1: Assume (x>y)
1. By assumption and definition of max know max(x,y)=x 1. By assumption know max(x,y)=x.
2. By assumption and definition of min know min(x,y)=y
2. By assumption know min(x,y)=y.
3. By assignment statement, 1 and 2 know z=x+y
3. By assignment statement, 1 and 2 know z=x+y
Case 2: Assume (x<y)
1. By assumption and definition of max know max(x,y)=y
2. By assumption and definition of min know min(x,y)=x
3. By assignment statement, 1 and 2 know z=y+x
4. By 3 and commutativity of + know z=x+y

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 |AB| = |A|+|B|.

• How can we prove that this theorem is not true?


• Remember that if a theorem is true then whenever its
assumptions are true the conclusion must be true.
• To prove a theorem is false we give a counter example:
find values that make the assumptions true but the
conclusion false.
• So to show the theorem above is false we need to find
values for A and B which make the assumption |A|>|B|
true but the conclusion |AB| = |A|+|B| false.
CSC1031: Fundamentals of Computing Slide 141 CSC1031: Fundamentals of Computing Slide 142

Counter Examples Counter Examples

Theorem: For any sets A and B such that |A|>|B|


• Another example:
we have |AB| = |A|+|B|. Theorem: For any natural number n  N
such that n>1 we have n2  2n
Counter Example:
1) Values: Counter Example:
A={1,2,3}, B={3,4} 1) Values: n=5
2) Assumptions true:
2) Assumptions true: Yes, 5N and 5>1
Yes, A and B are sets and |A|>|B|
3) Conclusions false: 3) Conclusions false:
|AB| = |{1,2,3,4}| = 4 52 = 25  25 = 32
But |A|+|B| = 3 + 2 = 5

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 nN
we have property P(n) is true.

• The property P(n) can be any property (i.e. Boolean


expression) over the natural numbers.
• Based on using the fact that the natural numbers is a
recursively defined set of values:

Base Case: 0N


Recursive Case: kN then k+1N

CSC1031: Fundamentals of Computing Slide 145 CSC1031: Fundamentals of Computing Slide 146

Idea behind Proof by Induction Idea behind Proof by Induction


• Consider making a row of dominoes fall: • To see why consider how we can use 1) and 2) to show that
the last domino will fall if there are four dominoes in a row.
... by 2) by 2) by 2)

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 kN, 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 nN 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: kN 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

Template for Proof by Induction Proof by Induction: Example 1


• Start by identifying the property P(n) we want to show Theorem: For all n  N we have 2n+3 = 2n * 23
holds for all natural numbers nN.
• Then always have two cases that require proofs: Proof: By induction on nN.
Let P(n) be 2n+3 = 2n * 23
1) Induction Base: Let n=0. Show P(0) holds.
Direct Proof 1) Induction Base: Let n=0.

2) Induction Step: Let kN. Show 20+3 = 20 * 23 holds.


Assume P(k)holds (induction hypothesis). Give direct proof
Then show P(k+1) holds. 2) Induction Step: Let kN.
Direct Proof using induction hypothesis Assume 2k+3 = 2k * 23 holds (Induction Hypothesis)
Show 2(k+1)+3 = 2(k+1) * 23 holds.
• Always use above template for proof by induction. Give direct proof which uses induction hypothesis
CSC1031: Fundamentals of Computing Slide 151 CSC1031: Fundamentals of Computing Slide 152
Proof by Induction: Example 1 Proof by Induction: Example 1

2) Induction Step: Let kN.


Theorem: For all n  N we have 2n+3 = 2n * 23
Assume 2k+3 = 2k * 23 holds (Induction Hypothesis)
Proof: By induction on nN.
Show 2(k+1)+3 = 2(k+1) * 23 holds.
1) Induction Base: Let n=0. Show 20+3 = 20 * 23 holds.
1. By definition of power know 2(k+1)+3 = 2 * 2k+3
1. By definition of + know 20+3 = 23
2. By definition of power know 20 * 23 = 1 * 23 2. By induction hypothesis know 2 * 2k+3 = 2 * (2k * 23)

3. By definition of * know 1 * 23 = 23 3. By associativity of * know 2 * (2k * 23)=(2 * 2k)* 23


4. By definition of power know (2 * 2k)* 23 = 2k+1 * 23

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

Proof by Induction: Example 2 Proof by Induction: Example 2


• Consider the recursive function double:NN defined by
Theorem: For all nN we have double(n)=2*n is true.
1) Base Case: double(0)= 0
2) Recursive Case: for any x  N, x>0 define Proof: By induction on nN.
double(x)= 2 + double(x-1) P(n) is equation double(n) = 2*n
1) Induction Base: Let n=0.
• Suppose we want to check that double(n) calculates 2*n. Show double(0) = 2*0 holds.
1. By definition of double know double(0) = 0
Theorem: For all nN we have double(n)= 2*n
2. By definition of * know 0 = 2*0

• We can prove this using Proof by Induction on nN. double(0) 2*0

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 kN.
following questions: Assume double(k) = 2*k holds (Induction Hypothesis)
What is the induction hypothesis here? kN 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)

3. By definition of + and * know


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

double(k+1) 2*(k+1)
CSC1031: Fundamentals of Computing Slide 157 CSC1031: Fundamentals of Computing Slide 158

Proof by Induction: Example 3 Proof by Induction: Example 3


• Consider an elimination competition where whenever two Theorem: It takes n-1 matches to decide the winner of a
teams play a match, the loser is eliminated from the
competition. competition with n teams (assuming n>0).
• Example:
Proof: By induction on nN, n>0.
Team 1
Team 2 P(n) is “Takes n-1 matches for n teams”
Team 2
Team 3 1) Induction Base: Let n=1. Show P(1)holds.
Team 3 P(1) is “Takes 0 matches for 1 team”
Team 3
Team 4
1. Clearly, if there is only one team then 0 matches
are needed.
• Question: How many matches have to be played to find the
winner if we have n teams?

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 kN, k>0. • Consider the recursive function add:NNN 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).

2. By induction hypothesis know k teams takes k-1 matches


• Suppose we want to show add performs addition:
to find a winner.
Theorem: For all n,mN we have add(n,m) = n+m
3. Step 1 required one match so we add one to number of
matches in Step 2 giving(k-1)+1 which equals k
matches.
• We can prove this using Proof by Induction on nN.
CSC1031: Fundamentals of Computing Slide 161 CSC1031: Fundamentals of Computing Slide 162

Proof by Induction: Example 4 Proof by Induction: Example 4

2) Induction Step: Let k,mN.


Theorem: For all n,mN we have add(n,m) = n+m
Assume add(k,m) = k+m holds (Induction Hypothesis)
Proof: By induction on nN. Show add(k+1,m) = (k+1)+m holds.
P(n) is equation add(n,m) = n+m
1. By definition of add know
1) Induction Base: Let n=0 and mN add(k+1,m) = 1 + add(k,m)
Show add(0,m) = 0+m
2. By induction hypothesis know
1. By definition of add know add(0,m) = m 1 + add(k,m) = 1 + (k+m)
2. By definition of + know m = 0+m 3. By definition of + know
1 + (k+m) = (k+1)+m
add(0,m) 0+m

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

Introduction to Propositional Logic Propositional Logic


• Mathematical logic provides a framework for reasoning • Propositional logic is concerned with propositions which
about computing systems in a rigorous way. are statements that must be true or false.
• It allows properties to be written as mathematical • Abstractly represent these by variables p, q, r,…
expressions and provides rules to allow further properties
to be deduced from existing ones.
• Use the logical connectives not, and, or to form complex
propositional expressions.
• We now introduce propositional logic, a simple logic for • Such propositional expressions are used to represent
reasoning about expressions that are either true or false. important properties or conditions.
• Such expressions are very important in computing, e.g. • We use the properties of the logical connectives to reason
 conditional statements, like if…then, while…do etc; and analyse such propositional expressions.

 computer hardware, especially circuit design;


 modelling software systems.
 Requirement analysis.
CSC1031: Fundamentals of Computing Slide 167 CSC1031: Fundamentals of Computing Slide 168
Propositional Logic: Syntax Propositional Logic: Semantics
• We now formally introduce propositional logic. • Now fix the meaning (semantics) of the symbols.
• Start by fixing the symbols (syntax) we use. • Each proposition p,q,r,… has the value T or F.
• Use p, q, r,… to represent propositions (propositional • Let’s define the meaning of the connectives:
variables). and (conjunction)
• Define propositional logic over B={T,F}. not (negation) p q pq
• Use the following mathematical symbols: p p F F
F F T
Informal Maths Circuits T T F
True T 1 T T
or (disjunction)
False F 0
_ p q pq
not p p p F F
p and q pq p.q F T
T F
p or q pq p+q T T

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)
TF
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)

Number of propositional variables = n


Number of rows = 2n
CSC1031: Fundamentals of Computing Slide 173 CSC1031: Fundamentals of Computing Slide 174

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:

A propositional formula is satisfiable if there exists


• Formulate problem as a set of constraints in
propositional logic.
values for its propositional variables that make it true.
• If formula is satisfiable then problem has a solution.
• To decide if a formula is satisfiable we can construct its • Checking if a formula is satisfiable is a difficult problem in
truth table to see if there is a row with result true. practice and no general efficient algorithm is known
• Example: Consider the formula (q  p) • Remember that the size of the truth table grows
p q (q  p) exponentially with the number of propositional variables.
Sudoku Puzzle: 789 variables gives 2789  10237 rows!
• Checking satisfiability is an NP-complete problem.
• Range of SAT solvers exist which work efficiently in
practice for many problem instances (but not all).

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:

A tautology is a propositional formula that A contradiction is a propositional formula


always evaluates to true regardless of the that always evaluates to false regardless of
values given to it’s propositions. the values given to it’s propositions.
• To decide if a formula is a tautology or not we simply look • To decide if a formula is a contradiction or not follow similar
at it’s truth table to see if the result column is always true. approach to that used for tautologies based on truth tables.
• Example: Consider the formula p  p • Example: Consider the formula p  p
p p  p p p  p
F F
T T

CSC1031: Fundamentals of Computing Slide 177 CSC1031: Fundamentals of Computing Slide 178

Logical Equivalence
• Consider the following two propositional formulas:

4.3 Logical equivalence and (p  q) (p  q)


simplifying formulas • It turns out that they both mean the same thing, that is their
truth tables are the same:
p q (p  q) (p  q)
F F F F
F T F F
T F F F
T T T T

• We say that the two propositions are logically equivalent


and denote this by
(p  q)  (p  q)

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, xy  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, xx  x

CSC1031: Fundamentals of Computing Slide 181 CSC1031: Fundamentals of Computing Slide 182

Using Logical Equivalence Rules De Morgan’s Laws


• Can use these logical equivalence rules to reason about • The following are very important logical equivalences
formulas in a symbolic way. known as de Morgan’s Laws:
• Example: Show that the following two formulas are logically
(x  y)  x  y
equivalent:
(p  q)  p p  (p  q) (x  y)  x  y

(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
 (k5)  (k10) 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

 (p  p)   q by associative rule


 p   q
 (p  q)

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? pq
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

Examples of using Implication


• Consider propositional formula: (p   q)  q
p q (p   q) (p   q)  q
F F T F 4.5 Formalising sentences in
F T F T
T F T F Propositional Logic
T T T T
• Consider propositional formula: (p  q)  r
p q r (p  q) (p  q) r
F F F T F
F F T T T
F T F T F
F T T T T
T F F F T
T F T F T
T T F T F
T T T T T
CSC1031: Fundamentals of Computing Slide 191 CSC1031: Fundamentals of Computing Slide 192
Formalizing Sentences as Formulas Formalizing Sentences
• How can we use propositional formulas to capture the • Let’s consider in detail the steps involved in formalizing
meaning of natural language sentences? this sentence:
• Idea is to take a sentence and then formalize it as a 1) Start by identifying the atomic propositions and the
propositional formula. connectives used, i.e.
Example: Consider the following sentence: “if John knows Sue then he must know Joe or Kay”
p  (q  r)
“if John knows Sue then he must know Joe or Kay” p= "John knows Sue"
Then we can formalize this as the following formula: r= "John knows Kay"
q= "John knows Joe"
p  (q  r) 2) Then construct formula using your understanding of the
sentence, i.e.
where p = “John knows Sue”
q = “John knows Joe” p (q  r)
r = “John knows Kay”

CSC1031: Fundamentals of Computing Slide 193 CSC1031: Fundamentals of Computing Slide 194

Formalization Examples Ambiguous Sentences


Formalize following sentences as propositional formulas: • Problem with natural language is that meaning of
sentences can be ambiguous, e.g.
a) “Valve is closed or if the valve is open then pipe is blocked”
p  (¬p  q) “if its cold then wear a jumper and keep fit”

p  (¬p  q) • However, every formula in propositional logic has one


p= "Valve is closed" precise meaning.
p= "Valve is open" q= "Pipe is blocked" • So to formalize above sentences have two possibilities:
b) “If Sue and Jim are cold then the heater is off or the heater is on where p = “it’s cold”
but the window is open” (1) (p  q)  r
q = “wearing a jumper”
(p  q)  (r  ( ¬r  s )) (2) p  (q  r) r = “keeping fit”
• Use understanding and context to choose which one is
p= "Sue is cold" r= "Heater is off" correct (sometimes hard to do!).
q= "Jim is cold" r= "Heater is on" • Note the above is why we use logical formulas to state
s= "Window is open" properties of a system rather than natural language.
CSC1031: Fundamentals of Computing Slide 195 CSC1031: Fundamentals of Computing Slide 196
Topic 5: Predicate Logic
Introduce predicate logic which extends
propositional logic with the universal and 5.1 Introduction to Predicate Logic
existential quantifiers:
5.1 Introduction to Predicate Logic
5.2 The universal quantifier.
5.3 The existential quantifier.
5.4 Combining quantifiers.
5.5 Formalizing sentences using quantifiers.

CSC1031: Fundamentals of Computing Slide 197 CSC1031: Fundamentals of Computing Slide 198

Motivation for Predicate Logic Predicates


• Propositional logic works well for simple sentences but can • Predicate logic is built on top of propositional logic.
fail to capture structure of more complicated sentences. • The first step is to allow propositions to contain variables:
• Example: consider the following sentence:
“Every natural number is equal to or greater than 0.” old(x) = “x is over 65 years old”

How can we represent this in propositional logic? odd(x) = “x is an odd number”


1) P= "Every natural number …" large(x) = “city x has a population above 250,000”
2) P0= "0  0" , P1= "1  0",…
P0  P1  P2  P3  …
• We refer to such propositions as Predicates.
• Problem is this requires an “infinite” number of propositions • The variable x is said to be a free variable.
so we can’t write it down.
• Predicate logic allows us to capture such infinite
• Denote predicates by P(x) where P is the predicates
name and x is the variable it contains.
propositions and to reason about them.
CSC1031: Fundamentals of Computing Slide 199 CSC1031: Fundamentals of Computing Slide 200
Predicates Combining Predicates
• A predicate P(x) becomes a proposition and has a truth • Can combine predicates using the normal propositional
value, when we instantiate its variables with values, e.g. connectives: , , , , e.g.

old(Jason) = “Jason is over 65 years old”


odd(x)  prime(x) x = 9
odd(9)  prime(9)
= F
= T  F = T
odd(6) = “6 is an odd number” = T
= F
odd(x)  old(y) x = 5, y = Jason
odd(7) = "7 is an odd number" odd(5)  old(Jason)
= T = T  F
= F

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

instantiating x=7 gives:


odd(7)  (prime(7)  odd(y))

instantiating y=12 gives:

odd(7)  (prime(7)  odd(12))

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:
xN & (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
xCity & (metro(x)  large(x))
xN Set of values
Universal Quantifier
variable 3) xPeople & (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) xN & (prime(x)  odd(x))
xN & P(x)
formula "Every natural number which is prime is also odd"

CSC1031: Fundamentals of Computing Slide 205 CSC1031: Fundamentals of Computing Slide 206

Meaning of Universal Quantifier Meaning of Universal Quantifier


• Given some set S and some predicate logic formula P(x) • How do we decide if a universally quantified formula is
we can write a formula using a universal quantifier: true or false?
• Consider the formula: xS & P(x)
xS & P(x) (Predicate Logic)
1) To show formula is true:
‐ Need to show for each element vS that P(v) is true.
• What is the meaning of this formula?

2) To show formula is false:


“For every value x in set S we have that P(x) holds” ‐ Need to find just one value vS such that P(v) is false.
‐ This value is called a witness value.
(English)

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:

1) x{3,5,2,9} & odd(x) xN & x0


x=3: odd(3) = T Witnesss Value: • What’s the problem with showing that this formula is true?
x=5: odd(5) = T x=2: odd(2) = F
• Need to provide a mathematical justification, e.g.
2) x{1,4,6,12} & (x<6  even(x)) Direct Proof: Clearly since 00 and 10 holds, and
x=1: (1<6  even(1)) = T since all remaining numbers in N are greater than 1 we
can conclude that the formula is true.
x=4: (4<6  even(4)) = T
x=6: (6<6  even(6)) = T
• Can you think of another way of proving this?

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

Some Further Examples


• Are following universally quantified formulas true or false:
1) xN & (even(x)  prime(x))
Witnesss Value: x=9 5.3 The existential quantifier
(even(9)  prime(9)) = F  F = F

2) xN & ((x>10)  ((x*x)>100))


Proof by Cases:
Case 1: (x>10)
Direct Proof: minimum x=11 and (11*11)>100
Case 2: (x<=10)
Direct Proof: implies must be true (false implies anything).

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”
xCity & ¬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
xN & (odd(x)  prime(x))
Existential xN Set of values
Quantifier 3) xPeople & 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) xN & (x<5  odd(x))
Predicate Logic
xN & 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

Meaning of Existential Quantifier Meaning of Existential Quantifier


• Given some set S and some predicate logic formula P(x) • How do we decide if an existentially quantified formula is
we can write a formula using an existential quantifier: true or false?
• Consider the formula: xS & P(x)
xS & P(x) (Predicate Logic)
1) To show formula is true:

• What is the meaning of this formula?


‐ Need to find just one element vS that makes P(v)
true.
‐ This value is called a witness value.
“Exists at least one value x in set S such that P(x) holds”
2) To show formula is false:
(English)
‐ Need to show for each element vS that P(v) is false.

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?

1) x{6,4,2,8} & odd(x) 3) xN & (even(x)  prime(x))


x=6: odd(6) = F Witnesss Value:
Since every value in set
x=4: odd(4) = F or {6,4,2,8} is even we know x=2: (even(2)  prime(2)) = T  T = T
x=2: odd(2) = F odd(x) can never be true.

x=8: odd(8) = F

2) x{1,4,6,12} & (x>4  x<10)


Witnesss Value: x=6: (6>4  6<10)
=(T  T)
= T
CSC1031: Fundamentals of Computing Slide 217 CSC1031: Fundamentals of Computing Slide 218

Existential Quantifier continued…


• Note the difference between universal and existential
quantifiers when considering their truth values:
Quantifier True False 5.4 Combining quantifiers
Show predicate is Find one witness
 true for all values value making
predicate false
Find one witness Show predicate is
 value making false for all values
predicate true

• So in a way they are the opposite of each other.


xS & P(x)  (xS & P(x))
xS & P(x)  (xS & P(x))
CSC1031: Fundamentals of Computing Slide 219 CSC1031: Fundamentals of Computing Slide 220
Combining Quantifiers Combining Quantifier Examples
• We can combine quantifiers to produce more expressive 1) “Every number has an odd number greater than it”
predicate logic formulas, e.g.
xN & yN & (odd(y)  (x<y))
x{2,…,4} & y{1,…,5} & (x>y)
For every value in {2,…,4} there exists a value in 2) “At least one person is friends with everyone in the world”
{1,…,5} which is less than it.
xPeople & yPeople & friends(x,y)
• To check if this formula is true or false simply start with
outermost quantifier and work inwards: 3) x,yPeople & (happy(x)  friends(x,y))
x=2, y{1,…,5} & (2>y) xPeople & yPeople &
Witness value: y=1 (2>1) = T "Every person who is happy is friends with everyone
x=3, y{1,…,5} & (3>y) in the world"
Witness value: y=2 (3>2) = T
4) xPeople & yCity & lives(x,y)  large(y)
x=4, y{1,…,5} & (4>y)
Witness value: y=1 (4>1) = T “All people live in a large city"

CSC1031: Fundamentals of Computing Slide 221 CSC1031: Fundamentals of Computing Slide 222

Combining Quantifiers: True or False? Combining Quantifiers: True or False?


1) x{2,5,3} & y{1,3,5} & (x = (y-2))
3) x{3,6,2} & y,z{1,2,0,5} & x = y+z
x=3: y=5: (5-2)=3 Witness values
x=3: y=1: z=2: 3=1+2

2) x{1,…,4} & y{1,…,5} & (x>y) x=6: y=5: z=1: 6=5+1

x=1: y=2: (1>2) = F x=2: y=1: z=1: 2=1+1

x=2: y=5: (2>5) = F

x=3: y=4: (3>4) = F


x=4: y=5: (4>5) = F

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"

car(x)= "x owns a car"

People= Set of all People

xPeople & (walk(x)  car(x))

CSC1031: Fundamentals of Computing Slide 225 CSC1031: Fundamentals of Computing Slide 226

Some examples Some examples


1) “Some people like walking and own a car.” 3) “For every natural number there is another number which
is greater than it.”
xPeople & (walk(x)  car(x))
xN & yN & (y>x)

4) “Every car is owned by a happy person.”


2) “Every car which is driven has four wheels.”
cCar & pPeople & (own(c,p)  happy(p))
cCar & (driven(c) 
? four(c))
own(x,y)="Car x is owned by y"
Car=Set of all cars.
What is wrong with the following formulation?
driven(x)="x is a car which is driven"
pPeople & cCar & (own(c,p)  happy(p))
four(x)="x has four wheels"

CSC1031: Fundamentals of Computing Slide 227 CSC1031: Fundamentals of Computing Slide 228

You might also like