Programming concepts in real life – Algorithm

This is the second post on the series of interesting thoughts of “programming concepts in real-life”. Today it occurred to me that one fundamental concept of programming – algorithm, is also heavily used in real life outside the IT world.

What is algorithm

Algorithm is a step-by-step procedure for calculations.

Algorithm used in programming

There are a lot of examples of algorithms in programming, the most basic ones include  breadth-first search (BFS) and  depth-first search (DFS).

Algorithm in real life

Algorithms are applied in our everyday life.

DFS/BFS (Depth-first-search/Breadth-first-search)

For example, for a freshman who just entered NUS, he may choose to study the area of his interest, such as marketing or psychology. He can do this in two ways, one is to really devote all the time and effort into this area and do nothing else for a very long time. So for marketing, he/she will be learning introductory modules on marketing, intermediate modules on marketing, advanced modules on marketing or even do a research on marketing. Only after he/she feels there is nothing else to do on marketing, then the person will consider other options. And surprisingly(or rather not surprisingly), this is exactly the concept of a particular algorithm called DFS. In DFS, you prioritize depth more than breadth, so when you have a chance to go wider or deeper, you will go deeper first.

And of course you can see how the opposite algorithm BFS works for real life. That is when a person takes all the introductory modules for different areas, however after all that still indecisive enough to make a decision for specialization. So he/she would take all the intermediate level modules for different areas and so on. This may work very badly for a freshman but the concept is similar to a liberal arts curriculum, where students are exposed to different disciplines for 1 or 2 years before they decide their major.

Amortized complexity optimization – CS3230 lecture on 13 Feb 2015

We use amortized complexity optimization in real life, for cleaning our rooms. Normally we do not clean our rooms every single day. We wait for the room to fall below a certain cleanliness standard or when there is an important visitor, then we start cleaning the room. Cleaning the room is an expensive operation which takes a lot of time. Hence, by doing it infrequently, only when absolutely necessary, we reduce the total amount of work needed to keep our room clean.

This idea is similar to amortized complexity optimization in algorithm analysis, where we optimize the average runtime to achieve better run time.

 

Other algorithms such as greedy algorithm and dynamic programming are also practised in real life(often without being realized).

 


More of this

The best Friday Hacks session so far

