Tag Archives: everyday

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

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