MAT2033 · Week 5 · sequences and sums, cardinality of sets, intoduction to algorithms

Sequences, summations, cardinality and an introduction to algorithms

Arithmetic/geometric sequences, Σ summation formulas and double sums, countable and uncountable sets (Cantor), maximum in pseudocode, linear/binary search, bubble sort and greedy algorithms.

sequencearithmetic progressiongeometric progressionrecurrence relationFibonacci sequenceclosed formulasummation notationindex of summationdouble summationproduct notationcardinalitycountableuncountableCantor diagonal argumentalgorithmpseudocodelinear searchbinary searchbubble sortinsertion sortgreedy algorithm

This week combines three separate topics: sequences and sums, the size of sets (cardinality), and an introduction to algorithms. A summation computation is a sure source of marks on the midterm: there is no calculator, so the numbers are small enough to compute by hand and knowing the formulas is enough. From cardinality there is usually a short part like “is this set countable, give a bijection”. The algorithms part is the groundwork for next week’s Big-O topic: you will learn to read and trace pseudocode.

Golden rules:

  1. Split the sum: ∑(ak+b)=a∑k+∑b\sum (ak + b) = a\sum k + \sum b.
  2. If the lower bound is not 1: sum from 1 to the upper bound, and subtract the sum from 1 to (lower bound − 1).
  3. In a geometric sum, count the terms. If j=0..nj = 0..n there are n+1n + 1 terms.
  4. Countable = I can list the elements as 1st, 2nd, 3rd, one by one.
  5. When tracing an algorithm, write every variable in a table. Don’t do it in your head.

Sequences

Sequence: a function from the integers (usually {0,1,2,… }\{0, 1, 2, \dots\} or {1,2,3,… }\{1, 2, 3, \dots\}) to a set. Its nnth term is ana_n.

TypeRuleExample
Arithmetic progressionan=a+nda_n = a + nd5,8,11,14,…5, 8, 11, 14, \dots (a=5a = 5, d=3d = 3)
Geometric progressionan=arna_n = a r^n3,6,12,24,…3, 6, 12, 24, \dots (a=3a = 3, r=2r = 2)
Recurrence relationana_n in terms of earlier termsan=an−1+3a_n = a_{n-1} + 3, a0=5a_0 = 5
Fibonaccifn=fn−1+fn−2f_n = f_{n-1} + f_{n-2}, f0=0f_0 = 0, f1=1f_1 = 10,1,1,2,3,5,8,13,210, 1, 1, 2, 3, 5, 8, 13, 21

Solving a recurrence relation means finding a closed formula that writes ana_n only in terms of nn. The simple method is iteration: write a few terms, see the pattern, verify with induction. For an=an−1+3a_n = a_{n-1} + 3, a0=5a_0 = 5, we get an=5+3na_n = 5 + 3n.

Order for finding a formula from terms: are the differences constant (arithmetic)? Are the ratios constant (geometric)? Compare with squares, cubes, 2n2^n, n!n!. Is each term twice the previous one plus one?

Summations

∑k=mnak=am+am+1+⋯+an\sum_{k=m}^{n} a_k = a_m + a_{m+1} + \dots + a_n

kk is the index of summation, mm the lower bound, nn the upper bound. The name of the index does not matter: ∑k=15k2=∑j=15j2\sum_{k=1}^5 k^2 = \sum_{j=1}^5 j^2.

Formulas to memorize

SumResultExample
∑k=1nc\sum_{k=1}^{n} ccncn∑k=185=40\sum_{k=1}^{8} 5 = 40
∑k=1nk\sum_{k=1}^{n} kn(n+1)2\dfrac{n(n+1)}{2}n=100n = 100: 50505050
∑k=1nk2\sum_{k=1}^{n} k^2n(n+1)(2n+1)6\dfrac{n(n+1)(2n+1)}{6}n=10n = 10: 385385
∑k=1nk3\sum_{k=1}^{n} k^3(n(n+1)2)2\left(\dfrac{n(n+1)}{2}\right)^2n=4n = 4: 100100
∑j=0narj\sum_{j=0}^{n} ar^ja(rn+1−1)r−1\dfrac{a(r^{n+1} - 1)}{r - 1}, r≠1r \ne 1∑j=052⋅3j=728\sum_{j=0}^{5} 2 \cdot 3^j = 728

