csci-2200 foundations of computer sciencecs.rpi.edu/~pattes3/csci2200/slides/feb12.pdf · more...

35
CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCE Spring 2019 1

Upload: others

Post on 29-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

CSCI-2200FOUNDATIONS OF COMPUTER SCIENCESpring 2019

1

Page 2: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Announcements

• Exam 1 is Wednesday, Feb 13. 6pm – 7:50pm

• If you get special accommodations, you should have received email from Ms. Eberwein with the date, time, and location of your exam.• If you did not receive this email, email me asap.

• Please see slides from Feb. 5 for other announcements about Exam 1.• Will include material up through Feb 8th lecture.

• Homework 3 is due Feb. 12 at 11:59pm

2

Page 3: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

SET CARDINALITYSection 2.5

3

Page 4: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Countable Sets• An infinite set S is countable iff there exists a bijective

function f: S → Z+

• f maps each element of S to exactly one element of Z+.• Every element of Z+ is mapped to by some element of S, under f.

• All infinite countable sets are the same size.• They all have the same size as Z+

• All finite sets are countable.

4

Page 5: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

• Theorem: The set of positive even integers is countable.

5

Page 6: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

More about countable sets• A subset of a countable set is countable.

• An infinite set is countable if and only if it is possible to list all of the elements in the set in a sequence.• We can enumerate the set.

• If I read off elements from the sequence, for every element, there some time in the future at which I will read that element.• No element waits infinitely long to be read.

6

Page 7: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

7

Example: The set of positive rational numbers is countable

PositiveRational numbers:

!,87,

43,

21

Page 8: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

8

Naïve Approach

Rational numbers: !,31,

21,

11

Positive Integers

Correspondence:

Doesn�t work:we will never count numbers with numerator 2: !,

32,

22,

12

Numerator 1

1, 2, 3

Page 9: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

9

Better Approach

11

21

31

41

12

22

32

13

23

14

!

!

!

!

Page 10: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

10

11

21

31

41

12

22

32

13

23

14

!

!

!

!

Page 11: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

11

11

21

31

41

12

22

32

13

23

14

!

!

!

!

Page 12: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

12

11

21

31

41

12

22

32

13

23

14

!

!

!

!

Page 13: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

13

11

21

31

41

12

22

32

13

23

14

!

!

!

!

Page 14: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

14

11

21

31

41

12

22

32

13

23

14

!

!

!

!

Page 15: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

15

Rational Numbers: !,22,

31,

12,

21,

11

Correspondence:

Positive Integers: !,5,4,3,2,1

Page 16: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

16

Theorem:

The real numbers are uncountable.

A set is uncountable if it is not countable.

Definition:

Page 17: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

17

Theorem: If A is countable and B is countable, then A U B is countable.

Page 18: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Schroder-Bernstein Theorem• Theorem: If A and B are sets with |A| ≤ |B| and |B| ≤ |A|,

then |A| = |B|.

In other words, if there are injective functions f: A → B and g: B → A, then there is a bijection from A to B (and B to A).

18

Page 19: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

• Show that | (0,1) | = |(0, 1]|

19

Page 20: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

SEQUENCES AND SUMMATIONSection 2.4

20

Page 21: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Sequences• Imagine a person (with a lot of spare time) who decides to

count her ancestors.• She has two parents, four grandparents, eight grand-

grandparents, etc.• We can write this in a table

• Can guess that the kth element is 2k.• Just a guess – we would need to prove this.

21

1 2 3 4 5 62 4 8 16 32 64

Page 22: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Sequences• A sequence is a ordered list of elements.

• Each element has a unique position in the list.• Formally, a sequence is a function from a subset of the integers to a

set S.• Usually maps from the set {0,1,2,3,4,…..} or {1,2,3,4,….} to the

set S.

• We do not write f(n) for an element in a sequence.• Instead, the notation an is used to denote the image of the integer n.

• The sequence is {a0, a1, a2, a3, …}

