Você está na página 1de 5

Cover page for CSC263 Homework #1

Please type and print your homework (no handwriting) Thanks Fill and attach this page to your homework

Submitted by:

(1) Family Name: Given Name: Student Number:

(2) Family Name: Given Name: Student Number:

Graded Homework should be returned in Tutorial:

(tutorial room number)

For help with your homework you may consult only the instructor, the teaching assistants, your homework partner (if you have one), your textbook and your class notes. You may not consult any other source. By virtue of submitting this homework I/we acknowledge that I am/we are aware of the policy on homework collaboration for this course.

Computer Science CSC263H St. George Campus Homework Assignment #1 Due: January 30, 2014, by 5:30 pm (in the drop box for this course in Bahen 2220)

January 9, 2014 University of Toronto

1. On the cover page of your assignment, in addition to your name(s), you must write the location of the tutorial where you want the graded homework to be returned. 2. For any question, you may use data structures from class without describing how the operations on these data structures are implemented. You may also use any result (e.g., a worst-case time complexity bound) that we covered in class, or is in the course textbook, by referring to it. 3. Unless we explicitly state otherwise, you should justify your answers. Your paper will be marked based on the correctness and completeness of your answers, and the clarity, precision and conciseness of your presentation.

Question 1. (16 marks) In the following procedure, the input is an array A of arbitrary integers, A.size is a variable containing the size of array A (assume that A contains at least 2 elements), and HALT means stop executing and return from the procedure call. 0. Procedure bizarre(A) 1. if A[1] is not equal to 1 then HALT 2. for i = 2 to A.size do 3. if A[i] is not equal to A[i-1] then HALT 4. else do 5. for j = 1 to i-1 do A[j] := A[j] - 3 6. A[i] := A[i] + 3 Let T (n) be the worst-case time complexity of executing procedure bizarre() on an array A of size n > 1. Assume that comparisons and arithmetic operations, like additions, take a constant amount of time each. a. (4 marks) b. (12 marks) State whether T (n) is O(n2 ) and justify your answer. State whether T (n) is (n2 ) and justify your answer.

Any answer without a sound and clear justication will receive no credit. Question 2. (16 marks) In this question, H is a min-heap with distinct positive integers. Tony Sopranino, a famous CSC263 graduate, claims that there is an algorithm List(H, k ) which, given a min-heap H and an integer k , satises the following: it prints the set Sk of all the integers in H that are smaller or equal to k , and it runs in time proportional to the size of the set Sk that it prints (i.e., its worst-case running-time is O(m) where m is the size of the set Sk ). Is Tonys claim true? 2

If his claim is true, give such an algorithm by giving its pseudo-code (do not justify its correctness or running time). If his claim is false, briey argue why no such algorithm exists. Question 3. (14 marks) We want an ecient algorithm for the following on-line problem. The algorithm is given an integer m 2, and then a (possibly innite) sequence of distinct keys are input to the algorithm, one at a time. A query operation can occur at any point between any two key inputs in the sequence. When a query occurs, the algorithm must return (in any order) the m smallest keys among all the keys that were input before the query. Assume that at least m keys are input before the rst query occurs. For example, suppose m = 3, and the key inputs and query operations occur in the following order: 20, 15, 31, 6, 13, 24, query, 10, 17, query, 9, 16, 5, 11, query, 14, . . . Then the rst query should return 6, 13, 15 (in any order); the second query should return 6, 10, 13 (in any order); the third query should return 5, 6, 9 (in any order) etc. a. (12 marks) Describe a simple algorithm that, for every m 2, solves the above problem with the following worst-case time complexity: O(log m) for each key input operation, and O(m) for each query operation. Your algorithm must use a data structure that we learned in class. Give a clear and precise English description of your algorithm, then give the corresponding pseudo-code. b. (2 marks) Explain why your algorithm achieves the above worst-case time complexity.

Question 4. (10 marks) In this question, H denotes a binomial max heap, n is the number of items in H , x is (a pointer to the node of) an item inside H , and k is a number (key). a. (5 marks) Describe a simple algorithm to increase the key of a given item x in a binomial max heap H to become k . Your algorithm should not change anything if k x.key . The worst-case running-time of your algorithm must be O(log n). Give a high-level description of your algorithm in clear English. b. (5 marks) Using part (a), describe a simple algorithm to delete a given item x from a binomial max heap H . The worst-case running-time of your algorithm must be O(log n). Give a high-level description of your algorithm in clear English.

