Binary Question
Problem : Let B be the smallest set of binary strings such
that:
. 10 \in B, and
. if b \in B, then b00 \in B.
Let f: B -> N be the function that maps each binary string
b to the number that has b as a binary representation. Prove
by induc
Algorithm Correctness Examples
Example 1: recursive binary search
RecBinSearch(x,A,f,l)
if f = l then
if A[f] = x then
return f;
else
return 0;
end if
else
m := (f + l) div 2;
cfw_ integer division
if A[m] >= x then
return RecBinSearch(x,A,f,m);
else
ret
Termination:
- Standard method: find an expression E involving program variables such
that:
. E_k is a natural number for all k;
. for all k >= 0, E_cfw_k+1 < E_k if the loop is executed at least k+1
times (E_cfw_k+1 = E_k otherwise);
(where E_k is the va
Algorithm Complexity
- Standard measures for algorithms: space (memory) and time (speed). In
this course, we will consider only time.
- Want measure independent of one machine or implementation, and related
only to algorithm in an abstract way.
- One cand
Algorithm Complexity Tips and Examples
- Example 1: Prove 2n^2 + 6n + 2 is O(n^2).
Scratch work (NOT part of proof): for all n >= 1, 2n^2 <= 2n^2
and 6n <= 6n^2 and 2 <= 2n^2, so 2n^2 + 6n + 2 <= 2n^2 + 6n^2 + 2n^2 =
10n^2. [Alternatively, 2n^2 <= 2n^2 an
Algorithm Correctness
- We don't need just programs that usually work
they should always work correctly.
- We have to prove they produce "correct output" on "acceptable inputs", these
are expressed in terms of Postconditions and Pretconditions.
Example 1:
Closure properties of FSA-accepted languages
If we apply an operation on a language that is accepted by a FSA and
the result is also a language accepted by a FSA we say the operation
has closure property.
- Suppose L and L' are two languages accepted by t
Satisfiability and validity:
- The truth-value of a formula F under an interpretation I=(S,v) is
defined as follows:
. if F is an atomic formula P(t_1,.,t_m), where each t_i is a term
(either a variable or a constant), then F is true iff the relation
A^S
Constructing a NFSA
Example 1: Construct a NFSA that accepts the language of
(010+01)^*010(1)^*+1^*.
Answer:
Following the rules described in the notes (and lectures), we get
the following NFSA, with 13 states cfw_s,a,b,c,d,e,f,g,h,i,j,k,m, initial
state
CNF Form
We can also find a formula in CNF form which has the same truth table as
f(p,q,r) given above, and therefore is LEQV to it:
p q r | f(p,q,r)
-1 1 1 |
1
1 1 0 |
1
1 0 1 |
1
1 0 0 |
0
0 1 1 |
1
0 1 0 |
0
<= p \/ ~q \/ r
0 0 1 |
0
<= p \/ q \/ ~r
0
Advanced Finite State Automata
- Example: Come up with F.S.A. that recognizes L = cfw_ w in cfw_a,b* : w
contains an even number of a's . Use states that represent information
about string processed so far. In this case, only need to remember if
number of