See once why the geometric formula is correct: S=a+ar+⋯+arnS = a + ar + \dots + ar^n. rS=ar+⋯+arn+1rS = ar + \dots + ar^{n+1}. Subtract: rS−S=arn+1−arS - S = ar^{n+1} - a, that is S=a(rn+1−1)r−1S = \tfrac{a(r^{n+1} - 1)}{r - 1}. All the middle terms cancelled each other.

Shifting the index

Write ∑k=48k2\sum_{k=4}^{8} k^2 with j=k−3j = k - 3: k=4⇒j=1k = 4 \Rightarrow j = 1, k=8⇒j=5k = 8 \Rightarrow j = 5, the term is (j+3)2(j + 3)^2.

∑k=48k2=∑j=15(j+3)2=16+25+36+49+64=190.\sum_{k=4}^{8} k^2 = \sum_{j=1}^{5} (j+3)^2 = 16 + 25 + 36 + 49 + 64 = 190.

A faster way: ∑18k2−∑13k2=204−14=190\sum_{1}^{8} k^2 - \sum_{1}^{3} k^2 = 204 - 14 = 190.

Double sums and products

In a double sum, compute the inner sum first (treat the outer index as a constant), then the outer sum. This is the same as two nested for loops:

total := 0
for i := 1 to 3
    for j := 1 to 4
        total := total + (2i − j)

Product notation: ∏i=142i=2⋅4⋅6⋅8=384\prod_{i=1}^{4} 2i = 2 \cdot 4 \cdot 6 \cdot 8 = 384. n!=∏i=1nin! = \prod_{i=1}^{n} i.

PITFALL · The number of terms in ∑k=mn\sum_{k=m}^{n}. From k=10k = 10 to 3030 there are 30−10+1=2130 - 10 + 1 = 21 terms, not 20. This difference costs marks in the sum of a constant term: ∑k=10302=42\sum_{k=10}^{30} 2 = 42.

Cardinality

For finite sets, size is the number of elements. To compare infinite sets you cannot count; instead you match them up. ∣A∣=∣B∣\lvert A \rvert = \lvert B \rvert if and only if there is a bijection from AA to BB.

  • Countable: finite, or of the same cardinality as Z+\mathbb{Z}^+ (countably infinite). You can list its elements as a1,a2,a3,…a_1, a_2, a_3, \dots without skipping any.
  • Uncountable: not countable.

Stadium intuition: a stadium has infinitely many seats numbered 1,2,3,…1, 2, 3, \dots. If you can give every element of the set a seat (nobody left standing, no two people in the same seat), the set is countable.

Countable sets

  • Positive even numbers: f(n)=2nf(n) = 2n. An infinite set can have the same size as its own proper subset; this is impossible for finite sets.
  • Z\mathbb{Z}: list in the order 0,1,−1,2,−2,3,−3,…0, 1, -1, 2, -2, 3, -3, \dots (the function in question 8).
  • Positive rationals Q+\mathbb{Q}^+: group the p/qp/q by the sum p+qp + q. Each group is finite. Write the fractions that do not reduce in order, skipping repeats:
p+q = 2:  1/1
p+q = 3:  1/2, 2/1
p+q = 4:  1/3, 3/1           (2/2 = 1/1 skipped)
p+q = 5:  1/4, 2/3, 3/2, 4/1
p+q = 6:  1/5, ...
List: 1/1, 1/2, 2/1, 1/3, 3/1, 1/4, 2/3, 3/2, 4/1, 1/5, ...

The union of two countable sets, and the union of finitely or countably many countable sets, is countable.

R\mathbb{R} is uncountable: Cantor’s diagonal argument

Theorem: the interval (0,1)(0, 1) is uncountable (so R\mathbb{R} is too).

Idea of the proof (by contradiction): assume there is a list of all the numbers in (0,1)(0, 1). Look at the iith decimal digit of the iith number in the list. Build a new number: its iith digit is 5 if the iith digit of the iith number is not 5, and 4 if it is 5. This number differs from every number in the list in at least one digit, so it is not in the list. Contradiction.

r1 = 0.[3]1415          diagonal digits: 3, 7, 1, 2, 4
r2 = 0.2[7]182          none is 5 → make them all 5
r3 = 0.14[1]42          new number: 0.55555...
r4 = 0.577[2]1          differs from r_i in the i-th digit
r5 = 0.6931[4]

(In the real proof the list is infinite and each number has infinitely many digits; choosing 5 and 4 avoids the double-representation problem such as 0.4999… = 0.5.)

Consequences: the irrational numbers are uncountable, and P(Z+)\mathcal{P}(\mathbb{Z}^+) is uncountable. A set with an uncountable subset is also uncountable.

