Professional Documents
Culture Documents
UNIT - II
UNIT- II
BRUTE FORCE AND DIVIDE AND CONQUER
PART – A
1. Distinguish between Dynamic Programming and Divide and Conquer. [N/D 18]
S. No. Dynamic Programming Divide and Conquer
1 Many decision sequences are The problem is divided into small
generated and all the subproblems. These subproblems are
overlapping subinstances are solved independently and finally all the
considered. solutions are combined to get final
solutions.
2 It is efficient because of It is less efficient because of rework.
avoiding recomputations.
3 It uses iterative method. It uses recursive method.
2. What is meant by Hamiltonian Circuit? Give example. [N/D 18, N/D 13]
Hamiltonian Circuit/Cycle is a circuit that visits every vertex exactly once and returns to
the starting vertex.
4. Design a Brute Force algorithm for computing the value of a polynomial P(x) = anxn
+ an-1xn-1+….+ a1x….a0 at a given point x0 and determine the steps and its
worst-case efficiency class. [A/M 15]
Algorithm Polynomial Eval
{
int sum,n,i;
write(“enter the value of n”);
Read(n);
sum=0;
write(“enter the value of x”);
Read(x);
for(i=n; i>0; i--)
{
write(“enter the coefficient”);
Read(a);
sum=sum + (a * pow(x,n));
write(“value of polynomial is”, sum);
}
}
7. Give the general strategy for Divide and Conquer method. [M/J 16, M/J 13]
The general strategy for Divide and Conquer method is the given problem is broken into
smaller subproblems and their solutions are obtained. Later all the solutions of the
subproblems are combined to get the solution for original problem.
9. Give the time efficiency and drawback of Merge Sort algorithm. [N/D14]
The best-case, worst-case and average-case time efficiencies of Merge Sort algorithm are
O(n log n).
Drawbacks of Merge Sort algorithm
a) It requires extra storage to execute this method.
b) It is complicated to code.
c) It is slower than Quick Sort.
16. What is meant by Closest Pair Problem? Give the Euclidean Distance formula.
The Closest Pair Problem is a problem of finding the two closest points from the set of n
points. The distance between two points pi(xi, yi) and pj(xj, yj) is denoted by Euclidean
Distance formula
d(pi, pj) = √(xi–xj)2 + (yi–yj)2 where pi and pj are two points for which i < j.
19. Name the algorithmic strategy that uses the “just do it” approach.
The Brute Force approach makes use of “just do it” approach. It is straight forward
approach to solve problems.
PART – B
Merge sort definition: Mergesort sorts a given array A[0..n-1] by dividing it into two halves
a[0..(n/2)-1] and A[n/2..n-1] sorting each of them recursively and then merging the two smaller
sorted arrays into a single sorted one.
ALGORITHM Quicksort(A[l..r])
//Sorts a subarray by quicksort
//Input: A subarray A[l..r] of A[0..n-1],defined by its left and right indices l and r
//Output: The subarray A[l..r] sorted in nondecreasing order
if l < r
s Partition (A[l..r]) // s is a split position
Quicksort(A[l..s-1])
Quicksort(A[s+1..r]
• It is fragile i.e., a simple mistake in the implementation can go unnoticed and cause it to
perform badly.
Efficiency of Quicksort
Based on whether the partitioning is balanced.
Best case: split in the middle — Θ( n log n) C(n) = 2C(n/2) + Θ(n) //2 subproblems of size n/2
each
Worst case: sorted array! — Θ( n2) C(n) = C(n-1) + n+1 //2 subproblems of size 0 and n-1
respectively
Average case: random arrays — Θ( n log n)
Since the number of permutations to be considered for the general case of the assignment
problem is n!,