UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions 1: Function Order of Growth (10 points) Given: 1) f1(n) is in (n2 lgn) 2) f2(n) is in (n3) 3) f3(n) is in (n lg2n) Can we conclude from these 3 statements (1)-(3) that f1(n) must be in ( f3(n))? Why or why not? n3 n2 SOLUTION: YES. f2 lg n n lg2 n f1(n) in (n2 lgn) and n2 lgn in (n lg2n) together imply (via transitivity) that f1(n) is in (n lg2n). f3(n) in (n lg2n) implies (via definition of and transpose symmetry) that n lg2n is in ( f3(n)). f1(n) is in (n lg2n) and n lg2n is in ( f3(n)) together imply (via transitivity) that f1(n) is in ( f3(n)). f1(n) in (n2 lgn) and n2 lgn in (n lg2n) and n lg2n in ( f3(n)) (transitively) f1(n) in ( f3(n)) (page 1 of 8) f1 f3 UMass Lowell CS 91.404 (Section 201) Midterm Exam Solutions 2: Solving a Recurrence (15 points) Find a tight upper bound on the closed-form solution for: T (n) max 1 q n 1 (T (q) T (n q)) (1) You may assume that T(1) = 1. SOLUTION: T(n) is in O(n) Intuition is that T(n) is in O(n). This is because the (1) term is a factor of n smaller than the corresponding part of the worst-case QuickSort recurrence, and that recurrence has an upper bound of O(n2). Using the inductive hypothesis T(n) <= cn yields: T (n) max 1qn1 (cq c(n q)) (1) T (n) cn (1) Note that –cq cancels cq and the resulting expression cn is independent of q, so we need not maximize a function of q. However, the T(n) <= cn + (1) result is problematic because we need to show exactly T(n) <= cn. (See p. 56 of text for example [under “subtleties”].) We therefore modify our inductive hypothesis to accommodate the constant factor: T(n) <= c(n-1). Using this new hypothesis yields: T (n) max 1qn1 (c(q 1) c(n q 1)) (1) T (n) cn 2c (1) If we can show that cn-2c+(1) <= cn, then transitivity will give us the desired result of T(n) <= cn. Now, cn-2c+(1) <= cn implies that (1) <= 2c. We can force this to be true by choosing c to be larger than whatever constant is in the (1) expression, so we’re done. (page 2 of 8) Fall, 2001 UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions 3: Counting & Probability (25 points) In this problem we assume that each node has exactly one outgoing operational edge. [The nodes in Layer 3 are exceptions; they have no outgoing edges.] 16 a) (5 points) How many different network configurations can exist? 8 <A,A,A>, <A,B,A>, <A,B,B>, <A,A,B>, b) (5 points) How many different delivery paths can exist? <B,A,A>, <B,B,A>, <B,B,B>, <B,A,B> (You can draw one configuration in each box below by “filling in” dashed arrows to change broken edges to operational ones.) A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A B A (page 3 of 8) A B B A B A B A B UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions C) (15 points) Suppose that, for each node in Layers 1 and 2, the choice of which edge is broken is made such that each of the node’s two outgoing edges is equally likely to be broken. In that case, what is the probability that a configuration contains at least one node that appears on at least two different delivery paths in that configuration? SOLUTION: ¾ Here are 2 different ways to derive this: 1) Let a = number of configurations in which at least one node appears on at least 2 different delivery paths in that configuration. Let b = total number of different possible configuration = 16. Solution is a/b = 12/16 = 3/4. (Refer to picture, in which nodes participating in at least 2 different delivery paths are shaded.) 2) Let c = number of configurations in which at no node appears on at least 2 different delivery paths in that configuration. Let b = total number of different possible configuration = 16. Solution is 1 – (c/b) = 1 – (4/16) = 3/4. (page 4 of 8) UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions 4: PseudoCode Analysis (20 points) MysterySort has 1 argument: A : a one-dimensional array of n integers MysterySort(A) for i 1 to length[A] do min i for j i+1 to length[A] do if A[ j ] < A[min] then min t j A[min] A[min] A[i] A[i] t Analyze the asymptotic running time of this sorting algorithm as a function of the number of elements n in array A. Provide a tight upper bound on the worst-case running time. SOLUTION: T (n) O(n 2 ) The worst-case running time is dominated by the time to execute the inner for loop, which is: n n n n T (n) (1) (1) n i (1) n i (1)(n 2 ) (n 2 ) i 1 j i 1 i 1 i 1 i 1 n Therefore, T (n) O(n 2 ) Why is this bound tight? Because: n n n n2 n n n 1 (1) n i (1) n i (1) n 2 i (1) n 2 (n)( n 1) (1) (n 2 ) 2 i 1 i 1 i 1 i 1 i 1 2 2 (page 5 of 8) UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions 5: Designing an Algorithm (30 points) In this problem you are given a full (i.e. complete) binary tree T. Each node of T has an integer key value. T satisfies the Maximum Heap Property (see Example T on the right). 30 12 20 Design an efficient algorithm that accepts T and a search key k. Your algorithm should return: - TRUE if one or more node(s) of T has a key value = k FALSE if no node of T has a key value = k 4 2 Example T a) (10 points) Provide pseudocode for your algorithm Use the following notation and operations to access parts of the tree: - - T refers to the root node as well as the tree itself. In your pseudocode you may update the contents of T so that it represents a node in the tree other than the root. key[ T ] refers to the key associated with node T. left[ T ] refers to the node that is the root of T’s left subtree. If T has no left subtree, then left[ T ] has the value NULL. right[ T ] refers to the node that is the root of T’s right subtree. If T has no right subtree, then right[ T ] has the value NULL. parent[ T ] refers to the node that is the parent of a node T. If T has no parent, then parent [ T ] has the value NULL. Assume that key[T], left[T], right[T] and parent[T] each take (1) time. Your pseudocode should not assume that T is stored in an array. A SOLUTION: SEARCH( A, k ) if key[ T ] < k then return FALSE if key[ T ] = k then return TRUE if left[ T ] is not NULL then L SEARCH(left[ T ] , k) if L = TRUE then return TRUE if right[ T ] is not NULL then R SEARCH(right[ T ] , k) if R = TRUE then return TRUE return FALSE (page 6 of 8) 7 8 UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions b) (10 points) Show that your pseudocode is correct SOLUTION: There are 2 cases: 1) k is in T 2) k is not in T Case 1: If k = some key value in T, then the equality test will correctly return TRUE. We need only find one occurrence of k in T before we stop. Case 2 has 2 subcases: 2a) k > than the largest key in T. Since T is a MaxHeap, we need only compare key[T] with k for equality to detect this case. 2b) k <= the largest key in T - If k is smaller than the smallest key, then the NULL child tests will terminate the recursion and FALSE will be correctly returned. - If k is in between the key values for some parent and child, then since k is larger than the child’s key, it will be larger than the current node’s key for some recursive call. FALSE is returned by the if key[ T ] < k test in this case. FALSE is the correct outcome here, because the Maximum Heap Property guarantees than no node in the child’s subtrees has key value greater than that of the child. Note that the algorithm would not be correct if we replaced the pseudocode: if left[ T ] is not NULL then L SEARCH(left[ T ] , k) if L = TRUE then return TRUE with the pseudocode: if left[ T ] is not NULL then return SEARCH(left[ T ] , k) The change would be incorrect because it would prevent the algorithm from finding a match in the right subtree when there is no match in the left subtree. [Note that the HEAP Property does not guarantee relative key value order of siblings, so we cannot disregard ½ of the remaining nodes at each step like we can in an algorithm like Binary Search.] (page 7 of 8) UMass Lowell CS 91.404 (Section 201) Fall, 2001 Midterm Exam Solutions c) (10 points) Analyze the asymptotic running time of your algorithm as a function of the number of nodes n in T. Provide a tight bound on the worst-case running time. SOLUTION: T(n) = (n). In the worst case, the key is not in the tree and its value is smaller than every leaf key value. In this case, every node of the tree must be visited in order to conclude that the key is not in the tree. SEARCH has the following worst case recurrence: T(n) = 2T(n/2) + (1) (Note that the 2T(n/2) is due to the fact that the binary tree is full (i.e. complete). The Master Theorem applies in this case. The ratio test yields (1)/n, which is case 1. The solution is T(n) = (n) . (page 8 of 8)
© Copyright 2026 Paperzz