Introduction to algorithms

Algorithm: a finite, precise sequence of steps for solving a problem. Its properties: input, output, definiteness (every step is clear), correctness (right result), finiteness (ends in a finite number of steps), effectiveness (every step is doable), generality (applies to a whole class of problems).

Finding the maximum

procedure max(a1, a2, ..., an: integers)
max := a1
for i := 2 to n
    if max < ai then max := ai
return max

Trace: for 4, 9, 2, 11, 7, max takes the values 4, 9, 9, 11, 11. Each element is compared once: n−1n - 1 comparisons.

Linear search: look one by one from start to end. The list may be unsorted. In the worst case, comparison with nn elements.

Binary search: the list must be sorted. At each step it halves the interval:

procedure binary search(x: integer, a1 < a2 < ... < an)
i := 1; j := n
while i < j
    m := ⌊(i + j)/2⌋
    if x > am then i := m + 1
    else j := m
if x = ai then location := i else location := 0
return location

Trace: x=72x = 72 in the list 2, 5, 8, 12, 16, 23, 38, 56, 72, 91:

iijjmmama_mx>amx > a_m?
110516yes, i=6i = 6
610856yes, i=9i = 9
910972no, j=9j = 9
99loop ends, a9=72a_9 = 72, location 9

Because the interval halves in each round, about log⁡2n\log_2 n rounds are enough. Even with 1,000,000 elements, about 20.

Sorting: bubble sort and insertion sort

Bubble sort: compare neighbouring elements and swap them if they are out of order. At the end of each pass the largest remaining element settles at the end.

3, 2, 4, 1, 5
pass 1:  2, 3, 1, 4, 5      (5 is already in place, 4 settled into place)
pass 2:  2, 1, 3, 4, 5
pass 3:  1, 2, 3, 4, 5

The number of comparisons is always (n−1)+(n−2)+⋯+1=n(n−1)2(n-1) + (n-2) + \dots + 1 = \tfrac{n(n-1)}{2}.

Insertion sort: keep the left part sorted and insert the next element in the right place. Like arranging the cards in your hand in a card game.

Greedy algorithm

At each step make the choice that looks best at that moment. Making change: give the largest coin that fits. With coins of 25, 10, 5, 1 cents the greedy algorithm always gives the fewest coins (this can be proved). But it can fail when the coin set changes: with 25, 10, 1 and 30 cents, greedy gives 6 coins, although 3 dimes are enough. The answer to “is the greedy algorithm optimal?” is either a proof or a counterexample.

Common mistakes

Wrong ideaThe truth
∑k=1030k=∑130−∑110\sum_{k=10}^{30} k = \sum_1^{30} - \sum_1^{10}∑130−∑19\sum_1^{30} - \sum_1^{9}; 10 is included
In ∑j=05rj\sum_{j=0}^{5} r^j the exponent is 5In the formula it is rn+1=r6r^{n+1} = r^6; there are 6 terms
∑(k2+k)=∑k2⋅∑k\sum (k^2 + k) = \sum k^2 \cdot \sum kSums split, they are not multiplied
Doing the outer sum first in a double sumInner sum first, with the outer index fixed
Infinite set = uncountableZ\mathbb{Z}, Q\mathbb{Q} are infinite but countable
The even numbers are fewer than Z+\mathbb{Z}^+The same cardinality: f(n)=2nf(n) = 2n
Binary search works on an unsorted listA sorted list is required
Bubble sort makes fewer comparisons on a sorted listIn this version always n(n−1)/2n(n-1)/2
Greedy is always the bestCounterexample: 30 cents with 25, 10, 1

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Evaluate ∑k=120(3k−2)\displaystyle\sum_{k=1}^{20} (3k - 2).

Show answer
590

Split the sum: 3∑k=120k−∑k=1202=3⋅20⋅212−2⋅20=3⋅210−40=5903\sum_{k=1}^{20} k - \sum_{k=1}^{20} 2 = 3 \cdot \tfrac{20 \cdot 21}{2} - 2 \cdot 20 = 3 \cdot 210 - 40 = 590. The sum of a constant = constant × number of terms.

Question 2easy

Evaluate ∑j=052⋅3j\displaystyle\sum_{j=0}^{5} 2 \cdot 3^j.

Show answer
728

Geometric sum, a=2a = 2, r=3r = 3, j=0..5j = 0..5, so 6 terms: a(rn+1−1)r−1=2(36−1)2=729−1=728\tfrac{a(r^{n+1} - 1)}{r - 1} = \tfrac{2(3^6 - 1)}{2} = 729 - 1 = 728. The exponent is n+1=6n + 1 = 6, because jj starts at zero.

