1 divide & conquer algorithms. 2 recursion review a function that calls itself either directly...

14
1 Divide & Conquer Algorithms

Upload: marshall-wade

Post on 17-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

1

Divide & Conquer Algorithms

Page 2: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

2

Recursion Review

• A function that calls itself either directly or indirectly through another function

• Recursive solutions involve:– Base case – the function returns a solution– Recursive case

• divide the problem into one or more simpler or smaller parts of the problem

• call the function (recursively) on each part, and• combine the solutions of the parts into a solution for the problem.

Page 3: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

3

Designing Algorithms

• Incremental Design– Most of the algorithms you have seen and

programmed– Iterative

• An algorithmic technique where a function solves a problem by repeatedly working on successive parts of the problem

Page 4: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

4

Designing Algorithms (cont)

• Divide & Conquer Design• Three steps

– DIVIDE• Problem is divided into a number of subproblems

– CONQUER• Solve the subproblems recursively• Base cases are simple enough to solve directly

– COMBINE• The solutions to the subproblems are combined to solve the

original problem

Page 5: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

5

Analyzing Divide & Conquer Algorithms

• Use a recurrence• For small subproblem, solution takes constant time• DIVIDE step creates a subproblems, each of size n/b

– D(n) time to divide into subproblems– C(n) time to combine solutions of subproblems

otherwisenCnDaT

cnifnT

bn )()()(

)1()(

Page 6: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

6

MergeSort

• Requires additional memory space as a function of n– Unlike Insertion Sort which sorts in place

• Requires only a constant amount of additional space

• Sorts in (nlgn)

Page 7: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

7

MergeSort

• DIVIDE the n element sequence to be sorted into two subsequences of n/2 elements each

• CONQUER (sort) the two subsequences recursively using merge sort– The recursion stops when subproblem contains only one

element

• COMBINE (merge) the two sorted subsequences to produce the sorted answer

Page 8: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

8

MergeSort (Cont)

… 19 9 62 74 94 13 90 48 …

19 9 62 74 94 13 90 48

19 9 62 74 94 13 90 48

19 9 62 74 94 13 90 48

Page 9: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

9

MergeSort (Cont)

… 9 13 19 48 62 74 90 94 …

9 19 62 74 13 48 90 94

9 19 62 74 13 94 48 90

19 9 62 74 94 13 90 48

Page 10: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

10

MergeSort (cont)

To sort entire array: MergeSort( A, 1, length(A) )

MergeSort( A, p, r )1. if p < r2. q (p + r) / 23. MergeSort( A, p, q )4. MergeSort( A, q+1, r )5. Merge( A, p, q, r )

Page 11: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

11

MergeSort (Cont)Merge( A, p, q, r )1. n1 q – p + 12. n2 r – q3. create arrays L[1..n1+1] and

R[1..n2+1]4. for i 1 to n15. L[ i ] A[p+i-1]6. for j 1 to n27. R[ i ] A[q+j]8. L[n1+1] 9. R[n2+1] 10. i 111. j 112. for k p to r13. if L[ i ] R[ j ]14. A[ k ] L[ i ]15. i = i + 116. else A[ k ] R[ j ]17. j = j + 1

Sentinel values

Page 12: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

12

Analysis of MergeSort

• Merge function:– Line: 1- 2 (1)– Line: 3 (n)– Line: 4 – 7 i loop + j loop = (n)– Line: 8 – 11 (1)– Line: 12 (n)– Line: 13 – 17 (1)

• Total run time = (n)

Page 13: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

13

Analysis of MergeSort (cont)

• MergeSort function– For simplicity, assume n is a power of 2– If n = 1, takes constant time– If n > 1 then

• DIVIDE – Lines 1, 2 (1)– D(n) = (1)

• CONQUER – Lines 3, 4 2T(n/2)– two subproblems, each of size n/2

• COMBINE – Line 5 (n)– C(n) = (n)

Page 14: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions

14

Analysis of MergeSort (cont)

• So the recurrence is:

• Note: D(n) + C(n) = (1) + (n) = (n)

• The solution to the recurrence

1)()(2

1)1()(

2

nifnT

nifnTn

)lg()( nnnT