Daa 1
Daa 1
• This notation provides an upper bound on a function which ensures that the
function never grows faster than the upper bound. So, it gives the least upper
bound on a function so that the function never grows faster than this upper
bound.
It is the formal way to express the upper boundary of an algorithm running time. It
measures the worst case of time complexity or the algorithm's longest amount of time
to complete its operation. It is represented as shown below:
Asymptotic Analysis
For example:
If f(n) and g(n) are the two functions defined for positive integers,
then f(n) = O(g(n)) as f(n) is big oh of g(n) or f(n) is on the order of g(n)) if there
exists constants c and no such that:
This implies that f(n) does not grow faster than g(n), or g(n) is an upper bound on
the function f(n). In this case, we are calculating the growth rate of the function
which eventually calculates the worst time complexity of a function, i.e., how worst
an algorithm can perform.
f(n)<=c.g(n)
We know that for any value of n, it will satisfy the above condition, i.e., 2n+3<=c.n.
If the value of c is equal to 5, then it will satisfy the condition 2n+3<=c.n. We can
take any value of n starting from 1, it will always satisfy. Therefore, we can say that
for some constants c and for some constants n0, it will always satisfy 2n+3<=c.n. As
it is satisfying the above condition, so f(n) is big oh of g(n) or we can say that f(n)
grows linearly. Therefore, it concludes that c.g(n) is the upper bound of the f(n).
Divide and conquer strategy is as follows: divide the problem instance into two or
more smaller instances of the same problem, solve the smaller instances recursively,
and assemble the solutions to form a solution of the original instance. The recursion
stops when an instance is reached which is too small to divide. When dividing the
instance, one can either use whatever division comes most easily to hand or invest
time in making the division carefully so that the assembly is simplified.
Divide : Divide the problem into a number of sub problems. The sub problems are
solved recursively.
Conquer : The solution to the original problem is then formed from the solutions to
the sub problems (patching together the answers).
Traditionally, routines in which the text contains at least two recursive calls are called
divide and conquer algorithms, while routines whose text contains only one recursive call
are not. Divide–and–conquer is a very powerful use of recursion.
DANDC (P) {
else {
SMALL (P) is a Boolean valued function which determines whether the input size is
small enough so that the answer can be computed without splitting. If this is so
function ‘S’ is invoked otherwise, the problem ‘p’ into smaller sub problems. These
sub problems p1, p2, . . . , pk are solved by recursive application of DANDC.
If the sizes of the two sub problems are approximately equal then the computing time
of DANDC is:
a is no. of subproblems
Given weights and values of n items, we need to put these items in a knapsack of
capacity W to get the maximum total value in the knapsack.
In the 0-1 Knapsack problem, we are not allowed to break items. We either take the
whole item or don’t take it.
A brute-force solution would be to try all possible subset with all different fraction
but that will be too much time taking.
An efficient solution is to use Greedy approach. The basic idea of the greedy
approach is to calculate the ratio value/weight for each item and sort the item on
basis of this ratio. Then take the item with the highest ratio and add them until we
can’t add the next item as a whole and at the end add the next item as much as we
can. Which will always be the optimal solution to this problem.
• We can even put the fraction of any item into the knapsack if taking the complete
item is not possible.
• Arrange all the items in decreasing order of their value / weight ratio.
• Start putting the items into the knapsack beginning from the item with the
highest ratio.
Time Complexity-
• The main time taking step is the sorting of all items in decreasing order of their
value / weight ratio.
• If the items are already arranged in the required order, then while loop takes
O(n) time.
The following are the steps that the dynamic programming follows:
fact(n){
ans = 1;
ans *= i ;
}
return ans;
Backtracking
When we find the solution using backtracking then some bad choices can be made.
The state space tree is searched until the solution of the problem is obtained.
In backtracking, all the possible solutions are tried. If the solution does not satisfy the
constraint, then we backtrack and look for another solution.
Backtracking solves the given problem by first finding the solution of the subproblem
and then recursively solves the other problems based on the solution of the first
subproblem.
When we find the solution using Branch n bound then it provides a better solution so
there are no chances of making a bad choice.
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 needs to be searched completely as the optimum solution can be
present anywhere in the state space tree.
In branch and bound, based on search; bounding values are calculated. According to
the bounding values, we either stop there or extend.
Branch and bound solves the given problem by dividing the problem into two atleast
subproblems.