Question 3medium

Evaluate ∑k=1030k\displaystyle\sum_{k=10}^{30} k.

Show answer
420

The lower bound is not 1: ∑k=1030k=∑k=130k−∑k=19k=465−45=420\sum_{k=10}^{30} k = \sum_{k=1}^{30} k - \sum_{k=1}^{9} k = 465 - 45 = 420. The sum you subtract goes up to 9, not 10. Check: 21 terms, average 20, 21⋅20=42021 \cdot 20 = 420.

Question 4medium

Evaluate the double sum ∑i=13∑j=14(2i−j)\displaystyle\sum_{i=1}^{3} \sum_{j=1}^{4} (2i - j).

Show answer
18

First the inner sum (with ii fixed): ∑j=14(2i−j)=4⋅2i−(1+2+3+4)=8i−10\sum_{j=1}^{4}(2i - j) = 4 \cdot 2i - (1+2+3+4) = 8i - 10. The values for i=1,2,3i = 1, 2, 3 are −2,6,14-2, 6, 14. Outer sum: −2+6+14=18-2 + 6 + 14 = 18.

Question 5easy

Let a0=1a_0 = 1 and an=2an−1+1a_n = 2a_{n-1} + 1 for n≥1n \ge 1. List a0,…,a4a_0, \dots, a_4 and guess a closed formula.

Show answer
1, 3, 7, 15, 31; a_n = 2^(n+1) − 1

Iteration: a1=2⋅1+1=3a_1 = 2 \cdot 1 + 1 = 3, a2=7a_2 = 7, a3=15a_3 = 15, a4=31a_4 = 31. All are one less than a power of 2: 2,4,8,16,322, 4, 8, 16, 32 minus 1. Guess an=2n+1−1a_n = 2^{n+1} - 1. (Proof by induction: 2(2k+1−1)+1=2k+2−12(2^{k+1} - 1) + 1 = 2^{k+2} - 1.)

Question 6medium

Find a simple formula for the nnth term (n≥1n \ge 1) of each sequence: (a) 3, 7, 11, 15, ... (b) 2, 6, 18, 54, ...

Show answer
(a) a_n = 4n − 1 (b) a_n = 2 · 3^(n−1)

(a) The consecutive differences are a constant 4: arithmetic sequence, an=3+4(n−1)=4n−1a_n = 3 + 4(n - 1) = 4n - 1. (b) The consecutive ratios are a constant 3: geometric sequence, an=2⋅3n−1a_n = 2 \cdot 3^{n-1}. Look at the differences first, then the ratios.

Question 7medium

Evaluate ∑k=110(k2+k)\displaystyle\sum_{k=1}^{10} (k^2 + k).

Show answer
440

∑k2=n(n+1)(2n+1)6=10⋅11⋅216=385\sum k^2 = \tfrac{n(n+1)(2n+1)}{6} = \tfrac{10 \cdot 11 \cdot 21}{6} = 385, ∑k=55\sum k = 55. Total 385+55=440385 + 55 = 440.

Question 8medium

Let f:Z+→Zf: \mathbb{Z}^+ \to \mathbb{Z} be f(n)=n/2f(n) = n/2 if nn is even and f(n)=−(n−1)/2f(n) = -(n-1)/2 if nn is odd. Find f(1),f(2),f(3),f(7),f(10)f(1), f(2), f(3), f(7), f(10). What does this function show?

Show answer
0, 1, −1, −3, 5; it is a bijection, so Z is countable

f(1)=0f(1) = 0, f(2)=1f(2) = 1, f(3)=−1f(3) = -1, f(7)=−3f(7) = -3, f(10)=5f(10) = 5. The list is 0,1,−1,2,−2,3,…0, 1, -1, 2, -2, 3, \dots: every integer appears exactly once. This is a bijection, so ∣Z∣=∣Z+∣\lvert \mathbb{Z} \rvert = \lvert \mathbb{Z}^+ \rvert and Z\mathbb{Z} is countable.

Question 9hard

Which of these sets are countable? (a) the positive multiples of 7 (b) Z×Z\mathbb{Z} \times \mathbb{Z} (c) the irrational numbers (d) the set of all finite bit strings.

Show answer
(a) countable (b) countable (c) uncountable (d) countable

