143
Foundations of Computer Science and Software Engineering module 3/30 WALID S. SABA

Intro to computer science module 3

Embed Size (px)

DESCRIPTION

Revision uploaded March 14, 2014 This is module 3 of an introductory course that will introduce computer science and software engineering to the novice, and it is also a course that will teach even veteran computer scientists some new concepts, or explain concepts they thought they understood well. Concepts are introduced with hands on experience using object-oriented pseudo code (which can very easily be translated to C#, Java, C++, etc.) and functional programming pseudo code (which can easily be translated into F#, Erlang, Python, Haskell, etc.)

Citation preview

Page 1: Intro to computer science   module 3

Foundations of Computer Science and Software Engineering

module 3/30

WALID S. SABA

Page 2: Intro to computer science   module 3

Foundations of Computer Science and Software Engineering

ALBERT EINSTIEN

TO THE TEACHER

Page 3: Intro to computer science   module 3

Foundations of Computer Science and Software Engineering

TO THE STUDENT

SOCRATES

Page 4: Intro to computer science   module 3

Foundations of Computer Science and Software Engineering

About this CourseThis is an introductory course that will introduce computer science and software engineering to the novice, and it is also a course that will teach even veteran computer scientists some new concepts, or explain concepts they thought they understood well. Concepts are introduced with hands on experience using object-oriented pseudo code (which can very easily be translated to C#, Java, C++, etc.) and functional programming pseudo code (which can easily be translated into F#, Erlang, Python, Haskell, etc.)

About the AuthorWalid Saba has 20 years of experience in information technology, where he worked at such places as the American Institutes for Research, AT&T Bell Labs, Metlife, Nortel Networks, IBM and Cognos. He has also spent 7 years in academia where he has taught computer science at Carelton University, the New Jersey Institute of Technology, the University of Windsor, and the American University of Beirut. He has published over 30 technical articles, including an award wining paper that he recieved at KI-2008 in Germany. Walid holds a PhD in Computer Science which he obtained from Carleton University in1999

Page 5: Intro to computer science   module 3

MODULE 3 (1 hour)

MATHEMATICAL PRILIMENARIES – SETS, RELATIONS AND FUNCTIONS

Foundations of Computer Science and Software Engineering

Page 6: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set is an unordered collection of distinct objects

Page 7: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set is an unordered collection of distinct objects

The following is a set that contains 4 distinct objects (in this case numbers)

Page 8: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set is an unordered collection of distinct objects

Although we have to list the objects in some order, the order is not important at all

The following is a set that contains 4 distinct objects (in this case numbers)

Page 9: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set is an unordered collection of distinct objects

Although we have to list the objects in some order, the order is not important at all

The following is a set that contains 4 distinct objects (in this case numbers)

Since objects in a set are unordered, the sets A, B and C are equal

Page 10: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set is an unordered collection of distinct objects

Although we have to list the objects in some order, the order is not important at all

The following is a set that contains 4 distinct objects (in this case numbers)

It is therefore useful to visualize the sets A, B and C as a Venn diagram

Since objects in a set are unordered, the sets A, B and C are equal

Page 11: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

Objects are collected in a set for some purpose.

Page 12: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

Objects are collected in a set for some purpose. Here are some examples:

Set of countries that have a permanent seat in the United Nations Security Council (countries with veto power)

Page 13: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

Objects are collected in a set for some purpose. Here are some examples:

Set of countries that have a permanent seat in the United Nations Security Council (countries with veto power)

Set of US presidents that died while in office

Page 14: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set can be defined extensionally, by actually enumerating all the objects

EXAMPLES

Page 15: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set can be defined extensionally, by actually enumerating all the objects

The set of prime numbers that are less than 19 (a prime number is a number that is divisible by itself and 1 only)

EXAMPLES

Page 16: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set can be defined extensionally, by actually enumerating all the objects

EXAMPLES

The set of actors that starred in The Untouchables movie

The set of prime numbers that are less than 19 (a prime number is a number that is divisible by itself and 1 only)

Page 17: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A set is often defined by comprehension – that is, by some predicate

Page 18: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A is the set of

A set is often defined by comprehension – that is, by some predicate

Page 19: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A is the set of all objects x

A set is often defined by comprehension – that is, by some predicate

Page 20: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A is the set of all objects x such that

A set is often defined by comprehension – that is, by some predicate

Page 21: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

A is the set of all objects x such that the property (or predicate) P is true of x

A set is often defined by comprehension – that is, by some predicate

Page 22: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

EXAMPLES

A set is often defined by comprehension – that is, by some predicate

Page 23: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

EXAMPLESThe set of all objects x such that x is an even number that is greater or equal to 2 and less than or equal to 8

A set is often defined by comprehension – that is, by some predicate

Page 24: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

EXAMPLES

A set is often defined by comprehension – that is, by some predicate

Page 25: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

EXAMPLES

The set of all pairs (x,y) such that x is an even number between 1 and 9 and y is the square of x

A set is often defined by comprehension – that is, by some predicate

Page 26: Intro to computer science   module 3

Sets

Introduction to Computer Science – Preliminaries

EXAMPLES

A set is often defined by comprehension – that is, by some predicate

Page 27: Intro to computer science   module 3

Set Membership

Introduction to Computer Science – Preliminaries

Objects either belong to (are members of) a set, or they are not.

Page 28: Intro to computer science   module 3

Set Membership

Introduction to Computer Science – Preliminaries

Objects either belong to (are members of) a set, or they are not.

When an object e belongs to (or is a member of) a set S, we write

Page 29: Intro to computer science   module 3

Set Membership

Introduction to Computer Science – Preliminaries

Objects either belong to (are members of) a set, or they are not.

When an object e belongs to (or is a member of) a set S, we write

If an object e does not belong to a set S, we write

Page 30: Intro to computer science   module 3

Set Membership

Introduction to Computer Science – Preliminaries

Objects either belong to (are members of) a set, or they are not.

When an object e belongs to (or is a member of) a set S, we write

If an object e does not belong to a set S, we write

In advanced computer science curses you will encounter another kind of set membership that is not crisp (yes, or no), but fuzzy!

For example, in defining membership in the set of BIRDS, an eagle might be considered a member with degree 1.0, while a penguin might be considered a member with degree 0.6 only

Fuzzy sets and fuzzy membership are beyond the scope of this course, but reading a high-level introduction to the subject is not a bad idea

Page 31: Intro to computer science   module 3

Empty Set

Introduction to Computer Science – Preliminaries

What happens if the predicate (or property) that defines membership in the set is not true of any object?

Page 32: Intro to computer science   module 3

Empty Set

Introduction to Computer Science – Preliminaries

What happens if the predicate (or property) that defines membership in the set is not true of any object?

Consider these sets:

Page 33: Intro to computer science   module 3

Empty Set

Introduction to Computer Science – Preliminaries

What happens if the predicate (or property) that defines membership in the set is not true of any object?

Consider these sets:

Page 34: Intro to computer science   module 3

Empty Set

Introduction to Computer Science – Preliminaries

What happens if the predicate (or property) that defines membership in the set is not true of any object?

Consider these sets:

Now what members belong to this set?

Page 35: Intro to computer science   module 3

Empty Set

Introduction to Computer Science – Preliminaries

It turns out that the set is an empty set – i.e., it has no members, since there are no objects that are members of D and also members of E

Page 36: Intro to computer science   module 3

Empty Set

Introduction to Computer Science – Preliminaries

It turns out that the set is an empty set – i.e., it has no members, since there are no objects that are members of D and also members of E

When a set B is empty we write:

Page 37: Intro to computer science   module 3

The Universal Set

Introduction to Computer Science – Preliminaries

Unlike the empty set, which has no members, the Universal Set, often denoted by U, is the set that has all objects as members, including itself – its is the set of all objects in the domain of discourse

Page 38: Intro to computer science   module 3

The Universal Set

Introduction to Computer Science – Preliminaries

Unlike the empty set, which has no members, the Universal Set, often denoted by U, is the set that has all objects as members, including itself – its is the set of all objects in the domain of discourse

Graphically, we usually depict sets and the Universal Set as follows:

Page 39: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

Note the figure below.

Page 40: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

Note the figure below.

o C is entirely contained in the set A (we say that C is a subset of A)

Page 41: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

Note the figure below.

o C is entirely contained in the set A (we say that C is a subset of A)o E and B have something in common (their intersection is the set H)

Page 42: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

Note the figure below.

o C is entirely contained in the set A (we say that C is a subset of A)

o if we take the union of A and F, it will also contain all the elements of Co E and B have something in common (their intersection is the set H)

Page 43: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

SUBSET

A set R is a subset of another set S if every element of R is also an element of S.

We write

We can state this in first-order logic (FOL) as follows:

Page 44: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

SUBSET

A set R is a subset of another set S if every element of R is also an element of S.

We write

We can state this in first-order logic (FOL) as follows:

What the above says is the following:R is a subset of S if and only if any x that belongs to R also belongs to S

Page 45: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

We can state this in first-order logic (FOL) as follows:

UNION

The union of a set R and a set S is a set that has the elements that are in R and those that are in S

We write

Page 46: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

UNION

The union of a set R and a set S is a set that has the elements that are in R and those that are in S

We write

We can state this in first-order logic (FOL) as follows:

What the above says is the following:The union of R and S contains any x that is an element of R or an element of S

Page 47: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

We can state this in first-order logic (FOL) as follows:

INTERSECTION

The intersection of A and B is a set that has all the elements that are in both, A and B

We write

Page 48: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

We can state this in first-order logic (FOL) as follows:

What the above says is the following:The intersection of R and S contains any x that is in both R, and S

INTERSECTION

The intersection of A and B is a set that has all the elements that are in both, A and B

We write

Page 49: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

We can state this in first-order logic (FOL) as follows:

COMPLEMENT

The complement of a set A is a set that contains all the elements that are not in A

We write

Page 50: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

We can state this in first-order logic (FOL) as follows:

What the above says is the following:The complement of a set A is a set that has all the elements that are not in A

COMPLEMENT

The complement of a set A is a set that contains all the elements that are not in A

We write

Page 51: Intro to computer science   module 3

Set Operations

Introduction to Computer Science – Preliminaries

For example, if S = {x,y,z} then P(S) = {{x,y,z},{x,y},{x,z},{y,z},{x},{y},{z},{ }}

If |S| is the size of a set S, then |P(S)| = 2 | S|

The Power Set of a set S, denoted to by P(S), or 2S, is the set of all subsets of S

Page 52: Intro to computer science   module 3

Correspondence between Set Theory and Logic

Introduction to Computer Science – Preliminaries

Page 53: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Correspondence between Set Theory and Logic

Page 54: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Correspondence between Set Theory and Logic

Page 55: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Correspondence between Set Theory and Logic

Page 56: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 57: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Recall also that corresponds to

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 58: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Recall also that corresponds to

Let us assign truth values for R and S – where T means we are in the set

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 59: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Recall also that corresponds to

Let us assign truth values for R and S – where T means we are in the set

T T TIt possible to be in R and S at the same time

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 60: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Recall also that corresponds to

Let us assign truth values for R and S – where T means we are in the set

T T T

F T T

It possible to be in S but not in R

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 61: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Recall also that corresponds to

Let us assign truth values for R and S – where T means we are in the set

T T T

F T TF F T

It is also possible not to be in R and not to be in S

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 62: Intro to computer science   module 3

Introduction to Computer Science – Preliminaries

Set Theory Explanation of Implication

Recall also that corresponds to

Let us assign truth values for R and S – where T means we are in the set

T T TT F FF T TF F T

But it is not possible to be in R and not in S at the same time

Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false

Page 63: Intro to computer science   module 3

Sets – An Example

Introduction to Computer Science – Preliminaries

Suppose that in a certain school we have the following data:

Suppose the school is holding a Christmas party and we want to make sure we have a tag for everyone in the school. How many tags should we make?

Page 64: Intro to computer science   module 3

Sets – An Example

Introduction to Computer Science – Preliminaries

Suppose that in a certain school we have the following data:

We need a tag for every student, teacher and staff member. The entire set is

Suppose the school is holding a Christmas party and we want to make sure we have a tag for everyone in the school. How many tags should we make?

Page 65: Intro to computer science   module 3

Sets – An Example

Introduction to Computer Science – Preliminaries

OPERATIONS

= 11

DATA

Page 66: Intro to computer science   module 3

Sets – An Example

Introduction to Computer Science – Preliminaries

OPERATIONS

= 11

DATA

(1) How would you find out the number of presents the school should prepare if the school is giving a present to every teacher and every staff member?

(2) How can we know the names of those that will receive more than one present?

Page 67: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

Page 68: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

Cartesian product is an operation that returns a set which is the product between two or more sets.

CARTESIAN PRODUCT

For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y

Page 69: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

Cartesian product is an operation that returns a set between two or more sets.

CARTESIAN PRODUCT

For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y

Formally, we have the following:

Page 70: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

Cartesian product is an operation that returns a set between two or more sets.

CARTESIAN PRODUCT

For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y

Formally, we have the following:

Note that, in general,

Page 71: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

Cartesian product is an operation that returns a set between two or more sets.

CARTESIAN PRODUCT

For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y

Formally, we have the following:

Note that, in general,

If |X| is the size of X and |Y| is the size of Y then |X x Y| is |X|.|Y|

Page 72: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

A relation between two sets is some subset of the Cartesian product between the two sets

Page 73: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

A relation between two sets is some subset of the Cartesian product between the two sets

For example, assuming that N is the set of natural numbers, and S is the set of all possible words in English, then the following is one possible relation that is a subset of S x N

R = { (“Hendrix”, 7), (“Dylan”, 5), (“Marley”, 6), (“Morrison”, 8) }

Page 74: Intro to computer science   module 3

Relations

Introduction to Computer Science – Preliminaries

A relation between two sets is some subset of the Cartesian product between the two sets

R = { (“Hendrix”, 7), (“Dylan”, 5), (“Marley”, 6), (“Morrison”, 8) }

This relation might be used to pair strings with their size (number of characters in the string)

For example, assuming that N is the set of natural numbers, and S is the set of all possible words in English, then the following is one possible relation that is a subset of S x N

Page 75: Intro to computer science   module 3

Visualizing a Relation

Introduction to Computer Science – Preliminaries

R = { (“Hendrix”, 7), (“Dylan”, 5), (“Marley”, 6), (“Morrison”, 8) }

SN

Page 76: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

Recall the quantifiers of first-order logic:

Page 77: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

Recall the quantifiers of first-order logic:

This says that for all x, R is true of x (or, R is true of any x)

Page 78: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

Recall the quantifiers of first-order logic:

This says that for all x, R is true of x (or, R is true of any x)

This says that there exists some x that R is true of (or, R is true of some x)

Page 79: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

Recall the quantifiers of first-order logic:

But what if the property (or predicate) R is a 2-place property, or a property that is true of two variables?

This says that for all x, R is true of x (or, R is true of any x)

This says that there exists some x that R is true of (or, R is true of some x)

Page 80: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

Recall the quantifiers of first-order logic:

But what if the property (or predicate) R is a 2-place property, or a property that is true of two variables?

The predicate R here is true of some x and y if x and y are related to each other by R – or, if the pair (x,y) is in the relation that corresponds to R

This says that for all x, R is true of x (or, R is true of any x)

This says that there exists some x that R is true of (or, R is true of some x)

Page 81: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

For example, we can have the following 2-place predicates:

Page 82: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

For example, we can have the following 2-place predicates:

john knows mary

Page 83: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

For example, we can have the following 2-place predicates:

carlos likes pineapple

Page 84: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

For example, we can have the following 2-place predicates:

Ottawa is the capital of Canada

Page 85: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

For example, we can have the following 2-place predicates:

Length of the string “abba” is 4

Page 86: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

For example, we can have the following 2-place predicates:

BMW is made in Germany

Page 87: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

Suppose we have a relation Likes that might hold between some x and some y, where x is from some set X and y is from set Y

Page 88: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

When we have 2 variables and more than the order of the quantifiers is important

Page 89: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

When we have 2 variables and more than the order of the quantifiers is important

Here there is at least one x that is related to some (at least one) y

Page 90: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

When we have 2 variables and more than the order of the quantifiers is important

Here every x is related to some (at least one) y

Page 91: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

When we have 2 variables and more than the order of the quantifiers is important

Here there is at least one x that is related to every y (at least one person likes all fruit types!)

Page 92: Intro to computer science   module 3

Relations and Quantifiers in First-Order Logic

Introduction to Computer Science – Preliminaries

When we have 2 variables and more than the order of the quantifiers is important

Here we are saying that every x is related to every y (everyone likes all fruits types). Can you complete the picture of this relation?

Page 93: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

A relation R can have any of these properties

Page 94: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

A relation R can have any of these properties

Page 95: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

A relation R can have any of these properties

Page 96: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

A relation R can have any of these properties

Page 97: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

AN EXAMPLE

The subset relation on sets is

Page 98: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

AN EXAMPLE

The subset relation on sets is

sr t

Page 99: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

AN EXAMPLE

The subset relation on sets is

Why is the subset relation not symmetric?

sr t

Page 100: Intro to computer science   module 3

Properties of Relations

Introduction to Computer Science – Preliminaries

Consider the relation (‘less than or equal to’) on N x N

Here are some pairs in this relation

= {(0, 1), (0, 2), (0, 3), ... (1, 2), (1, 3), (1, 4), (1, 5), ... (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), ... ... }

Is this relation reflexive? Symmetric? Transitive?What about the equality (=) relation on natural numbers?

Page 101: Intro to computer science   module 3

Relations in Data Modeling

Introduction to Computer Science – Preliminaries

Consider the following sets, that represent certain data objects:

Page 102: Intro to computer science   module 3

Relations in Data Modeling

Introduction to Computer Science – Preliminaries

Consider the following sets, that represent certain data objects:

Several relations can be defined on these sets to represent, for example, data about employees in a certain organization:

Page 103: Intro to computer science   module 3

Relations in Data Modeling

Introduction to Computer Science – Preliminaries

These relations might have the following data stored in them:

Company Name Age

Company City

Page 104: Intro to computer science   module 3

Relations in Data Modeling

Introduction to Computer Science – Preliminaries

These relations might have the following data stored in them:

Company Name Age

Company City

Note that a relation on sets is itself a set: a set of pairs, triples, etc.. In mathematics we call these objects tuples. In database systems we call these objects records, and we call the relations tables

Page 105: Intro to computer science   module 3

Relations in Data Modeling

Introduction to Computer Science – Preliminaries

These relations might have the following data stored in them:

Company Name Age

Company City

LOOKING AHEAD

These two relations are connected somehow (they share one data object, namely the company an employee works for)

Given these tables/relations, how would we get the list of employees that work in Tokyo?

Page 106: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

A function is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 107: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

An example function that maps objects in some set to their color

A function is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 108: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 109: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(0) = 0

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 110: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(1) = 2

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

f(0) = 0

Page 111: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(2) = 4f(1) = 2f(0) = 0

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 112: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(3) = 6f(2) = 4f(1) = 2f(0) = 0

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 113: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 114: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(5) = 10f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 115: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(5) = 10f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0

Can you guess what this function is doing?

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 116: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

012345...

0123456789

1011...

X: input

Y: output

f(5) = 10f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0

Can you guess what this function is doing?The function seems to map every number to its double ...

A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y

Page 117: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

A function is thus a relation from a set of inputs to a set of outputsA function is sometimes also called a map, since it maps inputs to outputs

Page 118: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

A function is thus a relation from a set of inputs to a set of outputsA function is sometimes also called a map, since it maps inputs to outputs

In engineering, machine is used as a metaphor for function since it takes some objects as input, does something with them, and produces new objects

Page 119: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

A function is thus a relation from a set of inputs to a set of outputsA function is sometimes also called a map, since it maps inputs to outputs

In engineering, machine is used as a metaphor for function since it takes some objects as input, does something with them, and produces new objects

In computer science, a function is a program or a procedure

Page 120: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

The output of a function is some value, and so it can be the input to another function

DoubleSquare3

Page 121: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

The output of a function is some value, and so it can be the input to another function

DoubleSquare3 9

Page 122: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

The output of a function is some value, and so it can be the input to another function

DoubleSquare3 9 18

Page 123: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

The output of a function is some value, and so it can be the input to another function

DoubleSquare5

Page 124: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

The output of a function is some value, and so it can be the input to another function

DoubleSquare5 25

Page 125: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

The output of a function is some value, and so it can be the input to another function

DoubleSquare5 25 50

Page 126: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

Functions can have any number of inputs:

Page 127: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

x y

f1 : if (x > y) then x else y

Functions can have any number of inputs:

Page 128: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

f2 : (x * x) + (y * y)

x y

f1 : if (x > y) then x else y

x y

Functions can have any number of inputs:

Page 129: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

f2 : (x * x) + (y * y)

x y

f1 : if (x > y) then x else y

x y

What do you thing the function f1 does?

Functions can have any number of inputs:

Page 130: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

f2 : (x * x) + (y * y)

x y

f1 : if (x > y) then x else y

x y

What do you thing the function f1 does?What about the function f2?

Functions can have any number of inputs:

Page 131: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

What if we want to return more than a single output value for the same input?

Page 132: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

What if we want to return more than a single output value for the same input?

For example, suppose we want a function that takes a string and return

1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates

Page 133: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

What if we want to return more than a single output value for the same input?

For example, suppose we want a function that takes a string and return

1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates

f“santana”

Page 134: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

What if we want to return more than a single output value for the same input?

We can return a single output value, which is a triple that contains the three values

f“santana”

For example, suppose we want a function that takes a string and return

1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates

Page 135: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

What if we want to return more than a single output value for the same input?

We can return a single output value, which is a triple that contains the three values

f“santana” (“anatnas”, 7, “sant”)

For example, suppose we want a function that takes a string and return

1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates

Page 136: Intro to computer science   module 3

Functions

Introduction to Computer Science – Preliminaries

What if we want to return more than a single output value for the same input?

We can return a single output value, which is a triple that contains the three values

f“santana” (“anatnas”, 7, “sant”)

For example, suppose we want a function that takes a string and return

1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates

In some programming languages these tuples (pairs, triples, 4-tuples, etc.) are exactly as you see it here. In other languages, however, we need to define a structure that holds this triple. This is done as follows:

structure Triple{ string reverseOfString, integer numberOfCharactersInString, string stringWithNoDuplicates}

Page 137: Intro to computer science   module 3

Conditions in Functions

Introduction to Computer Science – Preliminaries

In most cases functions compute an output value in several ways, based on some conditions of the input

For example, the max function, which returns the maximum of two values, uses an if-then-else construct:

max (x,y) = if (x > y) then return x else return y

Page 138: Intro to computer science   module 3

Conditions in Functions

Introduction to Computer Science – Preliminaries

As another example, recall the truth table of the logical and operator

Page 139: Intro to computer science   module 3

Conditions in Functions

Introduction to Computer Science – Preliminaries

As another example, recall the truth table of the logical and operator

and (x,y) = if ((x == T) & (y == T)) then return T else return F

A function to compute logical and using if-then-else

Page 140: Intro to computer science   module 3

Conditions in Functions

Introduction to Computer Science – Preliminaries

As another example, recall the truth table of the logical and operator

and (x,y) = if ((x == T) & (y == T)) then return T else return F

A function to compute logical and using if-then-else

and T T = Tand x y = F

In some languages we can handle the different input cases by doing pattern-matching on the input values

Page 141: Intro to computer science   module 3

Conditions in Functions

Introduction to Computer Science – Preliminaries

As another example, recall the truth table of the logical and operator

and (x,y) = if ((x == T) & (y == T)) then return T else return F

A function to compute logical and using if-then-else

and T T = Tand x y = F

In some languages we can handle the different input cases by doing pattern-matching on the input valuesIn these languages cases are tested in the order they appear. If

the input does not match one case, the next case is applied.

1) Try different inputs on this function2) Define the logical OR function in the same manner

