10
Design and Analysis of Algorithms PART II-- Median & Runtime Analysis Recorded by Chandrasekar Vijayarenu and Shridharan Muthu. Date Jan 30 2008 Medians and Order Statistics Minimum and maximum How many comparisons are necessary to determine the minimum of a set of n elements? We can easily obtain an upper bound of n 1 comparisons: examine each element of the set in turn and keep track of the smallest element seen so far. In the following procedure, we assume that the set resides in array A, where length[A]= n. MINIMUM(A) 1 minA[1] 2 for i 2 to length[A] 3 do if min > A[i ] 4 then minA[i ] 5 return min To get the minimum or maximum the operation is linear and hence the order for min and max is O(n). Mean Mean is the average of all the numbers.

QuicksortMedian.pdf

Embed Size (px)

Citation preview

  • Design and Analysis of Algorithms PART II-- Median & Runtime Analysis

    RecordedbyChandrasekarVijayarenuandShridharanMuthu.DateJan302008

    MediansandOrderStatistics

    Minimumandmaximum

    Howmanycomparisonsarenecessarytodeterminetheminimumofasetofn

    elements?Wecaneasilyobtainanupperboundofn1comparisons:examine

    eachelementofthesetinturnandkeeptrackofthesmallestelementseenso

    far.Inthefollowingprocedure,weassumethatthesetresidesinarrayA,where

    length[A]=n.

    MINIMUM(A)

    1minA[1]

    2fori2tolength[A]

    3doifmin>A[i]

    4thenminA[i]

    5returnmin

    TogettheminimumormaximumtheoperationislinearandhencetheorderforminandmaxisO(n).

    Mean

    Meanistheaverageofallthenumbers.

  • =avg(A)

    Median

    Medianisexactlythemiddleoffallnumbers.

    Thisfunctionismorestableandrobustsincethereisnosquarefactorcomparedtomean

    Algorithmsforfindingthemedian.

    1. Simplea. SortA[]

    OrderforsortisO(nlogn)

    b. Median=A[size/2]

    2. Randamizedalgorithma1,a2.an

    a. step1:Order(formgroups)b. step2:Findmedianineachgroupc. step3:findmedianofthemedian(m1,m2,m3,..m5)=Md. step4:DopartitionofAbyM

    LR

    If|L|=n/21thenk=n/2andthemiddletermisthemedian.

    If|L|=n/2thenmedian(L,k)

    Example:

  • ConsiderA[]={2544442555321839987212647199131656241021971}

    Formagroupof5numbers

    25444425

    55321839

    987212647

    199131656

    241021971

    Findthemedianforeachrow

    2544442525

    5532183918

    98721264726

    19913165616

    24102197119

    Findthemedianofthemedians

    Medianof2518261619=19

    ThereforeM=19

    DopartitionofA[]byM

    2544442555321839987212647199131656241021971

  • 254

    24445425

    245542544

    245525445432

    2455184454322539

    2455189543225394487212647

    245518919322539448721264754

    2455189199253944872126475432

    245518919913394487212647543225

    245518919913164487212647543225395624

    24551891991316108721264754322539562444

    245518919913161022126475432253956244487

    2455189199131610219264754322539562444872171

    SinceM=19isthemiddleterm,themedianis19.

    Thisexampleshowsthatiftherearekelements=pivot,thesekelementsdonotnecessarilylocatetogetherinthefinalresults.

  • DateFeb4th2008

    RuntimeanalysisofQuickSort:

    T(n)=T(n/10)+T(9/10n)+cn=O(nlog2n)

    AverageCase:

    Thesplitupofsubarraysintheaveragecasewouldbe

    BestCase:(LUCKY)

    Forthebestcase,thearraywillbesplitexactlyinthemiddlei.e.pivotelementwillbeexactlyinthemiddleofthearray.

    RuntimeAnalysisofMedianFind:

    Letstakeanexample

    Heretheentirearrayisbeingsplitintodatachunksofsize5i.e.n/5

    0 1 2 3 4

    2 54 44 4 255 5 32 18 399 87 21 26 4719 9 13 16 5624 10 2 19 71

    1/10n 9/10n

  • Aftersortingeachandeverycolumn,wewillget

    Intheabovefigure,themiddlerowwhichiscircledisthemedianofthecorrespondingcolumns.

    Nowweneedtofindthemedianofmedians.Sothemiddlerowneedstobesorted.Aftersortingthemiddlerow,themiddleelementwillbethemedianofmedians.

    18isthemedianofmedians.Intheabovefigurethecolumn3andcolumn4areswapped.

    Generallythepartitionwillbelike

    3n/10

  • T(n)=T(1/10n)+T(9/10n)+cnWeneedtoprovethisisoforderO(nlogn)

    2. ForMedianfindingT(n)=T(1/5n)+T(7/10n)+cnWeneedtoprovethisisoforderO(n)

    Recurrence1(QuickSort)T(n)=T(1/10n)+T(9/10n)+cn =T((1/10)2n)+T(9/10*1/10n)+cn/10+ T(1/10*9/10n)+T((9/10)2n)+9cn/10+cn =T((1/10)2n)+2T(9/10*1/10n)+T((9/10)2n)+2cnGeneralizingtheaboveequationweget T((1/10)kn)+(K1)T((1/10)

    k19/10n)+(K2)T((1/10)k2(9/10)2n)+..+(K1)T((1/10)

    2(9/10)k1n)+(K0)T((9/10)

    kn)+kcn

    Theabovecircledfactoristhelargestfactorintheaboveequationbecauseofthefollowingreasons1. (K0)willgivethelargestnumber.2. (9/10)kwillbethelargestfactorintheaboveequation.Thesmallestfactoris(1/10)k.

    WeneedtodeducetheaboveequationtoClosedFormi.e.toT(1)Therefore (9/10)kn=1 n=(10/9)k logn=klog(10/9) k=(logn)/log(10/9)substitutingvalueofkandtakingthelargestfactoroutsideintheaboveequation,weget

    T(n)

  • =O(nlog2n)HenceT(n)=O(nlog2n)

    Recurrence2(MedianFinding)T(n)=T(1/5n)+T(7/10n)+cn =T((1/5)2n)+T(1/5*7/10n)+cn/5 +T(7/10*1/5n)+T((7/10)2n)+7cn/10 +cn =T((1/5)2n)+2T(1/5*7/10n)+T((7/10)2n)+(9/10+1)cn

    =T((1/5)kn)+(K1)T((1/5)k17/10n)+(K2)T((1/5)

    k2(7/10)2n)+..+(K1)T((1/5)2(7/10)k1

    n)+(K0)T((7/10)kn)+[(9/10)k1+(9/10)k2+..+1]cn

    Kterms=[(1(9/10)k+1)/(1(9/10))]cnTodeducetheaboveequationtoClosedForm,weset(7/10)kn=1n=(10/7)k

    Takinglogonbothsideslogn=klog(10/7) k=logn/log(10/7)Substitutingthevalueofkintheaboveequationweget

    T(n)

  • Normallywepartitionthearrayby5i.e.n/5

    Whatifwepartitionthearrayby3i.e.n/3(sortingwillbeeasierfor3elements)n/3

    X X X

    X M X

    X X X

    Therearetotally6halflinesintheabovediagram.2/6n>guaranteedlowerelementsthanY.Theotherhalfis12/6n=4/6n=2/3nSotheruntimeanalysisT(n)

  • Theoreticallyn/7+5/7n=6/7nwhichis