CSE 502 Fundamentals of Computer Science Spring Semester 2015 Homework 5 Assigned: 4/10/2015 Due Date: 4/21/2015 You must read and sign the following statement to get credit for this assignment: I have read and understood the CSE 502 collaboration policy posted on the course web site. I understand that these rules govern homeworks, labs, and exams assigned in this class, and that violations are subject to severe penalties. I have neither violated the collaboration policy nor helped others to violate it in completing this assignment. Name: Signed: Date: In the box below, please describe any sources of help you received in completing this assignment,and for which problem(s) the help was received. You need not list the instructor & TAs, our book/notes/handouts, or the class Piazza forum. Problem Pts Score Grader Problem Pts Score Grader 1 25 4 25 2 25 5 25 3 25 Total
To receive full credit for the assignment, you must: print and include the required cover sheet; staple all pages together and write your name on each page; typeset or hand-write all solutions clearly, concisely, and legibly; show your work. The approximate weights of each problem (out of 100%) are given in parentheses, but things that come up in the course of grading may cause these weights to be adjusted. Note that you need only do any four problems out of five to get full credit for this homework, though you may do all five for extra credit. Unless otherwise noted, all occurrences of log refer to the base-2 logarithm. 1. (25%) Consider the following min-first binary heap stored as an array: {3, 6, 5, 8, 12, 9, 7, 10, 11, 14}. (a) Show the state of the heap after each of the following series of calls: insert(1); insert(4). Each call should modify the result of the previous call. To help us grade, please indicate which elements change during each insertion. (b) Starting from the original array before any insertions, show the state of the heap after each of the following series of calls: extractmin(); extractmin(); extractmin(). Again, each call should modify the result of the previous call. To help us grade, please indicate which elements change during each extraction. (c) A binary heap can be made to support deletion of an arbitrary element in worst-case time Θ(log n). (Note: this is not the deletion of a particular element of your choice; just an element with arbitrary index.) Write pseudocode for a procedure Delete(i) that removes the element in slot i of an array-based min-first heap Q containing exactly n elements and and fixes up the heap afterwards. Your code may call the insert() and extractmin() functions from the class notes, but should otherwise manipulate the heap s elements directly. (Hint: extractmin() will remove the root element... can you manipulate the element in slot i so that it ends up in the root position, all the while maintaining a valid heap?) Justify the running time of your procedure (informally if you like). 2. (25%) A heap can be used to sort an array in worst-case time Θ(n log n). More interestingly, this sort can be done in place without using temporary storage other the the input array. (a) Given an unsorted array A[1... n] of length n, show how to use n 1 heap insertions to turn A into a max-first heap. You should not use any arrays besides A itself to hold array elements. You do not need to provide pseudocode, but your description must be precise enough to be unambiguous. (Hint: start the heap from A[1] with size 1 and let it expand gradually into higher-numbered cells. In what order must you insert the elements?) 1
(b) Given your max-first heap A from (a), show how repeated extractmax operations can rearrange the elements of A into sorted order. (Hint: Which cells of the array become free to hold the extracted values?) Again, you should not use any arrays besides A itself to hold array elements, and you do not need to provide pseudocode, just a clear explanation. (c) Illustrate your in-place HeapSort algorithm on the following array: {4, 2, 9, 5, 1, 6}. Show the state of the array after each insert and each extractmax operation. 3. (25%) One way to describe an undirected graph is to list the degree of each of its vertices. A vertex s degree is the number of edges incident on it (i.e., the number of edges touching the vertex). If every vertex in a graph G has degree k, we say that G is k-regular. (a) Give an argument that in any undirected graph, the sum of degrees for all vertices is exactly twice the number of edges. (b) Does there exist an undirected graph whose vertices have degrees 5, 3, 3, 2, and 2 respectively? Show such a graph, or give an argument that it does not exist. (c) Two graphs G 1 and G 2 are said to be isomorphic if there is a mapping φ from the vertices of G 1 to the vertices of G 2 such that edge (u, v) exists in G 1 iff edge (φ(u), φ(v)) exists in G 2. (In non-math language, this just means that you can set up a direct correspondence between vertices in the two graphs such that for every edge in one graph, there s a corresponding edge in the other.) For example, the two graphs of Figure 1 are isomorphic under the mapping {A E, B F, C H, D G}. Are all 2-regular graphs on n vertices isomorphic? Give a proof or a counterexample. Figure 1: Two isomorphic graphs on four vertices. How does your answer change if we require that the graph be connected, i.e. that there is a path of edges between any two vertices? (An informal argument for the second part is fine.) (d) Give an example of two 3-regular graphs on six vertices that are both connected but are not isomorphic. (Hint: to show non-isomorphism, measure the lengths of the cycles in the two graphs and show that they are not the same.) 2
4. (25 %) It s final exam time, and you re in charge of scheduling rooms for the exams. Each exam has a designated start and end time, and no two exams whose time periods overlap may be scheduled in the same room. (a) Describe how to construct an (unweighted) graph that shows which pairs of exams can/cannot be scheduled in the same room. What are the vertices? What are the edges? (b) Unfortunately, campus construction projects have left you only two rooms to use for exams. Describe how to use breadth-first search to determine whether it is possible to schedule all exams using only the two rooms provided. If it is possible to do so, your procedure should assign a room to each exam. Your procedure should run in worst-case time proportional to the size of your graph (V + E). 5. (25%) Consider the directed graph G shown in Figure 2. (a) Suppose we run Dijkstra s shortest paths algorithm on G starting from the vertex A. Show the contents of the priority queue (a list of vertices in the queue, along with their key values) just before each extractmin() call made by the algorithm. If the priority queue must choose between vertices with equal keys, assume that it extracts the vertex whose label is alphabetically first. (b) Sketch the shortest path tree that results at the end of the algorithm. (c) Suppose now that we run Prim s minimum spanning tree algorithm in G starting from the vertex J (pretend for this problem that G is undirected; that is, ignore the arrowheads). Show the contents of the priority queue before each extractmin() call, as in part (a). (d) Sketch the minimum spanning tree produced by the algorithm. 3
Figure 2: Graph for running Dijkstra s and Prim s Algorithms. 4