Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 4 Sequences CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine."— Presentation transcript:

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

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

3 CSCI 1900 Lecture 4 - 3 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

4 CSCI 1900 Lecture 4 - 4 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

5 CSCI 1900 Lecture 4 - 5 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,a,b,a,b… –This,is,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

6 CSCI 1900 Lecture 4 - 6 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

7 CSCI 1900 Lecture 4 - 7 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 abababab… –{1,2,3} is the set corresponding to the sequences 1,2,3 1,2,3,3,3,3,2,1,2,1 1,2,3,3,2,1,1,2,3,3,2,1….

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

9 CSCI 1900 Lecture 4 - 9 Explicit Sequences Easy to calculate any specific element By convention a sequence starts with n = 1 a n = 2 * n –2, 4, 6, 8, 10, 12, 14, 16, … –a 3 = 2 * 3 = 6 –a 5 = ??? a n = 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

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

11 CSCI 1900 Lecture 4 - 11 Recursive Sequences Used when the n th 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 a n = a n-1 + a n-2 Defines the values a variable is assigned in a program loop when the current value depends upon its previous value(s)

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

13 CSCI 1900 Lecture 4 - 13 Characteristic Functions Characteristic function – function defining membership in a set, over the universal set f A (x) = 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) = ??? 1 if x  A 0 if x  A

14 CSCI 1900 Lecture 4 - 14 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

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

16 CSCI 1900 Lecture 4 - 16 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) – f A (x)f B (x) – f A  B (x) = f A (x) + f B (x) – 2f A (x)f B (x)

17 CSCI 1900 Lecture 4 - 17 Proving Characteristic Function Properties A way to prove these is by enumeration of cases Example: Prove f A  B = f A f B f A (a) = 0, f B (a) = 0, f A (a)  f B (a) = 0  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 = 0 = f A  B (c) f A (d) = 1, f B (d) = 1, f A (d)  f B (d) = 1  1 = 1 = f A  B (d) AB a b c d

18 CSCI 1900 Lecture 4 - 18 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

19 CSCI 1900 Lecture 4 - 19 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 ?

20 CSCI 1900 Lecture 4 - 20 Countable Sets A finite set S is always countable –It can correspond the sequence a n = n for all n  N where n  |S| Countable { x | x = 5 * n and n  Z + } –Corresponds to the sequence a n = n for all Set51015202530 Seq123456

21 CSCI 1900 Lecture 4 - 21 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, b, a, b, a, … is a sequence, i.e., “abababa…” is a string –The corresponding set is {a, b}

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


Download ppt "Lecture 4 Sequences CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine."

Similar presentations


Ads by Google