Today I attended the Friday Hacks(#68) by NUS Hackers as usual. I knew that the topic today is on NUSMods, a web app developed by a SoC senior – Beng, that is extremely popular and successful in NUS, and there will be a guest Jeff Moss who is a famous security expert. It turned out both talks were interesting and meaningful.

For the first part, Beng mainly talked about the history and design philosophy of the NUSMods. It was an inspiring story considering that he did this in year 2 and the web app went through a lot of changes before it finally become successful and popular among NUS students. In particular, the part on designing the app is interesting, he talked about “hackers who can design the app well enough is as rare as unicorn.” And I found this to be relevant. Sometimes the app may have the best functionality but the poor design may not attract the users or simply render the app unusable. Luckily, there are some books on design philosophies that are useful in designing apps. I figure that I should take a look at them when I have free time and before doing some serious web project. He also mentioned about the future of NUSMods which includes some APIs for new developers to use the data from NUSMods as well as using NUSMods’ existing popularity to transform it into a platform for displaying other apps relevant to NUS(taking the interpretation of NUSMods as NUS Modifications). These ideas may be relevant for me when I do my future projects.

The second part of the session was a short Q&A session for Jeff Moss and some of his colleagues. A lot of insightful questions ranging from personal security, NSA’s surveillance, issue of trust and prospect of computer security were asked and the reply given by the pros were very informative and insightful. We learnt about how strange phenomenons may be related to NSA’s surveillance programmes, the prospect of post Edward Snowden era for computer security. Some even drilled into the fundamental issue of the extent of trust we should give to others. A good point is that in IT industry, we have to ultimately give our trust to some strangers. It is not possible for us to build a functional computer, or in similar sense a full range of software. Hence, whatever we do, we need to trust the other people who developed the building blocks for the technology that we are using, be it the operating system or the processor.

Programming concepts in real life – Interface

I did not know about the concept of interface until I took the module CS2020 where we use Java to learn data structures and algorithms. In the first few lectures, I was introduced to the idea of interface. I found it extremely intuitive and useful.

What is interface

In simple terms, an interface in programming is set of functions, but only names and descriptions of the functions, not the actual codes. You can write a program to implement(support) these instructions in some ways. Later, you can tell other programs that your program implements this interface and supports these instructions. Then, other programs will be able to directly execute the instructions without knowing how they are implemented. Interfaces can be extended so that the extended interfaces specifies additional functions.

Interface used in programming

One term that people often hear in tech news is API, which just is Application programming interface. With APIs, we can use functions that other applications implements in our own applications. The concept of interface is also important for the communication between hardware and the software. There is a communication layer between hardware and software called instruction set architecture (ISA), which specifies the operations that the processor should support so that the compliers(software) can use them. Then the actual implementation of the functions is done by the producer of the processor.

Interface in everyday life

However, I realised that the concept of interface is not limited to the field of programming. It is actually ubiquitous in our everyday real life as well. Think about the banks, they are essentially different implementations of the same interface – bank, which specifies 2 basic operations for customers: deposit and withdraw. Different banks would then go on and implement these two functions in different ways, then add some more functions that customers can perform using banks. This may lead to the problem of different banks having different functions and they are not compatible with each other. For example, cheques implemented in bank A may not be the same format as bank B. Hence, we still the concept of interface and by extending the interface to support more functions, different banks can communicate with each other.

More examples of interface in everyday life

Other examples of interface? You can just list almost everything.

A car? Yes, it is an interface that specifies functions like move, board, exit and etc.

A TV? Yes, it is an interface that specifies functions like display channel, switch channels, switch on, switch off, adjust volume and etc.

A house? Yes, it is an interface that specifies functions like live in, demolish, get address and etc.

People may think that, “Oh, actually this concept of interface has always been there before the invention of computers. What is the big deal?” That is precisely the big deal that I want to point out. Programming helps people to analyse and formulate things systematically. Although the concept has been always around, it was not formally identified and given a proper name so that it can be studied. Different sectors and fields do have their own regulating bodies but the more general concepts similar to interface are not explicitly there(or not conclusive to encompass all sectors). With programming terminologies, we are able to identify patterns or practices that are common across different fields and sectors and give it a generic name. I believe there are other programming concepts(perhaps object) with this ability, I just need to learn more and find out more.


More of this

Linear Progression and Stages – Belief or fact

Linear progression is a good concept, it assumes that things go from stage 1 to stage 2, then to stage 3, and eventually reach the final stage. However, the fact that it is simple and nice does not justify its correctness.

In real life, many things do not go through linear progression, say the development of the country. One may argue that a country can only develop according to the sequence of stages in the development model, i.e. pre-industrial, industrial and post-industrial. However, this may not be true for some countries which jump from pre-industrial to post-industrial by some alternative ways. Also, common linear progression has an end point which is definite, but this can be a belief rather than a fact. For example, a U.S. citizen may think that capitalism is the end of the stages for ideology, whereas a PRC citizen may think communism is the real end. Clearly if the perceived end is different, the progression is not clear any more. A U.S. citizen may claim that U.S. is at the final stage of development, whereas a PRC citizen would reject that and say U.S. has yet to go through socialism to reach communism.

In fact, building on that idea of indefinite end, we will never know if the progression has come to an end. For example, current computer technology has evolved to a stage where ordinary people can get access to a computer easily. Is this the end of the progression for computer’s accessibility? I think not, one possible subsequent stage of development would be a “computer” planted inside each person. And even at that stage, we cannot conclude that we have come to the end of the progression.

Progression can also be in terms of humans’ development. A person goes through different stages as he/she grows up. It would be logical to think that everyone follows a linear progression of life, biologically, from young to old. However, the real maturity of each individual may develop in a non-linear progression. Some people grow up with care and love from parents hence slowly turn from immature to somewhat mature, then finally mature. Others may not have such privilege, they have to become mature immediately because they do not have such luxury of time to go through transition before facing the serious world.

Now if we take a step back and redefine linear progression as always going from early stages to later stages(like jumping from stage 1 to stage 3 and skipping stage 2, but not the other way round), would it be perfect to fit everything inside? It appears so. However, now the hard part would be to define the stages clearly so that they follow a total order. Progression like EQ or maturity are hard to define stages as there are just so many factors to consider.

Facebook survey. Randomly chosen or I was somehow selected?

Somehow Facebook just asked me to do a survey on my experience with news feed as well privacy settings(some questions are more like tests of my understanding).

Well I guess this might be just random and nothing special but who knows.

Maybe Facebook has got some interests in me since I am actually very active on Facebook now(for some unknown reasons I find it relaxing when posting things.)