1 Exam 2 Computer Engineering 251 Santa Clara University Thursday, 23 April 1998 NAME èprintedè NAME èsignedè Login Day of Lab Session Please show all your work and circle your answers. Your grade will be based on the the work shown. Question 1 è10 pointsè Question 2 è10 pointsè Question 3 è10 pointsè Question 4 è10 pointsè Question 5 è10 pointsè Question 6 è10 pointsè Question 7 è20 pointsè Question 8 è20 pointsè TOTAL
Name: 2 Useful Formulas Pn i=1 i = nèn+1è 2 Pn i=1 xi = xn+1,1 x,1 P Pn 1 =lnn + Oè1è 1 i=1 i i=0 = ixi x è1,xè 2 e x =1+x + x2 + x 3 2! 3! + ::: lim n!1è1 + x n èn = e x n! = p 2çnè n e èn è1+æè 1 n èè n! =oènn è n! =!è2 n è lgèn!è = æèn lg nè ç çn enk ç è èk çn kç = n! è n k!èn,kè! k èk ç EëXë = Px xp rëx = xë VarëXë =EëèX, EëXëè 2 ë = EëX 2 ë, E 2 ëxë k íput gatesèformulas hereí
Name: 3 1. è10 pointsè 5 short answers èmorningè or 10 TèF èeveningè èaè Inaæow network, the æow on an edge is always larger than the capacity of the edge. èbè The longest simple path in an undirected graph has at most V, 1 edges. ècè èdè èeè Greedy algorithms always ænd the optimal solution to a problem. Every logical gate has fan-in 2 èthat is, has exactly 2 inputsè. Prim's Algorithm and Kruskal's Algorithm solve the same problem. èfè How many diæerent minimal spanning trees are there on a triangle with edge weights 1 èthat is, the graph G =èf1; 2; 3g; fè1; 2è; è1; 3è; è2; 3èg and wè1; 3è = wè1; 3è = wè2; 3è = 1è? ègè What is the most number of edges possible in a matching on a graph G =èv; Eè, where the number of vertices is even? èhè In a æow network, what is the capacity of a path, p, if the capacity of each edge èu; vè iscèu; vè? èiè How can the number of strongly connected components in a graph change if a new edge is added? èjè If an undirected graph has E edges and V vertics, how many edges does a spanning tree of the graph have? 2. è10 pointsè Huæman encoding èaè Given the following character set and the frequency each letter occurs, construct a Huæman code for the character set: a b c d e f Frequency èin thousandsè 45 13 12 16 9 5 èbè Given the following character set and the frequency each letter occurs, construct a Huæman code for the character set: a b c d e f Frequency èin thousandsè 5 11 1 4 9 1
Name: 4 3. è10 pointsè Give algorithm and example èaè èmorningè i. Write Dijkstra's Algorithm. ii. Give a simple example of a directed graph with negative-weight edges for which Dijkstra's Algorithm produces incorrect answers. èbè èeveningè i. Write Kruskal's Algorithm ii. Suppose that all the edge weights in the graph are integers in the range from 1 to jv j. How fast can you make Kruskal's Algorithm run? 4. è10 pointsè Analyze graph algorithms èaè Analyze the following algorithm: G is a directed graph with weight function w and s is a vertex of G. BELLMAN-FORDèG,w,sè 1 INITIALIZE-SINGLE-SOURCEèG,sè 2 for i é- 1 to VëGë - 1 3 do for each edge èu,vè in EëGë 4 do RELAXèu,v,wè 5 for each edge èu,vè in EëGë 6 do if dëvë é dëuë + wèu,vè 7 then return FALSE 8 return TRUE INITIALIZE-SINGLE-SOURCEèG,sè 1 for each vertex v in VëGë 2 do dëvë é- infinity 3 pëvë é- NIL 4 dësë é- 0 RELAXèu,v,wè 1 if dëvë é dëuë + wèu,vè 2 then dëvë é- dëuë + wèu,vè 3 pëvë é- u èbè Analyze the following algorithm. You may assume that all æows are integer-valued èthat is, fëu,vë is always an integerè and jf æjis the value of the maximal æow.
Name: 5 G is a directed graph with s and t vertices in G. ènote: Due to typesetting FORD-FULKERSONèG,s,tè 1 for each edge èu,vè in EëGë 2 do fëu,vë é- 0 3 fëv,uë é- 0 4 while there exists a path p from s to t in the residual network Gf 5 do cfèpè é- minícfèu,vè : èu,vè is in pí 6 for each edge èu,vè in p 7 do fëu,vë é- fëu,vë + cfèpè 8 fëv,uë é- - fëu,vë 5. è10 pointsè Simple circuit question èaè i. Draw a circuit that takes 3 bit inputs x; y; z and return 1 if their sum is odd and 0 if their sum is even. ii. Draw a circuit that takes 4 bit inputs x; y; z; w and return 1 if their sum is odd and 0 if their sum is even. èbè i. Draw a circuit that takes 3 bit inputs x; y; z. The circuit should have 2 outputs: c and s. The output c should be 1 iæ at least 2 of the inputs are 1. The output s should be 1 if the sum of the inputs is odd and 0 if sum of the inputs is even. ii. Use your circuit from above to design a circuit that will add two 4- digit binary numbers. èyou may represent the circuit from above as a ëblack box" instead of redrawing it for this part of the question.è 6. è10 pointsè èaè Draw an example of æow network with a æow that is not maximal. Label the edges of your graph with the æow and capacity of the edge. Also indicate an augmenting path for your æow. èbè Show that for any graph G =èv; Eè, there exists a minimal spanning tree that contains the minimal weight edge. 7. è20 pointsè Solve recurrences using generating functions èaè Solve the following recurrence: T n+2 =2T n+1, T n for n ç 0, T 0 =0;T 1 =1 èhint: let T èxè = Pn T n x n, solve for Tèxè, and use formulas about sums to decide the coeæentsè.
Name: 6 èbè Solve the following recurrence: T n = nx k=0 T k for n ç 1, T 0 =1 èhint: let T èxè = Pn T n x n, solve for Tèxè, and use formulas about sums to decide the coeæentsè. 8. è20 pointsè Design an algorithm... èaè è20 pointsè Design a greedy algorithm for storing web pages in memory. The total size of memory is M. As each page arrives, you should place it memory if space is available. If space is not available, you should remove another page èor pagesè from memory and insert your page. The pages will be speciæed by their size only. For example, if M = 10 and the sequence of page sizes is 1; 5; 8; 1, you would insert the ærst 2 pages, then need to remove at least the second page to insert the third page. After that you, you can insert the fourth page. Please specify the data structures you used and give an analysis of the program. èbè è20 pointsè Arbitrage is the use of discrepencies in currency exchange rates to transform one unit of currency into more than one unit of the same currency. For example, suppose that 1 US dollar buys 0.7 British pounds, 1 British pound buys 9.5 French francs, and 1 French franc buys 0.16 dollars. By converting currencies, a trader can start with 1 US dollar and end up with 1 æ 0:7 æ 9:5 æ 0:16=1:064 dollars, and then returning a proæt of 6.4è. Suppose that we are given n currencies c 1 ;c 2 ;:::;c n and an n æ n table R of exchange rates such that one unit of c i buys Rëi; jë units of currency c j. Give an eæcient algorithm for deciding whether or not there exists a sequence of currencies that will yield a proæt. Specify any data structures you use and give ananalysis of the program.