Unit Eleven
Unit Eleven
Unit Eleven
Algorithms
Introduction
• Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
created independent of underlying languages, i.e. an algorithm can be
implemented in more than one programming language.
• From the data structure point of view, following are some important categories of
algorithms −
• Search − Algorithm to search an item in a data structure.
• Sort − Algorithm to sort items in a certain order.
• Insert − Algorithm to insert item in a data structure.
• Update − Algorithm to update an existing item in a data structure.
• Delete − Algorithm to delete an existing item from a data structure.
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following
characteristics −
• Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases),
and their inputs/outputs should be clear and must lead to only one meaning.
• Input − An algorithm should have 0 or more well-defined inputs.
• Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
Analysis of an Algorithm
For a standard algorithm to be good, it must be efficient. Hence the efficiency of an algorithm
must be checked and maintained. It can be in two stages:
1. Priori Analysis:
• “Priori” means “before”. Hence Priori analysis means checking the algorithm before its
implementation. In this, the algorithm is checked when it is written in the form of theoretical
steps. This Efficiency of an algorithm is measured by assuming that all other factors, for
example, processor speed, are constant and have no effect on the implementation. This is
done usually by the algorithm designer. This analysis is independent of the type of hardware
and language of the compiler. It gives the approximate answers for the complexity of the
program.
2. Posterior Analysis:
• “Posterior” means “after”. Hence Posterior analysis means checking the algorithm after its
implementation. In this, the algorithm is checked by implementing it in any programming
language and executing it. This analysis helps to get the actual and real analysis report about
correctness(for every possible input/s if it shows/returns correct output or not), space
required, time consumed, etc. That is, it is dependent on the language of the compiler and the
type of hardware used.
Complexity of an Algorithm
• An algorithm is defined as complex based on the amount of Space and Time it consumes.
Hence the Complexity of an algorithm refers to the measure of the time that it will need to
execute and get the expected output, and the Space it will need to store all the data (input,
temporary data, and output). Hence these two factors define the efficiency of an algorithm.
The two factors of Algorithm Complexity are:
• Time Factor: Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
• Space Factor: Space is measured by counting the maximum memory space required by the
algorithm to run/execute.
Therefore the complexity of an algorithm can be divided into two types:
1. Time Complexity
2. Space Complexity
1. Time Complexity
The time complexity of an algorithm refers to the amount of time required by the algorithm to
execute and get the result. This can be for normal operations, conditional if-else statements,
loop statements, etc.
● A heuristic algorithm is one that is designed to solve a problem in a faster and more
efficient fashion than traditional methods by sacrificing optimality, accuracy, precision, or
completeness for speed.
● Heuristic algorithms often times used to solve NP-complete problems, a class of decision
problems.
● In these problems, there is no known efficient way to find a solution quickly and accurately
although solutions can be verified when given.
● Heuristics can produce a solution individually or be used to provide a good baseline and
are supplemented with optimization algorithms.
● Heuristic algorithms are most often employed when approximate solutions are sufficient
and exact solutions are necessarily computationally expensive.
Algorithm Design
● A given problem can be solved in various different approaches and some approaches
deliver much more efficient results than others.
● Algorithm analysis is a technique used to measure the effectiveness and performance of the
algorithms.
● It helps to determine the quality of an algorithm based on several parameters such as user-
friendliness, maintainability, security, space usage and usage of other resources.
● Selecting a proper algorithm design is critical for the performance & success of the
application.
● Some of the algorithm designs are:
1. Brute-Force Or Exhaustive Search
2. Divide And Conquer
3. Greedy Algorithms
4. Dynamic Programming
5. Branch And Bound Algorithm
6. Randomized Algorithm
7. Backtracking
Brute-Force or Exhaustive Search
● Brute Force algorithm is a typical problem-solving technique where the possible solution
for a problem is uncovered by checking each answer one by one, by determining whether
the result satisfies the statement of a problem or not.
● It is used when the problem size is limited since its various tries are equally proportional to
a solution's candidates.
● In Computer Science, Brute Force is a trial and error methodology, which is used by
attackers to break into any website's data or systems.
● They try to steal information by generating every possible combination of the password-
protected information.
● Brute Force relies on the computational power of the machine to try every possible
combination to achieve the target.
Example: Finding optimal Visit sequence of factories for a businessman.
Greeding Algorithm
● As the name implies, this is a simple approach which tries to find the best solution at every
step.
● Thus, it aims to find the local optimal solution at every step so as to find the global optimal
solution for the entire problem.
● Consider that there is an objective function that has to be optimized (maximized/
minimized).
● This approach makes greedy choices at each step and makes sure that the objective
function is optimized.
● The greedy algorithm has only one chance to compute the optimal solution and thus,
cannot go back and look at other alternate solutions.
● However, in many problems, this strategy fails to produce a global optimal solution.
● Let's consider the following binary tree to understand how a basic greedy algorithm
To find the path with largest sum. Since we need to maximize the objective function, Greedy
approach can be used.
Following steps are followed to find the solution:
Step 1: Initialize sum = 0
Step 2: Select the root node, so its value will be added to sum, sum = 0+8 = 8
Step 3: The algorithm compares nodes at next level, selects the largest node which is 12,
making the sum = 20.
Step 4: The algorithm compares nodes at the next level, selects the largest node which is 10,
making the sum = 30.
Thus, using the greedy algorithm, we get 8-12-10 as the path. But this is not the optimal
solution, since the path 8-2-89 has the largest sum i.e. 99. This happens because the algorithm
makes decision based on the information available at each step without considering the overall
problem.