U.C. Berkeley CS170 : Algorithms Midterm 1 Solutios Lecturers: Sajam Garg ad Prasad Raghavedra Feb 1, 017 Midterm 1 Solutios 1. (4 poits) For the directed graph below, fid all the strogly coected compoets ad draw the DAG of strogly coected compoets. Label each strogly coected compoet with all the odes it cotais. B A J C D E F G H Draw the DAG i the box below: B AJ CDEGH F. (8 poits) Execute DFS o the same graph (reproduced here for coveiece) startig at ode A ad breakig ties alphabetically. Draw the DFS tree/forest. Mark the pre ad post values of the odes with umberig startig from 1.
B A J C D E F G H Node pre post A B C D E F G H J Draw the DFS Tree/Forest i the box below: B A J C D E F G H Node pre post A 1 4 B 5 18 C 6 17 D 7 16 E 8 15 F 11 1 G 10 13 H 9 14 J 3 3. (4 poits) I the DFS executio from above, mark the followig edges as as T for Tree, F for Forward, B for Back ad C for Cross. (No justificatio ecessary)
Edge G! C A! J B! A B! D Type B T C F 4. (a) (4 poits) Draw a strogly coected graph with 6 vertices with the smallest possible umber of edges i the box below. A B C D E F (b) ( poits) I geeral, the miimum umber of edges i a strogly coected directed graph with vertices is. (o justificatio ecessary) You ca t have less tha edges as it will make the graph discoected. So the miimum is simply a loop through all the vertices. 5. (6 poits) Suppose G =(V,E) is a udirected graph with positive iteger edge weights {w e e E}. We would like to fid the shortest path betwee two vertices s ad t with a additioal requiremet: if there are multiple shortest paths, we would like to fid oe that has the miimum umber of edges. We would like to defie ew weights {w 0 e e E} for the edges so that, a sigle executio of Dijkstra s algorithm o the graph G with ew weights {w 0 e}, startig from s fids the shortest path to t with this additioal requiremet. How should we set the ew weights w 0 e No justificatio ecessary. w 0 e = w e + 1 V The ituitio here is that we wat to add somethig to the edges so that we ca di eretiate a path usig 4 edges ad a path usig 5 edges but with the same distace. I the meatime, we eed to make sure the origial Dijkstra s does t break. Therefore, we add the fractio 1 V so that the total legth of ay path will be icreased by at most 1. Sice all the edges are positive itegers, Dijkstra s will ru exactly the same as before. 6. (8 poits) Here is a implemetatio of Bellma-Ford algorithm: Iput: Directed Graph G =(V,E), with edge legths {`e e E}. Output: Compute distaces dist(u) to each vertex u from a start vertex s. 3
for i =1to do dist(u) 1 prev(u) il ed for dist(s) 0 k 0 repeat for i =1to do for each directed edge (i, j) do update(i, j) ed for ed for k k +1 util all dist values stop chagig OR (k = ) Algorithm 1: Bellma-Ford Algorithm It turs out that the rutime of the above algorithm ca be very sesitive to the way i which vertices i a graph are umbered. I other words, the rutime of the algorithm o the same graph ca widely vary, if we chage the umberig of the vertices. Give oe graph G o 11 vertices ad two ways to label the vertices of G, such that i oe labellig the algorithm makes 0 calls to update, while i the other labellig the algorithm termiates i 10 calls to update. The followig graph will call update exactly 0 times. A B C D E F G H I J K The followig graph will call update exactly 10 times. K J I H G F E D C B A 7. (6 poits) We computed the miimum spaig tree T o a graph G with costs {c e } ee. Ufortuately, after computig the miimum spaig tree, we discover that the costs of all the edges i the graph have chaged as follows: the ew cost w e are give by, w e = ( c e if c e > 100 0 if c e apple 100 Is the tree T that we computed earlier, still a miimum spaig tree of the graph Write yes or o : yes If yes, prove; if o, disprove with a couterexample. Cosider a edge e that is part of the MST T composed from graph G with costs {c e } ee. will show that every such edge e must also be part of the MST T 0 after the weight update. We Case 1 Suppose c e apple 100, the w e = 0. Sice e was part of T, it is a edge with the miimal weight coectig 4
two sub-msts. If we have a edge e 0 such that c e 0 apple 100, the i the ew MST T, w e = w e 0. Thus edge e will still be part of MST T 0 as addig either e or e 0 would ot add ay cost to the total cost of T 0.Ifc e 0 > 100, the w e <w e 0 = 0 ad thus e is part of MST T 0. Case Now suppose c e > 100, the w e = c e. Suppose for the sake of cotradictio that e is ot part of MST T 0. There must be some edge e 0 part of MST T 0 coectig the two sub-msts that e is coectig. If w e 0 = 0, the c e 0 apple 100 <c e. Thus e 0 must be part of MST T rather tha e. This is a cotradictio because e is part of T ad havig both e ad e 0 coectig the two sub-msts would create a cycle ad violate the tree property. Now if w e 0,c e 0 > 100 ad we assume that e 0 is part of T 0 rather tha e, c e 0 < c e.thusc 0 e <c e. Agai e 0 must be part of MST T, a cotradictio. [The most commo mistake was to igore the case where a edge e of weight c e > 100 may be replaced by a edge e 0 with prior weight c e 0 apple 100. For full credit, we eeded a explaatio of why this case was impossible.] Alterative solutio A less commo, but correct alterative solutio was to argue that Kruskal s algorithm produces valid MSTs by cosiderig each edge e G i icreasig order of weight c e. The orderig of the edges is preserved i this update. Therefore, if T was a valid solutio produced by Kruskal s algorithm prior to the update, the T will remai a valid solutio after the update. 8. (6 poits) I this graph, some of the edge weights are kow, while the rest are ukow. B 3 E F 1 A D 4 C G H cost(a, D) =, cost(b,d) =1, cost(c,d) =4, cost(b,e) =3 List all edges that must belog to a miimum spaig tree, regardless of what the ukow edge weights tur out to be. Justify each of your edges briefly (a setece or less is eough). Edges that must belog to every MST Justificatio G H This is the oly edge that ca coect H to ay other vertex, so it must be icluded i ay MST. Remember that ay MST must spa all of the vertices. B D The cut property. More details: let S 1 = D, ad S = V S 1 = ABCEF GH. The three edges from the origial graph will coect these two forests, with edge weights: 1,, 4. So ay MST must cotai the B D edge of weight 1. B E Also by cut property. Cosider S 1 = ABD, ad S = V S 1 = CEFGH. The the 3 edge is the shortest edge coectig both forests. 5
Alterative justificatios: Ru Prim s MST algorithm, startig from H. H G is the shortest edge, so it will be added to the MST. The we kow at least oe MST will iclude this edge. Ad similarly, we ca ru Prim s, startig from D. ThetheD to the MST, so we kow at least oe MST will iclude D B. B edge is the first edge to be added But for B E, the Prim s argumet gets more complicated. You could argue that ruig Prim s startig from ay of ABD will always add the edge of weight 3, ad ever the edge of weight 4. 9. Desig a e ciet algorithm for the followig problem Iput: umbers {a 1,...,a } Goal: Compute the polyomial with a 1,...,a as its roots. I other words, compute coefficiets b 0,...,b so that (x a 1 ) (x a ) (x a )=b 0 + b 1 x +...b x. (Hit: Try divide ad coquer & use O( log ) time polyomial multiplicatio algorithm as a blackbox) (a) (10 poits) Pseudocode: procedure PolyomialWithRoots({a 1,...,a }): if =1the b 0 a 1 b 1 1 retur b 0,b 1 ed if q(x) PolyomialWithRoots({a 1,...,a b/c }) r(x) PolyomialWithRoots({a b/c,...,a }) p(x) MultiplyPolyomials(q(x), r(x)) retur coe ciets of p(x) (b) (3 poits) Write the recurrece for the ruig time of the algorithm i the box. T () = T + O( log ) (c) (6 poits) Solve the recurrece to compute the ruig time ad put your aswer i the box. Show your work below the box. T () = O( log ) To show this, we use the recurrece tree: log Sum: log log v ( log Sum: log! 4 log 4 4 log 4 4 log 4 }! 4 log 4 Sum: log 4. 6
The total, the, is log + log = log + log + log 4 + log + + 4 = (log + (log log ) + (log log 4) + ) = ((log + log + + log ) (log + log 4 + + log )) = (log ) (1 + + + log ) log log = log = log We could also use a more geeral versio of the Master Theorem, which states that if T () = at (/b) +f(), where a 1, b>1, ad f() = ( c log k )wherec = log b a,thet () = c log k+1. I this case, a = b =, c = 1 = log b a = log, ad k = 1. 10. (13 poits) You are give the road etwork G =(V,E) of a coutry, ad the legths {`e e E} of each road i the etwork. Some of the cities have airports, while others do t. Let F be the subset of cities that have a airport i them. Devise a algorithm to compute the distace from each city to the earest airport. (Assume that the graph is directed ad that all edge legths are o-egative). Remember every correct algorithm will receive a score depedig o its rutime. (ca you do it with the same ru-time as Dijkstra s). (a) Mai Idea: (try less tha 6 seteces if you ca, but do t fret if you go over) Reverse the graph. Add a dummy source vertex s to the graph. Add a zero weight edge from the source vertex to each airport. The ru Dijkstra o s to fid the distace from s to every vertex i the graph. This shortest distace from each city to the earest airport is the distace from the vertex that represets this city to vertex s. It is equivalet to merge all airports ito a sigle vertex or to ru a modified Dijkstra s i which all airports are iitialized with dist = 0. Brute force solutios were commo, ivolvig ruig Dijkstra from each vertex or just from each airport vertex. These are correct but ie ciet, as the ruig time will be O( V ( V + E ) log V ) or O( F ( V + E ) log V ), ad we have o boud o F except that it is at most V. (b) Rutime of the algorithm = O(( V + E ) log V ) (c) Proof of Correctess (try less tha 4 seteces if you ca, but do t fret if you go over) This is a directed graph ad the dummy ode s that we add is a source ode which has oly edges goig out ad o edges goig i. Thus addig the ode s wo t add a path betwee ay two odes from the origial graph. I additio, we set the legth of all edges of s to zero, so it wo t add ay additioal legth. Through Dijkstra (which we kow fids shortest distaces from a source ode to all others) we will fid the shortest distace from each ode to ode s (because 7
this is equivalet to the reversed path i the reversed graph), which is the same as the shortest path to ay airport. A brute force solutio ca be see to be correct by otig that Dijkstra will fid the shortest path betwee every relevat pair of odes, via multiple calls, ad that we ca compare them to fid the miimum i each case. 11. (10 poits) Suppose you are give a array A[1...] of sorted itegers that has bee circularly shifted k positios to the right for some k. For example, [35, 4, 5, 15, 7, 9] is a sorted array that has bee circularly shifted k = positios, while [7, 9, 35, 4, 5, 15] has bee shifted k = 4 positios. We ca obviously fid the largest elemet i A i O() time. Assumig all the itegers i the array are distict, describe a O(log ) algorithm to fid the largest elemet i A. Brief but precise descriptio of the algorithm: (try less tha 6 seteces if you ca, but do t fret if you go over) Mai idea: We wat to leverage the sorted-ess of the array, so we will implemet a modificatio of biary search where you always recurse o the usorted array. Compare A[0] ad A[mid] (where mid = d e). If A[0] <A[mid] the the left half of the array is sorted; the max value is either the mid or i the right half of the array. Store the mid as the temporary max ad repeat o the right half. If A[0] >A[mid] the the left half is usorted ad the max is somewhere i that array, so repeat o the left half. We ca also compare the mid poit with the last elemet. If A[last] <A[mid], we look at the right half of the array, icludig the mid poit. If A[last] >A[mid], we look at the left half of the array. Other splits are also possible. For example, comparig A[d 4 e]witha[d 3 4 e]. As log as a solutio compares poits that are far eough apart, it ca be used to determie the portio of the array to make the recursive call. If left poit is greater tha right poit, make the recursive call o the elemet betwee them. Otherwise the maximum elemet is i the other part of the array, which ca be obtaied by joiig the two parts i the same relative positioig, i.e. A[1,...,d 4 e]+a[d 3 4 e,...,]. A commo mistake was to compare immediate eighbors. But this ca oly be used to determie if the left elemet is the max. It does ot have eough iformatio to tell you which side the max is o ad thus caot obtai a O(log ) solutio. 8