320 likes | 494 Views
Chapter 6 Divide and Conquer. Introduction Binary Search Mergesort The Divide and Conquer Paradigm Quicksort Multiplication of Large Integers Matrix Multiplication The Closest Pair Problem. 6.1 Introduction. Main idea:
E N D
Chapter 6 Divide and Conquer • Introduction • Binary Search • Mergesort • The Divide and Conquer Paradigm • Quicksort • Multiplication of Large Integers • Matrix Multiplication • The Closest Pair Problem
6.1 Introduction Main idea: A divide-and-conquer algorithm divides the problem instance into a number of subinstances, recursively solves each subinstance separately, and then combines the solutions to the subinstances to obtain the solution to the original problem instance.
6.1 Introduction E.g.: The problem of finding both the minimum and maximum in an array of integers A[1..n] and assume for simplicity that n is a power of 2. A straightforward algorithm might look line the one below. 1.X A[1]; y A[1] 2.For I 2 to n 3.If A[i]>y then y A[i] 4.If A[i]<x then x A[i] 5.End for 6.Return (x,y)
6.1 IntroductionAlgorithm 6.1 MINMAX Input:An array A[1..n] of n integers, where n is a power of 2. Output: (x,y): the minimum and maximum integers in A. 1.minmax(1,n) Procedure minmax(low,high) 1. if high-low=1 then 2. if A[low]<A[high] then return(A[low],A[high]) 3. else return(A[high],A[low]) 4. end if 5. else 6. mid 7. (x1,y1) minmax(low,mid) 8. (x2,y2) minmax(mid+1,high) 9. x min{x1,x2} 10. y max{y1,y2} 11. return (x, y) 12. end if
6.1 IntroductionTheorem 6.1 • Given an array A[1..n] of n elements, where n is a power of 2, it is possible to find both the minimum and maximum of the elements in A using only (3n/2)-2 element comparisons.
6.2 Binary Search • Binary search algorithm is one of the divide-and-conquer algorithm.
6.2 Algorithm 6.2 BINARYSEARCHREC Input: An array A[1..n] of n elements sorted in nondecreasing order and an element x. Output: j if x=A[j],1<=j<=n, and 0 otherwise. 1.binarysearch(1,n) Procedure binarysearch(low,high) 1. if low>high then return 0 2. else 3. mid 4. if x=A[mid] then return mid 5. else if x<A[mid] then return binarysearch(low,mid-1) 6. else return binarysearch(mid+1,high) 7. end if
6.2 Theorem 6.2 The number of element comparisons performed by Algorithm BINARYSEARCHREC to search for an element in an array of n elements is at most +1. Thus, the time complexity of Algorithm BINARYSEARCHREC is O(log n).
6.3 MergesortAlgorithm 6.3 MERGESORT Input: An array A[1..n] of n elements. Output: A[1..n] sorted in nondecreasing order. 1.mergesort(A,1,n) Procedure mergesort(low,high) 1. if low<high then 2. mid 3. mergesort(A,low,mid) 4. mergesort(A,mid+1,high) 5. MERGE(A,low,mid,high) 6. end if
6.3 Theorem 6.3 Algorithm MERGESORT sorts an array of n elements in time (n log n) and space (n).
6.4 The Divide-and-conquer Paradigm • In general, a divide-and-conquer algorithm has the following format. • If the size of the instance I is “small”, then solve the problem using a straightforward method and return the answer. Otherwise, continue to the next step. • Divide the instance I into p subinstances I1,I2,…,Ip of approximately the same size. • Recursively call the algorithm on each subinstance Ij, 1<=j<=p, to obtain p partial solutions. • Combine the results of the p partial solutions to obtain the solution to the original instance I. Return the solution of instance I.
6.5 Selection: Finding the Median and the kth Smallest Element • Problem: the median of a sequence of n sorted numbers A[1..n] is the “middle” element. If n is odd, then the middle element is the (n+1)/2 th element in the sequence. If n is even, then there are two middle elements occurring at positions n/2 and n/2+1. in this cases, we will choose the n/2th smallest element. Thus, in both cases, the median is the n/2 th smallest element.
6.5 Algorithm 6.4 SELECT Input:An array A[1..n] of n elements and an integer k, 1 k n. Output: The kth smallest element in A. 1. select(A,1,n,k) Procedure select(A,low,high,k) 1. p high-low+1 2. if p<44 then sort A and return (A[k]) 3. Let q= . Divide A into q groups of 5 elements each. If 5 does not divide p, then discard the remaining elements 4. Sort each of the q groups individually and exact its median. Let the set of medians be M. 5. mm select(M,1,q, ). {mm is the median of medians} 6. Partition A[low..high] into three arrays: A1={a|a<mm} A2={a|a=mm} A3={a|a>mm} 7.case |A1| k: return select(A1,1,|A1|,k) |A1|+|A2| k: return mm |A1|+|A2|<k: return select(A3,1,|A3|,k-|A1|-|A2|) 8. end case
6.5 Theorem 6.4 The kth smallest element in a set of n elements drawn from a linearly ordered set can be found in (n) time. In particular, the median of n elements can be found in (n) time.
6.6 QuicksortAlgorithm 6.5 SPLIT Input: An array of elements A[low..high]. Output: (1) A with its elements rearranged, if necessary, as described above. (2) w,the new position of the splitting element A[low]. 1. i=low 2. x=A[low] 3. for j=low+1 to high 4. if A[j]<=x then 5. i=i+1 6. if i/=j then interchange A[i] and A[j] 7. end if 8. end for 9. interchange A[low] and A[i] 10. w=i 11. Return A and w
6.6 QuicksortAlgorithm 6.6 QUICKSORT Input: An array A[1..n] of n elements. Output: The elements in A sorted in nondecreasing order. 1.quicksort(A,1,n) Procedure quicksort(A,low,high) 1.if low<high then 2. SPLIT(A[low..high],w) {w is the new position of A[low]} 3. quicksort(A,low,w-1) 4. quicksort(A,w+1,high) 5. end if
6.6 Theorem 6.5 • The running time of Algorithm QUICKSORT is in the worst case. If, however, the median is always chosen as the pivot, then its time Complexity is (n log n).
6.6 Theorem 6.6 • The average number of comparisons performed by Algorithm QUICKSORT to sort an array of n elements is (n log n).
6.7 Multiplication of Large Integers • Let u and v be two n-bit integers. The traditional multiplication algorithm requires (n2) digit multiplications to compute the product of u and v. Using the divide and conquer technique, the bound can be reduced significantly.
6.7 W X Y Z
6.8 Matrix Multiplication • Let A and B to be two n*n matrices. We with to compute their product C=AB. 6.8.1 The traditional algorithm:
6.8 Matrix Multiplication 6.8.2 Recursive version Assume n=2k, k>=0.
6.8 Matrix Multiplication 6.8.3 Strassen’s algorithm Assume n=2k, k>=0.
6.8 Matrix Multiplication 6.8.3 Strassen’s algorithm
6.8 Matrix Multiplication 6.8.3 Strassen’s algorithm
6.9 The Closest Pair Problem • Let S be a set of n points in the plane. In this section, we consider the problem of finding a pair of points p and q in S whose mutual distance is minimum. In other words, we want to find two points p1=(x1,y1) and p2=(x2,y2) in S with the property that the distance between them defined by Is minimum among all pairs of points in S.
6.9 Algorithm 6.7 CLOSESTPAIR Input: A set S of n points in the plane. Output: The minimum separation realized by two points in S. 1.Sort The points in S in nondecreasing order of their x-coordinates. 2.Y The points in S sorted in nondecreasing order of their y-coordinates. 3. cp(1,n)
6.9 Procedure cp(low,high) • If high-low+1 3 then compute by a straightforward method. 2. else • mid • x(S[mid]) • cp(low,mid) • cp(mid+1,high) • min{ , } • k 0 • for i 1 to n {Extract T from Y} • if |x(Y[i])- | then • k k+1 • T[k] Y[i] • end if • end for {k is the size of T} • 2 {Initialize to any number greater than } • for i 1 to k-1 {compute } • for j i+1 to min{i+7,k} • if d(T[i],T[j])< then d(T[i],T[j]) • end for • end for • min{ , } • End if • Return
6.9 • Observation 6.5 Each point in T needs to be compared with at most seven points in T.
6.9 Theorem 6.7 • Given a set S of n points in the plane, Algorithm CLOSESTPAIR finds a pair of points in S with minimum separation in ⊙ (n log n) time.