You are on page 1of 8

# Department of Computer Science and Engineering

CS41 - Design and Analysis of Algorithm Assignment Questions Unit II 1. What is divide and conquer strategy? 2. Give the complexity of divide and conquer algorithm in recurrence form? 3. Define Binary search. 4. Describe the computing time of binary search for successful searches. 5. Describe the computing time of binary search for unsuccessful searches. 6. Write the straight forward algorithm to find max and min elements in array of n elements. 7. Design an algorithm to find the position of maximum element among N numbers using divide and conquer. 8. Design an algorithm to find the minimum element among N numbers using divide and conquer. 9. Define merge sort. 10. Write algorithm for merge sort. 11. Write algorithm for merging two sorted arrays. 12. Give the computing time of merge sort in recurrence relation. 13. Write the greedy method control abstraction for the subset paradigm. 14. What is container loading problem? 15. State the Knapsack problem using Greedy approach.

Keys: 1. Divide and conquer strategy: divide problem into sub problems recursively solve sub problems combine solutions to sub problems to get solution to original problem

2. Complexity of divide and conquer algorithm in recurrence form: Complexity of Divide and Conquer is given by, If n=1; T (n) =T (1) If n > 1, then T (n) = a T (n/a) + f (n) By using substitution method, T (n) = nlogba [T (1) + u (n)] where, u (n) =kj=1 h (bj) and h (n) = f (n)/nlogba. 3. Binary search: Binary search is a remarkably efficient algorithm for searching in a sorted array. It works by comparing a search key K with the arrays middle element A[m]. if they match the algorithm stops; otherwise the same operation is repeated recursively for the first half of the array if K < A[m] and the second half if K > A[m]. 4. Computing time of binary search for successful searches: (1) - Best case (log n) Average case (log n) Worst case

5. Computing time of binary search for unsuccessful searches: (log n) Best case , Average case , Worst case

6. Algorithm to find min and max element: void straightmaxmin (a[], int n) { int max, min; max = min = a [1]; for (int i=2; i<=n; i++) {

if (a [i] > max) { max = a [i]; } if (a [i] < min) { min = a [i]; } }

7. Algorithm to find the position of maximum element among N numbers using divide and conquer: void maxposition(int i, int j) //a[ 1 : n ] is a global array. Parameters i & j are integers, 1 <= i <= j <= n. { int pos1, pos2, max1,max; if (i == j) { max = a [i]; //max position is i; } else if (i == j-1) { if (a [i] < a [j]) { max = a [j]; pos1=j; } else max = a [i]; pos1=i; } else { int mid = (i + j)/2; maxpos (i, mid, max); max1=max; pos2=pos1; maxpos ( mid+1, j, max1); if (max < max1) { max = max1; pos1=pos2;

//max position is pos1; } else { //max position is pos1; } } } 8. Algorithm to find the minimum element among N numbers using divide and conquer: void minposition(int i, int j) //a[ 1 : n ] is a global array. Parameters i & j are integers, 1 <= i <= j <= n. { int pos1, pos2, min1,min; if (i == j) { min = a [i]; //min position is i; } else if (i == j-1) { if (a [i] > a [j]) { min = a [j]; pos1=j; } else min = a [i]; pos1=i; } else { int mid = (i + j)/2; minpos (i, mid, max); min1=min; pos2=pos1; minpos ( mid+1, j, max1); if (min > min1) { min = min1; pos1=pos2; //min position is pos1; } else

## { //min position is pos1; } } }

9. Merge Sort: Merge sort 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. 10. Algorithm for merge sort: void MergeSort (int low, int high) /* a [low: high] is a global array to be sorted. Small (p) is true if there is only one element to sort. In this case the list is already Sorted */ { If (low<high) {// If there are more than one element // Divide P into sub problems. // Find where to split the set. int mid = (low+high)/2; // Solve the sub problems MergeSort (low, mid); MergeSort (mid +1, high); // Combine the solutions Merge (low, mid, high); } }

11. Algorithm to merge two sorted array: void Merge (int low, int mid, int high) /* a[low: high] is a global array containing two sorted subsets in a[low: high] and in a [mid +1: high]. The goal is to merge these two sets into a single set residing in a a[low: high]. b [] is an auxiliary global array. */ { int h = low, i= low, j= mid+1, k; while (( h<= mid) && (j<=high)) { if (a[h] <= a[j]) { b[i] = a[h]; h++; } else { b[i] = a[j]; j++; } } if ( h > mid) { for ( k=j; k<=high; k++) { b[i] = a[k]; i++; } } else { for ( k=h; k<=mid; k++) { b[i]=a[k]; i++; } } {

## for (k=low; k<=high; k++) a[k] = b[k]; }}

12. Computing time of merge sort in recurrence relation: Let T (n) be worst case time on a sequence of n keys If n = 1, then T (n) = f (1) (constant) If n > 1, then T (n) = 2 T (n/2) + f (n) Two sub problems of size n/2 each that are solved recursively f (n) time to do the merge

T (n) = O (n log n) 13. Greedy method control abstraction for the subset paradigm: SolType Greedy (Type a[], int n) { SolType solution = EMPTY; for (int i=1; i<=n; i++) { Type x= Select (a); if ( Feasible (solution, x)) {

solution = Union (solution, x); } } return solution; } 14. Container loading problem: A large ship is loaded with cargos. The cargo is containerized. Different containers may have different weights. Let wi be the ith container, 1 i n. The cargo capacity of the ship is c. We have to load the ship with the maximum number of containers.

Feasible solution n wixi c and xi {0,1} , 1 i n i=1 x=1 > container is to be loaded x= 0 -> container is not to be loaded

Objective function Maximize n xi i=1 Optimal Solution - Every feasible solution that maximizes the objective function. 15. Knapsack Problem Greedy Approach. Maximize l i n

pixi m