Describe and explain in detail the Johnson algorithm that finds… Describe and explain in detail the Johnson algorithm that finds all-pair
Describe and explain in detail the Johnson algorithm that finds… Describe and explain in detail the Johnson algorithm that finds all-pairsshortest-paths by repeatedly applying Dijkstra to each vertex, even in graphswith negative edge weights. [Pseudocode is not required but all phases of thealgorithm must be clearly explained.] (c) Some people wonder why it would not be simpler to reweight edges by adding asufficiently large constant K to each edge weight so as to make all the weightspositive. Prove that this cannot work.(d) In Johnson’s algorithm, why do we introduce a new vertex s? Could we notuse, instead of a new vertex, one of the vertices of the original graph? Eitherprove that we can or provide a counterexample. (a) Describe in detail an algorithm that returns the minimum distance from apoint to a line segment in two dimensions. Ensure that you include all of yourassumptions and all necessary mathematical calculations. [7 marks](b) A quadratic Be´zier curve is defined by three points, P1, P2, P3, and aparameter, t:P(t) = (1 ? t)2P1 + 2t(1 ? t)P2 + t2P3, 0 ? t ? 1Describe an algorithm that draws the quadratic Be´zier curve, using straightlines only, to within a tolerance ? . You may use the algorithm from part (a)and you may assume that you already have an algorithm for drawing a straightline. [8 marks](c) Consider the control of detail in a curve that is represented by a sequence ofmany straight line segments. Describe how Douglas and P¨ucker’s algorithmcan be used to remove superfluous points. You may use the algorithm frompart (a). (a) Consider a simple random walk, Sn, defined by S0 = a and Sn = Sn?1 + Xnfor n ? 1 where the random variables Xi (i = 1, 2, . . .) are independent andidentically distributed with P(Xi = 1) = p and P(Xi = ?1) = 1 ? p for someconstant p with 0 ? p ? 1.(i) Find E(Sn) and Var (Sn) in terms of a, n and p. [4 marks](ii) Use the central limit theorem to derive an approximate expressionfor P(Sn > k) for large n. You may leave your answer expressed in termsof the distribution function ?(x) = P(Z ? x) where Z is a standardNormal random variable with zero mean and unit variance. [6 marks](b) Consider the Gambler’s ruin problem defined as in part (a) but with theaddition of absorbing barriers at 0 and N where N is some positive integer.Derive an expression for the probability of ruin (that is, being absorbed at thezero barrier) when starting at position S0 = a for each a = 0, 1, . . . , N in thet25 Logic and Proof(a) State (with justification) whether the following formula is satisfiable, valid orneither. Note that a and b are constants.h?x [q(x) ? r(x)] ? ¬r(a) ? ?x [¬r(x) ? ¬q(a) ? p(x) ? q(x)]i? p(b) ? r(b)(b) Attempt to prove the formula [?x ?y R(x, y)] ? ?x ?z R(x, f(z)) by resolution,with brief explanations of each step, including the conversion to clause form.[4 marks](c) Give a model for the following set of clauses, or prove that none exists.{¬R(x, y), ¬R(y, x)}{R(x, f(x))}{¬R(x, y), ¬R(y, z), R(x, z)} The Prolog predicate perm(+In,-Out) generates all permutations of the input listIn. A programmer implements perm/2 as follows:perm([],[]).perm(L,[H|T]) :- take(L,H,R), perm(R,T).The predicate take(+L,-E,-R) removes one element (E) from the input list L andunifies R with the remainder of L. Thus, the list R has one element fewer than L.(a) Consider the perm/2 predicate:(i) Explain briefly in words the operation of the perm/2 predicate.(ii) Provide an implementation of the take/3 predicate.(iii) Give the complete sequence of answers (in the correct order) generatedby perm([1,2,3],A).(b) A student attempts to invoke the query perm(A,[1,2,3]).(i) Explain what happens and why. [5 marks](ii) Implement a predicate sameLength/2 which is true if the two parametersare lists of the same length.(iii) Using sameLength/2, or otherwise, provide an implementation ofsafePerm/2 which generates permutations regardless of the order inwhich the parameters are provided: both safePerm(+In,-Out) andsafePerm(-Out,+In) should generate all permutations of In. The orderin which these permutations are generated is not important. Write complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file.Write function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them.Write function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper How do software engineering tools change as systems scale? Discuss this questionwith reference to(a) a 2000-line device driver for a safety-critical sensor on board an aircraft;(b) a 100,000-line engine control unit for a diesel engine that adapts it for use intrucks, generators or irrigation pumps;(c) a 1,000,000-line social networking site such as Facebook or MySpace;(d) a 50,000,000-line operating system. (a) A web server is an application that listens for incoming network connectionson TCP port 80. Once a connection is established, the task of processingclient requests and sending replies can be handled by an instance of aWorker class which you may assume already exists. Worker implements thejava.lang.Runnable interface and has an accessible constructor that takes asargument a java.net.Socket object representing the network connection toa client.Provide the Java code for a webserver which, upon start-up, attempts to listenon TCP port 80 and starts a new Thread running a new Worker for everyconnection. Your program should print helpful error messages indicating thelikely cause of problems when it is unable to proceed as expected. [10 marks](b) A busy web server might expect to handle concurrent requests to read andupdate some shared data and could use Timestamp Ordering (TSO) to enforceisolation between concurrent transactions.(i) Explain how TSO enforces isolation.(ii) Is TSO appropriate for a web server application? Explain your reasoning. Consider the following grammar for expressions (where Id is a terminal symbolrepresenting an identifier resulting from lexical analysis):Expr ::= 1 | 2 | Id | Expr + Expr | Expr / Expr Foundations of Computer Science (a) Write brief notes on ML datatypes and pattern-matching in function declarations. [6 marks] (b) A binary tree is either a leaf (containing no information) or is a branch containing a label and two subtrees (called the left and right subtrees). Write ML code for a function that takes a label and two lists of trees, returning all trees that consist of a branch with the given label, with the left subtree taken from the first list of trees and the right subtree taken from the second list of trees. [6 marks] (c) Write ML code for a function that, given a list of distinct values, returns a list of all possible binary trees whose labels, enumerated in inorder, match that list2 CST.2013.1.3 2 Foundations of Computer Science The function perms returns all n! permutations of a given n-element list. fun cons x y = x::y; fun perms [] = [[]] | perms xs = let fun perms1 ([],ys) = [] | perms1 (x::xs,ys) = map (cons x) (perms (rev ys @ xs)) @ perms1 (xs,x::ys) in perms1 (xs,[]) end; (a) Explain the ideas behind this code, including the function perms1 and the expression map (cons x). What value is returned by perms [1,2,3]? [7 marks] (b) A student modifies perms to use an ML type of lazy lists, where appendq and mapq are lazy list analogues of @ and map. fun lperms [] = Cons ([], fn() => Nil) | lperms xs = let fun perms1 ([],ys) = Nil | perms1 (x::xs,ys) = appendq (mapq (cons x) (lperms (rev ys @ xs)), perms1 (xs,x::ys)) in perms1 (xs,[]) end; Unfortunately, lperms computes all n! permutations as soon as it is called. Describe how lazy lists are implemented in ML and explain why laziness is not achieved here. [5 marks] (c) Modify the function lperms, without changing its type, so that it computes permutations upon demand rather than all at once. [8 marks] All ML code must be explained clearly and should be free of needless complexity. 3 (TURN OVER) CST.2013.1.4 SECTION B 3 Discrete Mathematics I (a) Consider the following assertions about the sets A, B and C. Write them down in the language of predicate logic. Use only the constructions of predicate logic (?, ?, ¬, ?, ?, ?) and the element-of symbol (?). Do not use derived notions (?, ?, =, etc.). Example: “A is a subset of B” can be formalized as ?x. x ? A =? x ? B. (i) The sets A and B are equal. (ii) Every element of A is in the set B or the set C. (iii) If A is disjoint from B then B and C overlap. [6 marks] (b) State the principle of induction over lists. Use the language of predicate logic. [2 marks] (c) Consider the following functions over lists of integers, written in ML syntax. fun app([],ys) = ys | app(x::xs,ys) = x::app(xs,ys); fun rev([]) = [] | rev(x::xs) = app(rev(xs),x::[]); fun revapp([],ys) = ys | revapp(x::xs,ys) = revapp(xs,x::ys); Prove that ?xs. revapp(xs,[]) = rev(xs) Your proof should be clear but it does not need to be a structured proof. You may use the abbreviation xs @ ys for app(xs,ys). You may assume the following facts. ?xs. xs @ [] = xs ?xs, ys, zs. xs @ (ys @ zs) = (xs @ ys) @ zs Hint: first use induction to show that ?xs. ?ys. revapp(xs,ys) = app(rev(xs),ys). [12 marks] 4 CST.2013.1.5 4 Discrete Mathematics I (a) Write down the introduction and elimination rules for the universal quantifier (?), the existential quantifier (?) and negation (¬) in structured proof. [6 marks] (b) Write down the introduction rule for implication (=?) in structured proof. [1 mark] (c) Write down a structured proof of the following sentence. (?x. ¬P(x)) =? ¬?x. P(x) [5 marks] (d) Write down a structured proof of the following sentence. Clearly state any proof rules that you use in addition to those included in part (a) and part (b). (¬?x. ¬P(x)) =? ?x. P(x) [8 marks] 5 (TURN OVER) CST.2013.1.6 SECTION C 5 Algorithms I One of several ways to perform string matching efficiently is with a(a) Consider the perm/2 predicate:(i) Explain briefly in words the operation of the perm/2 predicate.(ii) Provide an implementation of the take/3 predicate.(iii) Give the complete sequence of answers (in the correct order) generatedby perm([1,2,3],A).(b) A student attempts to invoke the query perm(A,[1,2,3]).(i) Explain what happens and why. [5 marks](ii) Implement a predicate sameLength/2 which is true if the two parametersare lists of the same length.(iii) Using sameLength/2, or otherwise, provide an implementation ofsafePerm/2 which generates permutations regardless of the order inwhich the parameters are provided: both safePerm(+In,-Out) andsafePerm(-Out,+In) should generate all permutations of In. The orderin which these permutations are generated is not important. Write complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file.Write function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them.Write function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper How do software engineering tools change as systems scale? Discuss this questionwith reference to(a) a 2000-line device driver for a safety-critical sensor on board an aircraft;(b) a 100,000-line engine control unit for a diesel engine that adapts it for use intrucks, generators or irrigation pumps;(c) a 1,000,000-line social networking site such as Facebook or MySpace;(d) a 50,000,000-line operating system.answer the question clearly Computer Science Engineering & Technology Java Programming CE 123A Share QuestionEmailCopy link Comments (0)


