


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



University of Virginia Computer Science CS216: Program and Data Representation, Spring 2006 01 March 2006
For each of the questions below, fill in the missing symbol with one of these choices:
= — the sets are equal ⊂⊂⊂⊂ — the left set is a strict subset (cannot be equal) of the right set ⊃⊃⊃⊃ — the left set is a strict superset (cannot be equal) of the right set ⊆⊆⊆⊆ — the left set is a subset (can be equal) of the right set ⊇⊇⊇⊇ — the left set is a superset (can be equal) of the right set ≠≠≠≠ — the sets are not equal, but there is no subset or superset relationship
You should select the strongest possible choice (for example, if two sets are equal and you select subset, that is a correct statement, but not worth full credit).
For each answer, provide a short justification of your answer. Your justification should follow from the definitions of the order notations.
1. (Average 4.49 out of 5 points) O ( n ) ≠≠≠≠ Θ ( n^2 )
The sets are unequal (in fact they are disjoint, and have no members in common). The set Θ( n^2 ) is all functions that grow as fast as n^2 ; the set O ( n ) is the set of all functions that grow no faster than n. Since n^2 grows faster than n , there is no overlap between the sets.
2. (4.11 / 5) Θ ( n ) ⊂⊂⊂⊂ O (2 n )
First, note that the factor has no effect on the growth rate, so O (2 n ) is equivalent to O ( n ). The set Θ( n ) is the set of all functions that grow as fast as n ; this is a proper subset of O ( n ) which is the set of all functions that grow no faster than n. There are functions in O ( n ) that are not in Θ( n ) such as constant functions or log n.
3. (4.14 / 5) ∅ = O ( n !) ∩ Ω( nn )
The intersection of O ( n !) and Ω( nn ) is empty, since nn^ grows faster than n! (see Problem Set 1).
4. (4.82 / 5) O (1) ⊃⊃⊃⊃ Θ (1)
This one is tricky, and we accepted = , ⊇⊇⊇⊇ or ⊃⊃⊃⊃ as a full credit answer (with a good explanation). Certainly we known every element of Θ(1) is also in O (1) from the definitions of Θ and O. The tougher question is figuring out if there is any function in O (1) that is not in Θ(1). From the informal definitions, this would mean is there any function that produces a positive value as output that grows slower than a constant. Intuitively, it would seem that functions like 1/ n have this property — as n increases, the value of the function decreases asymptotically. To answer it more convincingly, we need to consider the definitions of O and Θ.
If f is in O (1), then we know there are constants c > 0 and n 0 ≥ 0 such that f ( n ) < c for all n ≥ n 0. (Since g(n) = 1, the g(n) term goes away.) For O (1) to be equal to Θ(1), we would need to show that this also implies f is in Ω(1). This requires that we can find constants c > 0 and n 0 ≥ 0 such that f ( n ) > c for all n ≥ n 0. That is, f is in Ω(1) if and only if 1 is in O ( f ). Consider f(n) =
1/ n. We cannot have 1 is in O (1/ n ) since for some value of n we know eventually 1 > c * 1 / n for some value of c (we can choose n > c for any choice of c ). Hence, we know O (1) is a strict superset of Θ(1).
5. (6.98 / 10) Complete the definition of the ListNode method reverse, that is called by the LinkedList method reverse to produce a reversed self list as its output (the same elements as in self, but in reverse order). For example,
l = LinkedList.LinkedList ().append(1).append(2).append(3) r = l.reverse ()
should make r the list [3, 2, 1] and leave l as the list [1, 2, 3].
The rest of the code is taken from the LinkedList.py implementation of an immutable list datatype from Problem Set 2.
The easiest way to think of reverse is simply adding the first element to the end of the list resulting from reversing the rest of the elements:
def reverse (self): head = ListNode (self.__info) if self.__next == None: return head else: return self.__next.reverse().append (self.__info)
There are more complex iterative ways (that are more efficient) to do this by switching the __next pointers down the list, but they are much tougher than the simple recursive solution.
6. (8.46 / 10) What is the asymptotic running time of your reverse implementation? Explain your answer convincingly, and be sure to define any variables you use and state any assumptions you make clearly.
Our reverse implementation has running time in Θ( n^2 ) where n is the number of elements in the input list. (Note, it is possible to implement reverse with running time in Θ( n ) by adjusting the __next pointers directly.)
There will be n calls to reverse, once for each element in the list (actually, there are n - 1 because the base case stops the recursion for the list of length 1). But, each call to reverse involves a call to our append method. The provided append implementation has running time in Θ( m ) where m is the size of the input to append (see Problem Set 2). The average length of the input list to append is n / 2, so the average running time of the append call is in Θ( n / 2) = Θ( n ).
We are making n calls to reverse, each of which involves running time Θ( n ) from the call to append, so the total running time will be in Θ( n^2 ).
7. (9.2 / 10) Prove the greedy partnering algorithm shown is not optimal by showing an input for which it would not produce the correct result.
We just need to find an input list where the best match for the first student does not produce the optimal global match. Here's a simple example:
students = { "Alice", "Bob", "Colleen" }
(Note: I applied the same rule to myself as you had on this exam of not actually trying the code in an interpreter. So, there is probably at least one bug in it. The first person to find a non-trivial bug in this and explain how to fix it gets 10 bonus points.)
10. (7.63 / 10) Explain why Zuma's partner assignment algorithm would not run fast enough to be used to assign partners for PS4. (Note: a good answer would include an explanation of the running time of assignPartners. Assume you have a correct and optimally efficient allPossiblePartnerAssignments implementation regardless of your answer to question 9. You should be able to answer question 10 well, even if you could not answer question 9.)
The algorithm we used in question 9 involves creating all possible permutations of a list of length 2 n , where n is the number of students (recall we added n Nones to the list we passed to allLists). Hence, there are (2 n )! possible orderings. For each ordering, we loop through all the elements (up to 2 n of them), so the work of allPossiblePartnerAssignments^ for each possible assignment is 2n * (2 n )!. Regardless of any other work, this exceeds 10^357. This far exceeds the number of atoms in the universe, so is most definitely not something we could compute in time for PS4.
This is not the best possible algorithm, however, especially because of how we dealt with the None matches. We could improve it by recognizing the each None is identical, so all the orderings that just involve moving Nones around with other Nones are identical. So, perhaps we could approach O ( n !). We know the brute force algorithm can't do better than this however — we need to try all possible assignments of students, so we need to try all possible orderings. But even 96! is way beyond the realm of anything tractable for computing.
I was surprised by the number of people who answered this by analyzing the complexity to be in something like O ( n^3 ), and then arguing that performing 100^3 iterations would be intractable. You should have a bit better of a sense of the power of the computers you use. When they say a 2GHz processor, that means it can perform 2 Billion operations per second (what an operation is is a bit complex, but you can think of any simple calculation as a few operations). Hence, numbers like 100^3 are not a problem at all. Its only 1 Million, and the computers in lab can do around two thousand million operations in one second.
11. (average 0.98 bonus points) Suggest a better algorithm to use to assign partners for future problem sets.
We'll discuss this in a future class.
CS216: Program and Data Representation University of Virginia
[email protected] Using these Materials