algorithm = takes one-or-more values, produces an outputs
Ex: contains
Given a list of and value , return
Input: is an array, is a value Output: true if , false otherwise
invariants
Induction hypothesis that holds at beginning of iteration.
- Base case:
inv
holds before the loop - Hypothesis: holds after
j-th, j<m
repetition of loop - Step: assume
inv
holds when startm-th
loop, proves it holds again at the end ofm-th
loop
Are we done?
- Assuming invariant → does it reach conclusion
- Does it end?
Formal argument: prove a bound function
bound function on the state of the algorithm such that the output of
- natural number (0, 1, 2, …)
- strictly decreases after each iteration
starts at
correctness.
- pre-condition: restriction require on input
- post-condition: should the output be
- 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 and memory complexity is
runtime.
models are simplification.
shows different growth rates.
Interested in scalability of algorithm For large enough inputs,
contains
will always be faster thanaltc
because order of growth of
upper bounded (at-most)
lower bounded (at-least)
equivalent (strictly bounded)