What is an algorithm?
An Algorithm is a procedure to solve a particular problem in a finite number of steps for a finite-sized input.
The algorithms can be classified in various ways. They are:
- Implementation Method
- Design Method
- Other Classifications
In this article, the different algorithms in each classification method are discussed.
Classification by Implementation Method: There are primarily three main categories into which an algorithm can be named in this type of classification. They are:
- Recursion or Iteration: A recursive algorithm is an algorithm which calls itself again and again until a base condition is achieved whereas iterative algorithms use loops and/or data structures like stacks, queues to solve any problem. Every recursive solution can be implemented as an iterative solution and vice versa.
Example: The Tower of Hanoi is implemented in a recursive fashion while Stock Span problem is implemented iteratively.
- Exact or Approximate: Algorithms that are capable of finding an optimal solution for any problem are known as the exact algorithm. For all those problems, where it is not possible to find the most optimized solution, an approximation algorithm is used. Approximate algorithms are the type of algorithms that find the result as an average outcome of sub outcomes to a problem.
Example: For NP-Hard Problems, approximation algorithms are used. Sorting algorithms are the exact algorithms.
- Serial or Parallel or Distributed Algorithms: In serial algorithms, one instruction is executed at a time while parallel algorithms are those in which we divide the problem into subproblems and execute them on different processors. If parallel algorithms are distributed on different machines, then they are known as distributed algorithms.
Classification by Design Method: There are primarily three main categories into which an algorithm can be named in this type of classification. They are:
- Greedy Method: In the greedy method, at each step, a decision is made to choose the local optimum, without thinking about the future consequences.
Example: Fractional Knapsack, Activity Selection.
- Divide and Conquer: The Divide and Conquer strategy involves dividing the problem into sub-problem, recursively solving them, and then recombining them for the final answer.
Example: Merge sort, Quicksort.
- Dynamic Programming: The approach of Dynamic programming is similar to divide and conquer. The difference is that whenever we have recursive function calls with the same result, instead of calling them again we try to store the result in a data structure in the form of a table and retrieve the results from the table. Thus, the overall time complexity is reduced. “Dynamic” means we dynamically decide, whether to call a function or retrieve values from the table.
Example: 0-1 Knapsack, subset-sum problem.
- Linear Programming: In Linear Programming, there are inequalities in terms of inputs and maximizing or minimizing some linear functions of inputs.
Example: Maximum flow of Directed Graph
- Reduction(Transform and Conquer): In this method, we solve a difficult problem by transforming it into a known problem for which we have an optimal solution. Basically, the goal is to find a reducing algorithm whose complexity is not dominated by the resulting reduced algorithms.
Example: Selection algorithm for finding the median in a list involves first sorting the list and then finding out the middle element in the sorted list. These techniques are also called transform and conquer.
Other Classifications: Apart from classifying the algorithms into the above broad categories, the algorithm can be classified into other broad categories like:
- Randomized Algorithms: Algorithms that make random choices for faster solutions are known as randomized algorithms.
Example: Randomized Quicksort Algorithm.
- Classification by complexity: Algorithms that are classified on the basis of time taken to get a solution to any problem for input size. This analysis is known as time complexity analysis.
Example: Some algorithms take O(n), while some take exponential time.
- Classification by Research Area: In CS each field has its own problems and needs efficient algorithms.
Example: Sorting Algorithm, Searching Algorithm, Machine Learning etc.
- Branch and Bound Enumeration and Backtracking: These are mostly used in Artificial Intelligence.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Basic Algorithm Techniques Not Taught in Academics
- Design a Chess Game
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Algorithms | Recurrences | Set 1
- Interesting Examples of algorithms in everyday life
- Mutation Algorithms for Real-Valued Parameters (GA)
- Difference between Deterministic and Non-deterministic Algorithms
- How can one become good at Data structures and Algorithms easily?
- Introduction to Algorithms
- Algorithms Sample Questions | Recurrences | Set 2
- Algorithms Sample Questions | Set 3 | Time Order Analysis
- Data Structures and Algorithms Online Courses : Free and Paid
- Sorting Algorithms Visualization : Bubble Sort
- Why companies like Amazon, Microsoft, Google focuses on Data Structures and Algorithms : Answered
- SCAN (Elevator) Disk Scheduling Algorithms
- Sorting Algorithms Visualization | Selection Sort
- FCFS Disk Scheduling Algorithms
- The Role of Algorithms in Computing
- Live Classes for Data Structures and Algorithms: Interview Preparation Focused Course
- Why Data Structures and Algorithms Are Important to Learn?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : coderanuj243