So, T(n) = 3T(n/2) + n, which implies T(n) is O(nlog23), by the Master Theorem.Observe that there is a different way to multiply parts:.Divide-and-ConquerĪn Improved Integer Multiplication Algorithm But that is no better than the algorithm we learned in grade school.Closest-Pair Problem by Divide-and-Conquer Step 1 Divide the points given into two subsets Pl and Pr by a vertical line x m so. Algorithms with better asymptotic efficiency are known but they are even more complex. We can then define I*J by multiplying the parts and adding: Number of multiplications: M (n) 7M (n/2), M (1) 1 Solution: M (n) 7log 2n nlog 27 n2.807 vs.Divide step: Split I and J into high-order and low-order bits.Algorithm: Multiply two n-bit integers I and J.The summation is a geometric series Divide-and-Conquer.Each part of the summation is equally dominant.We then distinguish the three cases as.Using iterative substitution, let us see if we can find a pattern: Introduce the divide-and-conquer approach to the design of algorithms and use it to develop an algorithm called merge sort.Example: (heap construction) Solution: logba=1, so case 1 says T(n) is O(n).Example: (binary search) Solution: logba=0, so case 2 says T(n) is O(log n).Example: Solution: logba=2, so case 3 says T(n) is O(n3).Example: Solution: logba=3, so case 1 says T(n) is O(n3).Example: Solution: logba=0, so case 3 says T(n) is O(n logn).Example: Solution: logba=1, so case 2 says T(n) is O(n log2 n).Example: Solution: logba=2, so case 1 says T(n) is O(n2).Many divide-and-conquer recurrence equations have the form:.In general, to use this method, you need to have a good guess and you need to be good at induction proofs. The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b.according to C ifS.size() > 1 (S1, S2)partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) Smerge(S1, S2) Divide-and-Conquer.Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1and S2 of about n/2 elements each Recur: recursively sort S1and S2 Conquer: merge S1and S2 into a unique sorted sequence Merge-Sort Review AlgorithmmergeSort(S, C) Inputsequence S with n elements, comparator C Outputsequence S sorted 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conquer Divide-and-Conquerĭivide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two or more disjoint subsets S1, S2, … Recur: solve the subproblems recursively Conquer: combine the solutions for S1,S2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations Divide-and-Conquer Divide-and-Conquer
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |