Homework Batch II: Trees and Algorithms
- Let H be a Min-Heap containing n integer keys and let k be an integer value. Solve the following exercises by using the procedures seen during the course lessons:
- Write the pseudo-code of an in-place procedure RetrieveMax(H) to efficiently return the maximum value in H without deleting it and evaluate its complexity.
- Write the pseudo-code of an in-place procedure DeleteMax(H) to efficiently deletes the maximum value from H and evaluate its complexity.
- Provide a working example for the worst case scenario of the procedure DeleteMax(H) (see Exercise 1b) on a heap H consisting in 8 nodes and simulate the execution of the function itself.
- Let A be an array of n integer values (i.e., the values belong to Z). Consider the problem of computing a vector B such that, for all i ∈ [1,n], B[i] stores the number of elements smaller than A[i] in A[i + 1,…,n]. More formally:
B[i] = |{z ∈ [i + 1,n]| A[z] < A[i]}|
- Evaluate the array B corresponding to A = [2,−7,8,3, −5,−5,9,
1,12,4].
- Write the pseudo-code of an algorithm belonging to O(n2) to solve the problem. Prove the asympotic complexity of the proposed solution and its correctness.
- Assuming that there is only a constant number of values in A different from 0, write an efficient algorithm to solve the problem, evaluate its complexity and correctness.
- Let T be a Red-Black Tree.
- Give the definition of Red-Black Trees.
1
- Write the pseudo-code of an efficient procedure to compute the height of T. Prove its correctness and evaluate its asymptotic complexity.
- Write the pseudo-code of an efficient procedure to compute the blackheight of T. Prove its correctness and evaluate its asymptotic complexity.
- Let (a1,b1),…,(an,bn) be n pairs of integer values. They are lexicographically sorted if, for all i ∈ [1,n − 1], the following conditions hold:
- ai ≤ ai+1;
- ai = ai+1 implies that bi ≤ bi+1.
Consider the problem of lexicographically sorting n pairs of integer values.
- Suggest the opportune data structure to handle the pairs, write the pseudo-code of an efficient algorithm to solve the sorting problem and compute the complexity of the proposed procedure;
- Assume that there exists a natural value k, constant with respect to n, such that ai ∈ [1,k] for all i ∈ [1,n]. Is there an algorithm more efficient than the one proposed as solution of Exercise 4a? If this is the case, describe it and compute its complexity, otherwise, motivate the answer.
- Assume that the condition of Exercise 4b holds and that there exists a natural value h, constant with respect to n, such that bi ∈ [1,h] for all i ∈ [1,n]. Is there an algorithm to solve the sorting problem more efficient than the one proposed as solution for Exercise 4a? If this is the case, describe it and compute its complexity, otherwise, motivate the answer.
- Consider the select During the lessons, we explicitly assumed that the input array does not contain duplicate values.
- Why is this assumption necessary? How relaxing this condition does affect the algorithm?
- Write the pseudo-code of an algorithm that enhance the one seen during the lessons and evaluate its complexity.
2
Reviews
There are no reviews yet.