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.
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:
- Split the sum: .
- If the lower bound is not 1: sum from 1 to the upper bound, and subtract the sum from 1 to (lower bound − 1).
- In a geometric sum, count the terms. If there are terms.
- Countable = I can list the elements as 1st, 2nd, 3rd, one by one.
- 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 or ) to a set. Its th term is .
| Type | Rule | Example |
|---|---|---|
| Arithmetic progression | (, ) | |
| Geometric progression | (, ) | |
| Recurrence relation | in terms of earlier terms | , |
| Fibonacci | , , |
Solving a recurrence relation means finding a closed formula that writes only in terms of . The simple method is iteration: write a few terms, see the pattern, verify with induction. For , , we get .
Order for finding a formula from terms: are the differences constant (arithmetic)? Are the ratios constant (geometric)? Compare with squares, cubes, , . Is each term twice the previous one plus one?
Summations
is the index of summation, the lower bound, the upper bound. The name of the index does not matter: .
Formulas to memorize
| Sum | Result | Example |
|---|---|---|
| : | ||
| : | ||
| : | ||
| , |
See once why the geometric formula is correct: . . Subtract: , that is . All the middle terms cancelled each other.
Shifting the index
Write with : , , the term is .
A faster way: .
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: . .
PITFALL · The number of terms in . From to there are terms, not 20. This difference costs marks in the sum of a constant term: .
Cardinality
For finite sets, size is the number of elements. To compare infinite sets you cannot count; instead you match them up. if and only if there is a bijection from to .
- Countable: finite, or of the same cardinality as (countably infinite). You can list its elements as without skipping any.
- Uncountable: not countable.
Stadium intuition: a stadium has infinitely many seats numbered . 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: . An infinite set can have the same size as its own proper subset; this is impossible for finite sets.
- : list in the order (the function in question 8).
- Positive rationals : group the by the sum . 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.
is uncountable: Cantor’s diagonal argument
Theorem: the interval is uncountable (so is too).
Idea of the proof (by contradiction): assume there is a list of all the numbers in . Look at the th decimal digit of the th number in the list. Build a new number: its th digit is 5 if the th digit of the th 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 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: comparisons.
Linear search and binary search
Linear search: look one by one from start to end. The list may be unsorted. In the worst case, comparison with 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: in the list 2, 5, 8, 12, 16, 23, 38, 56, 72, 91:
| ? | ||||
|---|---|---|---|---|
| 1 | 10 | 5 | 16 | yes, |
| 6 | 10 | 8 | 56 | yes, |
| 9 | 10 | 9 | 72 | no, |
| 9 | 9 | loop ends, , location 9 |
Because the interval halves in each round, about 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 .
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 idea | The truth |
|---|---|
| ; 10 is included | |
| In the exponent is 5 | In the formula it is ; there are 6 terms |
| Sums split, they are not multiplied | |
| Doing the outer sum first in a double sum | Inner sum first, with the outer index fixed |
| Infinite set = uncountable | , are infinite but countable |
| The even numbers are fewer than | The same cardinality: |
| Binary search works on an unsorted list | A sorted list is required |
| Bubble sort makes fewer comparisons on a sorted list | In this version always |
| Greedy is always the best | Counterexample: 30 cents with 25, 10, 1 |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Evaluate .
Show answer
Split the sum: . The sum of a constant = constant × number of terms.
Evaluate .
Show answer
Geometric sum, , , , so 6 terms: . The exponent is , because starts at zero.
Evaluate .
Show answer
The lower bound is not 1: . The sum you subtract goes up to 9, not 10. Check: 21 terms, average 20, .
Evaluate the double sum .
Show answer
First the inner sum (with fixed): . The values for are . Outer sum: .
Let and for . List and guess a closed formula.
Show answer
Iteration: , , , . All are one less than a power of 2: minus 1. Guess . (Proof by induction: .)
Find a simple formula for the th term () of each sequence: (a) 3, 7, 11, 15, ... (b) 2, 6, 18, 54, ...
Show answer
(a) The consecutive differences are a constant 4: arithmetic sequence, . (b) The consecutive ratios are a constant 3: geometric sequence, . Look at the differences first, then the ratios.
Evaluate .
Show answer
, . Total .
Let be if is even and if is odd. Find . What does this function show?
Show answer
, , , , . The list is : every integer appears exactly once. This is a bijection, so and is countable.
Which of these sets are countable? (a) the positive multiples of 7 (b) (c) the irrational numbers (d) the set of all finite bit strings.
Show answer
(a) is a bijection with . (b) List the pairs in order of the value ; each group is finite. (c) is uncountable and is countable; if the irrationals were countable too, then would be countable. Contradiction. (d) List by length: length 0, then 1, then 2 ... (there are of each length, a finite number).
Apply binary search (with loop, , "if then else ") to find in 2, 5, 8, 12, 16, 23, 38, 56, 72, 91. Which elements are compared with , and what location is returned?
Show answer
: , , → . , , not greater → . , → . , , is false → . Now , , location 6.
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: (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 .
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
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: coins. But 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).