Question 5. (24 marks) Design a data structure called super heap that supports the following operations: Insert(k ): inserts the key k into the super heap, ExtractMax(): removes a max key from the super heap, ExtractMin(): removes a min key from the super heap, Merge(D,D ): merges super heaps D and D into one super heap. The worst-case running-time of each operation in your data structure must be O(log n) where n is the total number of items. Your data structure must be based on a data structure that we discussed in this course. a. Explain the main high-level idea of your solution in clear English. As part of this explanation, (1) state what is the underlying data structure that you are using in your solution, and (2) explain clearly all the information that you are adding to this underlying data structure. Specically, what additional information are you storing in each node? b. Give a high-level description in clear English of how to implement each operation of the super heap. [The questions below will not be corrected/graded. They are given here as interesting problems that use material that you learned in class.] Question 6. (no marks) A min-max heap is a complete binary tree of which every node u satises the following properties: if u is at even level then its key is the minimum of the keys of us descendants (itself included), if u is at odd level then its key is the maximum of the keys of us descendants (itself included). An immediate consequence of the denition is that the minimum key in a min-max heap is in the root; while the maximum key is in one of the roots children. (If the root has no children then, of course, the minimum and maximum key is the key at the root which is the only key in the tree!) A min-max heap can be used to implement eciently (in time logarithmic in the size of the tree) the following operations: Insert(x, A): insert key x into the min-max heap A. ExtractMin(A): delete from A the minimum key and return it. ExtractMax(A): delete from A the maximum key and return it. As with ordinary heaps, we can store a min-max heap in an array. Write algorithms for Insert, ExtractMin, and ExtractMax in min-max heaps. Your algorithms should each run in time in O(log n) (where n is the number of elements in the heap) and use space in O(1) (other than the array). Describe your algorithms in understandable pseudo-code, and justify your time complexity claims. Question 7. (no marks) We wish to implement the priority queue ADT to store a set of elements. We know that the number of elements in the set will never exceed N . Using heaps, we can ensure that both Insert and ExtractMin can be accomplished in O(log N ) time. (Notice that here we are expressing the time complexity as a function of the maximum number of elements that could be in the priority queue, not the number of elements currently in it.) We want to speed up the Insert operation so that it runs in time O(log N/ log log N ); the ExtractMin operation should run as fast as possible subject to the requirement on the running time of Insert. 4

We can accomplish this by using a variant of heaps. Instead of using a complete binary tree to store the priority queue, we use a complete d-ary tree, for some appropriate value of d. A complete d-ary tree is a tree in which every node has at most d children, each level i other than the bottom one has the maximum possible number of nodes (namely di ), and the bottom level has all its nodes as far to the left as possible. a. Determine a value of d (expressed as a function of N ) that is as small as possible and has the property that a complete d-ary tree with at most N nodes has height at most log N/ log log N ? Justify your answer. b. Explain in detail the data structure you use to represent the priority queue. c. Explain how to implement the Insert and ExtractMin operations using clear, high-level pseudocode. d. Explain why your implementation of Insert achieves the required time bound of O(log N/ log log N ). e. State and justify the time complexity of your implementation of ExtractMin. 3. This question is about the cost of successively inserting k elements into a binomial heap of size n. 1. Prove that a binomial heap with n elements has exactly n (n) edges, where (n) is the number of 1s in the binary representation of n. 2. Consider the worst-case total cost of successively inserting k new elements into a binomial heap H of size |H | = n. In this question, we measure the worst-case cost of inserting a new element into H as the maximum number of pairwise comparisons between the keys of the binomial heap that is required to do this insertion. It is clear that for k = 1 (i.e., inserting one element) the worst-case cost is O(log n). Show that when k > log n, the average cost of an insertion, i.e., the worst-case total cost of the k successive insertions divided by k , is bounded above by constant. Hint: Note that the cost of each one of the k consecutive insertions varies some can be expensive, other are cheaper. Relate the cost of each insertion, i.e., the number of key comparisons that it requires, with the number of extra edges that it forms in H . Then use part (a).

Você também pode gostar