Introduction to Algorithms

Tusamma Sal Sabil
5 min readMar 14, 2020

--

What is Algorithm? Algorithm Basics

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

It can be understood by taking an example of cooking a new recipe. To cook a new recipe, one reads the instructions and steps and execute them one by one, in the given sequence. The result thus obtained is the new dish cooked perfectly. Similarly, algorithms help to do a task in programming to get the expected output.

The Algorithm designed are language-independent, i.e. they are just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.

What are the Characteristics of an Algorithm?

As one would not follow any written instructions to cook the recipe, but only the standard one. Similarly, not all written instructions for programming is an algorithm. In order for some instructions to be an algorithm, it must have the following characteristics:

  • Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning.
  • Well Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs.
  • Well Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well.
  • Finiteness: The algorithm must be finite, i.e. it should not end up in an infinite loops or similar.
  • Feasible: The algorithm must be simple, generic and practical, such that it can be executed upon will the available resources. It must not contain some future technology, or anything.
  • Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be same, as expected.

How to Design an Algorithm?

In order to write an algorithm, following things are needed as a prerequisite:

  1. The problem that is to be solved by this algorithm.
  2. The constraints of the problem that must be considered while solving the problem.
  3. The input to be taken to solve the problem.
  4. The output to be expected when the problem the is solved.
  5. The solution to this problem, in the given constraints.

Is algorithm related only to the Computer Science and do we really need to know how to code to learn it?

No, algorithms are not only related to Computer Science but in our world, it is the computer which handles and processes a very large amount of data. Even talking for non-Computer Science domains, algorithms are just well-defined steps to solve a problem and developing an algorithm is the work of a brain, so no coding is required here. We develop algorithms to calculate the position of a robotic arm or to calculate the trajectory of fluid and these are not purely Computer Science domains. And these algorithms are pretty much successful in getting the result if the size of the problem is small.

So, why algorithms are studied as a part of Computer Science?

Most of our real-world problem doesn’t operate on tens or hundreds of data but on a much larger number like thousands and millions. So, implementing those steps on a datasets which is much larger is not a task of any human and we need to develop code to implement those steps in a computer. Even a computer is going to take a significant amount of time (like days, weeks or even more) if the algorithm we have developed is not good and then we need to optimize our algorithm. So, designing an efficient algorithm is very important for many practical problems.

Do I really need to learn algorithms?

Suppose, you are having lunch at your favorite restaurant and you liked the food very much and you want to cook it yourself but you have never cooked anything before. Even though you have never cooked anything but still you know how food is cooked, but is this knowledge is enough to cook the food you ate at the restaurant? Possibly, no. But what if you have a strong piece of knowledge in cooking and know how to make a food with a specific taste, texture, etc., can you create the recipe that matches to the food you had tasted? Probably, yes.

The point is if you have a prior knowledge of solving problems (cooking food in this example), then you can tackle a more difficult problem on your own (in this case, cooking the food served at the restaurant). And any course on Algorithms makes you face some problems first so that you are ready to solve further much more difficult problem on your own or to make an algorithm to solve a problem which is entirely new.

We can develop algorithms but is it necessary to optimize it with today’s computer speed?

Now, this is a real question, at least if you are a beginner, you think it is. Also, time is not the only thing we are concerned about, we also optimize our algorithm to take less space (memory), programmer’s effort, etc. in many cases. The one line answer for these questions would be — we are not provided with a computer with unlimited speed and space, therefore, we need to optimize our approach to solve a problem using a computer.

Even with the high-end computers available in the market, a badly written algorithm can take a significant amount of time like days or weeks or even more. However, an optimized algorithm can finish the same job in minutes or seconds with a much slower computer.

Just try running this code to print the first 10,000 prime numbers and notice that how long it takes to get all results.

def is_prime(x):
prime = True
for i in range(2,x):
if x%i == 0:
prime = False
return prime
if __name__ == '__main__':
count = 0;
number = 2;
while (count < 10000):
if (is_prime(number)):
print(number)
count = count+1
number = number+1

--

--