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.

for (int i=2. T (n) =T (1) If n > 1. Algorithm to find min and max element: void straightmaxmin (a[]. max = min = a [1]. It works by comparing a search key K with the arrays middle element A[m]. u (n) =∑kj=1 h (bj) and h (n) = f (n)/nlogba. 3. i++) { . then T (n) = a T (n/a) + f (n) By using substitution method. Computing time of binary search for unsuccessful searches:  È (log n) – Best case . Worst case 6. 4. Complexity of divide and conquer algorithm in recurrence form: Complexity of Divide and Conquer is given by. Average case . min.Keys: 1. i<=n. T (n) = nlogba [T (1) + u (n)] where. If n=1. if they match the algorithm stops. Binary search: Binary search is a remarkably efficient algorithm for searching in a sorted array. Computing time of binary search for successful searches:    È (1) . int n) { int max.Best case È (log n) – Average case È (log n) – Worst case 5. 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]. 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.

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

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

.. int high) /* a [low: high] is a global array to be sorted. int mid = (low+high)/2. } } . high). In this case the list is already Sorted */ { If (low<high) {// If there are more than one element // Divide P into sub problems. // Combine the solutions Merge (low. } } } 9.n-1] sorting each of them recursively and then merging the two smaller sorted arrays into a single sorted one. Algorithm for merge sort: void MergeSort (int low. high). mid.n-1] by dividing it into two halves a[0. Small (p) is true if there is only one element to sort.(n/2)1] and A[n/2.{ //min position is pos1. MergeSort (mid +1. 10. mid). // Solve the sub problems MergeSort (low.. Merge Sort: Merge sort sorts a given array A[0. // Find where to split the set.

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

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. Different containers may have different weights. i<=n. int n) { SolType solution = EMPTY. i++) { Type x= Select (a). Let wi be the ith container. Container loading problem:     A large ship is loaded with cargos. for (int i=1. Greedy method control abstraction for the subset paradigm: SolType Greedy (Type a[]. 1 ≤ i ≤ n. Feasible solution n ∑ wixi ≤ c and xiå {0.1} . if ( Feasible (solution. 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.12. The cargo capacity of the ship is c. } } return solution. 1 ≤i ≤n i=1 x=1 –> container is to be loaded x= 0 -> container is not to be loaded . x). x)) { solution = Union (solution. The cargo is containerized. We have to load the ship with the maximum number of containers. } 14.

. 1 ≤ i ≤ n.Every feasible solution that maximizes the objective function. Knapsack Problem – Greedy Approach. Maximize ∑l ≤ i ≤ n pixi m Subject to ∑l ≤ i ≤n wixi ≤ and 0 ≤ xi ≤ 1. 15.Objective function Maximize n ∑ xi i=1 Optimal Solution .