Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Question 1: The Tower of Hanoi problem: After creating the world, God set on Earth 3 rods and m rings. The rings are all different in size. At the creation the rings were threaded on the first rod in order of size, the largest at the bottom and the smallest at the top. God told a monastery to transfer all the rings onto the second rod. The only operation permitted is to move a single ring from one rod to another in such a way that no ring is ever placed on top of another smaller one. Below is the algorithm: /* suppose the rods are labeled 1, 2, and 3 */ Hanoi (m, i, j) /* to move m rings from rod i to rod j */ If (m>0) If (i=1 and j=2) or (i=2 and j=1) Then intermediate_rod = 3 If (i=1 and j=3) or (i=3 and j=1) Then intermediate_rod = 2 If (i=2 and j=3) or (i=3 and j=2) Then intermediate_rod = 1 Hanoi(m-1,i,intermediate_rod) Move top ring of rod i to rod j Hanoi(m-1,intermediate_rod,j) Try to analyze the running time of this algorithm by the recurrence tree method, substitution method, and master method. (You may find question 4 of the Recurrences tutorial exercises useful). Show how this algorithm follows the Divide-and-Conquer paradigm. [For more details, you may visit: http://www.cut-the-knot.com/recurrence/hanoi.html for an animation and explanation] Answer: Formulate the recurrence equation: Let T(n) be the running time of the algorithm when number of rings is n. (1) if n=0 T(n) = 2T(n-1)+ (1) if n>0 Or simplified: c if n=0 T(n) = 2T(n-1)+ c if n>0 where c is a positive integer. Analysis by Recurrence Tree method: Fully Expanded recursion tree: c c c n 2c = 21c c T(n-2) T(n-2) T(n-2) T(n-2) 2n-1 c T(1) c 4c = 22c c c c c 2n c xc c c Total: (20c+21c+22c+..+2n-1c)+2nc = c(1+2+22+..2n) = c * (2n+1-1)/(2-1) = c * (2n+1-1) = c * 2n+1 - c = c * 2 * 2n - c = (2c)*2n - c ie. T(n) = (2n) 1 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Analysis by Substitution Method From c if n=0 T(n) = 2T(n-1)+ c if n>0 where c is a positive integer. Step 1. We guess that T(n)=(2n). To prove this we need to show: There exists positive constants c1, c2, n0 such that 0 c1 * 2n T(n) c2 * 2n for all n n0. Since c1 is positive and 2n is always positive, we have 0c1 * 2n directly. Hence we only need to prove for c1 * 2n T(n) and T(n) c2 * 2n, ie. T(n) c1 * 2n and T(n) c2 * 2n Step 2a. We’ll prove for T(n) c1 * 2n first. Assume that it holds for n-1, ie. there exists positive constants c1, n0 such that T(n-1) c1 * 2n-1 for all (n-1) n0. Then T(n)=2T(n-1)+c => T(n) 2 * (c1 * 2n-1) + c => T(n) c1 * 2 * 2n-1 + c => T(n) c1 * 2n + c => T(n) c1 * 2n Hence we have shown that given any positive constants c1 and n0, for all (n-1) n0, it is true for T(n-1) implies it is true for T(n). We then need to find the base case n0. If n=0, T(0)=c=c*20 c1 * 2n if c1=c Hence n=0 can form a base case: We can conclude that when c1=c and n0=0, T(n) c1 * 2n for all n n0. Step 2b. We’ll prove for T(n) c2 * 2n. It is difficult to prove “T(n) c2 * 2n” directly from “T(n-1) c2 * 2n-1” Assume that it holds for n-1, ie. there exists positive constants c2, n0 such that T(n-1) c2 * 2n-1 for all (n-1) n0. Then T(n) = 2T(n-1)+c => T(n) 2 * (c2 * 2n-1) + c => T(n) c2 * 2 * 2n-1 + c => T(n) c2 * 2n + c => T(n) c2 * 2n Hence we prove by showing that “T(n) c2 * 2n - b” for some positive constants c2 and b and n0, n n0. Then we can see it implies “T(n) c2 * 2n”. Assume that it holds for n-1, ie. there exists positive constants c1, n0 such that T(n-1) c2 * 2n-1 - b for all (n-1) n0. Then T(n)=2T(n-1)+c => T(n) 2 * (c2 * 2n-1-b) + c => T(n) c2 * 2 * 2n-1 –2b + c => T(n) c2 * 2n – 2b + c => T(n) c2 * 2n – b – (b–c) => T(n) c2 * 2n – b if (b-c)>=0, or b>=c Hence we have shown that given any positive constants c2, and n0, and given any positive b >= c, for all (n-1) n0, it is true for T(n-1) implies it is true for T(n). We then need to find the base case n0. If n=0, T(0)=c=c*20 = c and c2 * 2n - b = c2 * 20 - b = c2-b >= c if c2 = 2c and b=c ie. when n=0, T(n) c2 * 2n - b if c2=2c and b=c Hence n=0 can form a base case with c2=2c and b=c. We can say that when c2=2c and b=c and n0=0, T(n) c2 * 2n - b for all n n0. And we can conclude that when c2=2c and n0=0, T(n) c2 * 2n for all n n0. Combining the results from Step 2a and 2b, we conclude that since there exists positive integers c 1, c2 and n0 (c1=c, c2 =2c, n=n0) such that 0 c1 * 2n T(n) c2 * 2n for all n n0, we can say that T(n)=(2n). 2 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Analysis by Master Method The master method applies only when the recurrence is in the form T(n)=aT(n/b)+f(n). Hence it cannot be used to solve our problem: T(n) = 2T(n-1)+ c Divide and Conquer Divide: Divide the problem to move m rings from source rod to target rod as 3 steps: move m-1 rods from source to intermediate rod, then move the top of the rings from source to target rod, then move m-1 rods from intermediate rods to target rod. The 1st and 3rd steps can be solved as smaller problems. Conquer: Solve the first subproblem: move m-1 rods from source to intermediate rod. Solve the second subproblem: move m-1 rods from intermediate rod to target rod. Combine: Execute the solution to the first subproblem, then move the top of the remaining rings from source to target rod, then execute the solution to the second subproblem. Question 2: Give asymptotic tight bounds for the followings using master method. Show your work. a. T(n) = 9T(n/3) + 2n2 + n/3 b. T(n) = 9T(n/4) + n2 c. T(n) = 3T(n/2) + n Answer: a. a=9, b=3, f(n)=2n2+n/3 nlogba = nlog39=n2 f(n)=(n2)= (nlogba) => case 2 => T(n)= ( nlogba lg n) = n2 lg n b. a=9, b=4, f(n)=n2 nlogba = nlog49 since log49 = log109 / log104 = 0.954 / 0.602 = 1.585, we have nlogba = n1.585 f(n)=nlogba+0.415=(nlogba+) where =0.415>0 Therefore, It is possibly case 3, but we still need to show “a f(n/b) <= c f(n) for some constant c<1 and all sufficiently large n” before making this conclusion. a f(n/b) = 9 * (n/4)2 = 9/16 * n2 <= c * f(n) when c=9/16 < 1, for all n. => case 3, ie. T(n) = (f(n)) = n2 c. a=3, b=2, f(n)=n nlogba = nlog23 since log23 = log103 / log102 = 0.477 / 0.301 = 1.585, we have nlogba = n1.585 f(n)=nlogba-0.585=O(nlogba-) where =0.415>0 => case 1, ie. T(n) = ( nlogba) = n1.585 Question 3: Refer to question 4 in the quiz. Write a recursive QUICK_FINISH(course_code) algorithm in pseudocode to solve the problem. This algorithm should return the optimal number of weeks to finish a course by a new student. You may assume that there is a way to sort the courses according to increasing difficulties, and that the prerequisites of a course s i must be easier than si. Write a dynamic programming algorithm for the problem. Write a memoization algorithm for the problem. Which algorithm is more efficient? Answer: Let d(si) be the shortest time that a new student can graduate from a program that has the compulsory course s i: d(si) = ki if p(i)=q(i)=0 min (ki+d(sp(i)), ki+d(sq(i))) otherwise 3 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Dynamic programming solution DYNAMIC_SHORTEST_GRAD(program_compulsory_course) Sort the courses according to increasing difficulties. For each course si taken according to this order if (pi=qi=0) di=ki else di = min (ki + dPi, ki+dqi) if si = program_compulsory_course return di return “input_error” Memoization solution MEMOIZATION_SHORTEST_GRAD(program_compulsory_course_id) 1 For i=1 to n 2 di= 3 return LOOKUP_SHORTEST_FINISH_COURSE (program_compulsory_course_id) LOOKUP_SHORTEST_FINISH_COURSE(course_id) 1 if dcourse_id< 2 return dcourse_id 3 if (pcourse_id=qcourse_id=0) 4 dcourse_id = ki 5 else 6 7 dcourse_id =min (kcourse_id + dPcourse_id, kcourse_id+dqcourse_id) return dcourse_id Which algorithm is more efficient? Memoization solution is more efficient since it needs not solve for those courses that won’t be involved. Also, no sorting is needed. These should be good reason to use Memoization although it has some minor overhead on table maintenance and recursion. Question 4: A ski rental agency has m pairs of skis, where the height of the ith pair of skis is si. There are n skiers who wish to rent skis, where the height of the ith skier is hi. Ideally, each skier should obtain a pair of skis whose height matches his own height as closely as possible. Now, we want to assign skis to skiers so that the sum of the absolute differences of the heights of each skier and his skis is minimized. Formulate a recursive function to solve this problem. Answer I: Suppose the skis and the skiers are shorted according to their heights, so that h1 h2 … hn and s1 s2 … sm There is no advantage to do “cross matching”. Let Ai,j be the minimum sum of differences to match the first i skiers with the firs j pairs of skis. Then 0 if i=0 Ai,j = min(Ai,j-1, Ai-1,j-1 + |hi – sj|) if 1ij if i>j An,m is our solution. 4 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Answer II: Suppose the skis and the skiers are shorted according to their heights, so that h 1 h2 … hn and s1 s2 … sm There is no advantage to do “cross matching”. Define w as the 2-D matrix of differences in heights such that wi,j is the difference of the heights of ith skier and the jth pair of skies. jth pair of skies 2 3 4 5 .. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? m-1 ? ? ? ? For the matching we may consider: jth pair of skies wi,j 0 1 2 3 4 5 .. 0 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ith skier .. ? ? ? ? ? ? ? n-1 ? ? ? ? ? ? ? m-1 ? ? ? ? For the matching we may also consider: jth pair of skies wi,j 0 1 2 3 4 5 .. 0 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? th i skier .. ? ? ? ? ? ? ? n-1 ? ? ? ? ? ? ? m-1 ? ? ? ? For the matching we may even consider: jth pair of skies wi,j 0 1 2 3 4 5 .. 0 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ith skier .. ? ? ? ? ? ? ? n-1 ? ? ? ? ? ? ? m-1 ? ? ? ? wi,j ith skier 0 1 .. n-1 0 ? ? ? ? 1 ? ? ? ? Denote Dp,a,q,b as the minimum sum of differences to match a skiers starting from pth one: ( pth, (p+1)th, (p+2)th, .. (p+a-1)th skiers ) to b pairs of skies starting from the qth one: ( qth, (q+1)th, (q+2)th, .. (q+b-1)th skiers ) Such that all p,q,a,b are non negative integers and p+a<n, q+b<m. 5 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 if a=0, then Dp,a,q,b = 0 if a>b, then Dp,a,q,b = For any p,q,a: We have Dp,a,q,a = w(p,q) + w(p+1,q+1) + w(p+2,q+2) + .. + w(p+a-1,q+a-1) [Easy to calculate] p p+1 .. p+2 q q+1 p+a-1 .. q+2 q+a-1 Consider Dp,a,q,a+1 = min t=0 to a (Dp,t,q,t+Dp+t,a-t,q+t+1,a-t) a t p p+1 .. p+2 q q+1 p+t-1 .. q+2 .. .. p+t q+t-1 p+a-1 .. .. q+t q+t+1 q+a a+1 Consider Dp,a,q,a+2 = min t=0 to a (Dp,t,q,t+Dp+t,a-t,q+t+1,a+1-t) a t p p+1 .. p+2 q q+1 p+t-1 .. q+2 .. .. p+t p+a-1 q+t-1 q+t q+t+1 .. .. q+a q+a+1 a+2 Similarly: Consider Dp,a,q,a+3 = min t=0 to a (Dp,t,q,t+Dp+t,a-t,q+t+1,a+2-t) Hence we can see that Dp,a,q,b = min t=0 to a (Dp,t,q,t+Dp+t,a-t,q+t+1,b-1-t) a t p p+1 p+2 q q+1 .. p+t-1 p+t q+2 .. .. .. p+a-1 q+t-1 q+t q+t+1 .. .. .. q+b-1 b And D0,n,0,m is our solution. 6 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Question 5: Is the following statement right or wrong? Prove it if it is correct (eg. by proving directly or proving by contradiction); construct a ‘simple’ counter example if it is wrong. (Your answer should be brief.) a. In an undirected graph with positive edge weights, the shortest edge in the graph always belongs to any tree of shortest paths, provided the edge weights are distinct. b. To find the longest path between 2 given vertices in a graph G with positive weights, we can change the weight of every edge e from w(e) to k – w(e), where k is a value larger than any edge weight in G, and then find the shortest path in the resultant graph. c. If T is a tree of shortest paths from vertex s in a graph G, then T is also a tree of shortest paths from vertex s in a graph G’ obtained by increasing the weight of every edge by same value c. Answer: a. False. Consider the following graph: (B,C) is shortest edge. Using A as the source vertex, the tree of shortest path does not include (B,C). A 201 B b. False. 9 200 C Consider the following graph (The longest path from A to B should be A->C->B): A 1 B 2 C 3 If change every edge weight from w(e) to 100 – w(e), we have: A 99 98 C B 97 After the change, the shortest path from A to B is A->B, not A->C->B. Hence we can’t use this method to find the longest path in such a graph. c. False. Consider the following graph A->B->C is the only tree of shortest paths from A. A 1 9 C B 2 If we increase the weight of every edge by 100, we get: A 101 109 C B 102 For which the following is the only tree of shortest path from A: A 101 B 109 C 7 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Question 6: A bus company operates k bus routes serving n different bus stops. The i-th route, Pi, passes through mi stops and can be represented as a sequence of stops, (vi,1 => vi,2 => vi,3 => …=> vi,Mi), where vi,1 and vi,Mi are the first and last stops respectively. The return route (vi,Mi => vi,Mi-1 … vi,1) is considered a different route. The bus fare is ci dollars regardless of where you get on and off along the i-th route. Given 2 bus stops, s and t, we want to find the cheapest cost to travel from s to t. The problem can be specified as follows: Input: integer k > 0 specifying the number of routes, integer n > 0 specifying the number of different bus stops, array C[1..k] storing the cost of each route, array R[1..k] of pointers where R[i] points to a linked list containing the sequence of stops for the i-th route, integers s and t, specifying the start and destination stops. Output The cheapest cost to travel from stop s to t. Design an efficient algorithm to solve the above problem. Present your algorithm in pseudocode. Analyze the running time of your algorithm in terms of n and k. (Hint: Construct a weighted directed graph with n vertices.) Answer: Build a weighted directed graph with n vertices to represent n bus stops. For each pair of vertices, there can be some route that passes through both the source vertices and the target vertices, and that this route pass the source first before reaching the target vertex. If there is such a route or more than one such routes, join that pair of vertices with directed edge, and mark the edge weight as the cheaper route’s cost. Use Bellman-Ford or Dijkstra’s algorithm to find shortest path from the source vertex to the target vertex. CHEAPEST_ROUTING(k,n,C,R,,s,t) 1 Build a graph G with n vertices representing the bus stops. 2 for i=1 to k 3 p = first node of R[i] 4 while p <> NULL 5 q = p.next 6 while q <> NULL 7 if there is no edge from p.Vertex to q.Vertex 8 add the edge from p.Vertex to q.Vertex, with weight = C[i] 9 else 10 if weightp,q > C[i] 11 change the weight of the edge from p.Vertex to q.Vertex 12 q = q.next 13 p = p.next 14 Run Bellman-Ford [O(VE)] or Dijkstra [O(E lg V)] to find shortest path from s to t. Line 1: O(n) Line 2-13: O(kn2) Line 14: O(n3) or O(n2 lg n ) since there are n vertices and O(n2) edges. 8 Assignment and solutions CS3381 Design and Analysis of Algorithms Helena Wong, 2001 Question 7: A cycle is a path that starts and ends at the same vertex in a graph. The Hamiltonian Cycle (HC) problem is defined as follows. Input: an undirected graph G Output: “yes” if there is a cycle that visits every vertex in G exactly once; and “no” otherwise. The problem of Hamiltonian Path Between Two Points (HPBTP) is defined as follows. Input: an undirected graph G and two vertices, s and t, in G. Output: “yes” if there is a path with end-points s and t that visits every vertex in G exactly once; and “no” otherwise. Prove that HC is NP-complete assuming that HPBTP is NP-complete. Hint: Imagine another graph G’, that is the mirror image of G. Similar to G, there are also 2 vertices: s’ and t’ in G’. Connect s’ with s, and t’ with t. Observe what you get. [This question relates to the topics of NP-completeness, that will be introduced during week 12 lecture.] Answer: Part A: Prove that HC NP Consider the algorithm: bool Verify_HC (Input_Graph, Certificate /*a set of vertices in a c*/) This algorithm checks whether the certificate covers all vertices in the input graph exactly once and whether the sequence of the vertices represents a valid path (each vertex has an edge to the following vertex in the sequence), and whether there is an edge joining the first and the last vertex in the sequence. This algorithm runs in polynomial time. Since we can find such a 2-input polynomial-time verification algorithm for HC, we say that HC NP. Part B: Prove that HC is NP-hard We design an algorithm that can reduce any instance of HPBTP in polynomial time to an instance of HC as following: Given an instance of HPBTP, G, we construct a duplicate graph G’. Let the vertices in G’ corresponding to s and t in G be s’ and t’ respectively. Add the edges {s,s’} and {t,t’}. The resultant is an instance of HC. This construction takes polynomial time. If G+G’+{s,s’}+{t,t’} is a yes instance to HC, then (G,s,t) is a yes instance to HPBTP. If (G,s,t) is a yes instance to HPBTP, then G+G’+{s,s’}+{t,t’} is a yes instance to HC. 9