
Wednesday, November 26, 2008
How to nail an assignment

Tuesday, November 25, 2008
The Koch Curve
set="F"
for i in range(5): set=set.replace("F","FLFRFLF")
set=set+"R"+set+"R"+set
turtle.down()
for move in set:
____if move is "F": turtle.forward(100.0/3**i)
____if move is "L": turtle.left(60)
____if move is "R": turtle.right(120)
input ()
Wednesday, November 19, 2008
DFSA
but "100", 110000", "000", "111" is not contained in L.
∂(q0, 1) = q0
∂(q1, 0) = q1
∂(q1, 1) = q2
∂(q2, 0) = q2
∂(q2, 1) = q2

Now what if we try and modify it and say we want it to accept any string containing the string "0101"
Problem 2: Design a DFSA that accepts a string that has the sequence "0101" somewhere in the string
So our state transition table will change and we have to introduce new states q3 and a final accepting state q4. So if we start at q0 and find a "1", its pretty useless and we remain in the same state q0. but if we find a "0" means that it is the start of the substring we are looking for. So we jump to state q1. Now from q1, if we find a "0" as the next character, we are back to square 1 so we jump back to q0. But if we find a 1, we jump to q2. Similarly, if we get a "0" while being in state q3, its useless since its not forming the substring we are looking for and we jump back to q0. But if we find a "1", then we jump to the final accepting state q4. Once we are in state q4, we 'dont care' about the incoming characters since we have already found our substring and we remain in state q4.
So our transition table becomes:
∂(q0, 1) = q0 ---------- ∂(q0, 0) = q1
∂(q1, 0) = q0 ---------- ∂(q1, 1) = q2
∂(q2, 0) = q3 ---------- ∂(q2, 1) = q0
∂(q3, 0) = q0 ---------- ∂(q3, 1) = q4
∂(q4, 0) = q4 ---------- ∂(q4, 1) = q4
And our state diagram would look like this:

