Wednesday 5 December 2012

The "Problem-Solving Episode"

For my problem-solving episode I have decided to tackle the "StringSteps" problem located at this website: https://wwwcgi.cdf.toronto.edu/~heap/cgi-bin/Solvent/wiki.pl?Problem_Solving_Home_Page/StringSteps


EDIT: FOR SOME REASON the proof was showing up as a giant block of white and highlighting the text was the only way I could fix it. Sorry for the inconvenience.

The question says:

Binary strings are sequences of the two digits (or bits) 0 and 1. The two binary strings of length 1 (each having 1 character) are 0 and 1. These two strings can be arranged in a list so that each string on the list differs from its neighbours by exactly one bit:

  • 0
  • 1
I can arrange all four binary strings of length 2 in a list so that each string on the list differs from its immediate neighbours by exactly one bit, even if I insist that the first and last strings on the list are immediate neighbours:
  • 00
  • 01
  • 11
  • 10
Can you arrange all 2^n binary strings of length n in a list so that each string on the list differs from its immediate neighbours by exactly one bit, when you treat the first and last strings on the list as immediate neighbours?

----------------------------------------------------------

1. Understanding the problem

    The question is basically asking if you can arrange a list of all the binary strings of some length such that the binary string before or after the string on the list differs only by 1 bit, AND if the first and last binary strings on the list differ by 1 bit. 

2. Devising a plan

    To prove that this sorted list of binary strings is possible, I will use Mathematical Induction to prove it. First, I will show that this list possible for a base case where the binary string length is 1. Then assuming that it holds for any length binary string n, I will show it is true in the n + 1 case. There have been many cases in class where we had to work with binary strings, but the problems were quite different than the one presented now. 
    Another way to think of the question is can you constantly change a binary string of length n by only 1 bit, and eventually create a list of all the binary strings of length n, and then rearrange them so that the first and last strings also differ by 1 bit?

3. Carrying out the plan

    PROOF:

        Define:
         P(n) = All 2^n binary strings of length n in a list are arranged so that each string on the list differs from its immediate neighbours by exactly one bit, and the first and last strings on the list can be treated as immediate neighbours.

       We must show that for all n belonging to the natural numbers, P(n) is true.

        Base case: n = 1:

            Then 2^1 = 2, so there are 2 possible binary strings of length 1. These strings are 0 and 1. Clearly these strings only differ by 1 bit, and they can be arranged such that each string's immediate neighbour differs by 1 bit. Since they are the only 2 strings in the list, then the first and last strings on the list can also be treated as immediate neighbours, and thus differ by 1 bit. Therefore P(1) holds.

     Assume P(n) holds for all natural numbers n (IH). Then we must show that P(n + 1) holds.

             We define L to be the list that contains 2^n binary strings of length n, and also assume the list satisfies P(n). Then, we define two other lists named L_0 and L_1.

             The list L_0 will contain all of the binary strings in L with 0 prepended to each binary string in the list.  Likewise, L_1 will contain all of the binary strings in L with 1 prepended to each binary string in the list. Thus, each list will contain  2^n binary strings of length n + 1. Each list will be arranged such that each string's immediate neighbours differ by 1 bit, and the first and last binary string in the list can be treated as immediate neighbours (this is true since prepending the same bit to all the strings that already differ by 1 bit will not  change the number of bits it differs by). Then, if we take L_1 and flip it such that the first binary string is now the last binary string (and vice versa), we can add it to the bottom of L_0. This will give us a list of 2^n + 2^n = 2*2^n = 2^(n + 1) strings of length n + 1 such that each string's immediate neighbour differs by 1 bit. Now, the first and last binary string can be treated as immediate neighbours since we flipped L_1 before appending it to L_0. 

             The flip allows the first and last binary string to be treated as immediate neighbours since the first binary string in L_0 is the first binary string in L prepended with 0, and the first binary string in L_1 is the first binary string in L prepended with 1; this means those binary strings only differ by 1 bit since the rest of the string is the same. The same applies to the last binary strings in L_0 and L_1; the binary strings only differ by 1 bit since it was just the same string prepended with a different bit. By flipping L_1, the first binary string (which only differed by 1 bit to the first binary string in L_0), became the final string and therefore satisfies the condition in P(n) for the first and last binary strings to be considered immediate neighbours. Also, since the last binary string in L_1 (which only differed by 1 bit to the last binary string in L_0) became the first string, it satisfies the condition that the immediate neighbours only differ by 1 bit inside the list. Since these two "border cases" of each list come together and satisfy P(n), and since the rest of the list also satisfies P(n) (by IH), then therefore P(n + 1) holds.

    Therefore, since P(n + 1) holds, P(n) holds.

