22
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

Embed Size (px)

Citation preview

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

Lecture 4Sequences

CSCI – 1900 Mathematics for Computer Science

Fall 2014

Bill Pine

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

CSCI 1900 Lecture 4 - 2

Lecture Introduction

• Reading– Rosen - Section 2.4

• Sequences– Definition– Properties

• Recursion• Characteristic Function

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

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

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

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

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

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

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

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

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

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….

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

CSCI 1900 Lecture 4 - 8

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

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

CSCI 1900 Lecture 4 - 9

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

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

CSCI 1900 Lecture 4 - 10

Explicit Sequences and a Program

for n = 1 thru 100 by 1y = n * 5display “y sub” n “is equal to” y

next n

yn = 5 *n in this sequence

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

CSCI 1900 Lecture 4 - 11

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)

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

CSCI 1900 Lecture 4 - 12

Recursive Sequences and a Program

yMinus2 = 1print “y sub 1 is equal to” yMinus2yMinus1 = 2print “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

yn = yn-1+yn-2 in this sequence

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

CSCI 1900 Lecture 4 - 13

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) = ???

1 if xA

0 if xA

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

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

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

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

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

CSCI 1900 Lecture 4 - 16

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)

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

CSCI 1900 Lecture 4 - 17

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 Babc d

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

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

fU(x) = 1

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

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 ?

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

CSCI 1900 Lecture 4 - 20

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

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

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}

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

CSCI 1900 Lecture 4 - 22

Key Concepts Summary

• Sequences– Integers– Strings

• Recursion• Characteristic Function