You are on page 1of 12

CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

DHANALAKSHMI COLLEGE OF ENGINEERING


Tambaram, Chennai

Department of Computer Science and Engineering

CS8451 – Design and Analysis of Algorithms


Year / Sem : II / IV
2 Marks Q & A

UNIT - II

Department of Computer Science and Engineering Page 1


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

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.

Here, A–B–D–C–E–A is the Hamiltonian Circuit

3. What are best-case and worst-case complexities of Binary Search algorithm?


a) The best-case complexity of Binary Search algorithm is O(1).
b) The worst-case complexity of Binary Search algorithm is O(log2n)

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);

Department of Computer Science and Engineering Page 2


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

sum=sum + (a * pow(x,n));
write(“value of polynomial is”, sum);
}
}

5. Write the advantages of Insertion Sort. [N/D  17]


Advantages of Insertion Sort
a) Easy to implement
b) Compares with all individual element

6. What is meant by Closest Pair Problem? [A/M  17, M/J  16]


The Closest Pair Problem is a problem of finding the two closest points from the set of n
points.
Example: In air-traffic, Closest Pair Problem is used to avoid collision between the
planes.

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.

8. Distinguish between Linear Search and Binary Search. [N/D 14]


S. No. Linear Search Binary Search
1 Each and every element is The list is subdivided into two
compared with the key sublists. The key element is
element from the beginning searched in the sublist.
of the list.
2 Simple to implement Additional computation is required
for computing middle element.
3 Less efficient More efficient
4 For search, element need not For search, element is arranged in
be in specific order. either ascending or descending
order.

9. Give the time efficiency and drawback of Merge Sort algorithm. [N/D14]
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.

10. What is Knapsack Problem?


Knapsack problem is the problem of picking up the most valuable objects to fill
knapsack to its capacity.

Department of Computer Science and Engineering Page 3


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

11. What is meant by Traveling Salesman Problem? [M/J  16]


The Travelling Salesman Problem is the problem of finding the shortest tour through ‘n’
cities that visits every city exactly once and returns to the starting city.

12. Is Quick Sort a stable sorting algorithm?


No, Quick Sort is not a stable sorting algorithm because after applying this sorting
method, the ordering of similar elements is not preserved.

13. What is meant by Assignment Problem?


The Assignment Problem can be stated as follows:
a) Consider that there are ‘n’ people who need to be assigned to execute ‘n’ jobs i.e.,
one person is assigned to execute one job at a time.
b) Then the problem is to find such assignment that gives the smallest total cost.
c) The cost can be computed as cost C[i, j] i.e., ith person assigned to jth job.

14. What is meant by Heap Sort? Give its complexity.


Heap Sort is a comparison based sorting technique based on Binary Heap data structure.
It is similar to Selection Sort i.e., first it finds the maximum element and places the
maximum element at the end. And repeats the same process for the remaining elements.
Heap Sort works in two stages
a) Heap construction
b) Deletion of maximum key
Time complexity of Heap Sort is O(log n).

15. What is meant by Quick Sort? Give its steps.


Quick Sort is a sorting algorithm that uses the Divide and Conquer strategy. In this
method, division is dynamically carried out.
Steps involved in Quick Sort algorithm
a) Divide and Conquer
b) Combine

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.

17. What is meant by Brute Force?


Brute Force is a straight forward approach to solve problem. It is directly based on the
problem’s statement and definitions of the concepts involved.

18. What is meant by Convex-Hull Problem?


The Convex-Hull problem is a problem of finding the smallest convex polygon that
contains given n points in a plane. It can be solved by using Divide and Conquer method.

Department of Computer Science and Engineering Page 4


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

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.

20. What is meant by Exhaustive Search?


Exhaustive Search is a method in which solution is obtained by searching each element
of the given problem. It makes use of straight forward Brute Force approach.

21. What is the necessary precondition for the Binary Search?


The necessary precondition for the Binary Search is to arrange the list in ascending or
descending order.

PART – B

CS8451 – DESIGN AND ANALYSIS OF ALGORITHMS


UNIT – II 16 MARKS

1. Explain Divide and Conquer Method


The most well known algorithm design strategy is Divide and Conquer Method. It
 Divide the problem into two or more smaller sub-problems.
 Conquer the sub-problems by solving them recursively.
 Combine the solutions to the sub-problems into the solutions for the original
problem.

Department of Computer Science and Engineering Page 5


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

Divide and Conquer Examples


 Sorting: mergesort and quicksort
 Tree traversals
 Binary search
 Matrix multiplication-Strassen‘s algorithm

2. Explain Merge Sort with suitable example.

 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.

 Steps in Merge Sort


1. Divide Step If given array A has zero or one element, return S; it is already sorted. Otherwise,
divide A into two arrays, A1 and A2, each containing about half of the elements of A.
2. Recursion Step Recursively sort array A1 and A2.
3. Conquer Step Combine the elements back in A by merging the sorted arrays A1 and A2 into a
sorted sequence

 Algorithm for merge sort.


