csci-2200 foundations of computer sciencecs.rpi.edu/~pattes3/csci2200/slides/feb12.pdf · more...
TRANSCRIPT
CSCI-2200FOUNDATIONS OF COMPUTER SCIENCESpring 2019
1
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
SET CARDINALITYSection 2.5
3
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
• Theorem: The set of positive even integers is countable.
5
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
7
Example: The set of positive rational numbers is countable
PositiveRational numbers:
!,87,
43,
21
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
9
Better Approach
11
21
31
41
12
22
32
13
23
14
!
!
!
!
10
11
21
31
41
12
22
32
13
23
14
!
!
!
!
11
11
21
31
41
12
22
32
13
23
14
!
!
!
!
12
11
21
31
41
12
22
32
13
23
14
!
!
!
!
13
11
21
31
41
12
22
32
13
23
14
!
!
!
!
14
11
21
31
41
12
22
32
13
23
14
!
!
!
!
15
Rational Numbers: !,22,
31,
12,
21,
11
Correspondence:
Positive Integers: !,5,4,3,2,1
16
Theorem:
The real numbers are uncountable.
A set is uncountable if it is not countable.
Definition:
17
Theorem: If A is countable and B is countable, then A U B is countable.
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
• Show that | (0,1) | = |(0, 1]|
19
SEQUENCES AND SUMMATIONSection 2.4
20
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
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
Example of Sequence
23
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
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, . . .
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, . . .
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
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
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
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
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
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
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
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
Good Problems to Review• Section 2.4: 1, 3, 9, 11, 13, 15, 17, 19, 21, 23
35