Presentation is loading. Please wait.

Presentation is loading. Please wait.

分治法.

Similar presentations


Presentation on theme: "分治法."— Presentation transcript:

1 分治法

2 Software College Northeastern University
Divide and Conquer Given an instance of the problem to be solved, split this into several, smaller, sub-instances (of the same problem) independently solve each of the sub-instances and then combine the sub-instance solutions so as to yield a solution for the original instance. Data Structure Software College Northeastern University 2

3 Sort:Most common usage
Break up problem of size n into two equal parts of size n/2. Solve two parts recursively Combine two solutions into overall solution in linear time. 3

4 Sort Obviously application
Sort a list of names. Organize an MP3 library. Display Google PageRank results. List RSS news items in reverse chronological order. Problems become easy once items are in sorted order Find the median. Find the closest pair. Binary search in a database. Identify statistical outliers. Find duplicates in a mailing list. 4

5 Non-obvious applications
Data compression. Computer graphics. Computational biology. Supply chain management. Book recommendations on Amazon. Load balancing on a parallel computer. .... 5

6 Merge Sort John von Neumann 1945. If n = 1, done.
MERGE-SORT A[1 . . n] If n = 1, done. Recursively sort A[ n/2 ] and A[ n/2 n ] . “Merge” the 2 sorted lists. Divide Conquer Combine Key subroutine: “Merge” 6

7 Merging two sorted arrays
8 4 2 9 6 3 7

8 Merging two sortearrays
8 4 2 9 6 3 2 8

9 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 2 9

10 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 3 2 10

11 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 3 2 11

12 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 3 2 4 12

13 Merging two sorted arrays
8 9 6 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 3 2 4 13

14 Merging two sorted arrays
8 9 6 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 3 2 4 6 14

15 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 8 9 6 8 9 2 3 4 6 15

16 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 8 9 6 8 9 2 3 4 6 8 16

17 Merging two sorted arrays
8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 8 9 6 8 9 2 3 4 6 8 9 Time = O(n) to merge a total of n elements (linear time). 17

18 Analyzing merge sort MERGE-SORT A[1 . . n] T(n) O(1) 2T(n/2)
O(n) MERGE-SORT A[1 . . n] If n = 1, done. Recursively sort A[ n/2 ] and A[ n/2 n ] . “Merge” the 2 sorted lists Sloppiness: Should be T( n/2 ) + T( n/2 ) , but it turns out not to matter asymptotically. 18

19 Recurrence for merge sort
T(n) = O(1) if n = 1; 2T(n/2) + O(n) if n > 1. We shall usually omit stating the base case when T(n) = O(1) for sufficiently small n, but only when it has no effect on the asymptotic solution to the recurrence. Master theorem can find a good upper bound on T(n). 19

20 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
20

21 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
21

22 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
22

23 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
23

24 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
O(1) 24

25 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
h = lg n cn/4 cn/4 cn/4 cn/4 O(1) 25

26 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
h = lg n cn/4 cn/4 cn/4 cn/4 O(1) 26

27 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
h = lg n cn/4 cn/4 cn/4 cn/4 O(1) 27

28 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
h = lg n cn/4 cn/4 cn/4 cn/4 cn O(1) 28

29 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
h = lg n cn/4 cn/4 cn/4 cn/4 cn O(1) #leaves = n O(n) 29

30 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
h = lg n cn/4 cn/4 cn/4 cn/4 cn O(1) #leaves = n O(n) Total = Q(n lg n) 30

31 Examples: Fractal Tree
Data Structure Software College Northeastern University 31


Download ppt "分治法."

Similar presentations


Ads by Google