Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43

Similar presentations


Presentation on theme: "Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43"— Presentation transcript:

1 Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43 Prepared By: Shruthi N Department: CSE Date: 5/25/2018

2 Divide-and-Conquer The most-well known algorithm design strategy:
2 Divide-and-Conquer The most-well known algorithm design strategy: Divide instance of problem into two or more smaller instances. Solve smaller instances recursively. Obtain solution to original (larger) instance by combining these solutions.

3 Divide-and-Conquer Technique (cont.)
3 Divide-and-Conquer Technique (cont.) a problem of size n subproblem 1 of size n/2 subproblem 2 of size n/2 a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem It general leads to a recursive algorithm!

4 Divide-and-Conquer Examples
4 Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary tree traversals Binary search (?) Defective chess board

5 Divide-and-Conquer Recurrence
5 Divide-and-Conquer Recurrence Let T(n) be a monotonically increasing function that satisfies T(n) = a T(n/b) + f(n) T(1) = c where a  1, b  2, c>0. If f(n) is (nd) where d  0 then if a < bd T(n) = If a = bd if a > bd

6 6 Mergesort Split array A[0..n-1] into about equal halves and make copies of each half in arrays B and C Sort arrays B and C recursively Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: compare the first elements in the remaining unprocessed portions of the arrays copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.

7 Pseudocode of Mergesort
7 Pseudocode of Mergesort

8 8 Pseudocode of Merge Time complexity: Θ(p+q) = Θ(n) comparisons

9 Mergesort Example 12/8/13

10 Analysis of Mergesort All cases have same efficiency: Θ(n log n)
1010 even if not analyzing in detail, show the recurrence for mergesort in worst case: T(n) = 2 T(n/2) + (n-1) worst case comparisons for merge Analysis of Mergesort All cases have same efficiency: Θ(n log n) Number of comparisons in the worst case is close to theoretical minimum for comparison-based sorting: . Space requirement: Θ(n) (not in-place) Can be implemented without recursion (bottom-up) T(n) = 2T(n/2) + Θ(n), T(1) = 0

11 1111 Quicksort Select a pivot (partitioning element) – here, the first element. Rearrange the list so that all the elements in the first ‘s’ positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot Exchange the pivot with the last element in the first subarray — the pivot is now in its final position. Sort the two subarrays recursively.

12 Partitioning Algorithm
1212 Partitioning Algorithm or i > r or j = l < Time complexity: Θ(r-l) comparisons

13 1313 Quicksort Example

14 Analysis of Quicksort Best case: split in the middle — Θ(n log n)
1414 Analysis of Quicksort Best case: split in the middle — Θ(n log n) Worst case: sorted array! — Θ(n2) Average case: random arrays — Θ(n log n) Improvements: better pivot selection: median of three partitioning switch to insertion sort on small subfiles elimination of recursion These combine to 20-25% improvement Considered the method of choice for internal sorting of large files (n ≥ 10000)

15 1515 Binary Search public static int binarySearch(int [ ] list, int listLength, int key) { int first = 0, last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == key) found = true; else if(list[mid] > key) last = mid - 1; first = mid + 1; } if (found) return mid; return –1; } //end binarySearch

16 Analysis of Binary Search
1616 Analysis of Binary Search Time efficiency: This is VERY fast. Optimal for searching a sorted array Limitations: Must be a sorted array (not linked list) Bad (degenerate) example of divide-and-conquer because only one of the sub-instances is solved Has a continuous counterpart called bisection method for solving equations in one unknown f(x) = 0

17 Defective chess board The total idea of chessboard coverage algorithm based on the divide-and-conquer strategy in the teaching materials is to divide the chessboard into four sub-chessboards first, and then respectively cover these four sub- chessboards. 12/8/13

18 Contd.. For each sub-chessboard, the program will first judge whether the special pane is in this sub- chessboard. If it is in the sub-chessboard, the program will recursively transfer the program to cover this sub-chessboard, or else, cover the neighboring panes with other three sub- chessboards, and then recursively transfer the program to cover this sub-chessboard. 12/8/13

19 1919 Assignment questions 1. What is brute-force method? Write a brute-force string matching algorithm. Analyze its complexity. 2. Write the quick sort algorithm. Analyze its efficiency. Apply the algorithm to sort the list 4, 1, 6, 3, 9, 2, 7, 5 . 3. Using quick sort algorithm. Arrange the letters of the word a” EXAMPLE” in alphabetical order . 4. Using quick sort algorithm. Arrange the letters of the word a” QUESTION” in alphabetical order. 5. Write the algorithm for binary search and find the average case efficiency.

20 1-2020 2020 Contd.. 6. Discuss the merge sort algorithm with recursive tree and its efficiency. Apply the same algorithm to sort the list {4,6,1,3,9,5} 7. Using bubble sort algorithm. Arrange the letters of the word a” QUESTION” in alphabetical order 8. Using bubble sort algorithm. Arrange the letters of the word a” EXAMPLE” in alphabetical order 9. Give the general divide and conquer recurrence and explain the same. Give the master‟s theorem. 10. What is divide-and conquer technique? Explain the concept of divide and conquer methodology indicating three major variations.

21 Thank You..


Download ppt "Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43"

Similar presentations


Ads by Google