220 likes | 305 Vues
Learn about sequences - ordered lists of objects - and their properties, recursion, and characteristic functions. Explore different types of sequences, alphabetic sequences, and corresponding sets. Understand infinite sequences, explicit and recursive sequences, and how they are defined and used in programs. Discover the role of characteristic functions in representing sets and programming.
E N D
Lecture 4Sequences CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine
Lecture Introduction • Reading • Rosen - Section 2.4 • Sequences • Definition • Properties • Recursion • Characteristic Function CSCI 1900
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
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
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 CSCI 1900
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
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…. CSCI 1900
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
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, … • a3 = 2 * 3 = 6 • a5 = ??? • an = 3 * n + 6 • 9, 12, 15, 18, 21, 24, … • a5 = 3 * 5 + 6 = 21 • a100 = ??? • Defines the values a variable is assigned in a program loop when adding/subtracting/multiplying/dividing by a constant CSCI 1900
for n = 1 thru 100 by 1 y = n * 5 display “y sub” n “is equal to” y next n Explicit Sequences and a Program yn = 5 *n in this sequence CSCI 1900
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: a1 = 1a2 = 2an = 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
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 Recursive Sequences and a Program yn = yn-1+yn-2 in this sequence CSCI 1900
1 if xA 0 if xA Characteristic Functions • Characteristic function – function defining membership in a set, over the universal set • fA(x) = • Example A = {1, 4, 6} Where U = Z+ • fA(1) = 1, fA(2) = 0, fA(4) = 1, fA(5) = 0, fA(6) = 1, fA(7) = 0, … • fA(0) = ??? CSCI 1900
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
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 CSCI 1900
Properties of Characteristic Functions Characteristic functions of subsets satisfy the following properties (proofs are on page 15 of textbook) • fAB(x) = fA(x)fB(x) • fAB(x) = fA(x) + fB(x) – fA(x)fB(x) • fAB(x) = fA(x) + fB(x) – 2fA(x)fB(x) CSCI 1900
Proving Characteristic Function Properties • A way to prove these is by enumeration of cases • Example: Prove fAB = fAfB fA(a) = 0, fB(a) = 0, fA(a) fB(a) = 0 0 = 0 = fAB(a)fA(b) = 0, fB(b) = 1, fA(b) fB(b) = 0 1 = 0 = fAB(b)fA(c) = 1, fB(c) = 0, fA(c) fB(c) = 1 0 = 0 = fAB(c)fA(d) = 1, fB(d) = 1, fA(d) fB(d) = 1 1 = 1 = fAB(d) a A B d b c CSCI 1900
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 fU(x) = 1 CSCI 1900
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
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 CSCI 1900
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} CSCI 1900
Key Concepts Summary • Sequences • Integers • Strings • Recursion • Characteristic Function CSCI 1900