Therefore, for all n belonging to the natural numbers, P(n) holds.    QED

4. Looking back

The proof seems solid, since we take care of all the possible cases of n via Mathematical Induction. This result can most likely be not used for other problems. For instance, if we were to use alphabetical strings, then we can apply the same idea where each string will differ by one letter. However, there are many more types of letters than types of bits, then the first and last strings would also differ by many more letters. Whether there are more ways for this proof to be applied to other problems is something that would need to be looked into more.

Tuesday 27 November 2012

The (late) weekly post: Nov 27, 2012

It appears that I'm a week or two behind on this weekly post I had been attempting to keep, so hopefully this makes up for it....

We got back our second term tests and I was happy to see that I got perfect on it! I was a bit nervous due to the fact that I hadn't studied pre and post conditions and correctness of a program but I managed to pull through. The second question I was more confident about, since it was just using the Master Theorem to find the time complexity of the given program, and rewriting the code so that the time complexity changed.

Unfortunately my second assignment mark wasn't nearly as great as my term test. I had gotten the first question mostly wrong and since the assignment was only two questions... well you can see where this is going. I found the first question quite difficult but I had felt I made some progress on it. Looking at the marking it seems that my unwinding led me to the wrong closed form; I had written down that H(n) was just the closed form of the Fibonacci sequence, but it was actually that + 1. I probably should have double checked my work but I left it to the last minute to do so I had no time. The second question was about proving the time complexity of some recursive function, which wasn't too hard since for one part of the question we were just told to emulate the course notes proof with some minor changes to fit the question. To prove that the function was in the big Theta, I just showed that the function was in Big-O and Big-Omega. In the end I had chosen the wrong constant (it was also off by + 1) but the proof was fine.

In lectures we learned about languages and alphabets and how different languages are combined with others to form a whole bunch of strings. At first I didn't know what was going on but after the tutorial it seemed pretty easy, it just gets a bit confusing with being bombarded with new terms and notations on how to define a language and such. In the other lecture we learned about regular expressions and how languages were defined with those. The professor then proceeded to showing us finite-state automata which basically shows us whether or not a string is inside a specific language. These automata seemed fairly simple until he showed us the non-deterministic one, where the same character can lead to different states, which I found pretty weird.

Our third assignment has been posted but unfortunately I've been caught up in a presentation I'm supposed to be doing tomorrow, hopefully after I'll be able to work on that, my AST221 problem set and also my CSC207 assignment which I haven't started either... once again it's going to be another hectic few days for me as I try to blast through all of my homework... I also have to finish the mathematical proof post for this blog also, which is due next week. Hopefully I'll be able to make it through these last two weeks of classes before exams, wish me luck!


Monday 12 November 2012

The weekly post: Nov 12, 2012

