Download pdf - Daa Summary

Transcript
  • 20/11/2013

    1

    DAASummaryCSE3rd YearGCET

    Syllabus

    UnitI:Introduction:Algorithms,Analyzingalgorithms Complexity of algorithms Growth ofalgorithms,Complexityofalgorithms,Growthoffunctions,Performancemeasurements,SortingandorderStatistics Shellsort,Quicksort,Mergesort,Heapsort,Comparisonofsortingalgorithms,Sortinginlineartime.

    UnitII:AdvancedDataStructures:RedBlacktrees,B trees,BinomialHeaps,FibonacciHeaps.

    11Nov2013 2DAA(ECS502)

  • 20/11/2013

    2

    Syllabus Unit III:DivideandConquerwithexamplessuchasSorting,MatrixMultiplication,ConvexhullandSearching Greedy methods with examples such asSearching.GreedymethodswithexamplessuchasOptimalReliabilityAllocation,Knapsack,MinimumSpanningtrees PrimsandKruskals algorithms,Singlesourceshortestpaths Dijkstras andBellmanFordalgorithms.

    Unit IV:DynamicprogrammingwithexamplessuchK k All i h t t th W h l dasKanpsack,Allpairshortestpaths Warshals and

    Floydsalgorithms,Resourceallocationproblem.Backtracking,BranchandBoundwithexamplessuchasTravellingSalesmanProblem, GraphColoring, nQueenProblem, HamiltonianCyclesandSumofsubsets.

    11Nov2013 3DAA(ECS502)

    Syllabus

    UnitV:SelectedTopics:AlgebraicComputation, FastFourierTransform, StringMatching, TheoryofNPcompleteness,ApproximationalgorithmsandRandomizedalgorithms.

    References:1. ThomasH.Coreman,CharlesE.Leiserson andRonaldL.Rivest,

    IntroductiontoAlgorithms,Printice HallofIndia.2. RCT Lee, SS Tseng, RC Chang and YT Tsai, Introduction to the Design2. RCTLee,SSTseng,RCChangandYTTsai, IntroductiontotheDesign

    andAnalysisofAlgorithms,McGraw Hill,2005.3. E.Horowitz&SSahni,"FundamentalsofComputerAlgorithms",4. Aho,Hopcraft,Ullman,TheDesignandAnalysisofComputer

    AlgorithmsPearson

    11Nov2013 4DAA(ECS502)

  • 20/11/2013

    3

    INSERTIONSORTAlg.: INSERTIONSORT(A)forj 2 ton a8a7a6a5a4a3a2a1

    1 2 3 4 5 6 7 8

    dokeyA[ j ]InsertA[ j ] intothesortedsequenceA[1 . . j -1]i j - 1whilei > 0 andA[i] > key

    d A[i 1] A[i]

    key

    5

    doA[i + 1] A[i]i i 1

    A[i + 1] key Insertionsort sortstheelementsinplace

    MergeSortAlg.: MERGESORT(A, p, r)

    1 2 3 4 5 6 7 8

    62317425

    p rq

    ifp < r Checkforbasecasethenq (p + r)/2 Divide

    MERGESORT(A, p, q) ConquerMERGESORT(A, q + 1, r) Conquer

    (A )

    6

    MERGE(A, p, q, r) Combine

    Initialcall: MERGESORT(A, 1, n)

  • 20/11/2013

    4

    Merge PseudocodeAlg.: MERGE(A,p,q,r)1. Computen1 and n2

    1 2 3 4 5 6 7 8

    63217542

    p rq

    2. Copythefirstn1 elementsinto L[1 . . n1 + 1] andthenextn2 elementsintoR[1 . . n2 + 1]

    3. L[n1 + 1] ; R[n2 + 1] 4. i 1; j 15. fork p tor6 do if L[ i ] R[ j ]

    p q

    7542

    6321rq+1

    L

    R

    n1 n2

    7

    6. doifL[ i ] R[ j ]7. thenA[k] L[ i ]8. i i + 19. elseA[k] R[ j ]10. j j + 1

    RandomizedQuicksortAlg. : RANDOMIZEDQUICKSORT(A, p, r)

    ifp < r

    thenq RANDOMIZEDPARTITION(A, p, r)

    (A )

    8

    RANDOMIZEDQUICKSORT(A, p, q)

    RANDOMIZEDQUICKSORT(A, q + 1, r)

  • 20/11/2013

    5

    RandomizedPARTITION

    Alg.: RANDOMIZEDPARTITION(A, p, r)

    i RANDOM(p, r)

    exchange A[p] A[i]

    9

    exchangeA[p] A[i]

    returnPARTITION(A, p, r)

    PARTITIONAlg.:PARTITION(A,p,r)

    x A[r]i p - 1

    A[pi] x A[i+1j-1] > xp i i+1 rj1 ji p 1

    forj p tor - 1doifA[ j ] x

    theni i + 1exchangeA[i]A[j]

    exchangeA[i + 1]A[r]

    unknown

    pivot

    10

    returni + 1ChoosesthelastelementofthearrayasapivotGrowsasubarray [p..i]ofelementsxGrowsasubarray [i+1..j1]ofelements>xRunningTime:(n),wheren=rp+1

  • 20/11/2013

    6

    Alg: HEAPSORT(A)

    1. BUILDMAXHEAP(A) O(n)2. fori length[A] downto23. doexchangeA[1] A[i]4. MAXHEAPIFY(A, 1, i - 1)

    ( )

    O(lgn)

    n-1 times

    11

    Runningtime:O(nlgn) --- Can be shown to be (nlgn)

    BuildingaHeap ConvertanarrayA[1 n] intoamaxheap(n = length[A]) TheelementsinthesubarrayA[(n/2+1) .. n] areleaves

    Alg: BUILDMAXHEAP(A)1. n =length[A]2. for i n/2 downto 1

    ApplyMAXHEAPIFYonelementsbetween1 andn/2

    1

    4

    3

    1

    2 3

    12

    3. doMAXHEAPIFY(A, i, n) 214 8

    16

    7

    9 104 5 6 7

    8 9 10

    4 1 3 2 16 9 10 14 8 7A:

  • 20/11/2013

    7

    MaintainingtheHeapProperty Assumptions: LeftandRight

    bt f i

    Alg: MAXHEAPIFY(A, i, n)1. lLEFT(i)2 RIGHT(i)subtrees ofi

    aremaxheaps A[i] maybe

    smallerthanitschildren

    2. rRIGHT(i)3. if l n andA[l] > A[i]4. then largest l5. else largest i6. if r n andA[r] > A[largest]7 then largest r

    13

    7. then largest r8. if largest i9. then exchangeA[i] A[largest]10. MAXHEAPIFY(A, largest, n)

    AnalysisofCountingSortAlg.: COUNTINGSORT(A,B,n,k)1. fori 0 tor2 do C[ i ] 0 (r)2. doC[ i ] 03. forj 1 ton4. doC[A[ j ]] C[A[ j ]] + 15. C[i] containsthenumberofelementsequaltoi6. fori 1 tor7. doC[ i ] C[ i ] + C[i -1]

    (n)

    (r)

    14

    [ ] [ ] [ ]8. C[i] containsthenumberofelements i9. forj n downto 110. doB[C[A[ j ]]] A[ j ]11. C[A[ j ]] C[A[ j ]] - 1

    (n)

    Overalltime:(n + r)

  • 20/11/2013

    8

    AnalysisofBucketSortAlg.: BUCKETSORT(A,n)

    for i 1 to nfori 1 tondoinsertA[i]intolistB[nA[i]]

    fori 0 ton - 1dosortlistB[i] withquicksortsort

    B[0] B[1] B[ 1]

    O(n)

    (n)

    15

    concatenatelistsB[0], B[1], . . . , B[n -1]togetherinorderreturntheconcatenatedlists

    O(n)

    (n)

    RBINSERT(T, z)1. y NIL Initializenodesxandy

    Th h t th l ith i t

    26

    17 41

    30 47

    38 50

    2. x root[T]3. whilex NIL4. doy x5. ifkey[z] < key[x]

    Throughoutthealgorithmypointstotheparentofx

    Godownthetreeuntilreachingaleaf Atthatpointyisthe

    16

    6. thenx left[x]7. elsex right[x]8. p[z] y

    p yparentofthenodetobeinserted

    Setstheparentofztobey

  • 20/11/2013

    9

    RBINSERT(T, z)9. ify = NIL10. thenroot[T] z Thetreewasempty:setthenewnodetobetheroot

    26

    17 41

    30 47

    38 50

    11. elseifkey[z] < key[y]12. thenleft[y] z13. elseright[y] z14. left[z] NIL

    Otherwise,setztobetheleftorrightchildofy,dependingonwhethertheinsertednodeissmallerorlargerthanyskey

    17

    15. right[z] NIL16. color[z] RED17. RBINSERTFIXUP(T, z)

    Setthefieldsofthenewlyaddednode

    Fixanyinconsistenciesthatcouldhavebeenintroducedbyaddingthisnewrednode

    RBINSERTFIXUP(T, z)1. whilecolor[p[z]] =RED2. doifp[z] = left[p[p[z]]]

    i h [ [ [ ]]]

    Thewhilelooprepeatsonlywhencase1isexecuted:O(lgn) times

    Set the value of xs uncle3. theny right[p[p[z]]]4. ifcolor[y] = RED5. thenCase16. elseifz = right[p[z]]7. thenCase2

    Setthevalueofx s uncle

    18

    8. Case39. else(sameasthenclausewithright

    andleftexchanged)10. color[root[T]] BLACK Wejustinsertedtheroot,or

    Theredviolationreachedtheroot

  • 20/11/2013

    10

    LEFTROTATE(T,x)1. y right[x] Sety2. right[x] left[y] ysleftsubtreebecomesxsrightsubtree3 if left[y] NIL3. ifleft[y] NIL4. thenp[left[y]] x Settheparentrelationfromleft[y]tox5. p[y] p[x] Theparentofxbecomestheparentofy6. ifp[x] = NIL7. thenroot[T] y8 else if x = left[p[x]]

    19

    8. elseifx = left[p[x]]9. thenleft[p[x]] y10. elseright[p[x]] y11. left[y] x Putxonysleft12. p[x] y ybecomesxsparent

    PRIM(V, E, w, r)1. Q 2. foreachu V3. dokey[u] O(V) ifQisimplementedas

    Totaltime:O(VlgV + ElgV) = O(ElgV)

    y[ ]4. [u]NIL5. INSERT(Q, u)6. DECREASEKEY(Q, r, 0) key[r] 07. whileQ 8. douEXTRACTMIN(Q)

    aminheap

    Executed|V|timesTakesO(lgV)

    Minheapoperations:O(VlgV)

    O(lgV)

    20

    9. foreachv Adj[u]10. doifv Q andw(u, v) < key[v]11. then[v] u12. DECREASEKEY(Q, v, w(u, v))

    ExecutedO(E)timestotalConstant

    TakesO(lgV)O(ElgV)

  • 20/11/2013

    11

    UsingFibonacciHeaps Dependingontheheapimplementation,runningtimecould be improved!couldbeimproved!

    21

    1. A2. foreachvertexv V3. doMAKESET(v)

    KRUSKAL(V, E, w)

    O(V)3. doMAKE SET(v)4. sortEintonondecreasingorderbyw5. foreach(u, v) takenfromthesortedlist6. doifFINDSET(u) FINDSET(v)7. thenAA {(u, v)}8. UNION(u v)

    O(ElgE)

    O(E)

    O(lgV)

    22

    8. UNION(u, v)9. returnARunningtime:O(V+ElgE+ElgV)=O(ElgE) dependentonthe

    implementationofthedisjointsetdatastructure Runningtime:O(V+ElgE+ElgV)=O(ElgE) SinceE=O(V2),wehavelgE=O(2lgV)=O(lgV)

    O(lgV)

    O(ElgV)

  • 20/11/2013

    12

    BELLMANFORD(V, E, w, s)1. INITIALIZESINGLESOURCE(V,s)2. fori1to|V| 1

    (V)O(V)

    O(VE)3. doforeachedge(u,v) E4. doRELAX(u,v,w)5. foreachedge(u,v) E6. doifd[v]>d[u]+w(u,v)7. then return FALSE

    O(E)

    O(E)

    O(VE)

    23

    7. thenreturnFALSE8. returnTRUE

    Runningtime:O(V+VE+E)=O(VE)

    RelaxationStep Relaxinganedge(u,v)=testingwhetherwecan

    improvetheshortestpathtovfoundsofarbygoingthroughu

    d[ ] d[ ] ( ) Ifd[v] > d[u] + w(u, v) wecanimprovetheshortestpathtov d[v]=d[u]+w(u,v)[v] u

    2u v

    2u v

    Afterrelaxation:d[v] d[u] + w(u, v)s s

    24

    5 9

    5 72

    u v

    RELAX(u,v,w)

    5 6

    5 62

    u v

    RELAX(u,v,w)

    nochange

  • 20/11/2013

    13

    Dijkstra(G,w,s)1. INITIALIZESINGLESOURCE(V, s)2. S3 Q V[G]

    (V)

    O(V) build min heap3. QV[G]4. whileQ 5. do uEXTRACTMIN(Q)6. SS {u}7. foreachvertexv Adj[u]

    O(V)buildminheapExecutedO(V)times

    O(lgV)

    O(E)times

    O(VlgV)

    25

    j8. doRELAX(u, v, w)9. UpdateQ(DECREASE_KEY)

    Runningtime:O(VlgV + ElgV) = O(ElgV)

    ( )(total)

    O(lgV)

    O(ElgV)

    Floyd'sAlgorithm:Using2Dmatrices

    Floyd1.DW//initializeD arraytoW[]2.P 0//initializeParrayto[0]3.fork 1ton

    //ComputingDfromD4.dofori 1ton5.doforj 1ton6. if(D[i,j ]>D[i,k ]+ D[k,j ])7 th D[ i j ] D[ i k ] D[ k j ]

    FloydsAlgorithm26

    7. thenD[i,j ] D[i,k ]+ D[k,j ]8. P[i,j ] k;9. elseD[i,j ] D[i,j ]10.MoveDtoD.

  • 20/11/2013

    14

    Printingintermediatenodesonshortestpathfromqtor

    path(index q,r)if (P[q,r]0)

    h( P[ ]) 30 0

    1 2 3

    1path(q,P[q,r])println(v+P[q,r])path(P[q,r],r)return;

    //nointermediatenodeselse return

    BeforecallingpathcheckD[q,r]

  • 20/11/2013

    15

    rightDiagonal[row-col]=!available;if (row< squares-1)

    tQ ( +1)putQueen(row+1);else

    print(" solution found);column[col]=available;leftDiagonal[row+col]=available;rightDiagonal[row-col]= available;

    }}}

    Navestringmatching

    Runningtime:O((nm+1)m).

  • 20/11/2013

    16

    Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

    Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

  • 20/11/2013

    17

    11Nov2013 DAA(ECS502) 33

    11Nov2013 DAA(ECS502) 34