Sequences

See disclaimer about the slides.
See learning objectives and sources.

Sequence (definition)

An ordered collection (possibly infinite) of elements: $s_1, s_2, \ldots$.
Ex. powers of 2: $1,2,4,8, \ldots$.
Notation: general $\{s_n\}_{n=1}^{\infty}$, example $\{1/2^n\}_{n=0}^{\infty}$ (starts from 0).
Attributes: increasing, decreasing, finite, subsequence.

Sequence (example: summation)

Notation: $\sum\limits_{i=m}^n a_i = a_m + a_{m+1} + \cdots + a_n$.
Example: $\sum\limits_{i=3}^5 (2i+1) = 7+9+11 = 27$.
Problem: find close form for the summation,
A function of $n$ expressed with elementary operations (pocket calculator).

Some theorems

$\sum\limits_{i=0}^n i = n(n+1)/2$. Proved by induction later.
$\sum\limits_{i=0}^n a^i = \displaystyle {{a^{n+1}-1} \over {a-1}}$. Proved by induction later.
There are a lot of other similar theorems.

Exercises

Find close form of $\sum\limits_{i=0}^n (2i+1)$.
Solution: $(n+1)^2$, computed during the lecture.
Test: $1+3+5+7=4^2$.
Method: reduce to theorems by separating sums and factorizing products.

Find close form of $\sum\limits_{i=0}^n (2^i+1)$.
Solution: $2^{n+1}+n$, computed during the lecture.
Test: $2+3+5+9=19$.

Check your solution with an on-line solver.

Strings (definition and examples)

Finite sequences of ''characters'' with special notation.
Ex. $aabbb$ (juxtaposition, product-like), $a^2b^3$ (compact, power-like), $\lambda$ (empty string, why!?).
Let $X$ be an alphabet. Closures $X^*$ and $X^+$.
Operations: length $|a^2b^3|=5$, concatenation $\alpha=a^2$, $\beta=b^3$, $\alpha\beta=a^2b^3$.

Languages (definition)

Language: any set of strings over some alphabet.
Examples: the ''C'' programming language, the palindromes over $\{a,b,c\}$.
Operations: $L$ and $M$ are languages.
union: $L \cup M = \{ x \mid x \in L \lor x \in M\}$.
product: $LM = \{ lm \mid l \in L \land m \in M\}$.
closure: $L^* = L^0 \cup L^1 \cup \ldots$.
reference.

Languages (examples)

Let $L=\{\lambda,a\}$ and $M=\{a,aa\}$.
During the lecture compute: $L \cup M$, $LM$, $L^*$.
Solutions $L \cup M = \{\lambda,a,aa\}$, $LM = \{a,a^2,a^3\}$, $L^* = \{a^n \mid n \in {\mathbb N} \}$.

Let $L=\{1,a\}$, $M=\{\lambda,cc\}$ and $K=LM^2L$.
During the lecture tell which strings are/are not in $K$: 1a, 11, cc, 1c^41, ac^8a.
Solutions: yes, yes, no, yes, no.

Inductive definition of a sequence

A sequence of elements is either the singular value nil (also called empty, null, zip, 0, empty sequence, ...) or it is a pair $(x,y)$ (also called cons), where $x$ is a element and $y$ is a sequence.
Notation: if $A$ is an alphabet, $A$ also denotes the language of all the strings $a$ consisting of the letter $a$ of $A$.
How do you implemenent sequences in your favored programming language?

Problem

Code recursive operations computing the length and the concatenation of sequences.
Develop the code during the lecture (use previous inductive definition).

Induction

The principle

0. Have a statement $P$ parameterized by a natural $n$, i.e., $P(n)$.
1. Base: prove $P(0)$.
2. Induction: prove if $P(k)$, then $P(k+1)$.
Conclude $P(n)$ for every $n$.

Example

Prove by induction on $n$ that $\sum_{i=0}^n i = n(n+1)/2$.
Base, $n=0$: verify that $\sum_{i=0}^0 i = 0(0+1)/2$.
Induction: Assume that $\sum_{i=0}^k i = k(k+1)/2$ for $k \geqslant 0$ and prove that $\sum_{i=0}^{k+1} i = (k+1)(k+1+1)/2$.
$\sum_{i=0}^{k+1} i = (\sum_{i=0}^k i) + k+1 = k(k+1)/2 +k+1 = (k+1)(k/2+1) = (k+1)(k+2)/2.$

Non-inductive proof: assume $n$ odd. Sum is $(0+n)+(1+(n-1))+(2+(n-2))+\ldots$
There are $(n+1)/2$ addends all equal to $n$. Similar for $n$ even, add with 1.

Variations of induction

1. Base case starts higher than 0.
Ex. $n^2 \geq 2n$ must start with 2.

2. Induction step assumes all previous values (strong). if $P(0), \ldots P(k)$, then $P(k+1)$.
Ex. Every amount of postage that is at least 12 cents can be made from 4-cent and 5-cent stamps.

Inductive definition of a set

An infinite set $S$ can be finitely defined with the following points:
1. Base: define some elements in $S$.
2. Induction: define some rules that from elements in $S$ produce new elements in $S$.
3. Convention: agree that no other elements beside those of 1 and 2 are in $S$.

Examples of inductive definitions

Inductive definition of $\mathbb E$, the even naturals:
Base: $0 \in \mathbb E$.
Induction: if $x \in \mathbb E$ then $x+2 \in \mathbb E$.

Inductive definition of the odd integer, $\mathbb O$:
Base: $1 \in \mathbb O$.
Induction: if $x \in \mathbb O$ then $x+2 \in \mathbb O$.

Inductive definition of the palindromes $P$ over some alphabet $A$:
Base: $\lambda \in P$ and $x \in P$ for every $x \in A$.
Induction: if $p \in P$ and $x \in A$, then $xpx \in P$.

Inductive definition of $K$, the natural pairs $(x,y)$ with $x \leq y$.
Base: $(0,0) \in K$.
Induction: if $(x,y) \in K$, then $(x,y+1) \in K$ and $(x+1,y+1) \in K$.

Recurrences

Recurrence relation (definition)

Recursive function $r$ over the integers, typically presented by equations:
      $r_0 = b_0$,
      $r_{n+1} = a_n r_n + b_n$.
Problem: find close form for $r_n$.

Recurrence relation (example)

Naive reverse.
      $r_0 = 1$,
      $r_{n+1} = r_n + n$.
Close form is $r_n=1+(n-1)n/2$.
Spreadsheet.

Computation

Write equations for a few cases (up from 0 or down from $n$).
$r_0 = 1$.
$r_1 = r_0+0$.
$r_2 = r_1+1$.
$ \strut \ldots$
$r_n = r_{n-1}+n-1$.
Add all the values on each side and simplify common addends.
$r_n = 1+(0+1+2+\ldots n-1)$.
Make an educated guess for the dots and solve the summation.
$1+\sum_{i=0}^{n-1} i = 1+(n-1)n/2$.
The guess can be validated by a proof by induction.
You can "test" your conclusion for the summations.
You can find the solution on-line with this solver.

Recurrence relation (example)

$r_0 = 3$,
$r_n = 2r_{n-1} + 3$, for $n>0$.
Solve the recurrence during the lecture.

Example (divide and conquer)

This example computes the complexity (running time) of an advanced sorting algorithm, merge sort.
For simplicity, the input size is a power of 2.
$r_1 = 1$,
$r_{2n} = 2 r_n + 2n$, for $n > 0$.
Close form is $r_{2^k}=(1+k)2^k$.
Find the close form during the lecture.