Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide & Conquer Algorithms

Similar presentations


Presentation on theme: "Divide & Conquer Algorithms"— Presentation transcript:

1 Divide & Conquer Algorithms
Part 1

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 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 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 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 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 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 MergeSort (Cont) 19 9 62 74 94 13 90 48

9 MergeSort (Cont) 9 13 19 48 62 74 90 94

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

11 MergeSort (Cont) Merge( A, p, q, r ) n1  q – p + 1 n2  r – q
create arrays L[1..n1+1] and R[1..n2+1] for i  1 to n1 L[ i ]  A[p+i-1] for j  1 to n2 R[ i ]  A[q+j] L[n1+1]   R[n2+1]   i  1 j  1 for k  p to r if L[ i ]  R[ j ] A[ k ]  L[ i ] i = i + 1 else A[ k ]  R[ j ] j = j + 1 Sentinel values

12 Analysis of MergeSort Merge function: Total run time = (n)
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 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 Analysis of MergeSort (cont)
So the recurrence is: Note: D(n) + C(n) = (1) + (n) = (n) The solution to the recurrence


Download ppt "Divide & Conquer Algorithms"

Similar presentations


Ads by Google