• We call an a term of the sequence.

22

Page 23: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Example of Sequence

23

Page 24: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Formula for a Sequence?• Can we find an explicit formula for the nth term given only the first few

elements of a sequence?• Examples:

• 7, 11, 15, 19, 23, 27, 31, 35, ... • 3, 6, 11, 18, 27, 38, 51, 66, 83, ...• 0, 2, 8, 26, 80, 242, 728, 2186, 6560, 19682, ... • O, T, T, F, F, S, S, E, ...

• To do this, try to find a pattern• Are terms obtained from previous terms by adding the same

amount, or an amount that depends on position in the sequence? • Are terms obtained from previous terms by multiplying by a

particular amount? • Are terms obtained by combining previous terms in a certain way?

24

Page 25: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Arithmetic Progression• An arithmetic progression is a sequence of the form:

where the initial term a and the common difference d are real numbers.

• Another way to write this is

Examples:1. Let a = −1 and d = 4:

1. Let a = 7 and d = −3:

25

a+ nd, n = 0, 1, 2, . . .

Page 26: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Geometric Progression• A geometric progression is a sequence of the form:

where the initial term a and the common ratio r are real numbers.• Another way to write this isExamples:

Let a = 1 and r = −1.

Let a = 2 and r = 5.

Let a = 6 and r = 1/3.

26

tn = arn, n = 0, 1, 2, . . .

Page 27: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Recurrence Relations• A recurrence relation for the sequence {an} is an equation that

expresses an in terms of one or more of the previous terms of the sequence.

• The initial conditions for a sequence specify the terms that precede the first term where the recurrence relation takes effect.

• Example:• a0 = 2

an = an-1 + 3 for n = 1,2,3,4,….

• What are a1, a2 and a3?

27

Page 28: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Example Recurrence Relation• Let {an} be a sequence that satisfies the recurrence

relation an = an-1 – an-2 for n = 2,3,4,….• The initial conditions are a0 = 3 and a1 = 5.• What are a2 and a3?

28

Page 29: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Fibonacci SequenceDefine the Fibonacci sequence f0 ,f1 ,f2,…, by:• Initial Conditions: f0 = 0, f1 =1• Recurrence Relation: fn = fn-1 + fn-2

Example: Find f2 ,f3 ,f4 , and f5.

29

Page 30: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Solving Recurrence Relations• Finding a formula for the nth term of the sequence generated

by a recurrence relation is called solving the recurrence relation.

• Such a formula is called a closed formula.• Example:

• Let {an} be a sequence that satisfies the recurrence relation an = 2an-1 – an-2 for n = 2,3,4,….

• Is an = 3n is a solution?

30

Page 31: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Solving Recurrence Relations (cont.)• Let {an} be a sequence that satisfies the recurrence relation an = 2an-1 – an-2 for n = 2,3,4,….

• Is an = 2n a solution?

• Is an = 5 a solution?

31

Page 32: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Iterative Solution ExampleLet {an} be a sequence that satisfies the recurrence relation

an= an-1+ 3 for n =2,3,4,….Suppose that a1 = 2.Finding a Solution - Method 1: forward substitution

32

Page 33: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Iterative Solution Example• Let {an} be a sequence that satisfies the recurrence relation

an = an-1 + 3 for n =2,3,4,….Suppose that a1 = 2.

• Find a Solution – Method 2: backward substitution

33

Page 34: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Compound Interest Example • Suppose a person deposits $10,000 in a savings account at a

bank yielding 11% per year with interest compounded annually. How much will be in the account after 30 years?

34

Page 35: CSCI-2200 FOUNDATIONS OF COMPUTER SCIENCEcs.rpi.edu/~pattes3/csci2200/slides/Feb12.pdf · More about countable sets •A subset of a countable set is countable. •An infinite set

Good Problems to Review• Section 2.4: 1, 3, 9, 11, 13, 15, 17, 19, 21, 23

35