120 likes | 280 Views
CSNB143 – Discrete Structure. Topic 2 – Sequence and String. Topic 2 – Sequence and String. Learning Outcomes Students should be able to differentiate few characteristics of sequence. Students should be able to use sequence and strings.
E N D
CSNB143 – Discrete Structure Topic 2 – Sequence and String
Topic 2 – Sequence and String Learning Outcomes • Students should be able to differentiate few characteristics of sequence. • Students should be able to use sequence and strings. • Students should be able to concatenate string and know how to use them.
Topic 2 – Sequence and String Sequence • A list of objects in its order. That is, taking order as an important thing. • A list in which the first one should be in front, followed by the second element, third element and so on. • List might be ended after n, n N and it is named as Finite Sequence. We called n as an index for that sequence. • List might have no ending value, and this is called as Infinite Sequence. • Elements might be redundant (repeated) Example 1 : S = 2, 4, 6, …, 2n S = S1, S2, S3, … Sn where S1=2, S2= 4, S3=6, … Sn = 2n Example 2: T = a, a, b, a, b where T1=a, T2=a, T3=b, T4=a, T5=b
Topic 2 – Sequence and String Sequence • If the sequence is depending on the previous value, it is called Recursive Sequence. • If the sequence is not depending on the previous value, in which the value can be directly retrieved, it is called Explicit Sequence. Example 3: An = An-1 + 5; A1 = 1, this is a recursive sequence where: A2 = A1 + 5 A3 = A2 + 5 Example 4: An = n2 + 1; 1 n < , this is an explicit sequence where: A1 = 1 + 1 = 2 A2 = 4 + 1 = 5 A3 = 9 + 1 = 10 That is, we can get the value directly, without any dependency to previous value.
Topic 2 – Sequence and String • Both recursive and explicit formula can have both finite and infinite sequence. Example 5: Consider all the sequences below, and identify which sequence is recursive/explicit and finite/infinite. • C1 = 5, Cn = 2Cn-1, 2 n 6 • D1 = 3, Dn = Dn-1 + 4 • Sn = (-4)n, 1 n • Tn = 92 – 5n, 1 n 5 • Both sequences also can have an Increasing or Decreasing sequence. • A sequence is said to be increased if for each Sn, the value is less than Sn + 1 for all n, Sn Sn + 1 ; all n • A sequence is said to be decreased if for each Sn the value is bigger than Sn + 1 for all n, Sn Sn + 1 ; all n
Topic 2 – Sequence and String Determine either this sequence in increasing or decreasing. • Sn = 2(n + 1), n 1 • Xn = (½)n, n 1 • S = 3, 5, 5, 7, 8, 8, 13 A sequence is said to be increased if for each Sn, the value is less than Sn + 1 for all n, Sn Sn + 1 ; all n A sequence is said to be decreased if for each Sn the value is bigger than Sn + 1 for all n, Sn Sn + 1 ; all n
Topic 2 – Sequence and String When we use sequence? • It is being used in running the computer code, where the code must be executed one line after another. The codes cannot jump from one line to the next 10 lines because of the sequences. All the codes are following the concept of First In First Out (FIFO). • It is also being used in array, where every single data can be reached as an individual because we always know the sequence.
Topic 2 – Sequence and String String • A sequence of characters without comma (,), will produce string. • This sequence can be finite or infinite. • Example: A finite character sequence: • s, c, h, o, o, l = character sequence • school = string • Example: infinite character sequence • a, b, a, b, a, b, a, b,…. = sequence • abababab… = string
Topic 2 – Sequence and String • Given a set A, we can build set A* where it will contains all finite sequence of all elements in A. • If the elements in A are characters (a, b, c, …, z), then the product of A* is string or words. • Example: • Let say A = {a, b, c, …, z} • Then A* = {aaaa, computer, denda, pqr, sysrq,… }
Topic 2 – Sequence and String Subsequence • It is quite different from what we have learn in subset • A new sequence can be build from original sequence, but the order of elements must remains. • Example: • T = a, a, b, c, q where T1=a, T2=a, T 3=b, T4=c, T5=q • S = b, c is a subsequence of T • But R = c, b is not a subsequence of T
Topic 2 – Sequence and String Concatenation • Let say W1 = A1A2A3…An and W2 = B1B2B3…Bm, and W1 and W2 are both elements in A* for set A. Concatenation for W1 and W2 is: W1.W2 =A1A2A3…AnB1B2B3…Bm where W1.W2 is also an element of A* And it is known that W1. = W1 and .W1 = W1 • Example: Let say R = aabc, S = dacb So, R.S = aabcdacb S.R = dacbaabc R. = aabc .R = aabc
Topic 2 – Sequence and String Exercise • Write the first four elements of the following sequence • an = 5n • gn = 1.2.3…….n • c1 = 2.5, cn = cn-1 +1.5 • e1 = 0, en = en-1 - 2 • Write a recursive formula for the nth term of the sequence. • 1,3,5,7 …… • 1, 4, 7, 10, 13, 16