Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions."— Presentation transcript:

1 1 Divide & Conquer Algorithms

2 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.

3 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

4 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

5 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

6 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)

7 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

8 8 MergeSort (Cont) …199627494139048… 1996274 94139048 199 62749413 9048 19 9627494139048

9 9 MergeSort (Cont) …913194862749094… 9196274 13489094 919 62741394 4890 19 9627494139048

10 10 MergeSort (cont) To sort entire array: MergeSort( A, 1, length(A) ) MergeSort( A, p, r ) 1.if p < r 2.q   (p + r) / 2  3.MergeSort( A, p, q ) 4.MergeSort( A, q+1, r ) 5.Merge( A, p, q, r )

11 11 MergeSort (Cont) Merge( A, p, q, r ) 1.n1  q – p + 1 2.n2  r – q 3.create arrays L[1..n1+1] and R[1..n2+1] 4.for i  1 to n1 5.L[ i ]  A[p+i-1] 6.for j  1 to n2 7.R[ i ]  A[q+j] 8.L[n1+1]   9.R[n2+1]   10.i  1 11.j  1 12.for k  p to r 13.if L[ i ]  R[ j ] 14.A[ k ]  L[ i ] 15.i = i + 1 16.else A[ k ]  R[ j ] 17.j = j + 1 Sentinel values

12 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)

13 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)

14 14 Analysis of MergeSort (cont) So the recurrence is: Note: D(n) + C(n) =  (1) +  (n) =  (n) The solution to the recurrence


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

Similar presentations


Ads by Google