1 1431227-3 file organization and processing week 13 divide and conquer

34
1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

Upload: isaac-fox

Post on 01-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

1

1431227-3File Organization and

ProcessingWeek 13

Divide and Conquer

Page 2: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

2

Divide and Conquer• A Technique for designing algorithm that

decompose instance into smaller subinstances of the same problem

– Solving the sub-instances independently

– Combining the sub-solutions to obtain the solution of the original instance

Page 3: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

3

Divide and Conquer

• Basic Steps:– Divide: the problem into a number of subproblems

that are themselves smaller instances of the same type of problem.

– Conquer: Recursively solving these subproblems. If the subproblems are small enough, solve them straightforward.

– Combine: the solutions to the subproblems into the solution of original problem.

Page 4: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

4

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.

Page 5: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

5

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.

Page 6: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

6

Applications

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

Page 7: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

7

Merge-Sort Review

• Merge-sort on an input sequence S with n elements consists of three steps:– Divide: partition S into

two sequences S1 and S2 of about n2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2 into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S with n

elements, comparator C Output sequence S sorted

according to Cif S.size() > 1

(S1, S2) partition(S, n/2)

mergeSort(S1, C)

mergeSort(S2, C)

S merge(S1, S2)

Page 8: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

8

Merge Sort

• John von Neumann 1945.MERGE-SORT A[1 . . n]

1. If n = 1, done.2. Recursively sort A[ 1 . . n/2 ]

and A[ n/2+1 . . n ] .3. “Merge” the 2 sorted lists.

Key subroutine: “Merge”

Divide

Conquer Combine

Page 9: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

9

Merging two sorted arrays

8

4

2

9

6

3

Page 10: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

10

Merging two sorted arrays

8

4

2

9

6

3

2

Page 11: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

11

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

Page 12: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

12

3

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

Page 13: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

13

3

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

Page 14: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

14

43

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

Page 15: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

15

43

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

8 9

6

Page 16: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

16

43

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

8 9

6

6

Page 17: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

17

43

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

89

6

6

8 9

Page 18: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

18

43

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

89

6

6

8 9

8

Page 19: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

19

43

Merging two sorted arrays

8

4

2

9

6

3

2

8

4

9

6

3

8

4

9

6

89

6

6

8 9

8 9

Time = (n) to merge a total of n elements (linear time).

Page 20: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

20

Analyzing merge sort

MERGE-SORT A[1 . . n]1. If n = 1, done.2. Recursively sort A[ 1 . . n/2 ]

and A[ n/2+1 . . n ] .3. “Merge” the 2 sorted lists

T(n)(1)2T(n/2)

(n)

Page 21: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

21

Recurrence Equation Analysis

• The conquer step of merge-sort consists of merging two sorted sequences, each with n2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b.

• Likewise, the basis case (n < 2) will take at b most steps.• Therefore, if we let T(n) denote the running time of merge-sort:

• We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation.– That is, a solution that has T(n) only on the left-hand side.

2if)2/(2

2if )(

nbnnT

nbnT

Page 22: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

22

Iterative Substitution

• In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern:

• Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n. • So,

• Thus, T(n) is O(n log n).

ibnnT

bnnT

bnnT

bnnT

bnnbnT

bnnTnT

ii

)2/(2

...

4)2/(2

3)2/(2

2)2/(2

))2/())2/(2(2

)2/(2)(

44

33

22

2

nbnbnnT log)(

Page 23: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

23

The Recursion Tree

• Draw the recursion tree for the recurrence relation and look for a pattern:

depth T’s size

0 1 n

1 2 n2

i 2i n2i

… … …

2if)2/(2

2if )(

nbnnT

nbnT

time

bn

bn

bn

Total time = bn + bn log n(last level plus all previous levels)

Page 24: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

24

Recursion tree

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

Page 25: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

25

Recursion tree

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

T(n)

Page 26: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

26

Recursion tree

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

T(n/2) T(n/2)

cn

Page 27: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

27

Recursion tree

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

cn

T(n/4) T(n/4) T(n/4) T(n/4)

cn/2 cn/2

Page 28: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

28

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

Page 29: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

29

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

h = lg n

Page 30: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

30

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

h = lg n

cn

Page 31: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

31

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

h = lg n

cn

cn

Page 32: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

32

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

h = lg n

cn

cn

cn

Page 33: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

33

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

h = lg n

cn

cn

cn

#leaves = n (n)

Page 34: 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer

34

Recursion tree

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

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

(1)

h = lg n

cn

cn

cn

#leaves = n (n)

Total(n lg n)