Let's Learn Algorithm - Part 1 (Intro to Algorithm)

 


What is an algorithm and why should you care?

You may have heard this word “Algorithm” before, but do you know what it means? And why is it important to learn about algorithms? Let me explain.

What is an algorithm?

An algorithm is a set of instructions or rules that tells a computer or a person how to solve a problem or perform a task. For example, if you want to bake a cake, you need to follow a recipe, which is an algorithm. If you want to find the shortest route from your home to your school, you need to use a map, which is also an algorithm. If you want to play a video game, you need to follow the rules of the game, which are another example of an algorithm.

So, an Algorithm is all about solving a problem and when you encounter a problem, most of the time you will have some resources to solve it, and these resources are your input. For example, if you are trying to cook instant noodles for your friend, you must have the following:

  • A pack of instant noodles
  • A gas burner
  • A utensil for cooking
  • Water
  • A plate to serve the cooked noodles.

These are the input to solve the problem. If you do not have any one of these, you cannot cook the noodle and serve it to your friend.

Similarly, when you solve the problem, which means when you cook the noodle what will you do? You will serve the plate filled with cooked instant noodle to your friend. So the plate filled with cooked noodle is your Output.

Generalizing this idea, each and every algorithm will have an input and output. Even if you do not require any resource to solve the problem, that is even if the input is void, it must have an output, because solving a problem will always yield something.

Hence whenever we are to deal with an algorithm, always mention it’s input and output. Last but not the least, your problem must have a name so does your algorithm!

If your problem was “How to cook instant noodles for my friend?”, you can give the name of the algorithm, which actually tells you the stepwise solution for the problem, as “Cooking_Noodles”.

Hence, till now, we have the following:

Algorithm: Cooking_Noodles

Input: A pack of instant noodles, A gas burner, A utensil for cooking, Water, A plate to serve the cooked noodles
Output: Plate filled with cooked noodles

Now I can tell you how to cook instant noodles in different levels of granularity. Considering you as a human and knowing the fact that human have a natural intelligence, I might avoid great level of granularity and might jump few steps, but when you are making a computer to understand how to solve a problem, you must consider that computers do not have natural intelligence, so you must express the steps for solution of the problems in as details as possible.


Now considering you as a computer, let me tell you how to cook instant noodles.


Algorithm: Cooking_Noodles
Input: A pack of instant noodles, A gas burner, An utensil for cooking, Water, A plate to serve the cooked noodles
Output: Plate filled with cooked noodles
Step 1: Lit the gas burner.
Step 2: Pour water into the utensil.
Step 3: Unpack the instant noodles from the packet.
Step 4: Put the instant noodles into the water held in utensil.
Step 5: Boil the instant noodles in the water for 2 minutes.
Step 6: Transfer the cooked instant noodle into the plate.
Step 7: End

Here is the algorithm to cook the instant noodle and now you can solve your problem and can serve your friend the same.

Do you see how I did I write the algorithm? I ensured the following:
Algorithm has a name.
  • Algorithm has an input.
  • Algorithm has an output.
  • Algorithm has some finite number of steps ending with an end statement to mark the end of the algorithm.
  • Algorithm is correct.
  • Algorithm is generic to any brand of the instant noodles.
  • Algorithm is more or less efficient in solving your problem of cooking instant noodles.
Hence an algorithm must have the following properties.
  1. It must have name.
  2. It must have one or multiple input; even if you have no input, mention input as none.
  3. It must have an output.
  4. It must have finite number of steps; the finiteness has to be explicitly marked with an end statement.
  5. It must be correct.
  6. It must be adequately generic catering to a class of problems.
  7. It must be considerably efficient.
I wrote the above algorithm in plain and natural English, following this style, you can write any algorithm as well.

Suppose you are given with an array of integers arr of length n, which holds n number of unique integers.  Now consider that you are asked to find out the integer x having maximum value out of this array arr.
How would you write the algorithm to solve this problem? May be like following.
 
Algorithm: Find_Max
Input: An array arr having n number of unique integers
Output: An element of array x which has got the maximum value
Step 1: Let the first element of arr be x
Step 2: Check x and second element, if x is smaller than second element, call the second element as x
Step 3: Do the same with other elements too till you reach the end of the array
Step 4: Print x
Step 5: End
 
This algorithm can be expressed in more granular level like
 
Algorithm: Find_Max
Input: An array arr having n number of unique integers
Output: An element of array, designated as x which has got the maximum value, gets printed
Step 1: Let the first element of arr be x
Step 2: For each of the element z of arr starting from 2nd position to nth position, do Step 3
Step 3:  if x < z, designate z as x
Step 4: Print x
Step 5: End
 
You see you can write the same algorithm differently in different granularity level. Now let me write the same algorithm in a different style which is close to style of writing a program and a bit distant from natural English.
 

function find_max(array)

  max = array[0] // assume the first element is the maximum
  for i = 1 to array.length - 1 // loop through the rest of the elements
   if array[i] > max // compare each element with the current maximum
      max = array[i] // update the maximum if a larger element is found
  return max // return the maximum at the end

This style of writing an algorithm is known as pseudocode. This is more like programming, written as a function. But you must have noticed that it is not a program at all.

So a pseudocode is not a program, but it an algorithm expressed in a technical way in a function like format.

If you are to write an algorithm, you can follow any of the two styles, both will do. It depends on the nature of the problem and to whom you are reporting this algorithm. If you are reporting an algorithm to your grandmom who does not know anything of computer or programming, you can express your algorithmic idea in plain and natural english, or if you are sharing your idea to your techie friend, you can prefer writing a pseudocode.

Why should you learn about algorithms?

Learning about algorithms can help you develop your logical thinking and problem-solving skills. You can use algorithms to solve various problems in different domains, such as mathematics, science, engineering, art, music, etc. You can also use algorithms to create your own programs, games, apps, or websites. Algorithms are the building blocks of computer science and software development, which are very popular and rewarding fields in today’s world.

Learning about algorithms can also help you understand how computers and technology work. You can learn how algorithms are implemented in different programming languages, such as Python, Java, C++, etc. You can also learn how algorithms are used in different applications, such as search engines, social media, encryption, artificial intelligence, etc. You can also learn how algorithms are evaluated and compared based on their time and space complexity, which are measures of their efficiency.

Let's conclude today's writing here, I will be coming up with Let's Learn Algorithm - Part 2 soon. Till then let me give you something to exercise your brain

Task 1

Is the Cooking_Noodles algorithm that I wrote, correct? Do you find any anomaly?  If you find any anomaly, correct it

Task 2

Write an algorithm to print the character(s) which has/have appeared most of the time within a word. For example, if the word is “correct”, you have to print c and r, if the word is “bull”, you have to print l. Similarly if the word “world”, you have to print w, o, r and d.


Post a Comment

0 Comments