Presentation is loading. Please wait.

Presentation is loading. Please wait.

DIVIDE AND CONQUER.

Similar presentations


Presentation on theme: "DIVIDE AND CONQUER."— Presentation transcript:

1 DIVIDE AND CONQUER

2 QUESTION - 1

3 Unimodal Search An array A[1 . .n] is unimodal if it consists of an increasing sequence followed by a decreasing sequence, or more precisely, if there is an index m ∈ {1, 2, . . ., n} such that A[i] < A[i + 1] for all 1 ≤ i < m, and A[i] > A[i + 1] for all m ≤ i < n. In particular, A[m] is the maximum element, and it is the unique “locally maximum” element surrounded by smaller elements (A[m − 1] and A[m + 1]).

4 Unimodal Search (Contd..)
Give an algorithm to compute the maximum element of a unimodal input array A[1 . .n] in O(lg n) time. Provide the bound on its running time.

5 Unimodal Search (Contd..)
Hint 1: Similar to Binary Search

6 Unimodal Search (Contd..)
Hint 2: Notice that by the definition of unimodal arrays, for each 1 ≤ i < n either A[i] < A[i + 1] or A[i] > A[i + 1]. The main idea is to distinguish these two cases: 1. By the definition of unimodal arrays, if A[i] < A[i + 1], then the maximum element of A[1..n] occurs in A[i + 1..n]. 2. In a similar way, if A[i] > A[i + 1], then the maximum element of A[1..n] occurs in A[1..i].

7 Unimodal Search (Contd..)
Solution: This leads to the following divide and conquer solution (note its resemblance to binary search): 1 a, b ← 1, n 2 while a < b 3 do mid ← ⌊(a + b)/2⌋ 4 if A[mid] < A[mid + 1] then a ← mid if A[mid] > A[mid + 1] then b ← mid 8 return A[a] The divide and conquer approach leads to a running time of T(n) = T(n/2)+Θ(1) = Θ(lg n).

8 Unimodal Search (Contd..)
Convex Polygon : A polygon is convex if all of its internal angles are less than 180◦ (and none of the edges cross each other). We represent a convex polygon as an array V [1 . .n] where each element of the array represents a vertex of the polygon in the form of a coordinate pair (x, y). We are told that V [1] is the vertex with the minimum x coordinate and that the vertices V [1..n] are ordered counterclockwise. You may also assume that the x coordinates of the vertices are all distinct, as are the y coordinates of the vertices.

9 Convex Polygon

10 Unimodal Search (Contd..)
b) Give an algorithm to find the vertex with the maximum x coordinate in O(lg n) time.

11 Unimodal Search (Contd..)
Solution: Notice that the x-coordinates of the vertices form a unimodal array and we can use part (a) to find the vertex with the maximum x-coordinate in Θ(lg n) time.

12 Unimodal Search (Contd..)
c) Give an algorithm to find the vertex with the maximum y coordinate in O(lg n) time.

13 Unimodal Search (Contd..)
Solution: After finding the vertex V [max] with the maximum x-coordinate, notice that the y-coordinates in V [max], V [max + 1], . . ., V [n − 1], V [n], V [1] form a unimodal array and the maximum y-coordinate of V [1..n] lies in this array. Again part (a) can be used to find the vertex with the maximum y-coordinate. The total running time is Θ(lg n).

14 QUESTION - 2

15 Median Of Sorted Arrays
There are two arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. Array of length 2n). The complexity should be O(log(n)).

16 Median Of Sorted Arrays (Contd..)
Hint : This method works by first getting medians of the two sorted arrays and then comparing them.

17 Median Of Sorted Arrays (Contd..)
1) Calculate the medians m1 and m2 of the input arrays arr1[] and ar2[] respectively. 2) If (m1 == m2) return m1 (or m2) 3) If ( m1 > m2 ) a) From first element of arr1 to m1 (arr1[0..n/2]) b) From m2 to last element of ar2 (arr2[n/2..n-1]) 4) If (m2 > m1) a) From m1 to last element of ar1 (arr1[n/2..n-1]) b) From first element of ar2 to m2 (arr2[0..n/2]) 5) Repeat the above process until size of both the subarrays becomes 2. 6) If size of the two arrays is 2 then use below formula to get the median Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2 Time Complexity: O(logn)

18 QUESTION - 3

19 MAXMIN Design and analyze a divide and conquer MAXMIN algorithm that uses ⌈3n/2⌉-2 comparisons for any n. Find the maximum and the minimum element -- MAXMIN

20 MAXMIN Solution Hint: Suppose we knew the maximum and minimum element in both of the roughly n/2 sized partitions of an n-element (n ≥ 2) list. Then in order to find the maximum and minimum element of the entire list we simply need to see which of the two maximum elements is the larger, and which of the two minimums is the smaller. We assume that in a 1-element list the sole element is both the maximum and the minimum element.

21 procedure maxmin(A[1...n] of numbers) -> (min, max) 1 begin 2 if (n == 1) 3 return (A[1], A[1]) 4 else if (n == 2) 5 if( A[1] < A[2]) 6 return (A[1], A[2]) 7 else 8 return (A[2], A[1]) 9 else 10 (max_left, min_left) = maxmin(A[1...(n/2)]) 11 (max_right, min_right) = maxmin(A[(n/2 +1)...n]) 12 if (max_left < max_right) 13 max = max_right 14 else 15 max = max_left 16 if (min_left < min_right) 17 min = min_left 18 else 19 min = min_right 20 return (min, max) 21 end MAXMIN Solution

22 MAXMIN Solution(Contd..)
Let T(n) be the number of comparisons performed by the maxmin procedure. When n = 1 clearly there are no comparisons. Thus we have T(1) = 0. Similarly, T(2) = 1. Otherwise when n > 2 clearly T(n) = 2T(n/2) since maxmin performs two recursive calls on partitions of roughly half of the total size of the list and then makes two further comparisons to sort out the max/min for the entire list.

23


Download ppt "DIVIDE AND CONQUER."

Similar presentations


Ads by Google