profile pic
⌘ '
raccourcis clavier

algorithm = takes one-or-more values, produces an outputs

Ex: contains

Given a list of LL and value vv, return vL.v \in L.

Input: LL is an array, vv is a value Output: true if vLv \in L, false otherwise

i, r := 0, false
while i neq |L| do
  if L[i] = v then
    r := true
    i := i + 1
  else
    i := i + 1
return r

invariants

Induction hypothesis that holds at beginning of iteration.

Deterministic

  1. Base case: inv holds before the loop
  2. Hypothesis: holds after j-th, j<m repetition of loop
  3. Step: assume inv holds when start m-th loop, proves it holds again at the end of m-th loop

Are we done?

  • Assuming invariant does it reach conclusion
  • Does it end?

Formal argument: prove a bound function

bound function ff on the state of the algorithm such that the output of ff

  • natural number (0, 1, 2, …)
  • strictly decreases after each iteration

f=Lif=|L| - i starts at L,L0|L|, |L| \geq 0

correctness.

  1. pre-condition: restriction require on input
  2. post-condition: should the output be
  3. prove the running the program turns pre-condition and post-condition

complexity.

assume V is not in the list Contains(N) = 5 + 7N

given V is in the list

contains is correct, runtime complexity is ContainsRuntime(|L|)=L\text{ContainsRuntime(|L|)}=|L| and memory complexity is ContainsMemory(|L|)=1\text{ContainsMemory(|L|)}=1

runtime.

graph comparison
graph comparison

models are simplification.

shows different growth rates.

Interested in scalability of algorithm For large enough inputs, contains will always be faster than altc because order of growth of CRuntime<AltCRuntime\text{CRuntime} < \text{AltCRuntime}

Growth
Growth

upper bounded (at-most)

f(n)=O(g(n))      n0 ,c>00f(n)cg(n)  nn0f(n) = \mathcal{O}(g(n)) \iff \space \exists \space n_{0}\ , c>0 \mid 0 \leq f(n) \leq c \cdot g(n) \space \forall \space n \geq n_{0}

lower bounded (at-least)

f(n)=Ω(g(n))      n0 ,c>00cg(n)f(n)  nn0f(n) = \Omega(g(n)) \iff \space \exists \space n_{0}\ , c>0 \mid 0 \leq c \cdot g(n) \leq f(n) \space \forall \space n \geq n_{0}

equivalent (strictly bounded)

f(n)=Θ(g(n))     n0,clb,cub0clbg(n)f(n)cubg(n)nn0f(n) = \Theta(g(n)) \iff \space \exists n_{0}, c_{lb}, c_{ub} \mid 0 \leq c_{lb} \cdot g(n) \leq f(n) \leq c_{ub} \cdot g(n) \forall n \geq n_{0}