I must thank Danny for showing us jFlap. Its a great tool to analyse state diagrams and find flaws.
Saturday, November 15, 2008
Problem Set 5
The first thing i noticed is that since L has a finite number of strings, there exists a Regular Expression (RE) that denotes L.
//This roof resembles a proof we did in linear algebra (MAT223) where we had to prove that a vector v = (v1,v2,v3,..,vm) belongs to a vector space.
Let L = {L1, L2, L3, ... ,Lm} over Ʃ
Any substring from L can be denoted as L_a where a should at least be 1 and at most be m. Given that strings are a concatenation of characters, we can define a string:
L_a = (L_a1, L_a2, L_a3, ... ,L_an), since L_a belongs to L and L_a belongs to Ʃ
Then L_a1 is a regex for Ʃ ==> L_a1L_a2 is also a regex;
Then L_a1L_a2...L_an is a regex for Ʃ
RE(L_a1L_a2...L_an) is a regex for Ʃ and given that L_a = (L_a1, L_a2, ... L_an), L_a defines the string.
Then RE(L_a1L_a2...L_an) is the String L_a.
All substrings in L have regular expressions RE_1, RE_2, RE_3, ... ,RE_n
RE_1 is a regex for Ʃ
(RE_1 + RE_2) is also a regex for Ʃ
.
. Concatenating the regexes
.
(RE_1 + RE_2 + RE_3 + ... + RE_n) is also a regex for Ʃ
The sum of all regexes is equivalent to the language L.
n
Ʃ RE _i <=> (RE_1 + RE_2 + RE_3 + ... + RE_n) <=> L
i=0
Then if L has a finite number of strings, there exists a regular expression RE that denotes L;
Friday, November 14, 2008
Problem set 4
def revString(s):
if len(s) <>
else: return revString(s[1:]+s[0])
(1) Define our P(n)
{
____P(n) = /forall s /in Strings+ { if len(s) == 1: revString(s)
____else if len(s) > 1: revString(s[1:]+s[0])
}
Lets assume that the program does not take empty strings (strings with length 0) and lets denot all strings with positive lengths but Strings+. i.e: /forall x /in Strings+, len(x) > 0.
Proof:
Base case:
len(s) = 1
Assume s /in Strings+
{
__Then s is a string with at least 1 element
__Then len(s) = 1 and 'if' condition is satisfied
__Then revString() executes line: 'if len(s) is less than 2: return s
__Then revString() is correct.
}
Then /forall s /in Strings+, len(s) < 2 =""> revString() is correct and our base case passes=> Post condition is true.
Inductive case:
Assume n /in N #Generic natural number
_Assume s /in Strings+
__Assume i=0_V^n P(i) (*) (Want to prove P(n+1))
___Assume len(s) >= 2
{
____Then 'if' condition is false and line 'if len(s) <>
____Then line 'else: return revString(s[1:] + s[0])' is executed.
____Then len(s[0])=1 && (len(s[0]) <= len(s[1:]).
____Since len(s[0]) = 1, from our base case we know that len(s) == 1 ==> return s.
____So return revString(s[1:]) + s[0] # *1 = s[0] part is then true.
____Then revString(s[1:]) is called.
____Case 1: len(s[1:]) == 1:
_____by base case, if len(s) == 1: then return s
_____Then s[1:] is returned and revString() is correct.
_____s[1:] + s[0] means string returned is reversed ==> Post condition is true.
____Case 2: len(s[1:]) >= 2:
_____Then line 'else: return revString(s[1:] + s[0])' is called.
_____# s[0] by *1 is true
_____Then by (*), revString() is correct.
_____==> Post condition is true
}
___Then revString() is correct # proof by cases
__Then P(n+1)
_Then i=0_V^n P(i) => P(n+1) # intro =>
Then /forall s /in Strings+, i=0_V^n P(i) => P(n+1) #intro
/forallThen /forall n /in N, /forall s /in Strings+, i=0_V^n P(i) => P(n+1) #intro /forall
Then Post condition holds and revString() is correct
Thursday, November 13, 2008
Test 2

Unfortunately, I had a lot of problems to look after (some related to school and some not) and I did not have enough time to study for any subject... :( As a result I did find test 2 difficult but i still gave it a shot from what I followed in lectures.
The first question was a bit confusing, I think I got the wrong idea... I stated unwinding G(n) and tried to prove that T(n) can be derived and exists in G(n). I used the same unwinding technique from problem set 3 but I dont know if that was the solution to it...
The second question was fine, we had done something similar in lecture and in CSC165 during summer. The third question was a bit tricky, i had trouble finding an appropriate invariant but I knew that we had to show that the loop terminates. We know that n keeps on getting smaller after each iteration and at some point, the condition would be false and the loop would terminate.
To summarize, i knew part of the solutions but i'm not sure whether they are correct... I will have to catch up on the forthcoming problem sets/assignemnts and tests.
State machines... we meet again
Professor Danny used a very interesting software called jKFLAP (? not sure) where he made the circuit simply by drag-and-drop. I was also amazed that it can even test the circuit.. Now, how I wished I knew back then that there was such a software...
Unfortunately because of some personal problems, I missed two lectures (the ones dealing with formal languages) but it was pretty easy to catch up on the slides. I found that the laws for the equivalent REs on the slides were similar to the laws whether a vector belongs to a vector space from linear algebra (MAT223).
For example, there has to be the zero element or empty string such that:
( R + Ø ) = R # Ø is the empty string.
Theres only one thing I'm not too sure about and that is the "Idempotence of Kleene start" where:
( R** = R* )
I missed that lecture, unfortunately, but i'm wondering whether there is a typo and if it should have been: Kleen star (thats what google says)
Wikipedia states that: http://en.wikipedia.org/wiki/Kleene_star
1) If V is a set of strings then V* is defined as the smallest superset of V that contains λ (the empty string) and is closed under the string concatenation operation. This set can also be described as the set of strings that can be made by concatenating zero or more strings from V.
2) If V is a set of symbols or characters then V* is the set of all strings over symbols in V, including the empty string.
So for example:
{'a', 'b', 'c'}* = {λ, "a", "b", "c", "aa", "ab", "ac", "ba", "bb", "bc", "ca", "cb", "cc", ...}
Applying the Kleene star would mean it contains the zero element λ and all the elements 'a', 'b', 'c'
And then it would looke like:

We can notice a pattern in the number of elements that form after every "step" as shown in the image below: