CSCI2100A

download CSCI2100A

of 2

Transcript of CSCI2100A

  • 8/17/2019 CSCI2100A

    1/2

    CourseCode: CSCI2100AFinalExamination Page

    1 f

    2

    M . . I f ~ , r ~

    r ~ J f t l l ~ p

    -j- $ t :

    Copyright Reserved

    The Chinese University of Hong Kong

    CourseExaminationsSecondTerm,2014-2015

    CSCI2100AFinalExamination

    CourseCode

    &

    Title

    2

    00 .

    Timeallowed

    ...........................hours

    ...........................minutes

    StudentLD.No. SeatNo. :

    ...........................

    1. Hashing(20)

    Giventheinput{84,12,48,16,38,108,36,60,72,63} andahashfunctionh x) = x mod 10, show

    whathappenswheneachelementinthelistisinsertedintothehashtableof size10 usingthefollowing

    collisionpolicy.

    (a) (4)Open hashtable.Explainwhatmethod

    of

    insertionyouareusing.

    (b) (4)Closedhashtableusinglinearprobing(assumingthattheincrementis

    1 .

    (c) (4)Closedhashtablewithsecondhashfunctionh2 X)

    =

    3- x mod 3.

    (d) (4)

    What

    aretheadvantagesanddisadvantages

    of

    thevariouscollisionstrategiesinthefirsttwo

    questions?Justifyyouranswer.

    (e) (4)Whichhashfunctionisabetterhashfunctionfortheabovesequence :

    1) i x) = x

    mod

    12

    or

    (2)

    h2 X)

    =

    x mod11? Justify youranswer. Note thatthe sizeof hashtablewillchange

    accordingtohashfunction.

    2. Graph (27)

    (a) (3)Drawthecorrespondingadjacencymatrixforthegraph.

    (b) (3)Pleaselistthedepth-firstsearchsequencestartedfromvertex

    D

    (c) (3)Pleaselistthebreath-firstsearchsequencestartedfromvertex

    A.

    (d) (6)Findtheshortestweighedpath fromvertex  to

    G

    Illustrateintermediatestepswithatable

    orfigures.

  • 8/17/2019 CSCI2100A

    2/2

    Course

    Code:

    CSCI 2100A Final Examination Page 2 o 2

    (e) (8)UseKruskal's algorithm to find the minimumcostspanning treeof thegraph. Show the

    intermediatesteps

    of

    the algorithmand the final resultof theminimumcostspanningtreethat

    youhaveobtained.

    (f) (4)Findthemaximumflow in thenetworkaboveassumingthat the sourceisnodeA andthe

    sinkisnodeG.

    No

    intermediatestepsarerequired.

    3. Sorting (27)

    (a) (3)Showall theinversionpairsinthe list  4,1,5,2, 3)? Listthemoutin an organizedmanner,

    i.e.,sorttheminanascendingorderbythefirstelementandthenbythesecondelement.

    (b) (5)Giventhesequence

      4,6,1,8,3,7,2,5),

    sortthesequenceusingtheInsertionSortalgorithm.

    Illustrate the resultafter eachpass. Howmany positions movedhave youperformed at each

    pass?Justifyyouranswerbyusingatablelikeinthelecturenotes.

    (c) (5)Giventhe sequence 5,2,8,4,1,6,3,7), sort the sequence usingMergesort. Illustratethe

    resultaftereachpass.Howmanycomparisonshaveyouperformed?

    (d) (6)

    You

    havealist

    of

    elementswithonlythreedifferentelements,e.g. 0,1.2. Forexample, the

    listcanbe{0,1,1,0,2,},0,0,2}.

    a)(3)Pleasegiveanalgorithmwhosetimecomplexityis

    O n).

    b)(3)Pleasegiveanalgorithmwhosetimecomplexityis O n) whilespacecomplexity is 0 1).

    (e) (4)Given an arraywith

    n

    elements

    to

    sort, what

    is

    the

    best

    and

    worst

    timecomplexitywhen

    usingBubblesort,Selectionsort,InsertionsortandMergesortinterms of Big-Onotation.

    (f) (4)Quicksortwiththemedian

    of

    the first, middle,and lastelement

    of

    thearray

    as

    pivotanda

    cutoffof 3,demonstrateONLY theprocessof partitionthesequence,  7,6,1,4,8,2,3,5), into

    twopartsof theQuicksort(donotshowthewholeQuicksortalgorithm!). Notethatassumethe

    size of array is

    n,

    themiddleelementindex is

    nj2

    where index starts from°o

    n

    1 and

    the first stepinthePartitionalgorithmistoswaptheselectedpivot(medianof 3)withthefirst

    element,

    Le

    afterthisstep,itisthesame

    as

    thecodeintroducedinthelecturenoteswhereusing

    the first element as thepivot,afterpartition,thelefthalfelementsislessor equaltothepivot,

    therighthalfisgreater thanthepivot. Illustrateintermediatestepsclearly.

    4. Short Answers (26)(Pleasegiveonlyconciseandshortanswers!)

    (a) (6) How many different stack-permutable final sequences are there if the input sequence is

    {1,2,3,4,5}?E.g., {5,4,3,2,1}and {1,3,2,5,4}arebothvalidfinalsequences.

    (b) (6)

    The

    followings arethepartialresultsof abinarytree'straversalsin pre-order, in-orderand

    post-order(XrepresentsUNKNOWN).Pleasedrawthecorrespondingtree. Notethat the tree

    has9nodes in total,whicharefromA to1.

    Pre-order: XAXIXXBXC In-order:XDXAHXFXC Post-order:XEDXBXGFH

    (c) (14)What

    is

    theAverage- andWorst-caseruntimesof asingleDeleteMin operationonthe

    following types

    of

    datastructures?Theanswersshouldbeexpressedin thebig-Ohnotationin

    termsof,

    n

    thenumber

    of

    elementsstoredinthedatastructure.

    DataStructure

     

    Average-Case

     

    Worst-Case

     

    Descending-SortedDoubleLinkedList

    UnsortedArray

    Ascending-SortedArray

    BinarySearchTree

    AVLTree

    Max-Heap

    Min-Heap

    End-