Page 142: Intro to computer science   module 3

Exercises for Modules 1, 2 and 3

Introduction to Computer Science – Preliminaries

1. Devise a coding scheme to represent simple text in binary code (i.e., as a sequence of 1’s and 0’s). You can assume that the text will contain only lower case characters of the English alphabet, the period (‘.‘), the comma (‘,‘) and the space (‘ ‘) character. According to your coding scheme, how many bits would be required to code a piece of text that has 100 characters.

2. Consider the following statements:a) Animals that have wings flyb) Eagles have wingsc) John owns a pet eagle d) The name of the eagle John owns is ‘Clay’

2.1 Represent the above statements in first-order logic (FOL)2.2 Show/prove using logical inefrencing rules that Clay flies

3. Show/prove using the inference rules of logic that the following are true:a) (A U (B \ C)) = (A U B) \ (A U C)

b) If (A µ B) and (B µ A) then A = B

c) if (A U B) = A then (A µ B)

4. Suppose we have an operation called difference (‘ – ‘) such that (A – B) returns a set that has all the elements that are in A but in B. How can you get the intersection of A and B using this operation only?

Page 143: Intro to computer science   module 3

Introduction to Computer Science

END OF MODULE 3

MATHEMATICAL PRILIMENARIES – RECURSION AND INDUCTIVE STRUCTURES (part a)

Next module