Algorithms

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

Requirements + example

Sequence of executable steps. Finite execution. Deterministic (usually). Input. Output.
Code or Pseudo-code (more forgiving).
Ex. average: Input $a$ and $b$ numbers. $c = (a+b) / 2$. Output $c$.
Constructs: assignment, conditional, repetition, input/output.
More constructs: call and return.

Recursion (definition)

Recursive algorithm is a procedure that calls itself.
Can be directly or indirectly.

Recursion (example)

Compute $a^n$, where $n$ is a natural.
Procedure: $f(a,n) = \mbox{if } n==0 \mbox{ then } 1 \mbox{ else } a*f(a,n-1)$
Also written as:
$\quad a^0 = 1$,
$\quad a^{n+1}=a*a^n$.
Other examples: factorial, Fibonacci, list concatenation and length, mergesort, quicksort.
Show during lecture the computation of $a^4$.

Problem

Recursion simplifies translating some problems into programs.
Compute all possible coin changes for a given amount: code.

Complexity

Running time: (misleading name), a function that given the size of the input returns the number of times some step is executed.
Choosing the "size" and the "step" takes some understanding.
Examples: Sorting: number of elements, number of comparisons.
Discuss during lecture puzzle to find lighter coin with pan balance.
Cases: Worst vs Average, e.g., search element in list.

Big Oh (definition)

A function $f(n)$ is Big Oh of $g(n)$ iff there exists a positive constants $c$ and $m$ such that $|f(n)| \leq c|g(n)|$, for $n \geq m$.
Typically written $f(n)=O(g(n))$.
$O(g(n))$ is the set of function bounded above by $g(n)$.
Lerma does not introduce $m$ explicitly, uses "almost all".
Wikipedia considers all non-negative, no absolute value, easier.

Big Oh (examples)

Prove or disprove during the lecture the following: $n = O(2n)$, $2n+3 = O(n)$, and (false) $n^2 = O(n)$ .

Big Omega

$f(n)=\Omega(g(n))$: similar definition but bounded below, $c|g(n)| \leq |f(n)|$.

Big Theta

$f(n)=\Theta(g(n))$: similar definition but bounded both above and below, two constants, $c_1|g(n)| \leq |f(n)| \leq c_2|g(n)| $.

Common complexities

Complexity Algorithm(s)
$O(log(n))$ binary search
$O(n)$ linear search
$O(n~log(n))~~~~~~$ efficient sort
$O(n^2)$ naive sort
$O(n^3)$ matrix mult., shortest path
$O(a^n)$ traveling salesman

Euclidean algorithm

Division theorem

$\forall a \in \mathbb {Z}, b \in \mathbb {Z^+} ~ ( \exists q, r \in {\mathbb Z} ~ (0 \leq r < b \land a=bq+r ))$.
$q$ (quotient) and $r$ (remainder) are unique.

Divisibility, prime

If $a,b \in \mathbb {Z}, b \ne 0$, The $b|a$ iff $\exists q ~(a=bq)$.
Divides, divisor, multiple.
$p > 1$ is prime if the divisors are $1$ and $p$ itself.
$\forall n>1 ~(\exists p ~(p|n))$, $p$ prime.
There are infinitely many primes.
If $p$ is prime and $p|ab$, then $p|a$ or $p|b$.

Fundamental Theorem of Arithmetic

Also called unique factorization.
$\forall n>1 ~(n=p_1^{s_1}p_2^{s_2}\cdots p_k^{s_k})$ uniquely.
Ex.: $24=2^3 \cdot 3^1$.

Greatest common divisor

Semantics straight from the name.
Euclid algorithm (recursive!):
$gcd(a , 0)=a$
$gcd (a , b) = gcd (b , a \,\mathrm {mod}\, b)$
Ex. find $gcd(189, 33)$:
          a    b     a mod b
        ------------------
        189 = 33 ∙ 5 + 24
         33 = 24 ∙ 1 +  9
         24 =  9 ∙ 2 +  6
          9 =  6 ∙ 1 +  3
          6 =  3 ∙ 2 +  0
          3 =  0 ... (we are done)
Important property: $ax+by=c$ has solutions in $x$ and $y$, for all $a$, $b$ and $c$ iff $gcd(a,b)|c$.
Algorithm.

Encryption

Modular arithmetic

Operations ($+$, $-$ and $*$) are done modulo some $k$.
Map letters to letters.
Caesar code, simply "two letters down"
        plain:   A  B  C  D  E  F  G  H  I  J  ...  Y  Z
        cipher:  C  D  E  F  G  H  I  J  K  L  ...  A  B
Ex.: HELLO becomes JGNNQ.
Affine code: $E(x) = ax+b \,\mathrm {mod}\, 26$, for some $a$ and $b$.
Must choose $a$ and $b$ such that $E$ is a bijection.

Theorem (bijection)

Let $n > 1$ and $f : \mathbb N_n \to \mathbb N_n$ defined by $f(x) = (ax + b) \,\mathrm {mod}\, n$.
Then: $f$ is bijective iff $gcd(a, n) = 1$.
Ex.: $f(x)=(4x + 1)\,\mathrm {mod}\, 5$ is bijective.

Theorem (decryption)

Given $f$ above, let $D(x) = (kx + c) \,\mathrm {mod}\, n$,
where $f(c) = 0$ and $ak + nm = 1$ for some $k$ and $m$.
Then $\forall x ~ (D(E(x))=E(D(x))=x)$.
Ex.: Find the inverse of $f$.

Theorem (fixpoint)

Let $n > 1$ and $f : \mathbb N_n \to \mathbb N_n$ defined by $f(x) = (ax + b) \,\mathrm {mod}\, n$.
Then: $f$ has no fix points iff $gcd(a–1,n)$ does not divide $b$.
Ex.: Find the fixpoints of $f(x)=3x+2\, \mathrm {mod}\, 10$.

Ideas

How difficult is it to break the above encryption?
Decrypt ''CHEXCJJYUE WHREKZWTEEB'', encryption program.
Any idea to make this method stronger?

Topological Sort

Definition

A linear ordering of a poset. If $a \prec b$ in the poset, $a$ is before $b$ in the topological sort.

hasse_divides_2.png

Good: $1,2,4,5,10,\ldots$
Bad:   $1,2,4,10,5,\ldots$

Algorithm

– For every element $n$ of the poset:
$\quad$ • $p_n$ is the number of immediate predecessors of $n$,
$\quad$ • $S_n$ is the set of immediate successors of $n$.
– While there are elements:
$\quad$ • output and remove some $n$ such that $p_n=0$;
$\quad$ • decrement $p_k$ for every $k \in S_n$;

Applications: make a cake, assemble a compound object.