This week, I am supposed to talk about recursion. Since I have already did an entry on the basics of that particular technique of coding, this week I shall discuss about a question that is frequently asked in computer science - Are there algorithms that can only be solved recursively?
Well, it turns out that the answer is yes and no.
No, theoretically, an advanced programmer may be able to make use of a Last-in-first-out (LIFO) stack that keeps a running list of the remaining tasks to handle the whole recursive process in a while loop which continues to push and pop tasks until the list is empty. This method is the most common among all the other workarounds, and is recognized by making the observation that recursion in programming is really just a special case of iteration.
On the other hand, in mathematical terms, yes, there are indeed certain problems that require recursion to arrive at an answer. A simple example is the Newton-Raphson method (learned in mat137). Another famous example is the Ackermann function. Of course, as mentioned earlier, in coding, one can always manage a stack to emulate the recursive calls on the function itself.
Wednesday, 26 February 2014
Thursday, 13 February 2014
Week 6 SLOG
This week we finally learned about trees. So why do we need to learn trees and other recursively defined data structures? Well, there are actually quite a few reasons why people went through all this trouble to come up with the idea. Here I've compiled a short list of the benefits of using trees and other recursive data structures:
- Simpler and more readable code.
- No need to preserve state on each iteration.
- More often than not more memory-efficient.
- Easier to search for items, since tree traversals can be easily coded.
Thursday, 6 February 2014
Week 5 Slog
This week I am going to talk a little bit about inheritance and composition in the context of object oriented programming (OOP).
Inheritance, in the context of OOP, refers to the method of basing a class (called a subclass) on another class (called a superclass). Python, in particular, allows the developer to inherit from more than one superclass, with a pretty convoluted reference hierarchy. Composition, on the other hand, refers to the method of embedding the class to be based on as a instance attribute of the new class. These 2 methods sport a very distinct syntax across all programming languages and are based on two very different programming philosophies.
So, the big question is - inheritance or composition? Well, the answer is, it depends. The first thing to consider is the programming language in question. Some programming languages offer one and some the other, while others offer clearer syntax for one than the other. But when it comes to high level, widely used programming languages like all the C based languages and a few others, it helps to have a simple guideline to follow when a decision has to be made.
Here are a few general strengths and weaknesses of inheritance and composition that can be helpful in making the choice:
Inheritance, in the context of OOP, refers to the method of basing a class (called a subclass) on another class (called a superclass). Python, in particular, allows the developer to inherit from more than one superclass, with a pretty convoluted reference hierarchy. Composition, on the other hand, refers to the method of embedding the class to be based on as a instance attribute of the new class. These 2 methods sport a very distinct syntax across all programming languages and are based on two very different programming philosophies.
So, the big question is - inheritance or composition? Well, the answer is, it depends. The first thing to consider is the programming language in question. Some programming languages offer one and some the other, while others offer clearer syntax for one than the other. But when it comes to high level, widely used programming languages like all the C based languages and a few others, it helps to have a simple guideline to follow when a decision has to be made.
Here are a few general strengths and weaknesses of inheritance and composition that can be helpful in making the choice:
- It is easier to change the interface of a back-end class (composition) than a superclass (inheritance).
- It is easier to change the interface of a front-end class (composition) than a subclass (inheritance).
- Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object.
- It is easier to add new subclasses (inheritance) than it is to add new front-end classes (composition).
- The delegation approach of composition will often have a performance cost as compared to inheritance's single invocation
Subscribe to:
Comments (Atom)