Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 4 Divide-and-Conquer What’s Self-Reducibility? • A problem can be reduced to several “same” problems with smaller inputs. Divide and Conquer • Divide the problem into subproblems. • Conquer the subproblems by solving them recursively. • Combine the solutions to subproblems into the solution for original problem. Tree Structure problem subproblem subsubproblem subproblem subsubproblem subsubproblem subsubproblem Insertion Sort 5,2,4,6,1,3 5,2,4,6,1 3 5,2,4,6 1 5,2,4 6 5,2 5 4 2 key 5, 2, 4, 6, 1, 3 2, 5, 4, 6, 1, 3 2, 5, 4, 6, 1, 3 2, 4, 5, 6, 1, 3 2, 4, 5, 6, 1, 3 2, 4, 5, 6, 1 , 3 2, 4, 5, 1 , 6, 3 2, 4, 1 , 5, 6, 3 2, 1 , 4, 5, 6, 3 1 , 2, 4, 5, 6, 3 1, 2, 4, 5, 6, 3 1, 2, 4, 5, 3, 6 1, 2, 4, 3, 5, 6 1, 2, 3, 4, 5, 6 Merge Sort Let Merge - Sort ( A, p, r ) be a procedure which sorts elements in subarray A[ p...r ]. Main Program begin Merge - Sort ( A,1, n); end Procedure Merge - Sort ( A, p, r ) if p r then begin q ( p r ) / 2; Merge - Sort ( A, p, q ); Merge - Sort ( A, q 1, r ); Merge ( A, p, q, r ); end - then. divide conquer combine Heapsort 16 14 7 8 2 10 4 1 9 3 Heapsort 16,14,10,8,7,9,3,2,4,1 1,14,10,8,7,9,3,2,4 16 Heapsort 16,14,10,8,7,9,3,2,4,1 14,8,10,4,7,9,3,2,1 16 Quicksort Quicksort ( A, p, r ) if p r the n begin q Partition ( A, p, r ); divide Quicksort ( A, p, q 1); conquer Quicksort ( A, q 1, r ); end - then combine is simplified Selection with O(n) Comparisons Algorithm Select : 1. Divide n elements into n/5 groups of 5 elements, possibly except one of less than 5 elements. divide 2. Find the median of each group by insertion sort. In case, the exceptiona l one has two medians, take the larger one. 3. Use Select recursivel y to find the median x of n/5 group medians. conquer 4. Exchange x with the last element in input array and apply Partition subroutine . Let k be the number of elements on the low side of the partition. 5. Use Select recursivel y to find the ith smallest element on the low side if i k , or the (i k ) th smallest element on the high side if i k . conquer Remark on Divide and Conquer Key Point: 1. Self - reducibili ty!!! 2. A recursive tree structure exists between problem and subproblem s. Merge Sort Quick Sort T (n) 2T (n / 2) (n) 1 E[T (n)] (E[T (0)] E[T (n 1)] c1n) n 1 (E[T (1)] E[T (n 2)] c1n) n 1 (E[T (n 1)] E[T (0)] c1n) n Selection T (n) T (n / 5) T (7n / 10 3) O(n) Analysis requires to solve a recurrence! Maximum Subarray Problem Given a sequence of integers a1 , a2 ,..., an , find a consecutuv e subsequenc e ai , ai 1 ,..., a j to maximize the sume ai ai 1 a j . Max - Subarray( A,low, hig h) begin Max - Subarray( low,mid ); Max - Subarray( mid 1,high ); Find - Max - Crossing - Subarray( A,low,mid,high ); return the max one among above three subsequenc es; end. Find Max Cross Subarray can be done in O(n) time. Therefore, T (n) 2T ( n / 2) O(n) T (n) O(n log n). Closest pair of points Given n points in the Euclidean plane, find a pair of points to minimize the distance between them. Divide all points into two half parts based on x coordinate s. Find the closest pair of points in each part. Suppose 1 and 2 are distances of closest pairs in two parts, respective ly. Let min( 1 , 2 ). Matrix Multiplication C AB Step 1 C11 C12 A11 C21 C22 A21 A12 B11 A22 B21 B12 B22 n/2 n/2 Step 2 Step 3 S1 B12 B22 S 6 B11 B22 S 2 A11 A12 S 7 A12 A22 S3 A21 A22 S8 B21 B22 S 4 B21 B11 S9 A11 A21 S5 A11 A22 S10 B11 B12 P1 A11 S1 P5 S5 S 6 P2 S 2 B22 P6 S 7 S8 P3 S3 B11 P7 S9 S10 P4 A22 S 4 ( n ) 2 7 T ( n / 2) Step 4 C11 P5 P4 P2 P6 C12 P1 P2 C21 P3 P4 C22 P5 P1 P3 P7 if n 1, (1) T ( n) 2 7 T ( n / 2 ) ( n ) if n 1. By Master Theorem, T (n) O(n log2 7 ). ( n ) 2 Master Theorem Let T (n) aT (n / b) f (n) where constants a 1, b 1, and n / b means n / b or n / b . If f (n) O(n logb a ) for some constant 0, then T (n) (n logb a ). If f (n) (n logb a ), then T (n) (n logb a lg n). If f (n) (n logb a ) for some constant 0, and if af (n / b) cf (n) for a constant c 1 and sufficient ly large n, then T (n) ( f (n)).