provide clear explanation ” Assume that the following two classes,…  provide clear explanation”Assume that the following two classes, whic

provide clear explanation ” Assume that the following two classes,…  provide clear explanation”Assume that the following two classes, which implement the standard stack (last-in first-out) and queue (first-in first-out) data structures, are available. class Stack class Queue void push(Item x) void enqueue(Item x) Item pop() Item dequeue() Boolean isEmpty() Boolean isEmpty() (i) A Multistack class is derived from Stack with the addition of two methods: • a void multipush(Itemlist l) that takes a list l of items and pushes each of the items onto the stack (each action of extracting an item from the list and pushing it onto the stack has constant cost), and • a void multipop(int m) that takes an integer m and pops that many items off the stack (raising an exception if there were fewer, but don’t worry about that). Is it true or false that, given an arbitrary sequence of n Multistack operations starting from an empty Multistack, each operation in the sequence has amortized constant cost? Justify your answer in detail. [5 marks] (ii) Provide an implementation of class Queue using no other data structures than Item, Boolean, int and Stack. The amortized running time of each Queue method must be constant. (Note that you may only use the Stack as a black box: you are not allowed to access its internal implementation.) [7 marks] (iii) Using the potential method, prove that the amortized running time of all your Queue methods from part (b)(ii) is indeed constant. [5 marks] (a) What is the time complexity of binary search on a list of N items? [1 mark] (b) Binary search requires list items to be in sorted order. What is the best possible worst-case time complexity achievable  algorithm? Credit will be given for a clear explanation of your answer, but there is no need to provide a formal mathematical analysis or proof. [7 marks] (c) A researcher proposes a ternary search algorithm which repeatedly compares the search key with the two list items that most accurately trisect the remaining sorted search space. (i) Derive asymptotic expressions for the number of list items queried by binary search and by ternary search in the worst case. Explain your derivations in terms of worst-case executions of the search algorithms. [6 marks] (ii) (a) Consider the radix sort algorithm. (i) Explain how radix sort works, to what inputs it can be applied and what its asymptotic complexity is. [5 marks] (ii) Explain why running radix sort does not proceed from most to least significant digit, as would at first seem more intuitive. [4 marks] (iii) Give a proof by induction of the correctness of radix sort. [4 marks] (b) Clearly describe an algorithm, strictly better than O(n 2 ), that takes a positive integer s and a set A of n positive integers and returns a Boolean answer to the question whether there exist two distinct elements of A whose sum is exactly s. Evaluate its complexity.(a) Explain the greedy strategy in algorithm design. To what problems does it apply? [3 marks] (b) If a problem can be solved with both dynamic programming and a greedy algorithm, what are the advantages of using one or the other? [2 marks] (c) An imaginary post office machine must issue decorative stamps adding up to a given amount of p pence. Its goal is to minimize the number of postage stamps issued, and the machine always has as many stamps as needed. (i) Let the set of available denominations for the stamps be D = {1p, 5p, 25p, 50p, £1, £2}. Can this problem be solved using bottom-up dynamic programming? If so, clearly describe your algorithm and determine its complexity. If not, prove that it cannot be done. [5 marks] (ii) Let c1 < c2 < · · · < cn be n stamp denominations. Prove that if each ci (a positive integer) is a multiple of ci?1 for every i = 2, . . . , n then the greedy strategy applied to the set D = {c1, c2, · · · , cn} finds the optimal solution for any amount p that is a multiple of c1. [7 marks] (iii) Provide a set of denominations for stamps D and an amount of pence p for which the greedy strategy fails to give an optimal solution, p being a multiple of the smallest denomination in D. Show what solution the greedy strategy would find and what the optimal solution is.(a) Consider the two standard representations of directed graphs: the adjacency-list representation and the adjacency-matrix representation. Find a problem that can be solved more efficiently in the adjacency-list representation than in the adjacency-matrix representation, and another problem that can be solved more efficiently in the adjacency-matrix representation than in the adjacency-list representation. [4 marks] (b) Prove or disprove (by giving a counter-example) the following claim: If a directed graph G contains a path from a vertex u to a vertex v, then any depth-first search must result in v.d ? u.f, where .d is the discovery time and .f the finishing time. [4 marks] (c) We are given an undirected, connected graph G = (V, E) with edge-weights w : E ? R + and a minimum spanning tree T of G. How would you update your minimum spanning tree T in each of the following three cases? Specify the runtime of your algorithm and give a proof that the returned tree is indeed a minimum spanning tree. (i) We increase the weight of an edge e which is not in T. [3 marks] (ii) We decrease the weight of an edge e which is in T. [3 marks] (iii) We add a new edge e with weight w(e) to G. The weight w(e) is arbitrary, but for simplicity you may assume that after adding the edge e no two edges in G have the same weight.(a) How do insertions and deletions in a 2-3-4 tree retain the structure's perfect balance? [2 marks] (b) Explain the structural relationship between 2-3-4 trees and red-black trees. [4 marks] (c) Draw diagrams to illustrate left and right rotations at the root node of a binary search tree. Label the positions of all subtrees before and after the rotation. [4 marks] (d) Write pseudocode for a recursive function move to root(x,k) which, given a binary search tree with root node x and a key value k, uses a sequence of rotations to move the node with key value k to the root of the tree and returns a pointer to the new root node.We wish to store a dynamic collection of records, each of the form {timestamp, value}, where value is a real number. The collection should support the operations append_newer(t,v) to add a new record (which we can assume has a larger timestamp than any existing record), pop_oldest() to remove the oldest record, and get_oldest() to inspect the oldest without removing it. (a) Define the Queue abstract data type. Describe an implementation using a linked list. Explain how to use it for this dynamic collection of records. [3 marks] The collection should also support get_max(), which returns a pointer to the record with the highest value in the collection. Ties may be broken arbitrarily. (b) A simple implementation of get_max() simply scans through the entire list. What is the worst-case cost, given the number n of items in the collection? [1 mark] (c) An engineer friend suggests keeping a pointer maxrecord to the record with the largest value so that the entire list only need be rescanned when the item pointed to by maxrecord is removed. Give an example to show that n operations could take ?(n 2 ) time. [3 marks] (d) Explain the terms amortized cost and potential method. Explain the relationship between aggregate true costs and aggregate amortized costs. [4 marks] (e) Devise an implementation in which all operations have O(1) amortized cost, and use the potential method to justify your answer. Illustrate what happens when we start with a list of values [5, 8, 3, 6, 2] where 5 is oldest and 2 is newest, and then append a newer record with value 7. [Hint: Where is the largest item newer than maxrecord, and the largest item newer than this, and so on?] [9 marksExplain the open addressing strategy of collision resolution and the term probing sequence used in that context. [3 marks] (c) Explain quadratic probing and its advantages and disadvantages. [Hint: refer to primary and secondary clustering.] [3 marks] (d) Give a general mathematical expression for the probing function p(k, i) used in quadratic probing. The expression should yield a 0-based index into the table, referencing the key k, the probe number i, the hash function h, the table size m and the constants c1 and c2. [3 marks] (e) Does the following pseudocode implement a form of quadratic probing? If so, derive values for c1 and c2 in the equation you produced for part (d). If not, prove it doesn't. In either case, clearly justify your reasoning. [8 marks] def get(k): j = h(k) i = 0 while true: if T[j].key == null: raise NotFound if T[j].key == k: return T[j].payload i = i+1 if i == m: raise NotFound (a) Transform the following recurrence f(x) = f( ? x) + c into a closed-form expression for the function f (that is, an expression that does not contain f). Having done that, give the asymptotic complexity of f using big-O notation. [4 marks] (b) (i) Explain the programming technique known as memoization, detailing the cases to which it applies. [4 marks] (ii) In a few lines of pseudocode, write a memoized recursive function to compute the ith Fibonacci number F(i), with i ? N {0}. Recall that F(1) = 1, F(2) = 1,... [4 marks] (c) Computing a recursive function f on arrays, when called on an array of size n, results in 2n recursive calls to f. After memoizing f, on an array of a specific size n0 we observe that about 90% of the calls to f return a memoized result rather than invoking f recursively. Is either of the following statements correct? Justify your answers. (i) "The number of recursive calls goes down by a factor of ten; so it will take 1/10 of the time it used to, that is, it will run 10 times faster." [2 marks] (ii) "Previously, the function did 2n recursive calls. Now it does 0.9· c1 +0.1·2 n recursive calls. That is still O(2n ), so the asymptotic complexity of the function is still the same (even after memoization)." [2 marks] (d) Some implementations of the Quicksort algorithm select the pivot at random, rather than taking the last entry in the input array. (i) Discuss the advantages and disadvantages of such a choice. [1 mark] (ii) How would you construct an input to trigger quadratic running time for this randomised Quicksort, without having access to the state of the random number generator?The chaining collision-resolution scheme for hash tables uses an array where each element is a linked list. (a) For a hash table that uses chaining to resolve collisions with an array of length m and n keys inserted, the average asymptotic complexity for search and insert is O(1 + n m ). (i) Explain why search and insert are considered to be O(1) and not O(n) in practice. [2 marks] (ii) Give the worst-case complexities for search and insert. State the factors that determine the performance observed in practice. [5 marks] (b) An alternative hash-table implementation replaces the linked lists in each slot with red-black trees. Discuss the advantages and disadvantages of this change. [4 marks] (c) The linked lists may also be replaced by dynamically sized arrays. When full, an array is expanded by a factor of k. (i) Derive a potential to compute the amortised cost of adding an element to such an array using the potential method. On inserting an element that triggers an expansion, your potential should be zero just after the expansion but before the insertion of the new element. [3 marks] (ii) Use your potential to compute the amortised cost of adding an element to the arrayThe datatype PRI defined below is to be used for the representation of priorityqueues which are finite or infinite ordered sets of integers.datatype PRI = E| N of int*(unit->PRI);Define an ML function intfromto(i,j) : (int*int)->PRI which will return arepresentation of the ordered set of integers{ i, i+1, …, j }Define the function first(p) : PRI->int that will return the first (and hencesmallest) integer in the given queue p, and rest(p) : PRI->PRI that will return(if possible) a representation of the given queue p with its smallest element removed.Your implementation should be such that the expressionfirst(rest (intsfromtoshould evaluate efficiently.Define an ML function ins(i,p) : (int*PRI)->PRI which will return a priorityqueue with the integer i inserted in the proper position of the given queue p Computer Science Engineering & Technology Java Programming COMPUTER S X_405082 Share QuestionEmailCopy link Comments (0)