Introduction To Algorithms
Introduction To Algorithms
Example:
Consider a box where no one can see what’s happening inside, we say a
black box.
We give input to the box and it gives us the output we need but the procedure
that we might need to know behind the conversion of input to desired output is
an ALGORITHM.
An algorithm is independent of the language used. It tells the programmer the
logic used to solve the problem. So, it is a logical step-by-step procedure that
acts as a blueprint to programmers.
Types of Algorithms:
1. Brute Force Algorithm:
This is the most basic and simplest type of algorithm. A Brute Force Algorithm
is the straightforward approach to a problem i.e., the first approach that comes
to our mind on seeing the problem. More technically it is just like iterating
every possibility available to solve that problem.
2. Recursive Algorithm:
This type of algorithm is based on recursion. In recursion, a problem is solved
by breaking it into subproblems of the same type and calling own self again
and again until the problem is solved with the help of a base condition.
Some common problem that is solved using recursive algorithms are Factorial
of a Number, Fibonacci Series, Tower of Hanoi, DFS for Graph, etc.
In Divide and Conquer algorithms, the idea is to solve the problem in two
sections, the first section divides the problem into subproblems of the same
type. The second section is to solve the smaller problem independently and
then add the combined result to produce the final answer to the problem.
Some common problem that is solved using Divide and Conquers Algorithms
are Binary Search, Merge Sort, Quick Sort, Strassen’s Matrix Multiplication,
etc.
c) Greedy Algorithm:
In the Greedy Algorithm, the solution is built part by part. The decision to
choose the next part is done on the basis that it gives an immediate benefit. It
never considers the choices that had been taken previously.
Some common problems that can be solved through the Greedy Algorithm are
Dijkstra Shortest Path Algorithm, Prim’s Algorithm, Kruskal’s Algorithm,
Huffman Coding, etc.
d) Backtracking Algorithm:
3. Randomized Algorithm:
In the randomized algorithm, we use a random number.it helps to decide the
expected outcome. The decision to choose the random number so it gives the
immediate benefit
Some common problems that can be solved through the Randomized
Algorithm are Quicksort: In Quicksort we use the random number for
selecting the pivot.
4. Sorting Algorithm:
The sorting algorithm is used to sort data in maybe ascending or descending
order. Its also used for arranging data in an efficient and useful manner.
Some common problems that can be solved through the sorting Algorithm are
Bubble sort, insertion sort, merge sort, selection sort, and quick sort are
examples of the Sorting algorithm.
5. Searching Algorithm:
The searching algorithm is the algorithm that is used for searching the specific
key in particular sorted or unsorted data. Some common problems that can be
solved through the Searching Algorithm are Binary search or linear search is
one example of a Searching algorithm.
6. Hashing Algorithm:
Hashing algorithms work the same as the Searching algorithm but they
contain an index with a key ID i.e a key-value pair. In hashing, we assign a
key to specific data.
Some common problems can be solved through the Hashing Algorithm in
password verification.
Consider two kids, Aman and Rohan, solving the Rubik’s Cube. Aman knows
how to solve it in a definite number of steps. On the other hand, Rohan knows
that he will do it but is not aware of the procedure. Aman solves the cube
within 2 minutes whereas Rohan is still stuck and by the end of the day, he
somehow managed to solve it (might have cheated as the procedure is
necessary).
So the time required to solve with a procedure/algorithm is much more
effective than that without any procedure. Hence the need for an algorithm is a
must.
Creating an Algorithm:
Since the algorithm is language-independent, we write the steps to
demonstrate the logic behind the solution to be used for solving a problem.
But before writing an algorithm, keep the following points in mind:
● The algorithm should be clear and unambiguous.
● There should be 0 or more well-defined inputs in an algorithm.
● An algorithm must produce one or more well-defined outputs that are
equivalent to the desired output. After a specific number of steps,
algorithms must ground to a halt.
● Algorithms must stop or end after a finite number of steps.
● In an algorithm, step-by-step instructions should be supplied, and
they should be independent of any computer code.
Step 6: Check how to give output, Here we need to print the output. So write
print c
Step 7: End
int 4
float 4
double 8
char 1
short int 2
long int 4
Note: The above table is based on common memory configurations and may
vary depending on the specific implementation and architecture of the system
being used.
Backtracking O(2^n)
Please note that the space-complexity of some algorithms may vary based on
their implementation and the data structure used.
cases in complexities:
There are two commonly studied cases of complexity in algorithms:
1.Best case complexity: The best-case scenario for an algorithm is the
scenario in which the algorithm performs the minimum amount of work (e.g.
takes the shortest amount of time, uses the least amount of memory, etc.).
2.Worst case complexity: The worst-case scenario for an algorithm is the
scenario in which the algorithm performs the maximum amount of work (e.g.
takes the longest amount of time, uses the most amount of memory, etc.).
In analyzing the complexity of an algorithm, it is often more informative to
study the worst-case scenario, as this gives a guaranteed upper bound on the
performance of the algorithm. Best-case scenario analysis is sometimes
performed, but is generally less important as it provides a lower bound that is
often trivial to achieve.
Advantages of Algorithms
● Easy to understand: Since it is a stepwise representation of a
solution to a given problem, it is easy to understand.
● Language Independent: It is not dependent on any programming
language, so it can easily be understood by anyone.
● Debug / Error Finding: Every step is independent / in a flow so it
will be easy to spot and fix the error.
● Sub-Problems: It is written in a flow so now the programmer can
divide the tasks which makes them easier to code.
Disadvantages of Algorithms
● Creating efficient algorithms is time-consuming and requires good
logical skills.
● Nasty to show branching and looping in algorithms.