11.algorithm Design
11.algorithm Design
11.algorithm Design
Decision is made that is good at that point without considering the future. This
means that some local best is chosen and considers it as the global optimal. There
are two properties in this algorithm.
• Greedily choosing the best option
• If an optimal solution can be found by retrieving the optimal solution to its
subproblems.
This algorithm is easy to device and most of the time the simplest one. But making
locally best decisions does not always work as it sounds. So, it is replaced by a
reliable solution called Dynamic Programming approach.
Applications - Sorting: Selection Sort, Topological sort, Prim’s & Kruskal’s
algorithms, Coin Change problem, Fractional Knapsack Problem, Job Scheduling
algorithm
6 June 2022 C-DAC 4
Recursive Algorithm
As the name suggests it has two parts: Divide the problem into subproblems
and solve them.
Combine the solution of each above problems.
This algorithm is extensively used in various problems as it is quite stable and
optimal for most of the problems asked.
Applications:
• Binary Search
• Merge Sort & Quick Sort
• Median Finding
• Matrix Multiplication
Its simply means remembering the past and apply it to future corresponding
results and hence this algorithm is quite efficient in terms of time complexity.
This algorithm has two version:
• Bottom-Up Approach: Starts solving from the bottom of the problems i.e.
solving the last possible sub-problems first and using the result of those
solving the above sub-problems.
• Top-Down Approach: Starts solving the problems from the very beginning
to arrive at the required sub-problem and solve it using previously solved
sub-problems.
Applications – Fibonacci sequence, Bellman-Ford algorithm, Chain Matrix
multiplication, Subset Sum, Knapsack Problem
Termination
A number of iterations may be known in advance or it may depend on certain
conditions to be satisfied. To find 5! loop should be iterated 5 times
The loop count is known in advance:
for (i = 1; i <= 5; i++)
{ ...
}
The loop count is not predefined:Sometimes loop may be forced to terminate
from in-between
for (i = 0; i < n; i++)
{
if A[i] == x
break;
}
Arrays are typically used to retain a huge amount of data and are accessible via
the loop. Consider the following two scenarios for locating the array’s smallest
element.
Algorithm POOR_FIND_MIN
k←0
for i ← 1 to n do
if A[i] < A[k] then
k←i
end
Min ← A[k]
if Index == -1 then
print “Key not found”
else
print “Key found on index i”
end
6 June 2022 C-DAC 15
Late termination
An algorithm may not have the same performance for different types of inputs.
With the increase in the input size, the performance will change.
The study of change in performance of the algorithm with the change in the
order of the input size is defined as asymptotic analysis.
Big-O notation represents the upper bound of the running time of an algorithm.
Thus, it gives the worst-case complexity of an algorithm.
Since it gives the worst-case running time of an algorithm, it is widely used to
analyze an algorithm as we are always interested in the worst-case scenario.
Omega notation represents the lower bound of the running time of an algorithm.
Thus, it provides the best case complexity of an algorithm.
For any value of n, the minimum time required by the algorithm is given by
Omega Ω(g(n))
The algorithm never reverses the earlier decision even if the choice is wrong. It
works in a top-down approach.
This algorithm may not produce the best result for all the problems. It's because
it always goes for the local best choice to produce the global best result..
2. Optimal Substructure
If the optimal overall solution to the problem corresponds to the optimal
solution to its subproblems, then the problem can be solved using a greedy
approach. This property is called optimal substructure.
1. Let's start with the root node 20. The weight of the right child is 3 and the
weight of the left child is 2.
2. Problem is to find the largest path. And, the optimal solution at the moment is
3. So, the greedy algorithm will choose 3.
3. Finally the weight of an only child of 3 is 1. This gives us our final result 20
+ 3 + 1 = 24.
However, it is not the optimal solution. There is another path that carries more
weight (20 + 2 + 10 = 32) as shown in the image below.
Let's find the fibonacci sequence upto 5th term. A fibonacci series is the
sequence of numbers in which each number is the sum of the two preceding
ones. For example, 0,1,1, 2, 3. Here, each number is the sum of the two
preceding numbers.
Algorithm
Let n be the number of terms.
1. If n <= 1, return 1.
2. Else, return the sum of two preceding numbers.
Optimizing: In this case, the best solution is found. To find the best solution, it
may either find all the possible solutions to find the best solution or if the value
of the best solution is known, it stops finding when the best solution is found.
For example: Finding the best path for the travelling salesman problem. Here
best path means that travelling all the cities and the cost of travelling should be
minimum.
Brute force search considers each and every state of a tree, and the state is
represented in the form of a node. As far as the starting position is concerned,
have two choices, i.e., A state and B state. In the case of B state, we have two
states, i.e., state E and F.
In the case of brute force search, each state is considered one by one. As we can
observe in the above tree that the brute force search takes 12 steps to find the
solution.
On the other hand, backtracking, which uses Depth-First search, considers the
below states only when the state provides a feasible solution. Consider the
above tree, start from the root node, then move to node A and then node C. If
node C does not provide the feasible solution, then there is no point in
considering the states G and H. We backtrack from node C to node A. Then, we
move from node A to node D. Since node D does not provide the feasible
solution, we discard this state and backtrack from node D to node A.
We move to node B, then we move from node B to node E. We move from node
E to node K; Since k is a solution, so it takes 10 steps to find the solution. In
this way, we eliminate a greater number of states in a single iteration. Therefore,
we can say that backtracking is faster and more efficient than the brute force
6 Juneapproach.
2022 C-DAC 38
Backtracking algorithms
The Brute force approach tries out all the possible solutions and chooses the
desired/best solutions.
The term backtracking suggests that if the current solution is not suitable, then
backtrack and try other solutions. Thus, recursion is used in this approach.
This approach is used to solve problems that have multiple solutions. To have
an optimal solution, one must go for dynamic programming.
A space state tree is a tree representing all the possible states (solution or non-
solution) of the problem from the root as an initial state to the leaf as a terminal
state.
Branch and bound is an algorithm design paradigm which is generally used for
solving combinatorial optimization problems. These problems are typically
exponential in terms of time complexity and may require exploring all possible
permutations in worst case. The Branch and Bound Algorithm technique solves
these problems relatively quickly.
The above are jobs, problems and problems given. Jobs = {j1, j2, j3, j4}, P =
{10, 5, 8, 3}, d = {1, 2, 1, 2}. Solutions in two ways are:
1) first way of representing the solutions is the subsets of jobs. S1 = {j1, j4}
2) second way of representing the solution is that first job is done, second
and third jobs are not done, and fourth job is done. S2 = {1, 0, 0, 1}
The solution s1 is the variable-size solution while the solution s2 is the fixed-
size solution.
The above are jobs, problems and problems given. Jobs = {j1, j2, j3, j4}, P =
{10, 5, 8, 3}, d = {1, 2, 1, 2}. Solutions in two ways are:
1) first way of representing the solutions is the subsets of jobs. S1 = {j1, j4}
2) second way of representing the solution is that first job is done, second
and third jobs are not done, and fourth job is done. S2 = {1, 0, 0, 1}
The solution s1 is the variable-size solution while the solution s2 is the fixed-
size solution.