Monday, 16 February 2015

SLOG #4 STACKS, QUEUES, AND TREES

Although the prescribed topic of this week's slog is a summary of Object-Oriented Programming concepts, I will mainly talk about the material I learnt from last week and this week's labs since I have mentioned OOP in a previous slog.

I found what I have learnt in the past two labs particularly helpful, partly because Diane only mentioned the abstract data types (ADTs): stacks, queues, and trees briefly in class. In the labs, we were required to read through the codes of these data structures and write methods or functions for them, which gave me a more solid understanding on how they work and how we can make use of them. We also learnt some applications of these ADTs. For example, we can use a queue to represent a queue in a restaurant, or we can use a tree to trace the order of a sequence of binary operations.

The materials in lab #4 is not very difficult, but it becomes quite tricky when I tried to implement recursive functions on trees in lab #5. It is easy to think of a base case(when the problem can be solved in one single step). In code, this is usually the "if" part. However, I got lost when I started trying to write something in the "else" part. Fortunately, the lab handout gave me some hints; it asked me look at a larger tree. Assuming that the function works on its subtrees, what should I do to make the function also work on this larger tree? This is very similar to the induction step in an induction. In induction, you prove the base case, assume the statement holds for all numbers smaller than a number k, and prove the statement also holds for k, using the above facts. In induction, we know our function works for the base case, we assume the function works for any level of input smaller than a certain number k(in this is it is the height of a tree), and we construct the function such that it will work for level of input k. Thinking in this way, the problems in the lab were greatly simplified; all I needed to do was to think of base case, and design function so that it works for trees with height two. Thanks to recursion, the subtrees collapsed to the leaves that are directly connected to the root!

Reading week is coming, so is assignment 2. Hopefully I will be able to have a draft for A2 by the end of this week.

Saturday, 7 February 2015

SLOG #3 MERGE SORT, AND THE MIDTERM

Last week we started our first lesson on recursion, an extremely powerful tool for computer scientist. So far, I only have a brief and vague idea of what recursion is, and I feel writing recursive function quite tricky(the last question in last week's lab). I attribute this feeling to a lack of exposure to this subject, so I will reserve it to the slog later in the semester that is prescribed to have this topic. Hopefully by that time I will feel more comfortable writing recursive functions.

The other topic that we covered last week was merge sort. Diane did not show us the code for this algorithm in the class. Instead she brought eight playing cards to show us how merge sort works. She first divided the cards into two groups, each group having four sorted cards. Then she put the sorted cards in two piles, held the cards at the top of each pile of cards, compared them, and put the card with smaller face value down on another area of the table. By doing so continuously, she completed the sort within four comparisons, suggesting that this way of "dividing and conquer" might be efficient in sorting lists. Then we had this vivid presentation of merge sort: Diane handed out the eight cards to a student in the first row. That student divided the cards into two piles, giving each pile to a person in the second row. The students in the second row and third row did the same thing until each of the eight students of the fourth row had one card. This is the "divide" part. The "conquer" part starts with students in the fourth row passing back the cards to the person in the third row, and that person needed to sort the cards. This is easy because these students only need to sort two cards. Now we have four sorted groups of cards. Apply this process again, we have two sorted groups of cards. Apply this process for the last time, we have a sorted list!

It is notable that the whole process consists of three "divide", and in each of the three sorting parts, we made four comparisons. This might be much faster than insertion sort if the list is not originally well sorted, in which case the run time may be as many as 64 times!

We also had a midterm this Wednesday. I have to say that the test was fair. Nothing really unexpected: two question about the design and implementation of class, the second of which tests on subclasses, and one that asks you to trace a recursive function. I don't find anything that is particularly tricky in these questions. Hopefully I did well in the midterm.