Last week in 236 was the dreaded test week... I had kept telling myself to study early on so that I wouldn't be overcome with it at the last minute, but as usual I put off studying to do other things. By the night before I barely studied anything that was potentially going to be on the test, so I just redid some of the tutorial questions. I only managed to get through two tutorials and a bit of the textbook before I went to sleep. A funny thing is that I had convinced myself that pre and post condition weren't going to be on the test... but I was wrong. When I got the test the first question was about pre and post condition, and the second was about using the Master Theorem (which isn't too hard), so I finished the second question within five or ten minutes and dedicated the rest of my time to figuring out the first question.

For the first question, it was about a recursive function that takes two parameters x and y, and returns the value of x^y. All I really knew about the first question was that the precondition has to lead to termination and the postcondition, so I used complete induction to show that the precondition led to termination when y was 0, 1, and an even or odd number. I personally didn't think the first question was going to go well but by the time I finished my proof it seemed pretty solid. Obviously since I didn't actually study the pre and post condition I may have left out some details but overall it wasn't so bad.

After that I left lecture early just because 236 was my last test for the whole period of midterms I was having and I felt like I needed to take a break. I'm pretty excited to see how my test turned out, since I went into with the preparing for the worst but came out of it pretty confident in myself.

Sunday 4 November 2012

The weekly post: Nov 4, 2012

It seems that I'm starting to slack off a bit in this class, which is definitely not good... but I have been caught up with my other classes terribly; I have a calculus test tomorrow and I've also been slacking off in that course, along with that I need to research my topic for a presentation in my astronomy class in a few weeks that I haven't started yet. I also have a group assignment in CSC207 and the first part of that assignment we didn't do too well on. In other words my procrastination is showing its true colours.

For this week's lecture, we analysed the recursive binary search function and talked about its precondition and postcondition. We were also showed how the precondition leads to the postcondition and the code's eventual termination using induction. Afterwards we looked at merge-sort once again and analysed it's pre- and postcondition. I would be lying if I said I wasn't confused, but that usually happens when I go to lecture anyways; I think it's due to a mix of the professor going a bit fast and my own preconceived thoughts of the difficulty of the material - I thought that since I already did pre- and post- condition in CSC165 that this wouldn't be hard to grasp, but this course goes more in-depth than what I had previously learned.

In my tutorial, I sat up in the front this time and I must say, there was a huge difference from sitting at the farther region of the room. At the back I could barely see what was on the board and the TA's voice did not reach down there, but at the front the chalk was finally visible and the TA's voice was somewhat clearer (my TA isn't very loud in the first place). I'm a bit uneasy with the quiz however, since I had gotten a suspicious answer... but we'll just have to wait for the results!

Sunday 28 October 2012

The weekly post: Oct 28, 2012

This week we went more in-depth with the time complexity and saw more examples of different algorithms. We talked about divide-and-conquer recurrences, or something like that, and I got confused as to what exactly was going on. The lectures had given us a proof sketch of these types of recurrences, so from what I was reading it says we should split up the problem into some amount of subproblems and solve that. We then recombine (which I am not sure what that means) and from there it should be proved. I'm guessing that the concept is that you have to prove a smaller case of the problem and extend it to the problem given - it sounds kind of like induction.

We then took a look at multiplying bits recursively and analyzed the time complexity of it, and in the end we ended up with big theta(n^2). However, our professor then proceeded with showing us Gauss' trick to reduce the number of multiplications for recombining the divided factors. Although it was only reduced by one, the time complexity became n^(1.5). Initially I thought "it was only reduced by 0.5?", but when seeing the graph of n^2 and n^(1.5) I was shocked at how big of a difference it made.

After that our professor showed us the time complexity of finding the two closest point pairs for a whole set of randomly scattered points. Once again he divided the problem into two, but after that I got very confused. There was something about taking the difference between two points and using that difference to narrow down the search, but I vaguely understood that part of the lecture, I will definitely need to review it.

The tutorial for this week was pretty much the same; I couldn't really hear what my TA was saying and his writing was very faint on the board.

Also, my second assignment for this course is due this coming Friday but I have yet to start it. I've been caught up with a midterm in AST221 this coming Monday that I need to study for, and it has been difficult for me to grasp some concepts, but I've been holding out. I also received my mark for the first assignment in CSC236 and I had gotten perfect, which was something I wasn't expecting! At the time I found it difficult and doubted my answers but now I feel like I have a good understanding on the flavours of induction.

Sunday 21 October 2012

The weekly post: Oct 21, 2012

There's not much to talk about this week, we had a lecture on time complexity again and it's great that it's touching up on what we had done in CSC165. A lot was taken off my shoulders after the test and it felt nice to be able to start reviewing some past material. I had done the tutorial handout for that week and I was able to get most of the questions. The only one I had trouble on was analyzing a recursive function and writing a closed form of it, but the TA went over it and I eventually got it.

We also got our test marks back! I only had one mark taken off, which I felt was an odd reason to remove a mark but I'll just have to trust the TA's judgement... I'm still waiting for the mark for my first assignment, which I think I did worse on, mainly because I still hadn't grasped the various forms of induction yet at that time. In the end up to this point my time here in 236 came across some obstacles but has generally been a positive experience. Hopefully I'll be able to say that in the future.

Saturday 13 October 2012

The weekly post: Oct 13, 2012

As a sidenote, I will hopefully try to post on this blog every once a week so as to keep up with the slog guidelines.

So this week we had a midterm test on Thursday, and I personally didn't feel like I was ready for it. I had a calculus quiz earlier that day too, so I had to find a way to manage my study time, and split my homework accordingly. Unfortunately I went into procrastinator mode and spent several hours doing God-knows-what, as long as it took away potential study time. Also I focused a lot more of my study time on calculus than for 236. Sooner or later it was around 4 in the morning, so I decided it was time to let my subconscious go to work.

Finally at school I took care of my calculus quiz, and then decided to spend my 4-hour break studying for the upcoming test, but I ended sleeping for about half of my break; the other half was spent reviewing my homework, tutorial handouts and lecture examples. When test time dawned I had a little more confidence in myself but of course there was some anxiety...

When I finished the test I felt like it went very well. I didn't leave a question blank or anything, and it seemed like my proofs were all pretty strong. I would say that the last problem was the only one that made me stop and think for longer than any of the other questions. However, I guess if you really do understand the basics of Mathematical Induction it shouldn't be too hard to apply it to some proof that seems difficult at first glance.

I guess in the end, I was really happy about how my midterm test went. Hopefully I'll be able to keep my confidence and expectations for this course, but I do think that if I want to keep this up, I really need to start fixing my study habits, and should start getting more sleep.