Lecture 4 Sequences CSCI 1900 Mathematics for Computer

  • Slides: 22
Download presentation
Lecture 4 Sequences CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Lecture 4 Sequences CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Lecture Introduction • Reading – Rosen - Section 2. 4 • Sequences – Definition

Lecture Introduction • Reading – Rosen - Section 2. 4 • Sequences – Definition – Properties • Recursion • Characteristic Function CSCI 1900 Lecture 4 - 2

Sequence • A sequence is an ordered list of objects • It can be

Sequence • A sequence is an ordered list of objects • It can be finite – 1, 2, 3, 2, 1, 0, 1, 2, 3, 1, 2 Or infinite – 3, 1, 4, 1, 5, 9, 2, 6, 5, … • In contrast to sets, with sequences – Duplicates are significant – Order is significant CSCI 1900 Lecture 4 - 3

Sequence Examples • 1, 2, 3, 2, 2, 3, 1 is a sequence, but

Sequence Examples • 1, 2, 3, 2, 2, 3, 1 is a sequence, but not a set • The sequences 1, 2, 3, 2, 2, 3, 1 and 2, 2, 1, 3 are made from elements of the set {1, 2, 3} • The sequences 1, 2, 3, 2, 2, 3, 1 and 2, 1, 3, 2, 2, 3, 1 only switch two elements, but that is sufficient to make them unequal CSCI 1900 Lecture 4 - 4

Alphabetic Sequences • Finite list of characters – D, i, s, c, r, e,

Alphabetic Sequences • Finite list of characters – D, i, s, c, r, e, t, e – d, a, b, d, c, a • Infinite lists of characters – a, b, a, b… – This, the, song, that, never, ends, It, goes, on, and, on, my, friends, Someone, started, singing, it, not, knowing, what, it, was, and, they’ll, continue, singing, it, forever, just, because… • String : a sequence of letters or symbols written without commas CSCI 1900 Lecture 4 - 5

Linear Array • Principles of sequences can be applied to computers, specifically, arrays •

Linear Array • Principles of sequences can be applied to computers, specifically, arrays • There are some differences though – Sequence • Well-defined • Modification of any element or its order results in a new sequence – Array • May not have all elements initialized • Modification of array by software may occur • Even if the array has variable length, we’re ultimately limited to finite length CSCI 1900 Lecture 4 - 6

Set Corresponding to a Sequence • Set corresponding to a sequence - the set

Set Corresponding to a Sequence • Set corresponding to a sequence - the set of all distinct elements of the sequence – {a, b} is the set corresponding to sequence abab… – {1, 2, 3} is the set corresponding to the sequences • 1, 2, 3, 3, 2, 1 • 1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1…. CSCI 1900 Lecture 4 - 7

Defining Infinite Sequences • Explicit Formula, – The value for the nth item –

Defining Infinite Sequences • Explicit Formula, – The value for the nth item – an – is determined by a formula based solely on n • Recursive Formula – The value for the nth item is determined by a formula based on n and the previous value(s) – an , an-1 , … – in the sequence CSCI 1900 Lecture 4 - 8

Explicit Sequences • Easy to calculate any specific element • By convention a sequence

Explicit Sequences • Easy to calculate any specific element • By convention a sequence starts with n = 1 • an = 2 * n – 2, 4, 6, 8, 10, 12, 14, 16, … – a 3 = 2 * 3 = 6 – a 5 = ? ? ? • an = 3 * n + 6 – 9, 12, 15, 18, 21, 24, … – a 5 = 3 * 5 + 6 = 21 – a 100 = ? ? ? • Defines the values a variable is assigned in a program loop when adding/subtracting/multiplying/dividing by a constant CSCI 1900 Lecture 4 - 9

Explicit Sequences and a Program for n = 1 thru 100 by 1 y

Explicit Sequences and a Program for n = 1 thru 100 by 1 y = n * 5 display “y sub” n next n “is equal to” y yn = 5 *n in this sequence CSCI 1900 Lecture 4 - 10

Recursive Sequences • Used when the nth element depends on some calculation on the

Recursive Sequences • Used when the nth element depends on some calculation on the prior element(s) • You must specify the values of the first term(s) • Example: a 1 = 1 a 2 = 2 an = an-1 + an-2 • Defines the values a variable is assigned in a program loop when the current value depends upon its previous value(s) CSCI 1900 Lecture 4 - 11

Recursive Sequences and a Program y. Minus 2 = 1 print “y sub 1

