hwassignment_1

Upload: lakshay-gupta

Post on 24-Feb-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 hwAssignment_1

    1/2

    IDAA432C Homework Assignment 1 Due on - Monday, February 8, 2016, 01:15pm

    Problem 1.1

    Heaps (in particular binary Max-Heaps) and operations on them were introduced in class. Equivalent operations

    fork-ary Max-heaps, were implemented by you as part of your second lab assignment. The operations we studied

    were,

    build MAX heap(A)

    insert(A,m)

    Extract max(A)

    In addition to these, you implemented another operation called change(A,i,m). The specification for change

    was as follows:

    Given ak-ary Max-Heap, represented as the array A, change(A,i,m)changes the value ofA[i] tom and

    makes sure that the changed array represents a Max-heap.

    This problem deals with the equivalent procedure for binaryMax-Heaps. For a binary Max-Heap, the equivalentproperty can be stated as

    () Given a Binary Max-Heap, represented as the array A, change(A,i,m) changes the value ofA[i] to

    mand makes sure that the changed array represents a Max-heap.

    1. Write the algorithm forchange(A,i,m)whereA represents a binary Max-Heap. Give the full algorithm.

    If you use any function as part of the algorithm, provide the algorithm for the function too.

    2. Write the precoditions (conditions that you assumed to be true before running the procedure) for the

    procedurechange(A,i,m)and prove the correctness of your algorithm by proving that change(A,i,m)

    indeed satisfies the property ().

    3. Design two algorithms named Extract max1(A) and insert1(A,m) for performing operations similartoExtract max(A)andinsert(A,m)with the exception that Extract max1(A)and insert1(A,m)

    can only utilize change(A,i,m) for manipulating (changing or swapping) values in the array. In other

    words, any changes performed in the elements of the array, are to be done by calling change(A,i,m)

    appropriately.

    4. Prove the correctness of the new algorithms provide by you : Extract max1(A) and insert1(A,m)

    (the one utilizing only change(A,i,m)). Note that, as demonstrated in class, you have to justify each

    call to change(A,i,m) with respect to its preconditions.

    5. Provide an asymptotic bound on the running time of the algorithms that you provided (change(A,i,m),

    Extract max1(A) and insert1(A,m)).

    6. Design an algorithm called build MAX heap1(A) which constructs a binary Max-Heap out of array Abut only utilizes change(A,i,m). (Hint: You may need to use a new array)

    7. Prove the correctness of build MAX heap1(A) (once again, justify each call to change(A,i,m) with

    respect to its preconditions).

    8. Give an asymptotic bound on the running time ofbuild MAX heap1(A) (do not forget to do a step-by-step

    analysis to perhaps obtain a better bound).

    Marks : 2+3+5+3+3+4+2+3

    Page 1 of 2

  • 7/25/2019 hwAssignment_1

    2/2

    IDAA432C Homework Assignment 1 Due on - Monday, February 8, 2016, 01:15pm

    Problem 1.2

    Recall the algorithm for build MAX heap(A) that we studied in class. A very naive and generalized analysis

    of the running time of the algorithm gave us the bound ofO(nlog2n). A futher detailed analysis, taking into

    account the exact cost at each step lead us to a better bound ofO(n). The property we exploited there for the

    detailed analysis was that the heap we were considering at each step was increasing at each step and was not

    the whole heap for all the steps.

    Now remember the algorithm for heapsort(A) that has been discussed in class. A similar generalizing argument

    in the analysis of running time lead us to the bound ofO(nlog2n). At the same time, we discussed about a

    similar property in the algorithm ofheapsort(A)which might be exploited to obtain a possibly better bound.

    For this problem write the algorithm for heapsort(A) and analyse it step-by-step to obtain a more precise

    bound. State the property that you utilise to perform the analysis. Compare the precise bound with the bound

    O(nlog2n) to find out whether or not it is better.

    Marks : 5

    Page 2 of 2