Anda di halaman 1dari 6

Divide and Conquer

Manuela Strinu

5Politehnica" University of Timisoara


Faculty of Automation and Computers Computer and Software Engineering epartment

1. Introduction
In computer science divide and conquer is an important algorithm design paradigm based on multi-branched recursion. A divide and conquer algorithm works by recursively breaking down a problem into two or more subproblems of the same type, until these subproblems become simple enough to be solved directly. The solutions to the subproblems are then combined to give a solution to the original problem. The divide and conquer approach is cost effective only if the time and effort required to complete the decomposition, solve all of the decomposed problems and then reassemble an answer is less than the cost of solving the problem as it originally stands.

2. The Divide and Conquer Approach


The divide-and-conquer paradigm involves three steps at each level of the recursion: - ivide the problem into sub-problems. - !onquer the sub-problems by solving them recursively. If the sub-problem si"es are small enough, the sub-problem is solved straightforward. - !ombine the solutions of the sub-problems into the solution for the original problem. #!$%%& The pseudo-code of a divide and conquer procedure is:
'rocedure ! (')* +egin ,plit the problem into sub-problems '-. 'k /or i0- to k do If si"e ('i* 1 p then solve 'i (getting si* 2lse si 3 ! ('i* !ombine si to the final solution 2nd

The original problem ' is replaced by a collection of subproblems, each of which is further decomposed into subproblems, until the problems are reduced to being trivial.

