Algorithms: A theoretical insight

Algorithms: A theoretical insight

DSA is incomplete without algorithms. Let's look at a general overview of what algorithms are and why we need them.


What are Algorithms?

An algorithm is a finite set of instructions, executed in a specific order to solve a particular problem. We apply algorithms in our daily lives, knowingly or unknowingly. Like making a particular dish, or the process of washing clothes, they all have a defined start, a step-by-step procedure and an end. From a technical perspective, we can create algorithms to solve a problem in an effective way.

For example: to calculate the product of two numbers, we

  1. start by creating a function

  2. declare variables to store both numbers and the answer

  3. read values from the user

  4. save the product in a 3rd variable

  5. display the product

  6. end by closing the function

This can also be displayed in a quick and visually elegant way using flowcharts.

Characteristics of an Algorithm

The following are some basic characteristics of algorithms:

  • Input: An algorithm might have none or some inputs.

  • Output: An algorithm gives one or more than one output.

  • Unambiguity: Algorithms are clear and simple

  • Effectiveness: Algorithms solve space and time complexities very effectively

  • Well defined: Algorithms are straightforward set of instructions and thus, are well defined.

  • Finiteness- Algorithms terminate after a finite number of sets.

Types of Algorithms

  1. Divide and conquer algorithms – Divide the problem into smaller subproblems of the same type; solve those smaller problems; and then integrate those solutions to solve the original problem.

  2. Searching algorithms- Search through a data structure in a particular order to find a particular value.

  3. Sorting algorithms- Arrange a collection of data in a particular order. like for numbers it can be ascending or descending.

  4. Randomized algorithms – To solve the problem, utilise a random number at least once during the computation.

  5. Greedy algorithms – In order to find the optimal solution for the entire problem, find the optimal solution at the local level.

  6. Recursive algorithms – Repeat a certain set of instructions, each time changing the number of instances thus finding your way to the solution.

  7. Backtracking algorithms – explore all possible solutions by building step by step and undoing some steps if they lead to a dead end.

  8. Dynamic programming algorithms – a complex problem can be broken down into a number of smaller, simpler subproblems. Each of these subproblems can then be solved just once, with the solutions being stored for later use rather than being recalculated.