Unit 2
Unit 2
Divide and Conquer algorithm consists of a dispute using the following three steps.
1. Relational Formula
2. Stopping Condition
1. Relational Formula: It is the formula that we generate from the given technique.
After generation of Formula we apply D&C Strategy, i.e. we break the problem
recursively & solve the broken subproblems.
2. Stopping Condition: When we break the problem using Divide & Conquer
Strategy, then we need to know that for how much time, we need to apply divide &
Conquer. So the condition where the need to stop our recursion steps of D&C is
called as Stopping Condition.
AD
AD
Greedy Algorithm
The greedy method is one of the strategies like Divide and conquer used to solve the
problems. This method is used for solving optimization problems. An optimization
problem is a problem that demands either maximum or minimum results. Let's
understand through some terms.
This technique is basically used to determine the feasible solution that may or may
not be optimal. The feasible solution is a subset that satisfies the given criteria. The
optimal solution is the solution which is the best and the most favorable solution in
the subset. In the case of feasible, if more than one solution satisfies the given
criteria then those solutions will be considered as the feasible, whereas the optimal
solution is the best solution among all the solutions.
o To construct the solution in an optimal way, this algorithm creates two sets where one
set contains all the chosen items, and another set contains the rejected items.
o A Greedy algorithm makes good local choices in the hope that the solution should be
either feasible or optimal.
o Candidate set: A solution that is created from the set is known as a candidate set.
o Selection function: This function is used to choose the candidate or subset which
can be added in the solution.
o Feasibility function: A function that is used to determine whether the candidate or
subset can be used to contribute to the solution or not.
o Objective function: A function is used to assign the value to the solution or the
partial solution.
o Solution function: This function is used to intimate whether the complete function
has been reached or not.
AD
AD
The above is the greedy algorithm. Initially, the solution is assigned with zero value.
We pass the array and number of elements in the greedy algorithm. Inside the for
loop, we select the element one by one and checks whether the solution is feasible
or not. If the solution is feasible, then we perform the union.
P:A→B
The problem is that we have to travel this journey from A to B. There are various
solutions to go from A to B. We can go from A to B by walk, car, bike, train,
aeroplane, etc. There is a restriction in the journey that we have to travel this journey
within 12 hrs. If I go by train or aeroplane then only, I can cover this distance within
12 hrs. There are many solutions to this problem but there are only two solutions
that satisfy the constraint(restriction).
If we say that we have to cover the journey at the minimum cost. This means that we
have to travel this distance as minimum as possible, so this problem is known as a
minimization problem. Till now, we have two feasible solutions, i.e., one by train and
another one by air. Since travelling by train will lead to the minimum cost so it is an
optimal solution. An optimal solution is also the feasible solution, but providing the
best result so that solution is the optimal solution with the minimum cost. There
would be only one optimal solution.
The problem that requires either minimum or maximum result then that problem is
known as an optimization problem. Greedy method is one of the strategies used for
solving the optimization problems.
It follows the local optimum choice at each stage with a intend of finding the global
optimum. Let's understand through an example.
We have to travel from the source to the destination at the minimum cost. Since we
have three feasible solutions having cost paths as 10, 20, and 5. 5 is the minimum
cost path so it is the optimal solution. This is the local optimum, and in this way, we
find the local optimum at each stage in order to calculate the global optimal
solution.
Dynamic Programming
Dynamic programming is a technique that breaks the problems into sub-problems,
and saves the result for future purposes so that we do not need to compute the
result again. The subproblems are optimized to optimize the overall solution is
known as optimal substructure property. The main use of dynamic programming is
to solve optimization problems. Here, optimization problems mean that when we are
trying to find out the minimum or the maximum solution of a problem. The dynamic
programming guarantees to find the optimal solution of a problem if the solution
exists.
The numbers in the above series are not randomly calculated. Mathematically, we
could write each of the terms using the below formula:
With the base values F(0) = 0, and F(1) = 1. To calculate the other numbers, we follow
the above relationship. For example, F(2) is the sum f(0) and f(1), which is equal to 1.
In the above example, if we calculate the F(18) in the right subtree, then it leads to
the tremendous usage of resources and decreases the overall performance.
The solution to the above problem is to save the computed results in an array. First,
we calculate F(16) and F(17) and save their values in an array. The F(18) is calculated
by summing the values of F(17) and F(16), which are already saved in an array. The
computed value of F(18) is saved in an array. The value of F(19) is calculated using
the sum of F(18), and F(17), and their values are already saved in an array. The
computed value of F(19) is stored in an array. The value of F(20) can be calculated by
adding the values of F(19) and F(18), and the values of both F(19) and F(18) are
stored in an array. The final computed value of F(20) is stored in an array.
How does the dynamic programming approach
work?
The following are the steps that the dynamic programming follows:
The above five steps are the basic steps for dynamic programming. The dynamic
programming is applicable that are having properties such as:
Those problems that are having overlapping subproblems and optimal substructures.
Here, optimal substructure means that the solution of optimization problems can be
obtained by simply combining the optimal solution of all the subproblems.
AD
AD
o Top-down approach
o Bottom-up approach
AD
AD
Top-down approach
The top-down approach follows the memorization technique, while bottom-up
approach follows the tabulation method. Here memorization is equal to the sum of
recursion and caching. Recursion means calling the function itself, while caching
means storing the intermediate results.
Advantages
o It is very easy to understand and implement.
o It solves the subproblems only when it is required.
o It is easy to debug.
Disadvantages
It uses the recursion technique that occupies more memory in the call stack.
Sometimes when the recursion is too deep, the stack overflow condition will occur.
AD
AD
1. int fib(int n)
2. {
3. if(n<0)
4. error;
5. if(n==0)
6. return 0;
7. if(n==1)
8. return 1;
9. sum = fib(n-1) + fib(n-2);
10. }
In the above code, we have used the recursive approach to find out the Fibonacci
series. When the value of 'n' increases, the function calls will also increase, and
computations will also increase. In this case, the time complexity increases
exponentially, and it becomes 2n.
One solution to this problem is to use the dynamic programming approach. Rather
than generating the recursive tree again and again, we can reuse the previously
calculated value. If we use the dynamic programming approach, then the time
complexity would be O(n).
In the above code, we have used the memorization technique in which we store the
results in an array to reuse the values. This is also known as a top-down approach in
which we move from the top and break the problem into sub-problems.
Bottom-Up approach
The bottom-up approach is also one of the techniques which can be used to
implement the dynamic programming. It uses the tabulation technique to implement
the dynamic programming approach. It solves the same kind of problems but it
removes the recursion. If we remove the recursion, there is no stack overflow issue
and no overhead of the recursive functions. In this tabulation technique, we solve the
problems and store the results in a matrix.
o Top-Down
o Bottom-Up
The bottom-up is the approach used to avoid the recursion, thus saving the memory
space. The bottom-up is an algorithm that starts from the beginning, whereas the
recursive algorithm starts from the end and works backward. In the bottom-up
approach, we start from the base case to find the answer for the end. As we know,
the base cases in the Fibonacci series are 0 and 1. Since the bottom approach starts
from the base cases, so we will start from 0 and 1.
Key points
o We solve all the smaller sub-problems that will be needed to solve the larger sub-
problems then move to the larger problems using smaller sub-problems.
o We use for loop to iterate over the sub-problems.
o The bottom-up approach is also known as the tabulation or table filling method.
Suppose we have an array that has 0 and 1 values at a[0] and a[1] positions,
respectively shown as below:
Since the bottom-up approach starts from the lower values, so the values at a[0] and
a[1] are added to find the value of a[2] shown as below:
The value of a[3] will be calculated by adding a[1] and a[2], and it becomes 2 shown
as below:
The value of a[4] will be calculated by adding a[2] and a[3], and it becomes 3 shown
as below:
The value of a[5] will be calculated by adding the values of a[4] and a[3], and it
becomes 5 shown as below:
In the above code, base cases are 0 and 1 and then we have used for loop to find
other values of Fibonacci series.
Initially, the first two values, i.e., 0 and 1 can be represented as:
When i=2 then the values 0 and 1 are added shown as below:
When i=3 then the values 1and 1 are added shown as below:
When i=4 then the values 2 and 1 are added shown as below:
When i=5, then the values 3 and 2 are added shown as below:
In the above case, we are starting from the bottom and reaching to the top.
What is Backtracking?
Backtracking is nothing but the modified process of the Brute force approach. It is a
technique where multiple solutions to a problem are available, and it searches for the
solution to the problem among all the available options. Let's consider the example
of brute force search as backtracking follows the brute force search approach.
Consider the box, and we have three objects of three different colors. One object is
of red color, the second object is of green color, and the third object is of blue color.
Now, we have to keep these objects inside the box, and we have multiple options. As
per the Brute force approach, we have to consider all the options and identify the
best option among all the possible options.
Suppose we first fill red object, after then green object and then blue object. This is
the first solution to this problem. The possible solution is red, green, and blue.
The second solution is to keep the red object, then the blue object, and then the
green object. The possible solution red, blue, and green.
The third solution can be to keep the green object, then the red object and then the
blue object. The possible solution is green, red and blue.
The fourth solution is to keep the green object, then the blue object and then the red
object. The possible solution is green, blue and red.
The fifth solution is to keep the blue object, then green object and then red object.
The possible solution is blue, green and red.
The sixth solution is to keep the blue object, then the red object and then the green
object. The possible solution is blue, red and green.
The above are the possible solutions, and we have to identify the best possible
solution out of all the possible solutions. This approach is known as a brute force
approach. Backtracking is similar to the brute force approach, but it is a modified
process of the brute force approach.
Let's see how backtracking is different from the brute force approach.
The above is the figure that shows the possible solutions to the above problem. Now
we will see that how these solutions are represented in backtracking.
In backtracking, solutions are represented in the form of a tree and that tree is
known as a state space tree. Since backtracking follows the DFS, the tree will be
formed using DFS, which is known as a State Space tree.
Consider the first solution, i.e., red, green, blue, and it can be represented in the
form of a tree as shown as below.
AD
AD
Consider the second solution, i.e., red, blue, green. Since there is no more object
after blue so we will backtrack and move to the green. Instead of taking green, we
first take blue and then green, shown as below:
The next solution is green, red and blue. Since we cannot explore the green object,
so we move back and reach the blue object. We cannot explore the blue object, so
we again move back and reach to the red object. Instead of using the red object, we
will use the green object. After the green object, we use the red object and then we
use the blue object. We cannot explore the blue object further. Now the sequence
green, red and blue is formed as shown as below:
The next solution is green, blue and red. Since we cannot explore the blue object,
we move back and reach the red object. Instead of using a red color object, we will
use the blue color object and then we use the red color object. Now the sequence
green, blue and red is formed.
The next solution is blue, green and red. Since we cannot explore the red object, so
we backtrack and reach the blue object. We cannot explore the blue object so we
backtrack and reach the green object. Instead of using the green object, we will use
the blue color object then we use the green object and then we use the red color
object. The sequence, i.e., blue, green and red, is formed as shown as below:
The next solution is blue, red and green. Since we cannot explore red object so we
backtrack and reach to the green object. Instead of using the green object, we use
the red object and then we use the green object.
AD
AD
The above is the state space tree that shows all the possible solutions related to the
problem. Therefore, we can say that the state space tree can be used to represent all
the solutions in backtracking. Using backtracking, we can solve the problem that has
some constraints and we can find the solution based on these constraints. Suppose
in the above example; the constraint is blue color object must not be in the middle
(bounding function).
The first solution is red, green and blue. Since blue color object is not in the middle
so we do need to perform any modification.
The second solution is red, blue, and green. Since blue color is in the middle so we
will remove the green color as shown as below:
The next solution is green, red, and blue. Since blue color object is not in the
middle so we do need to perform any modification.
The next solution is green, blue and red. Since blue color is in the middle so we will
remove the red color as shown as below:
The above is the state space tree that does not have blue object in the middle of the
solution.
It is similar to backtracking. The concept branch and bound and backtracking follow
the Brute force method and generate the state space tree. But both of them follows
different approaches. The way to generate the tree is different.
Backtracking follows the DFS, whereas the branch n bound follows the BFS to
generate the tree. Now we will understand the branch n bound through an example.
As it follows the BFS, so first all the nodes of the same level are added then we move
to the next level.
Now we have three possibilities that either we select red, green, or blue objects as
shown below. In this case, we have completed the first level.
Now we move to the next level.
In the case of red object, we have two possibilities that either we select green or blue
object shown as below:
In the case of green object, we have two possibilities that either we select red or blue
object shown as below:
In the case of blue object, we have two possibilities that either we select red or green
object shown as below:
We move to the third level.
In the case of a green object, only one object, i.e., blue, can be added.
In the case of a blue object, only one object, i.e., green, can be added.
In the case of a red object, only one object, i.e., blue, can be added.
In the case of a blue object, only one object, i.e., red, can be added.
In the case of a green object, only one object, i.e., red, can be added.
In the case of a red object, only one object, i.e., green, can be added.
Examples:
o 8 Queens problem
o Knapsack problem using backtracking
The problem that can be solved by using branch and bound is:
AD
AD
When we find the solution using backtracking then some When we find the solution using Branch n
bad choices can be made. bound then it provides a better solution so
there are no chances of making a bad choice.
Backtracking uses a Depth first search. It is not necessary that branch n bound uses
Depth first search. It can even use a Breadth-
first search and best-first search.
The state space tree is searched until the solution of the The state space tree needs to be searched
problem is obtained. completely as the optimum solution can be
present anywhere in the state space tree.
In backtracking, all the possible solutions are tried. If the In branch and bound, based on search;
solution does not satisfy the constraint, then we bounding values are calculated. According to
backtrack and look for another solution. the bounding values, we either stop there or
extend.
Applications of backtracking are n-Queens problem, Sum Applications of branch and bound are
of subset. knapsack problem, travelling salesman
problem, etc.
Backtracking is more efficient than the Branch and Branch n bound is less efficient.
bound.
Backtracking solves the given problem by first finding the Branch and bound solves the given problem
solution of the subproblem and then recursively solves by dividing the problem into two atleast
the other problems based on the solution of the first subproblems.
subproblem.
o Greedy Algorithm always makes the choice (greedy criteria) looks best at the
moment, to optimize a given objective.
o The greedy algorithm doesn't always guarantee the optimal solution however
it generally produces a solution that is very close in value to the optimal.
AD
4. Branch and Bound: In Branch & Bound algorithm a given subproblem, which
cannot be bounded, has to be divided into at least two new restricted subproblems.
Branch and Bound algorithm are methods for global optimization in non-convex
problems. Branch and Bound algorithms can be slow, however in the worst case they
require effort that grows exponentially with problem size, but in some cases we are
lucky, and the method coverage with much less effort.
Loop invariants
This is a justification technique. We use loop invariant that helps us to understand
why an algorithm is correct. To prove statement S about a loop is correct, define S
concerning series of smaller statement S0 S1....Sk where,
Offline Algorithms
In the offline version, we have all items upfront. Unfortunately offline version is also
NP Complete, but we have a better approximate algorithm for it. First Fit Decreasing
uses at most (4M + 1)/3 bins if the optimal is M.
4. First Fit Decreasing:
A trouble with online algorithms is that packing large items is difficult, especially if
they occur late in the sequence. We can circumvent this by *sorting* the input
sequence, and placing the large items first. With sorting, we get First Fit Decreasing
and Best Fit Decreasing, as offline analogues of online First Fit and Best Fit.
First Fit decreasing produces the best result for the sample input because items are
sorted first.
First Fit Decreasing can also be implemented in O(n Log n) time using Self-Balancing
Binary Search Trees.
Auxiliary Space: O(1)
Example
Item A B C
Profit 2 4 7
Weight 1 3 5
Solution
Step 1
So we add zeroes to the 0th row and 0th column because if the
weight of item is 0, then it weighs nothing; if the maximum
weight of knapsack is 0, then no item can be added into the
knapsack.
The remaining values are filled with the maximum profit
achievable with respect to the items and weight per column that
can be stored in the knapsack.
By computing all the values using the formula, the table obtained
would be −
To find the items to be added in the knapsack, recognize the
maximum profit from the table and identify the items that make
up the profit, in this example, its {1, 7}.
Analysis
This algorithm takes Ɵ(n.w) times as table c has (n+1).(w+1)
entries, where each entry requires Ɵ(1) time to compute.
For example, consider the graph shown in the figure on the right side. A TSP tour in
the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80. The problem
is a famous NP-hard problem. There is no polynomial-time know solution for this
problem. The following are different solutions for the traveling salesman problem.
Naive Solution:
1) Consider city 1 as the starting and ending point.
2) Generate all (n-1)! Permutations of cities.
3) Calculate the cost of every permutation and keep track of the minimum cost
permutation.
4) Return the permutation with minimum cost.
Time Complexity: ?(n!)
Dynamic Programming:
Let the given set of vertices be {1, 2, 3, 4,….n}. Let us consider 1 as starting and
ending point of output. For every other vertex I (other than 1), we find the minimum
cost path with 1 as the starting point, I as the ending point, and all vertices appearing
exactly once. Let the cost of this path cost (i), and the cost of the corresponding Cycle
would cost (i) + dist(i, 1) where dist(i, 1) is the distance from I to 1. Finally, we return
the minimum of all [cost(i) + dist(i, 1)] values. This looks simple so far.
Now the question is how to get cost(i)? To calculate the cost(i) using Dynamic
Programming, we need to have some recursive relation in terms of sub-problems.
Let us define a term C(S, i) be the cost of the minimum cost path visiting each vertex
in set S exactly once, starting at 1 and ending at i. We start with all subsets of size 2
and calculate C(S, i) for all subsets where S is the subset, then we calculate C(S, i) for
all subsets S of size 3 and so on. Note that 1 must be present in every subset.
If size of S is 2, then S must be {1, i},
C(S, i) = dist(1, i)
Else if size of S is greater than 2.
C(S, i) = min { C(S-{i}, j) + dis(j, i)} where j belongs to S, j !=
i and j != 1.
Below is the dynamic programming solution for the problem using top down
recursive+memoized approach:-
For maintaining the subsets we can use the bitmasks to represent the remaining nodes
in our subset. Since bits are faster to operate and there are only few nodes in graph,
bitmasks is better to use.
For example: –
10100 represents node 2 and node 4 are left in set to be processed
010010 represents node 1 and 4 are left in subset.
NOTE:- ignore the 0th bit since our graph is 1-based
Output
The cost of most efficient tour = 80
Time Complexity : O(n2*2n) where O(n* 2n) are maximum number of unique
subproblems/states and O(n) for transition (through for loop as in code) in every
states.
Auxiliary Space: O(n*2n), where n is number of Nodes/Cities here.
For a set of size n, we consider n-2 subsets each of size n-1 such that all subsets don’t
have nth in them. Using the above recurrence relation, we can write a dynamic
programming-based solution. There are at most O(n*2n) subproblems, and each one
takes linear time to solve. The total running time is therefore O(n2*2n). The time
complexity is much less than O(n!) but still exponential. The space required is also
exponential. So this approach is also infeasible even for a slightly higher number of
vertices. We will soon be discussing approximate algorithms for the traveling
salesman problem.
Heuristic techniques
In this article, we are going to discuss Heuristic techniques along with some
examples that will help you to understand the Heuristic techniques more clearly.
What is Heuristics?
A heuristic is a technique that is used to solve a problem faster than the classic
methods. These techniques are used to find the approximate solution of a problem
when classical methods do not. Heuristics are said to be the problem-solving
techniques that result in practical and quick solutions.
Heuristics are strategies that are derived from past experience with similar problems.
Heuristics use practical methods and shortcuts used to produce the solutions that
may or may not be optimal, but those solutions are sufficient in a given limited
timeframe.
History
Psychologists Daniel Kahneman and Amos Tversky have developed the study of
Heuristics in human decision-making in the 1970s and 1980s. However, this concept
was first introduced by the Nobel Laureate Herbert A. Simon, whose primary
object of research was problem-solving.
The heuristic method might not always provide us the finest solution, but it is
assured that it helps us find a good solution in a reasonable time.
Based on context, there can be different heuristic methods that correlate with the
problem's scope. The most common heuristic methods are - trial and error,
guesswork, the process of elimination, historical data analysis. These methods involve
simply available information that is not particular to the problem but is most
appropriate. They can include representative, affect, and availability heuristics.
AD
AD
The examples of Direct Heuristic search techniques include Breadth-First Search (BFS)
and Depth First Search (DFS).
The examples of Weak Heuristic search techniques include Best First Search (BFS) and
A*.
Before describing certain heuristic techniques, let's see some of the techniques listed
below:
o Bidirectional Search
o A* search
o Simulated Annealing
o Hill Climbing
o Best First search
o Beam search
It is also called greedy local search as it only looks to its good immediate neighbor
state and not beyond that. The steps of a simple hill-climbing algorithm are listed
below:
AD
AD
Step 1: Evaluate the initial state. If it is the goal state, then return success and Stop.
Step 2: Loop Until a solution is found or there is no new operator left to apply.
AD
AD
Else if it is better than the current state, then assign a new state as a current state.
Else if not better than the current state, then return to step2.
Step 5: Exit.
Step 3: Remove the node n from the OPEN list, which has the lowest value of h(n),
and places it in the CLOSED list.
Step 5: Check each successor of node n, and find whether any node is a goal node or
not. If any successor node is the goal node, then return success and stop the search,
else continue to next step.
Step 6: For each successor node, the algorithm checks for evaluation function f(n)
and then check if the node has been in either OPEN or CLOSED list. If the node has
not been in both lists, then add it to the OPEN list.
A* Search Algorithm
A* search is the most commonly known form of best-first search. It uses the heuristic
function h(n) and cost to reach the node n from the start state g(n). It has combined
features of UCS and greedy best-first search, by which it solve the problem
efficiently.
It finds the shortest path through the search space using the heuristic function. This
search algorithm expands fewer search tree and gives optimal results faster.
Algorithm of A* search:
Step 3: Select the node from the OPEN list which has the smallest value of the
evaluation function (g+h). If node n is the goal node, then return success and stop,
otherwise.
Step 4: Expand node n and generate all of its successors, and put n into the closed
list. For each successor n', check whether n' is already in the OPEN or CLOSED list. If
not, then compute the evaluation function for n' and place it into the Open list.
Step 5: Else, if node n' is already in OPEN and CLOSED, then it should be attached to
the back pointer which reflects the lowest g(n') value.
Some of the real-life examples of heuristics that people use as a way to solve a
problem:
AD
AD
Types of heuristics
There are various types of heuristics, including the availability heuristic, affect
heuristic and representative heuristic. Each heuristic type plays a role in decision-
making. Let's discuss about the Availability heuristic, affect heuristic, and
Representative heuristic.
Availability heuristic
Availability heuristic is said to be the judgment that people make regarding the
likelihood of an event based on information that quickly comes into mind. On
making decisions, people typically rely on the past knowledge or experience of an
event. It allows a person to judge a situation based on the examples of similar
situations that come to mind.
Representative heuristic
It occurs when we evaluate an event's probability on the basis of its similarity with
another event.
So, instead of evaluating the product based on its quality, customers correlate the
products quality based on the similarity in packaging.
Affect heuristic
It is based on the negative and positive feelings that are linked with a certain
stimulus. It includes quick feelings that are based on past beliefs. Its theory is one's
emotional response to a stimulus that can affect the decisions taken by an individual.
When people take a little time to evaluate a situation carefully, they might base their
decisions based on their emotional response.
If someone carefully analyzes the benefits and risks of consuming fast food, they
might decide that fast food is unhealthy. But people rarely take time to evaluate
everything they see and generally make decisions based on their automatic
emotional response. So, Fast food companies present advertisements that rely on
such type of Affect heuristic for generating a positive emotional response which
results in sales.
Limitation of heuristics
Along with the benefits, heuristic also has some limitations.
o Although heuristics speed up our decision-making process and also help us to solve
problems, they can also introduce errors just because something has worked
accurately in the past, so it does not mean that it will work again.
o It will hard to find alternative solutions or ideas if we always rely on the existing
solutions or heuristics.
Conclusion
That's all about the article. Hence, in this article, we have discussed the heuristic
techniques that are the problem-solving techniques that result in a quick and
practical solution. We have also discussed some algorithms and examples, as well as
the limitation of heuristics.