CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
CS1022 Computer Programming & Principles
-
Upload
jolene-norton -
Category
Documents
-
view
31 -
download
1
description
Transcript of CS1022 Computer Programming & Principles
Plan of lecture• Why set theory?• Sets and their properties• Membership and definition of sets• “Famous” sets• Types of variables and sets• Sets and subsets• Venn diagrams• Set operations
2CS1022
Why set theory?• Set theory is a cornerstone of mathematics• Provides a convenient notation for many concepts
in computing such as lists, arrays, etc. and how to process these
3CS1022
Sets• A set is – A collection of objects– Separated by a comma– Enclosed in {...} (curly brackets)
• Examples:– {Edinburgh, Perth, Dundee, Aberdeen, Glasgow}– {2, 3, 11, 7, 0}– {CS1015, CS1022, CS1019, SX1009}
• Each object in a set is called an element of the set• We use italic capital letters to refer to sets:– C = {2, 3, 11} is the set C containing elements 2, 3 and 11
4CS1022
Sets – indices• Talk about arbitrary elements, where each subscript
is a different integer:– {ai, aj, ..., an}
• Talk about systematically going through the set, where each superscript is a different integer:– {a1
i, a2j, ..., a7
n}– {Edinburgh1, Perth2, Dundee3, Aberdeen4, Glasgow5}
5CS1022
Properties of sets• The order of elements is irrelevant– {1, 2, 3} = {3, 2, 1} = {1, 3, 2} = {2, 3, 1}
• There are no repeated elements– {1, 2, 2, 1, 3, 3} = {1, 2, 3}
• Sets may have an infinite number of elements– {1, 2, 3, 4, ...} (the “...” means it goes on and on...)– What about {0, 4, 3, 2, ...}?
6CS1022
Membership and definition of sets• Membership of a set– a S – represents that a is an element of set S– a S – represents that a is not an element of set S
• For large sets we can use a property (a predicate!) to define its members:– S = {x : P(x)} – S contains those values for x which satisfy
property P– N = { x : x is an odd positive integer} = {1, 3, 5, ...}
7CS1022
Why set theory?• Example: check if an element occurs in a collection
8CS1022
begininput x, a1, a2, ...,an;if x {a1, a2, ...,an} then found := true;
end
begininput x, a1
4, a22, ...,a8
7; found := false;
for i := 1 to n do if x = ai
j then found := true and output found;
else output found;end
search thoughcollection by superscript.
Names of “famous” sets• Some sets have a special name and symbol:– Empty set: has no element, represented as { } or – Natural numbers: N = {1, 2, 3, ...}– Integers: Z = {..., -3, -2, -1, 0, 1, 2, 3, ...}– Rational numbers: Q = {p/q : p, q Z, q 0} – Real numbers: R = {all decimals}
• N.B.: in some texts/books 0 N
9CS1022
Types (of variables) and sets• Many modern programming languages require that
variables be declared as belonging to a data type• A data type is a set with a selection of operations on
that set– Example: type “int” in Java has operations +, *, div, etc.
• When we declare the type of a variable we state what set the value of the variable belongs to and the operations that can be applied to it.
10CS1022
Sets and subsets• Some sets are contained in other sets– {1, 2, 3} is contained in {1, 2, 3, 4, 5}– N (natural numbers) is contained in Z (integers)
• Set A is a subset of set B if every element of A is in B– We represent this as A B– Formally,
A B if, and only if, x ((x A) (x B))
11CS1022
• A diagram to represent sets and how they relate• A set is represented as an oval, a circle or rectangle– With or without elements in them
• Venn diagrams show area of interest in grey• Venn diagram showing a set and a subset
Venn diagrams
12CS1022
A1
2
3
472
B
C DD C
John
Ven
n
Set equality (1)• Two sets are equal if they have the same elements• Formally, A and B are equal if A B and B A• That is,
x ((x A) (x B)) and y ((y B) (y A))• We represent this as
A = B
13CS1022
SolutionSolutionIf x A then x2 is an odd integer, which means x is odd (this needs a proof, but let’s assume it has been proven). Therefore, x B and so A B.
SolutionIf x A then x2 is an odd integer, which means x is odd (this needs a proof, but let’s assume it has been proven). Therefore, x B and so A B.Conversely, if x B then x is an odd integer, and x2 is an odd integer (this also needs a proof, but again let’s assume it has been proven). Therefore, x A and so B A.
Set equality (2)• Let A = {n : n2 is an odd integer}• Let B = {n : n is an odd integer}• Show that A = B
14CS1022
Proof has two parts• Part 1: all elements of A are elements of B• Part 2: all elements of B are elements of A
Set equality (3)
15CS1022
• The union of sets A and B isA B = {x : x A or x B}
• That is, – Those elements belonging to A together with– Those elements belonging to B and– (Possibly) those elements belonging to both A and B – N.B.: no repeated elements in sets!!
• Examples:{1, 2, 3, 4} {4, 3, 2, 1} = {1, 2, 3, 4}
{a, b, c} {1, 2} = {a, 1, b, 2, c}
Set operations: union (1)
16CS1022
• The intersection of sets A and B isA B = {x : x A and x B}
• That is, – Only those elements belonging to both A and B
• Examples:{1, 2, 3, 4} {4, 3, 2, 1} = {1, 2, 3, 4}{a, b, c} {1, 2} = { } = (empty set)
Set operations: intersection (1)
18CS1022
• Venn diagram (area of intersection in darker grey)Set operations: intersection (2)
19CS1022
A
A B
B
• The complement of a set B relative to a set A isA – B = A \ B = {x : x A and x B}
• That is, – Those elements belonging to A and not belonging to B
• Examples:{1, 2, 3, 4} – {4, 3, 2, 1} = { } = (empty set)
{a, b, c} – {1, 2} = {a, b, c}{1, 2, 3} – {1, 2} = {3}
Set operations: complement (1)
20CS1022
• Sometimes we deal with subsets of a large set U– U is the universal set for a problem
• In our previous Venn diagrams, the outer rectangle is the universal set
• Suppose A is a subset of the universal set U – Its complement relative to U is U – A– We represent as U – A = A = {x : x A}
Universal set
22CS1022
A
• Symmetric difference of two sets A and B isA B = {x : (x A and x B) or (x B and x A)}
That is:– Elements in A and not in B or– Elements in B and not in A
Or: elements in A or B, but not in both (grey area)
Set operations: Symmetric difference
23CS1022
A
B
LetA = {1, 3, 5, 7} B = {2, 4, 6, 8} C = {1, 2, 3, 4, 5}
Find• A C• B C• A – C• B C
Examples
24CS1022
LetA = {1, 3, 5, 7} B = {2, 4, 6, 8} C = {1, 2, 3, 4, 5}
Find• A C = {1, 3, 5, 7} {1, 2, 3, 4, 5} = {1, 2, 3, 4, 5, 7}• B C• A – C• B C
Examples
25CS1022
LetA = {1, 3, 5, 7} B = {2, 4, 6, 8} C = {1, 2, 3, 4, 5}
Find• A C = {1, 3, 5, 7} {1, 2, 3, 4, 5} = {1, 2, 3, 4, 5, 7}• B C = {2, 4, 6, 8} {1, 2, 3, 4, 5} = {2, 4}• A – C• B C
Examples
26CS1022
LetA = {1, 3, 5, 7} B = {2, 4, 6, 8} C = {1, 2, 3, 4, 5}
Find• A C = {1, 3, 5, 7} {1, 2, 3, 4, 5} = {1, 2, 3, 4, 5, 7}• B C = {2, 4, 6, 8} {1, 2, 3, 4, 5} = {2, 4}• A – C = {1, 3, 5, 7} – {1, 2, 3, 4, 5} = {7}• B C
Examples
27CS1022
LetA = {1, 3, 5, 7} B = {2, 4, 6, 8} C = {1, 2, 3, 4, 5}
Find• A C = {1, 3, 5, 7} {1, 2, 3, 4, 5} = {1, 2, 3, 4, 5, 7}• B C = {2, 4, 6, 8} {1, 2, 3, 4, 5} = {2, 4}• A – C = {1, 3, 5, 7} – {1, 2, 3, 4, 5} = {7}• B C = (B – C) (C – B) = ({2, 4, 6, 8} – {1, 2, 3, 4, 5})
({1, 2, 3, 4, 5} – {2, 4, 6, 8}) = {6, 8} {1, 3, 5} = {1, 3, 5, 6, 8}
• N.B.: ordering for better visualisation!
Examples
28CS1022
• We can build an information model with sets– “Model” means we don’t care how it is implemented– Essence: what information is needed
• Example: information model for student record– NAME = {namei, ...., namen}
– ID = {idi, ...., idn}
– COURSE = {coursei, ...., coursen}– Student Info:
(namej, idk, courses), where namej NAME, idk ID, and
courses COURSE.– Student Database is a set of student info:
R = {(bob,345,{CS1022,CS1015}),(mary,222,{SX1009,CS1022,MA1004}),
(jill,246,{SX1009,CS2013,MA1004}),(mary,247,{SX1009,CS1022,MA1004}), ...}
Information modelling with sets
29CS1022
• R = {(bob,345,{CS1022,CS1015}),(mary,222,{SX1009,CS1022,MA1004}),(jill,246,{SX1009,CS2013,MA1004}),(mary,247,{SX1009,CS1022,MA1004}), ...}• Query to obtain a class list. Give set C, where:C = {(N,I) : (N,I,Courses) R and CS1022 Courses}= {(bob,345), (mary,222), (mary,247), ...}
Query the Student Database
30CS1022
You should now know:• What sets are and how to represent them• Venn diagrams• Operations with sets• How to build information models with sets and how
to operate with this model
Summary
31CS1022
Further reading• R. Haggarty. “Discrete Mathematics for
Computing”. Pearson Education Ltd. 2002. (Chapter 3)
• Wikipedia’s entry• Wikibooks entry
32CS1022