3. Applications
The divide-and-conquer paradigm is the basis of efficient algorithms for all kinds of problems, such as sorting (e.g., quicksort, merge sort*, multiplying large numbers, syntactic analysis, and computing the discrete /ourier transform. #4/%%&

3.1. Mergesort An early divide-and-conquer algorithm that was specifically developed for computers and properly analy"ed is the merge sort algorithm, invented by 6ohn von 7eumann in -89:. It operates in the ne;t steps: ivide the n-element sequence to be sorted into two subsequences of n<= elements - !onquer: sort the two subsequences recursively using mergesort. The recursion stops when the length of the sequence to be sorted is -. - !ombine: merge the two sorted subsequences to produce the sorted answer. The key operation of the merge sort algorithm is merging two sorted sequences. In this scope, an au;iliary procedure 4erge(A,p,q,r* is used, where A is an array, p,q,r are integers indicated the indices of the elements in the array such that A#p..q& and A #q>-..r& are in sorted order. After merging, the subarray A #p...r& will also be sorted. The 4erge procedure takes (n*, where n is the number of elements that are merged (n0r-p>-*. The basic divide-and-conquer procedure is 4ege-sort (A, p, r*, which sorts the elements of the array A. If pr, the subarray is already sorted, as it has at most one element. ?therwise, the subarray has more than one element, so the divide step computes the inde; q that divides the sequence A #p...r& into two subarrays: A #p...q& and A #q>-...n&, each of them having n<= elements. #!$%%&
4erge-,ort (A, p, r* If (p1r* Then q 0 (p>r*<= 4erge-,ort (A, p, q* 4erge-,ort (A, q>-, r* 4erge (A, p, q, r*

To sort the entire sequence A 0@A#-&,A#=&A,.,A#n&A, the programmer calls the procedure 4erge-,ort with the arguments (A,-,length#A&*.

/ig. B.-. 2;ample of sorting the elements of the array A 0 @:, =, 9, C, -, B, =, CA

3.2. Quicksort The Duicksort algorithm is based on divide-and-conquer paradigm and is developed in three steps: ivide: The array that has to be sorted A#p..r& is rearranged into two subarrays A#p..q& and A#q>-..r& such that each element of A#p..q& is less or equal to each element of A#q>-..r&. The inde; q is computed in this step. - !onquer: The two subarrays A#p..q& and A#q>-..r& are sorted by recursive calls to the quicksort method. - !ombine: the subarrays are sorted in place, so that the entire array A#p..r& is sorted. #!$%%& The procedure that implements the Duicksort algorithm is the following:
Duicksort (A,p,r* If p1r D 0 partition (A,p,r* Duicksort (A,p,q* Duicksort (A,q>-,r*

The key of the Duicksort algorithm is the 'artition procedure, which rearranges the subarray A#p..r&. The pseudo-code for the partition procedure is the following:
'artition (A,p,r* ; 0 A#p& i 0 p-E 0 r>while true do repeat E0E-- until A#E&; repeat i0i>- until A#i&; if i1E e;change A#i& with A#E& else return E

/ig. B.=. 2;ample of the partition procedure, which rearranges the elements of the array after choosing the pivot element The 'artition procedure selects an element from the sequence and then grows two regions A#p..i& and A#E..r& such that every element in A#p..i& is less than ; and every element in

A#E..r& is greater than ;. The procedure puts elements smaller than ; into the bottom region of the array and elements larger than ; into the top region. The running time of partition on an array A#p..r& is (n*, where n 0 r-p>-. Fandomi"ed versions of quicksort overcome the assumption that all permutations of the input numbers are equally likely. The randomi"ed version has the property that no particular input elicits its worst-case behavior. Instead, its worst case depends on the random-number generator. The changes in the partitioning procedure are:
Fandomi"ed-partition(A,p,r* i 0 random (p,r* e;change A#p& with A#i& return partition(A,p,r*.

4. Algorith

e!!icienc"

The algorithms that contain a recursive call to themselves can be analy"ed according to their running time by a recurrence equation or recurrence. In this manner, the overall running time on n-si"e problem is described, in terms of the running time on smaller inputs. 4athematical tools can then be used to solve the recurrence and provide bounds on the algorithm performance. /or the divide-and-conquer algorithm, the recurrence for running time is based on the three steps of the basic paradigm. $etting T(n* be the running time on a problem of si"e n, if the problem si"e is small enough, the straightforward solution takes constant time (-*. If the problem is divided into a sub-problems, each of which is -<b the si"e of the original, (n* is the time to divide the problems into sub-problems and !(n* the time to combine the solutions to the sub-problems into the solution for the original problem, the obtained recurrence is:
(-*, n c T @nA = aT (n < !* + (n* + C (n*, n > c

4.1. Mergesort e!!icienc" The recurrence for the merge sort algorithm is simplified if the original problem si"e is power of two. At each divide step, the subsequences are e;actly the si"e n<=. The recurrence of the algorithm is set up to T(n*, the worst-case running time of mergesort on n numbers, 4ergesort on one element takes constant time. If the sequence has more than one element, the running time is broken down in the following steps: ivide: the divide step computes the middle of the subarray, which takes constant time, so (n*0(-*. - !onquer: Two subproblems are recursively solved. 2ach of them has the si"e n<=, so the running time is =T(n<=*

!ombine: The 4erge procedure for an n-element sequence takes (n*, so !(n*0(n*. #!$%%& Adding the functions !(n*, (n* represents adding a function that is (n* with a function that is (-*, and the result is a linear function of n, (n*. In conclusion, the time T(n* of mergesort is: (-*, n = T @nA = =T (n < =* + (n*, n > - 4.2. Quicksort e!!icienc" The running time of Duicksort depends on whether the partitioning is balanced or not, and this depends on which elements are used for partitioning. The worst-case behavior for Duicksort occurs when the partitioning routine produces one region with n-- elements and one with - element. The recurrence for the running time is in this case: T(n* 0 T(n--* > (n*. +y iteration, T ( n* = " =- ( " * = ( " * = ( n = *
n " =n

Therefore the worst-case running time of Duicksort is (n=*. The best-case partitioning appears when the two regions are of si"e n<=. In this case, the recurrence is T(n* 0 =T(n<=* > (n*, which has the solution T(n* 0 (nlgn*. Typically, Duicksort is significantly faster in practice than other (nlogn* algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data, it is possible to make design choices which minimi"e the probability of requiring quadratic time. #4?%C&

#. Conclusions
ivide and conquer is a powerful tool for solving conceptually difficult problems: all it requires is a way of breaking the problem into subproblems, of solving the trivial cases and of combining subproblems to the original problem. The divide-and-conquer paradigm often helps in the discovery of efficient algorithms. It was the key, for e;ample, to the quicksort and mergesort algorithms. # A8G& In these e;amples, the divide-andconquer approach led to an improvement in the asymptotic cost of the solution. /or e;ample, if the base cases have constant-bounded si"e, the work of splitting the problem and combining the partial solutions is proportional to the problemHs si"e n, and there are a bounded number p of subproblems of si"e I n<p at each stage, then the cost of the divideand-conquer algorithm will be ?(nlogn*. A big advantage of the divide-and-conquer algorithms are that they are naturally adapted for e;ecution in multi-processor machines, especially shared-memory systems where the communication of data between processors does not need to be planned in advance, because distinct subproblems can be e;ecuted on different processors.

$. %e!erences
#-& #=& #B& #9& #4/%%& 4ichalewic" J., /ogel .+., #ow to Solve $t% Modern #euristics& ,pringer-Kerlag +erlin Leidelberg, =%%%. #!$%%& !ormen T.L., $eiserson !.2., Fivest F.F. $ntroduction to Algorithms, 4IT 'ress, 4cMraw-Lill +ook !ompany, =%%%. # A8G& 7ell ale, Programming in Pascal, 6ones and +artlett, -88G #4?%C& /aron 4oller, Analysis of 'uic"sort. !, BB=: esigning Algorithms, epartment of !omputer ,cience, ,wansea Nniversity,=%%C.

Anda mungkin juga menyukai