Recursive Sequences and a Program y. Minus 2 = 1 print “y sub 1 is equal to” y. Minus 2 y. Minus 1 = 2 print “y sub 2 is equal to” y. Minus 1; for n = 3 thru 100 by 1 y = y. Minus 1 + y. Minus 2 print “y sub” n “ is equal to” y y. Minus 2 = y. Minus 1 = y next n yn = yn-1+yn-2 in this sequence CSCI 1900 Lecture 4 - 12

Characteristic Functions • Characteristic function – function defining membership in a set, over the

Characteristic Functions • Characteristic function – function defining membership in a set, over the universal set • f. A(x) = 1 if x A 0 if x A • Example A = {1, 4, 6} Where U = Z+ – f. A(1) = 1, f. A(2) = 0, f. A(4) = 1, f. A(5) = 0, f. A(6) = 1, f. A(7) = 0, … – f. A(0) = ? ? ? CSCI 1900 Lecture 4 - 13

Representing Sets with a Computer • Recall: sets have no order and no duplicated

Representing Sets with a Computer • Recall: sets have no order and no duplicated elements • The need to assign each element in a set to a memory location – Gives the set order in a computer • We can use the characteristic function to define a set with a computer program CSCI 1900 Lecture 4 - 14

Characteristic Function in Programming • To see a simplistic implementation, consider the following example

Characteristic Function in Programming • To see a simplistic implementation, consider the following example with A ={1, 4, 6} function is. In. A( x ) if( (x == 1) OR (x == 4) OR (x == 6) ) return ( 1 ) else return ( 0 ) • Might be used to validate user input CSCI 1900 Lecture 4 - 15

Properties of Characteristic Functions Characteristic functions of subsets satisfy the following properties (proofs are

Properties of Characteristic Functions Characteristic functions of subsets satisfy the following properties (proofs are on page 15 of textbook) – f. A B(x) = f. A(x)f. B(x) – f. A B(x) = f. A(x) + f. B(x) – 2 f. A(x)f. B(x) CSCI 1900 Lecture 4 - 16

Proving Characteristic Function Properties • A way to prove these is by enumeration of

Proving Characteristic Function Properties • A way to prove these is by enumeration of cases • Example: Prove f. A B = f. Af. B a A c d B b f. A(a) = 0, f. B(a) = 0, f. A(a) f. B(a) = 0 0 = f. A B(a) f. A(b) = 0, f. B(b) = 1, f. A(b) f. B(b) = 0 1 = 0 = f. A B(b) f. A(c) = 1, f. B(c) = 0, f. A(c) f. B(c) = 1 0 = f. A B(c) f. A(d) = 1, f. B(d) = 1, f. A(d) f. B(d) = 1 1 = f. A B(d) CSCI 1900 Lecture 4 - 17

More Properties of Sequences • Any set with n elements can be arranged as

More Properties of Sequences • Any set with n elements can be arranged as a sequence of length n, but not vice versa – Sets have no order and duplicates don’t matter – Each subset can be identified with its characteristic function – A sequence of 1’s and 0’s • Characteristic function for universal set, U, is f. U(x) = 1 CSCI 1900 Lecture 4 - 18

Countable and Uncountable • A set is countable if it corresponds to some sequence

Countable and Uncountable • A set is countable if it corresponds to some sequence – Members of set can be arranged in a list – Elements have position • All finite sets are countable • Some, but not all infinite sets are countable, otherwise are said to be uncountable – An example of an uncountable set is set of real numbers – E. g. , what comes after 1. 23534 ? CSCI 1900 Lecture 4 - 19

Countable Sets • A finite set S is always countable – It can correspond

Countable Sets • A finite set S is always countable – It can correspond the sequence • an = n for all n N where n |S| • Countable { x | x = 5 * n and n Z+ } – Corresponds to the sequence an = n for all Set 5 10 15 20 25 30 Seq 1 2 3 4 5 6 CSCI 1900 Lecture 4 - 20

Strings • Sequences can be made up of characters • Example: W, a, k,

Strings • Sequences can be made up of characters • Example: W, a, k, e, , u, p • Removing the commas and you get a string “Wake up” • Strings can illustrate the difference between sequences and sets more clearly – a, b, a, … is a sequence, i. e. , “abababa…” is a string – The corresponding set is {a, b} CSCI 1900 Lecture 4 - 21

Key Concepts Summary • Sequences – Integers – Strings • Recursion • Characteristic Function

Key Concepts Summary • Sequences – Integers – Strings • Recursion • Characteristic Function CSCI 1900 Lecture 4 - 22