Fall 2007, Final Exam, Data Structures and Algorithms Name: Section: Email id: 12th December, 2007 This is an open book, one crib sheet (2 sides), closed notebook exam. Answer all twelve questions. Each Question is worth 10 points. You have 180 minutes to complete the exam. Happy Holidays and Have a Great New Leap Year. The formulas you need are: i=n i=0 i=n i=0 1 Formulas i=n i=0 i = n(n + 1)/2. i 2 = n(n + 1)(2n + 1)/6. a i = (1 a n+1 )/(1 a), where a 1. 1
1. Graphs/DFS/Path The 49 postal codes in USA (48 continental states and Washington DC) are AL, AR, AZ, CA, CO, CT, DC, DE, FL, GA. IA, ID, IL, IN, KS, KY, LA, MA, MD, ME, MI, MN, MO, MS, MT, NC, ND, NE, NH, NJ, NM, NV, NY, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VA, VT, WA, WI, WV, WY, in alphabetical order. Suppose we consider two states be adjacent if their postal codes agree in one place (Constructing an undirected graph with 49 states as vertices/nodes and the edges are defined as given above- However, you need not construct the graph to answer the questions below.).[10 points] (a) What is the degree of vertex/node NY? (b) Write a path of length 2 from CA to NY. (c) Write the paths (any length is acceptable) from AZ to ME, WA to SC and WA to FL. (d) Is the graph Connected? 2
2. Shortest Path Algorithms/Spanning Tree Here is a map of United States. as an undirected graph with adjacent states connected by an edge. [10 Points] (a) what is the shortest path from ND to SC (using Dijkstra s algorithm for unweighted graphs) (b) Give two different paths which are edge disjoint from NY to CA. (c) Draw DFS Spanning tree starting at NY (d) Draw a BFS Spanning tree starting at NY. 3
3. Eulerian Cycles, Topological Sort (a) Draw a simple connected directed graph with 8 vertices and 16 edges such that the in-degree and out-degree of each vertex is 2. Show that there is an Eulerian Cycle by listing the cycle.[5 Points] (b) Mr. Kay loves foreign languages and wants to plan his course schedule to take the following nine language courses: LA15, LA16, LA22, LS31, LA32,LA126,LA127,LA141 and LA169. The course prerequisites are LA15: (none) LA16: LA15 LA22: (none) LA31: LA15 LA32: LA16,LA31 LA126: LA22,LA32 LA127: LA16 LA141: LA22,LA16 LA169: LA32 Find a sequence of courses that allows Bob to satisfy all the prerequisites.[5 Points] 4
5
6
4. Network Flows Consider the following network (the numbers are edge capacities).[10 Points] (a) Find the maximum flow, f, from S to T (b) Draw the residual graph G f (along with edge capacities). In this residual network, mark the vertices reachable from S and the vertices from which T is reachable. (c) An edge in a network is a bottleneck edge if increasing its capacity results in an increase in the maximum flow. List ll bottleneck edges in the above network. (d) Give a very simple example (containing at most 4 nodes/vertices) of a network which has no bottleneck edges. 7
5. Dynamic Programming/Dijkstra Yuckdonald s is considering opening a series of restaurants along Judson Valley Highway (JVH). The n possible location are along a straight line, and the distances of these locations from the start of JVH are, in miles, and in increasing order m 1,m 2,,m n.the constraints are as follows: At each location, Yuckdonald s may open at most one restaurant. The expected profit from opening a restaurant at location i is p i, where p i > 0, for i = 1,2,,n. Any two restaurants should be at least k miles apart, where k is a positive integer. Give an efficient algorithm to compute the maximum expected total profit, subject to the given constraints. [10 Points] 8
6. Huffman (a) A long string consists of Characters A, C, G, T.; they appear with frequency 31%.20%,9% and 40% respectively. What is the Huffman encoding of these four characters? [5 Points] (b) We use Huffman s algorithm to obtain the encoding of alphabet {a, b, c} with frequencies f a,f b,f c. In each of the cases, either give an example of frequencies f a,f b,f c that would yield the specified code, or explain why the code cannot be possibly obtained (no matter what the frequencies are) [5 Points] i. Code:{0,10,11} ii. Code: {0,1,00} iii. Code: {10,01,00} 9
7. Analysis, Greedy (a) Give a linear time algorithm that takes input as a tree T and determines whether it has a perfect matching (i.e., a subset of edges of the given tree T) that touches each vertex only once). edges O(n), where n is the number of vertices in a tree.[5 Points] (b) Solve T(n) = T( n ) + n and T(1) = 1. 2 [5 Points] 10
8. Recursion, Search How many lines does the following program print for the function call f(16). Generalize this for f(n), n = 2 k.[5 Points] void f (int n) { if (n > 1) { cout << Still Going << endl; f(n/2); f(n/2); } } (a) Given a sorted array of distinct integers A[1,...n], you want to find an index i, which A[i] = i. (If no such index exists, you may report it too) Give an algorithm that runs in O(log n) time.[5 Points] 11
9. Selection, Median Professor Oiler is consulting for an oil company, which is planning a large pipeline running east to west through an oil field of n wells. The co-ordinates of each well is (x i,y i ). For each well, a spur pipeline is to be connected directly to the main pipeline along a shortest path (either north or south) as shown in the following figure. Given x, y co-ordinates of the wells, how should the professor pick the optimal location of the main pipeline (the one that minimizes the total lengths of the spurs)? Show that the optimal solution can be determined in linear time. The main pipeline is a line parallel to X-axis; i.e., the main pipeline is a horizontal line. For inefficient algorithm will get 5 points. Hint: A median, y m of a set of n numbers {y 1,y 2,,y n } minimizes the sum of absolute difference of y i y m [10 Points] 12
. 10. Heap Sort (a) Is the sequence 23, 17, 14, 6, 13, 10, 1, 5, 7, 12 a max heap? If not convert into a max heap.[4 Points] (b) Where in a max heap of size 12, might the smallest element reside assuming all elements are distinct.[3 Points] (c) What are the minimum and maximum number of elements in a heap of height h? [3 Points] 13
11. Hash Table, Binary Serach Tree (a) Write a recursive algorithm for inserting a key in a Binary Search Tree. [5 Points] (b) Demonstrate the insertion of the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a Hash Table of size 9 with collissions resolved by chaining. Let the hash function be h(k) = k mod 9. [5 Points] 14
12. Sorting Give an algorithm (sequences of comparisons) to sort 5 elements with 7 comparisons. (If you take more than 7 comparisons, you will get half the credit.) [5 Points] (a) Sort 3, 1, 4, 1, 5, 9, 2, 6 using merge sort (Show your steps.) [5 Points] 15