1 divide & conquer algorithms. 2 recursion review a function that calls itself either directly...
TRANSCRIPT
![Page 1: 1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022080914/56649cef5503460f949bdae7/html5/thumbnails/14.jpg)
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