Question: Algorithms (a) Explain the greedy strategy in algorithm…
Question Answered step-by-step Question: Algorithms (a) Explain the greedy strategy in algorithm… Question:Algorithms (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 adv of 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 solutiony-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. Assuming both operands are normalised and nonzero, what is the maximumdegree (number of shifts) of normalisation required in the result of a floatingpoint multiplication? Ignore zero and denormal results. [5 marks](b) A watertight capsule containing scientific instruments is dropped from ahigh-altitude plane into the ocean where it must approach close to the sea bottombefore floating up for retrieval by a boat. You are provided with subroutinesthat compute water and air resistance from velocity. Collision with the oceanbottom is a mission failure.(i) Set up a state vector and sketch code for a finite-difference, time-domainsimulation of the trajectory in one dimension. Include a check for missionfailure. [5 marks](ii) What three issues will affect your choice of simulation timestep?[3 marks](iii) Your simulation is now re-coded using interval arithmetic. New subroutinesfor resistance are provided with interval domain and range giving a pair withthe maximum and minimum resistance likely to be encountered. All inputparameters are likewise coded as pairs. Summarise how this will affect thestate vector and your code The Java class below defines a queue data structure with a fixed capacity. public class FixedCapacityQueue { int[] mData = null; int mHead=0; // index of first full cell int mTail=0; // index of next empty cell public FixedCapacityQueue(int capacity) { mData = new int[capacity]; } public boolean enqueue(int x) { if (mTail==mData.length) return false; mData[mTail]=x; mTail++; return true; } public int dequeue() { if (mTail==mHead) return -1; int v=mData[mHead]; mHead++; return v; } } (a) Explain the risks posed by the lack of access modifiers specified on the state. Which access modifier is appropriate for these entities? [3 marks] (b) Discuss the choice to signal enqueue and dequeue errors using return values of false and -1, respectively. Suggest a better alternative and modify enqueue and dequeue to use it. [5 marks] (c) Explain how an enqueue operation on a FixedCapacityQueue object could fail with an error even when there are fewer items in the queue than the assigned capacity of the object. Show how to fix this. [3 marks] (d) Rewrite the class to use Java's Generics so that it can be used to represent queues of arbitrary objects (Integers, Strings, etc). Use the java.util.ArrayList class instead of int[] for the type of mData. [4 marks] (e) Explain why it was necessary to replace the int[] type of mData in part (d), and why ArrayList does not suffer from the same issue. [5 marks] 3 (TURN OVER) CST.2015.1.4 4 Object-Oriented Programming A developer wishes to create a photo library application that can auto-detect faces in images to assist labelling. They wish to use some commercially available Java face detection software. The source to this software is unavailable—only the Java bytecode and documentation are supplied. The two key classes are as follows: Class name Method prototype Image // Constructor public Image(byte[] imageData, int w, int h) // Get the image value at pixel (x,y) public byte getPixel(int x, int y) // Set the image pixel at (x,y) to value val public void setPixel(int x, int y, byte val) // Get the image width public int getWidth() // Get the image height public int getHeight() Algorithm // Search for and mark the faces in Image im public static void markFaces(Image im) (a) Explain what is meant by source code, machine code and Java bytecode. Give two advantages of distributing Java software as bytecode rather than source code. [5 marks] (b) Explain why markFaces() is declared static. [1 mark] (c) The developer wishes to add filename information in the form of a String to all of the Image instances. Show how to achieve this efficiently using inheritance. [4 marks] (d) The developer wishes to know whether the markFaces() method visits every pixel in the image or uses a more intelligent search strategy. Provide a new class definition that can be used to determine the search sequence. Your class should be able to temporarily augment an Image object with logging capabilities while it is being processed by markFaces(). [Hint: Explain how shared data can be protected through the use of objects. [8 marks] The built-in facilities for restricting concurrency in Java allow only one thread at a time to be executing within the critical region.readers may share access at the same time, but only one writer may acquire exclusive access (excluding any readers while it does so). Specify a MultiSync class in Java with methods to acquire and release both read and write access, and sketch its implementation. [6 marks] Derive a MultiBuffer class that extends MultiSync with methods to store and read a data field, ensuring that any locks are released when a waiting thread is interrupted. Your example may use a simple data field such as an integer but you should explain why such an elaborate scheme of concurrency control is unnecessary in this case. [6 marks] 2 CST.99.4.3 4 Compiler Construction It is commonly suggested that Algol-60 call-by-name can be modelled by passing a function as a call-by-value parameter. Show how a program containing a definition int f(int x:name) { ... x ... x ... } of f (where x occurs only in Rvalue context) and a call f(e) to f can be replaced by an equivalent definition and call using only call-by-value. [6 marks] Most such explanations assume that the uses of x within f occur only in Rvalue context. However, Algol-60 also permits the equivalent of int g(int x:name) { if (p) { ... x := x+1; x := -x; ... } return x; } and calls like g(a[k()]) which, when p is true, would have the effect of calling k() five times and consequent access to five (possibly different) subscripts of array a[]. Develop your explanation for the first part of this question to cover also the case of a call-by-name parameter being used in both Lvalue and Rvalue contexts. [Hint: note that when p is false then the actual parameter to g need not be an Lvalue, so you may need two parameterless procedure arguments ("thunks").] [8 marks] Using the previous part or otherwise, give a translation of a definition and call h(e) using call-by-value-result (Ada in out mode) with no uses of the address-of (&) operator other than those involved in call-by-name. Your explanation is allowed to deviate from call-by-value-result by allowing side-effects in e to take place twice. [6 marks] 5 Data Structures and Algorithms Outline the mechanism used in the Burrows-Wheeler block compression algorithm, illustrating your description by applying it to the string ALFALFA. [14 marks] Briefly discuss the advantages and disadvantages of the Burrows-Wheeler algorithm compared with other commonly used compression methods. [6 marks] 3 [TURN OVER CST.99.4.4 6 Computer Design A modern processor's memory is constructed from a hierarchy of memories. Explain what the levels of the hierarchy are. [5 marks] Why do modern processors have several registers rather than an accumulator plus one or two index registers? [5 marks] Hand compile the following pseudo-code for both a register machine and an accumulator machine of your choice. You may invent an instruction set. Make the semantics of the code clear via comments. a=0; b=1; for(i=0; i<5; i++) { a=a+b; b=a-b; } [10 marks] 7 Operating System Functions The following are three ways which a file system may use to determine which disk blocks make up a given file. (a) chaining in a map (b) tables of pointers (c) extent lists Briefly describe how each scheme works Additional hint on notation: s u = path from s to u consisting of 0 or moreedges (0 when s ≡ u); u → v = path from u to v consisting of precisely oneedge; d[u] = weight of the shortest path found so far from source s to vertexu; δ(s, v) = weight of shortest existing path from s to v.](c) Why does the algorithm require non-negative edge weights? [2 marks](d) Would the algorithm work if the only negative weights were on edges leavingthe source? Justify your answer with a proof or counterexample. [5 marks](e) Consider the following approach for finding shortest paths in the presence ofnegative edges. "Make all the edge weights positive by adding a sufficientlylarge biasing constant to each; then find the shortest paths using Dijkstra'salgorithm and recompute their weights on the original graph." Will this work?Justify your answer with a proof or counterexample. [3 marks]2CST.2006.5.32 Computer Design(a) Why do pipelines exhibit branch and load delays? [6 marks](b) What impact does pipeline length have on clock frequency? [4 marks](c) Why might a shorter pipeline result in a more power-efficient design?[4 marks](d) Recently we have seen microprocessor manufacturers release dual-processorchips where each processor has a shorter pipeline than the earlier singleprocessor per chip designs. What sort of applications might run better onthe older chips and vice versa? [6 marks]3 Digital Communication I(a) Describe the concepts of circuit switching and packet switching. [5 marks](b) What are the fundamental advantages of each over the other? [5 marks](c) What is the role of buffering and buffering policy in each approach? [5 marks](d) There is an expectation that in the near future telephony will move from circuitswitching to packet switching. Why is this so in light of the advantages of eachapproach? [5 marks]3 (TURN OVER)CST.2006.5.44 Concurrent Systems and Applications(a) The following Java interface describes the API for a first-come, first-served(FCFS) mutual exclusion system, designed to work even if threads areinterrupted in the enter and exit routines.interface FCFS {public void enter();public void exit();}Sketch a concrete class, FCFSImpl, implementing this interface, which does notneed to be re-entrant, ensuring that you satisfy the following requirements:(i) if a thread is interrupted while executing the entry protocol, it shouldabort its attempt to gain entry and cleanly terminate the call; you mayassume that the calling code will not then enter the critical region;[6 marks](ii) the exit protocol should notify a particular thread and not simply callnotifyAll(). [6 marks](b) Object allocation graphs can be used to detect deadlock in a concurrentapplication.(i) Give an example of an object allocation graph and explain the meaningsof the different components. [2 marks](ii) Describe an algorithm which can use an object allocation graph and anobject request matrix to determine whether or not deadlock exists.[5 marks](iii) Describe how to distinguish between cases in which deadlock has occurredand those in which deadlock is inevitable but is yet to occur. [1 mark]4CST.2006.5.5SECTION B5 Computer Graphics and Image Processing(a) Give the definition of the cubic Be0zier curve. [4 marks](b) Derive the conditions necessary to ensure that two cubic Be0zier curves joinwith C1-continuity. [6 marks](c) Describe, in detail, an algorithm for drawing a cubic Be0zier curve to a giventolerance using straight lines. You may assume that you already have analgorithm for drawing a straight line. [6 marks](d) Explain why and how homogeneous co-ordinates are used in computergraphics. [4 marks]6 Compiler Construction(a) Consider the grammarS ::= (L) | aL ::= L, S | S(i) Present a right-most derivation for the string (a, ((a, a), (a, a))).[3 marks](ii) Present a left-most derivation for the same string (a, ((a, a), (a, a))).[3 marks](b) Automatic garbage collection is an important technique for the implementationof many programming languages. Define each of the following variations:(i) Mark and Sweep; [3 marks](ii) Copy Collection; [3 marks](iii) Generational Collection. [3 marks](c) Write a small program that will produce different values depending on whichkind of variable scoping mechanism is used, static or dynamic. Explain youranswer. [5 marks]5 (TURN OVER)You may find it useful to apply a common design pattern.] [10 marks] 4 CST.2015.1.5 SECTION C 5 Numerical Methods (a) Consider the iteration: xn+1 = (2xn + N/x2 n )/3 (i) The iteration converges to give what useful property of the constant argument N? [2 marks] (ii) Examine whether the above iteration should work for all possible values of N and x0? [6 marks] (iii) Find the order of convergence for the above iteration. You may use standard results but do not simply state an order without justification. [3 marks] (b) Cholesky provides an approach to solving certain systems of simultaneous equations. His method (and similar methods) perform upper/lower triangle decomposition of the equation coefficient matrix A such that A = LU and U T = L. (i) Under what circumstances can Cholesky's method be used? Can it be used if A is already a triangular matrix and, if not, what should be done instead? [3 marks] (ii) Give expressions for two of the four values in the upper-left 2×2 sub-matrix of L in terms of the elements of AImage transcription textflat h) - f(a) = 3. h Practice consider f(x) = 9x - 5. a. Compute: f(a) = b.Compute and simplify: f(a ... Show more... Show moreImage transcription textGiven 100 G3 =—, U s2+10/§3+100 do each ofthe following: 3 Compute can (Natural frequency)b Compute C (Damping ratio) c Co... Show more... Show more Computer Science Engineering & Technology C++ Programming COMPE COMPE375 Share QuestionEmailCopy link Comments (0)


