Divide and Conquer Manuela Strinu

Post on 04-Jun-2018

218 views 0 download

Transcript of Divide and Conquer Manuela Strinu

  • 8/13/2019 Divide and Conquer Manuela Strinu

    1/6

    1. Introduction

    In computer science divide and conqueris 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 thesesubproblems 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 isless 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 originalproblem. #!$%%&

    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 si3 ! ('i*!ombine sito 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 ofproblems, such as sorting (e.g., quicksort, merge sort*, multiplying large numbers,

    syntactic analysis, and computing the discrete /ourier transform. #4/%%&

    Divide and ConquerManuela Strinu

    5Politehnica" University of TimisoaraFaculty of Automation and Computers

    Computer and Software Engineering epartment

  • 8/13/2019 Divide and Conquer Manuela Strinu

    2/6

    3.1. Mergesort

    An early divide-and-conquer algorithm that was specifically developed for computers andproperly 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..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 nr*, r*

    4erge (A, p, q, r*

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

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

  • 8/13/2019 Divide and Conquer Manuela Strinu

    3/6

    3.2. Quicksort

    The Duicksort algorithm is based on divide-and-conquer paradigm and is developed inthree 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 eachelement 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 p1rD 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 thesubarray 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 arrayafter choosing the pivot element

    The 'artition procedure selects an element from the sequence and then grows two regionsA#p..i& and A#E..r& such that every element in A#p..i& is less than ; and every element in

  • 8/13/2019 Divide and Conquer Manuela Strinu

    4/6

    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 noparticular 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 totheir 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 boundson 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

  • 8/13/2019 Divide and Conquer Manuela Strinu

    5/6

    - !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:

    >+==

    -*,(*=

  • 8/13/2019 Divide and Conquer Manuela Strinu

    6/6

    $. %e!erences

    #& #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, =%%%.

    #B& #A8G& 7ell ale,Programming in Pascal, 6ones and +artlett, 88G

    #9& #4?%C& /aron 4oller,Analysis of 'uicsort. ! BB=: esigning Algorithms,epartment of !omputer cience, wansea Nniversity,=%%C.