(a) f(n)=7nf(n) = 7n is a bijection with Z+\mathbb{Z}^+. (b) List the pairs in order of the value ∣x∣+∣y∣\lvert x \rvert + \lvert y \rvert; each group is finite. (c) R\mathbb{R} is uncountable and Q\mathbb{Q} is countable; if the irrationals were countable too, then R=Q∪irrationals\mathbb{R} = \mathbb{Q} \cup \text{irrationals} would be countable. Contradiction. (d) List by length: length 0, then 1, then 2 ... (there are 2k2^k of each length, a finite number).

Question 10medium

Apply binary search (with i<ji < j loop, m=⌊(i+j)/2⌋m = \lfloor (i+j)/2 \rfloor, "if x>amx > a_m then i:=m+1i := m+1 else j:=mj := m") to find x=23x = 23 in 2, 5, 8, 12, 16, 23, 38, 56, 72, 91. Which elements are compared with xx, and what location is returned?

Show answer
Midpoints 16, 56, 38, 23; location 6

i=1,j=10i = 1, j = 10: m=5m = 5, a5=16a_5 = 16, 23>1623 > 16 → i=6i = 6. m=8m = 8, a8=56a_8 = 56, not greater → j=8j = 8. m=7m = 7, a7=38a_7 = 38 → j=7j = 7. m=6m = 6, a6=23a_6 = 23, 23>2323 > 23 is false → j=6j = 6. Now i=j=6i = j = 6, a6=23=xa_6 = 23 = x, location 6.

Question 11medium

Apply bubble sort to 5, 1, 4, 2, 8. Give the list after the first pass and after the second pass. How many comparisons does bubble sort make on a list of 5 elements?

Show answer
Pass 1: 1, 4, 2, 5, 8; pass 2: 1, 2, 4, 5, 8; 10 comparisons

Pass 1: (5,1) swap → 1,5,4,2,8; (5,4) swap → 1,4,5,2,8; (5,2) swap → 1,4,2,5,8; (5,8) stays. The largest element has settled at the end. Pass 2: (1,4) stays, (4,2) swap → 1,2,4,5,8; (4,5) stays. Number of comparisons (n−1)+(n−2)+⋯+1=n(n−1)2=10(n-1) + (n-2) + \dots + 1 = \tfrac{n(n-1)}{2} = 10.

Question 12hard

Using the greedy algorithm with coins of 25, 10, 5 and 1 cents, make change for 67 cents. Then show that with coins of 25, 10 and 1 cents only, the greedy algorithm does not give the fewest coins for 30 cents.

Show answer
67 = 25+25+10+5+1+1 (6 coins); for 30: greedy uses 6 coins, but 3 dimes suffice

Greedy: at each step the largest coin that fits. 67 → 25 (42) → 25 (17) → 10 (7) → 5 (2) → 1 → 1: 6 coins. For 30 without the 5-cent coin: greedy takes 25 and gives the remaining 5 in 1-cent coins: 1+5=61 + 5 = 6 coins. But 10+10+1010 + 10 + 10 is 3 coins. Greedy is not always optimal; a single counterexample shows this.

Verification: All sums were checked both by formula and by adding term by term, sequence terms by iteration, the Z+ → Z mapping over the window -1000..1000, binary search and bubble sort traces by running the code, and greedy change against dynamic programming, with node (verify/MAT2033/05.mjs).

Review card

Arithmetic: a_n = a + nd (constant difference) Geometric: a_n = a·r^n (constant ratio) Σ_{k=1}^n k = n(n+1)/2 Σ k² = n(n+1)(2n+1)/6 Σ k³ = [n(n+1)/2]² Σ_{j=0}^n ar^j = a(r^(n+1) − 1)/(r − 1), r ≠ 1 (starting from j=0: n+1 terms) Σ_{k=m}^n = Σ_{1}^n − Σ_{1}^{m−1} (subtract up to m−1!) Σ c = c·(number of terms) Σ(ak+b) = aΣk + Σb double sum: inner first ∏ product: ∏_{i=1}^4 2i = 384 |A|=|B| ⇔ a bijection exists. Countable: finite or in bijection with Z+ Countable: N, Z (0,1,−1,2,−2,...), Q+, Z×Z, finite bit strings Uncountable: R, (0,1) (Cantor diagonal), irrationals, P(Z+) Algorithm properties: input, output, definiteness, correctness, finiteness, effectiveness, generality Linear search worst case n comparisons; binary search needs a sorted list, ~log2 n steps Bubble sort: n(n−1)/2 comparisons, the largest goes to the end on each pass Greedy: best local choice at each step, may not be optimal (30 with 25,10,1)