Algorithms and Data Structures COMP 251) Midterm Solutions March 11, 2012 1. Stable Matching Problem a) Describe the input for the stable matching problem. Input: n men and n women. For each man, there would be a preference list ranking each woman in order, and for each woman, there would be a preference list ranking each man in order, i.e., 2n permutations of {1, 2,..., n}. b) Consider two matched paris m, w) and m, w ) in a matching of n men and n women. Dene what it means for m, w ) to be an instability. An instability in this case means a man m ranks woman w higher than w and a woman w ranks man m higher than m. c) True or False Give proof or counterproof). Suppose in a stable marriage problem with n men and n women, a man m is ranked rst on the list of a woman w, and w is ranked rst on the list of man m. Claim: m, w) is a matched pair in every stable matching. True. Suppose not, then there is an instability. If it is m, w ), then w is ranked higher than w by m, which is a contradiction since rank w) = 1 for ) m. If it is m, w, then m is ranked higher than m by w, which is a contradiction since rank m) = 1 for w. 1
2. Growth Rate and Divide and Conquer a) Rank the following list of functions in increasing order of growth rate, i.e., if f n) is ranked before g n), then f n) = O g n)). i)n 2 ii)n 21/10 iii) 21/10) n iv) 21n v) 3 n 21n, n 2 logn, n 21/10, 21/10) n, 3 n. b) Calculate directly T 16) where: T 2) = 2T 1) + 4 = 8 T 4) = 2T 2) + 8 = 24 T 8) = 2T 4) + 16 = 64 T 16) = 2T 8) + 32 = 160 T 2n) = 2T n) + 4n, T 1) = 2 c) Prove for any k > 0 that T 2 k) = k + 1) 2 k+1, where T is dened as in 2b). By induction: k = 0, T 2 0) = T 1) = 1.2 1 = 2 Assume for k 0, T 2 k) = k + 1) 2 k+1 For k + 1, T 2 k+1) = 2T 2 k+1) + 2 k+2 = 2 k + 1) 2 k+1 + 2 k+2 = k + 2) 2 k+2. 2
3. Graphs a) Describe what is meant by breadth rst search of a graph. Illustarate on a graph with about 6-8 nodes. Let G = V, E) and let u ɛ V be any vertex. Set L 0 = {u}. We dene L j by choosing all vertices not yet chosen that are adjacent to vertices in L j 1, for j = 1, 2, 3,... b) A prof wants to divide her n students into two study groups. For each student X she has a list of students she does not want in the same study group as X. Give a linear time algorithm that either outpus two study groups with the desired property, or shows that this is not possible. Illustrate with a small example for each case. Let the students be {1, 2,..., n} and the groups be A and B. Put 1 ɛ A. Do a BFS from 1. If j=even, L j is in A else L j is in B. If there is any edge inside L j, then the answer is NO. The graph above shows a NO case. If there are remaining nodes, keep doing BFS until all students are placed, i.e., original graph is disconnected. The graph below shows a YES case. 3
4. Greedy Algorithms a) Does Dijkstra's shortest path algorithm work correctly when the input, which is a directed graph, can have negative edge weights? Give a proof or counter example, and justify your answer. No. E.g., consider the graph, To nd the shortest path, Dijkstra computes d s, t)=4, using the two edges costing 2 each as shown below, but it actually is 1, obtained by 6 + 5)). b) You are given a connected, undirected graph where the vertices are routers and the edges are communication links between routers. For each edge there is a cost associated. It is required to connect all the routers together as cheaply as possible, without spending more than k dollars on any given link. Name an algorithm that was presented in class that could be used, and argue why it correctly solves this problem. Kruskal's algorithm. First, the edges are ranked by increasing weights w 1 w 2... w m, where m is the total number of edges. Kruskal's algorithm takes edges in this order until a spanning tree is found. If the last edge chosen, w t k, we are done. If not, then w t > k, but by construction the edges with weights > k do not form a spanning tree. 4
5. Dynamic Programming The input for the sushi problem are positive integers n,b,c 1,c 2,...,c n and p 1,p 2,...,p n. There are n pieces of sushi labelled 1,2,...,n. For each i=1,2,...,n, piece i costs c i dollars and gives you p i units of pleasure when you eat it. Your goal is to maximiaze your total eating pleasure by spending at most B dollars on shushi. a) Let M j, k) be the maximum pleasure you can obtain with the budget of k dollars and can choose from pieces of sushi labelled 1,2,...,j. Write down a dynamic programming recursion to compute M j, k) for j=1,2,...,n and k=1,2,...,b. M 0, k)=0, k=0,...,b. for j=1,...,n for k=0,...,b M j, k)=max{m j 1, k), p j + M j 1, k c j )}. b) Consider the input data n=4, B=8, c 1 =2, c 2 =4, c 3 =6, c 4 =3 and p 1 =1, p 2 =6, p 3 =9, p 4 =5. Write down the table of values M j, k) for j=1,2,3,4 and k=1,2,...,8. 0 1 2 3 4 5 6 7 8 Ø 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1,2 0 0 1 1 6 6 7 7 7 1,2,3 0 0 1 1 6 6 9 9 10 1,2,3,4 0 0 1 5 6 6 9 11 11 5