ALGORITHM Mergesort(A[0..n-1])
//Sorts an array A[0..n-1] by recursive mergesort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in nondecreasing order
if n > 1
copy A[0..(n/2)-1] to B[0..(n/2)-1]
copy A[(n/2)..n-1] to C[0..(n/2)-1]
Mergesort(B[0..(n/2)-1])
Mergesort(C[0..(n/2)-1])
Merge(B,C,A)

 Algorithm to merge two sorted arrays into one.


ALGORITHM Merge (B [0..p-1], C[0..q-1], A[0..p+q-1])
//Merges two sorted arrays into one sorted array
//Input: arrays B[0..p-1] and C[0..q-1] both sorted
//Output: sorted array A [0..p+q-1] of the elements of B & C
I=0; j= 0; k= 0 while I < p and j < q do if B[I] <= C[j]
A[k] B [I]; I I+1
else
Department of Computer Science and Engineering Page 6
CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

A[k] C[j]; j j+1


k k+1
if i = p copy C[j..q-1] to A [k..p+q-1]
else
copy B[i..p-1] to A [k..p+q-1]

3. Discuss Quick Sort in detail


Quick sort is an algorithm of choice in many situations because it is not difficult to
implement, it is a good \"general purpose\" sort and it consumes relatively fewer resources
during execution.
 Quick Sort and divide and conquer
Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1] and A[s+1..r] such that each element
of the first array is ≤A[s] and each element of the second array is ≥ A[s]. (Computing the
index of s is part of partition.)
Implication: A[s] will be in its final position in the sorted array.
Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r] by recursive calls
to quicksort
Combine: No work is needed, because A[s] is already in its correct place
after the
partition is done, and the two subarrays have been sorted.
 Steps in Quicksort
 Select a pivot w.r.t. whose value we are going to divide the sublist. (e.g., p = A[l])
Rearrange the list so that it starts with the pivot followed by a ≤ sublist (a
sublist
whose elements are all smaller than or equal to the pivot) and a ≥ sublist (a
sublist
whose elements are all greater than or equal to the pivot ) Exchange the
pivot with
the last element in the first sublist(i.e., ≤ sublist) – the pivot is now in its
final position
Sort the two sublists recursively using quicksort.

 The Quicksort Algorithm


ALGORITHM Quicksort(A[l..r])

Department of Computer Science and Engineering Page 7


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

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]

ALGORITHM Partition (A[l ..r])


//Partitions a subarray by using its first element as a pivot
//Input: A subarray A[l..r] of A[0..n-1], defined by its left and right indices l and r (l < r)
//Output: A partition of A[l..r], with the split position returned as this function‘s value
P A[l]
i l; j  r + 1;
Repeat
repeat i  i + 1 until A[i]>=p //left-right scan
repeat j j – 1 until A[j] <= p//right-left scan
if (i < j) //need to continue with the scan
swap(A[i], a[j])
until i >= j //no need to scan
swap(A[l], A[j])
return j

 Advantages in Quick Sort


• It is in-place since it uses only a small auxiliary stack.
• It requires only n log(n) time to sort n items.
• It has an extremely short inner loop
This algorithm has been subjected to a thorough mathematical analysis, a very precise statement
can be made about performance issues.

 Disadvantages in Quick Sort


• It is recursive. Especially if recursion is not available, the implementation is extremely
complicated. • It requires quadratic (i.e., n2) time in the worst-case.

Department of Computer Science and Engineering Page 8


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

• 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)

4. Explain in detail about Travelling Salesman Problem using exhaustive search.

 Given n cities with known distances between each


pair, find the shortest tour that passes
through all the cities exactly once before returning to the starting city.
 Alternatively: Find shortest Hamiltonian circuit in a
weighted connected graph

5. Explain in detail about knapsack problem.

Department of Computer Science and Engineering Page 9


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

6. Explain in detail about closest pair problem.


Find the two closest points in a set of n points (in the two-dimensional Cartesian plane).
Brute-force algorithm
Compute the distance between every pair of distinct points and return the indexes of the points
for which the distance is the smallest.

Department of Computer Science and Engineering Page 10


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

Efficiency: Θ(n^2) multiplications (or sqrt)

7. Explain Assignment problem with example and write its efficiency.


Assignment Problem:
There are n people who need to be assigned to execute n jobs, one person per job. (That
is, each person is assigned to exactly one job and each job is assigned to exactly one person.) The
cost that would accrue if the ith person is assigned to the jth job is a known quantity C[i, j ] for
each pair i, j = 1, 2, . . . , n. The problem is to find an assignment with the minimum total cost.
Assignment problem is a problem of n jobs allocating to n persons with minimum cost.

Since the number of permutations to be considered for the general case of the assignment
problem is n!,

Department of Computer Science and Engineering Page 11


CS8451- DESIGN AND ANALYSIS OF ALGORITHMS IV Semester CSE

Department of Computer Science and Engineering Page 12

You might also like