Wednesday, February 26, 2014

Recursion: When and Why?

Recursion is a strong technique of programming where a function calls itself to finish a task. Recursion requires two important parts: the base case and the recursive case. The base case is the one that prevents recursion from going on infinitely. On the other hand, the recursive case is the recursive technique where the function calls itself to solve a problem multiple times until the base case is reached. 

When should recursion be used and when should it not be used? In programming, sometimes there are problems where the large problem at hand can be broken down into a collection of the same problem, but at a smaller scale. For example, the Towers of Hanoi for 3 pegs is a problem where a stack of items must be moved from the source to the destination. This must all be done while following the rule that no bigger piece can ever go on top of a smaller piece. As a result, there exists a sequence of moves that move each item to an intermediate location and then later moving it to the destination. These same moves are repeatedly done over and over again for each item of the stack of items. Hence, in the big picture, the task of moving the large stack of items is just the processing of moving each item piece by piece. This is the recursive technique used in the towers of Hanoi. For problems similar to the towers of Hanoi, the number of times the main recursive technique is used is usually unknown to the coder. Thus, by using recursion the coder can make the program apply this recursive technique as much times as required until the base case is met, which stops the program from executing further. Thus, recursion should be used when the number of iterations required is unknown to the coder.


Another reason why a coder would use recursion is for more elegant code, which makes it easier to read for the coder and the user. This is because the alternative to using recursion is using loops to iterate, which requires more code to achieve the same process as recursion in some complicated scenarios. Also, another thing to take note is that sometimes this results in much messier code. From my understanding of recursion, I believe that recursion makes problems easier for cases such as nested lists, trees, linked lists and any format that has a smaller version of itself within itself. I can conclude that mastering this strong technique will simplify future code and make recursive problems easier to understand in the future.

Sunday, February 16, 2014

Binary Trees, Pre-order, In-order, and Post-order Traversals

Pre-order, in-order, and post-order traversal is three ways that computer scientists travel through binary trees that hold different data structures. Depending on the order chosen, the final output may be different. Firstly, binary trees can be thought of as trees where each node is either a root, or a leaf. Roots are the nodes that have children and leafs are the nodes that do not have children. These roots may lead to another tree, or simply a leaf. From a computer science perspective, this can be modeled by recursion by breaking a big tree into a collection of smaller tree and leaves. The question that is left to be answered is "which order do we follow in the tree?"

Pre-order traversal is the method where we start from the root of the tree, then move on to the left sub-tree if available and finally move on to the right sub-tree if available. Note if the sub-trees have other sub-trees within the tree, this method is applied again.

In-order traversal is another method of traveling through a binary tree, but is slightly different from pre-order traversal. Instead of going from root to left sub-tree to right sub-tree, in-order traversal starts from left sub-tree, then move to the root, and finally move to the right sub-tree if available. Note the method is applied recursively if more sub-trees exist within other subtrees.

Post-order traversal is the last way to travel through a binary tree and the method goes as follows: starts from left sub-tree, then move on the root of the tree, and lastly move on to the right sub-tree. Note the recursive techniques applies to this method as well.

These three traversal methods can be coded through recursion and understanding these methods is key before any coding can be done. I myself looked through the tree diagrams and traced each method on paper a couple of times before I could say i was okay with the recursive techniques.When I first stumbled into these three methods in class, I was startled because traversals was completely new to me and I was still quite new to recursion. However, I believe the problem lies in me not being prepared for the lectures due to not reading the lecture notes ahead of time. Thus, that is the main problem at hand that I must fix. Concentration is key and slacking off only leads to falling behind and is a path I would not like to walk in.

Saturday, February 8, 2014

Scope of Names and Variables

This week introduced a familiar topic to me known as the scope of names and variables. The immediate difference that I found between the scope in python and java was that scope in python is separated by indentation and white space. While in java the scope of the variables can be understood through what brackets the variables are within. In my understanding of scope, scope is the boundaries of where the variable and names can be accessed. This is a very important concept to understand because in a large program where multiple variables have the same name due to a limited number of ways to describe the same object, the coder will want to specify where each variable should and can be accessed by the program. As explained in the lectures, scope has multiple levels going from local to non-local and to global. In python, I learned that scope is determined by searching from the most inner scope and working outwards toward the global scope. For example, let us say there is the following program.
x = 5
def num_squared(x):
    x = 4
    return x**2

>>> num_squared(5)
16

As the output shows, the x that is considered first is the one inside the function definition and not the x that is equal to 5 which is in the global scope in this case. If the x that we wanted was not the x in the function header, or the x in the body of the function, another approach would have to be used to force x to refer to the x in the global scope. This is the part where the lecture created some confusion for me because we were analyzing scope where a program forced certain names and variables to be visible at different scopes. I wasn't able to keep track of what was happening all at once, but after listening to some discussion from other students in the class, some of this confusion was reduced. I believe the main reason why the concept was confusing to me was because I didn't jot down my thoughts to keep track of what each part of the scope program was doing. As a result, I missed many important things that was happening and led to my wrong conclusion and false understanding.

For class inheritance, my understanding of the concept was pretty much on the spot. The exercise in the lecture where we predicted which functions was used by class B helped practice my understanding of how inheritance worked and as well in a way helped improve my understanding of scope. This is because both concepts start from the inner and then work towards the outer. In the case of inheritance, a method is first searched for in the child class and then the parent class if and only if the method does not exist in the child class. Overall, I believe this week was a very successful one.
 

Sunday, February 2, 2014

Reflection of the Week

This week's lectures focused mainly on recursion which is an important concept to understand in order to be able to simplify difficult problems into smaller easier ones. At first glance at recursion, I had some difficulty in understanding the process because of a few minor reasons: not enough time to trace out the code completely, and not enough practice of the concept. For example, in the turtle problem I traced the first and second level of recursion incorrectly due to not understanding how the loops worked with recursion. In order to understand the process a bit more, I have chosen to go back to the first level of recursion and retracing it with pencil again. However, this time I will be tracing the turtle problem and other recursion problems at my own pace of learning and giving myself a longer time period to absorb the concept between each step. I believe this practice will help better my understanding of recursion if even slightly. I truly want to understand recursion because I value the virtue of being lazy in the first year of computer science. Recursion leads to this laziness by simplifying a program that may need 100 lines into a shorter one that only needs 20 through recursive calls. This saves time, effort, and code for the coder. My dream of designing games may cut down from an impossible goal to a reachable goal through recursion.  Mastering recursion will be my goal for week 4 and beyond for CSC148.