Algorithms
Table of contents
What is an Algorithm?
An algorithm is a series of steps used to complete a task. A task can be anything like going to school from home, cooking your favorite dish, watering plants or finding what you're looking for in the grocery store etc.
EXAMPLE: Searching in a grocery store
In computer science, an algorithm is nothing but a set of steps for a computer program to accomplish a task.
Algorithms don't depend on language; they must have a finite state and well-defined input and output.
What makes a good Algorithm?
Correctness
Efficiency
Types Of Algorithms
Brute Force Algorithms
Recursive Algorithm
Backtracking Algorithm
Searching Algorithm
Sorting Algorithm
Hashing Algorithm
Divide and Conquer Algorithm
Greedy Algorithm
Brute Force Algorithms
This is an algorithm that is simple to use and understand, and it provides a direct solution to a problem. This has less efficiency, but this algorithm helps to give a solution to a problem. We use this algorithm in our daily activities, even though we can use optimized algorithms. The brute-force method is good for simple and small problems. It is not limited to specific domain problems. For example, exploring all possible paths to find the shortest path to college.
Recursive Algorithm
It is an algorithm that divides the problem into subparts and implements the solution by calling itself recursively. Here we need to have a termination condition to stop the recursive calls; otherwise, the program gets stuck in the infinite loop. Every time the function is called, it creates a copy of the main program, but with a simpler version of the original program. These calls are stacked in the stack memory. If the call of the copy of the original function completes, it pops the call from the stack, and this continues until the stack is empty, at which point it returns the output. If the recursive calls of the function don't stop, then an error stack overflow occurs. A recursive algorithm is used to perform Tower of Hanoi, in-order, pre-order, and post-order tree transversals, DFS of graphs, and other operations easily.
Example: sum of n numbers
source code:
The recursion can be direct or indirect
Direct recursion
Indirect Recursion
Recursion programs require greater space requirements and also have high time complexity, although they have fewer lines of code, and we need to focus on understanding how inner calls are made, which is difficult to understand in complex problems.
Backtracking Algorithm
Backtracking is a technique for finding all possible solutions by exploring all possible solutions and discarding those that do not lead to the desired state. In general, the backtracking algorithm constructs the solution by searching all potential solutions. We keep constructing the solution based on the criteria using this technique. When a solution doesn't work, we go back to the point of failure and work on the next one, repeating this process until the problem is solved or all options have been considered.
Types of Backtracking Algorithms
Decision Problem (used to find feasible solutions)
Optimization Problem (used to find best solutions)
Enumeration Problem (used to find feasible solutions)
Searching Algorithms
This algorithm is used to search for or retrieve an element from a data structure. And search algorithms are of two types:
Linear Search
Binary Search
Linear Search
In this search algorithm, we go one element after another, and if an element is found, the search is terminated. Else, we continue the search until we reach the last element of the data structure.
Time Complexity: O(n) where n is the number of elements in the data structure
Searching for 1 using Linear Search
Binary Search
This type of search algorithm is only used on sorted data structures. In this search, we are considering a middle-indexed element and checking whether it is equal to the target element, or is it less than or greater than the target element, and moving the start and end indexes in such a way that we land on the target element.
Example: Searching for 7 in an array using Binary Search
Time Complexity: O(log(n))
Sorting Algorithm
The sorting algorithm is used to arrange the elements of a data structure in ascending or descending order by comparing the values of the elements.
Buble sort
Insertion sorting
Selection Sort
Cyclic Sort
Merge Sort
Quick sort
Hashing Algorithm
The hash algorithm is used for the fast retrieval, insertion and deletion of data points. A hash algorithm takes constant time for retrieval, insertion and deletion, unlike a binary search tree where it takes O(log N) time complexity.
$$h(x)=x \% s$$
where x is a hash key and s is the size of the table and x%s is a hash function. let us consider elements 33,64,98,100,88 placed in an array of size 10. Using the hash function value as an index, we can place elements in the array. However, there are some cases where these values collide, such as in the given example, where 98 and 88 are pointing at the index and collide with each other
To overcome these collisions, there are some techniques to increase the randomness so that collisions are reduced, and this randomness depends on the hash function.
Separate chaining
Where we maintain a linked list for each array index.
Open Addressing
Linear Probing
$$h1(x)=(x+i)\% s$$
Quadratic probing
$$h2(x)=(x+i**2)\%s$$
Double hashing
$$h3(x)=(h1(x)+h2(x))\%s$$
Divide and Conquer Algorithm
This algorithm divides a large or complex problem into smaller sub-problem, which are then solved, and the sub-solutions are then combined to yield the solution. It follows the following steps
Divide
The problem is divided into sub-problems
Solve
Here we find the solution for sub-problems
Combine
The solutions of subproblems are combined to give the solution for the original problem
Algorithms like QuickSort, MergeSort, Closet Pair of Points, and Strassen's Algorithms Follow Divide and conquer Algorithm.
Greedy Algorithm
Greedy algorithms solve the problem by selecting the next best option available at that instance. It doesn't concern the over-all solution, whether the solution is optimised or not; instead, it only chooses the option that is best at that moment.Therefore, not all solutions can be used with it.