Anda di halaman 1dari 9

Solutions: Midterm Practice Questions

1) Group the following into equivalent Big-Oh functions: a. X2 b. X c. X2 + X d. X2 - X e. ( X3 / (X-1) ) Q1 Answer: All are O(X2) except b, which is O(X). 2) Programs A and B are analyzed and found to have worst-case running times no greater than 150NlogN and N2, respectively. Answer the following questions, if possible: a. Which program has the better guarantee on the running time for large values of N ( N > 10,000 )? b. Which program has the better guarantee on the running time for small values of N ( N < 100 )? c. Which program will run faster on average for N = 1000? Q2 Answer: a. Program As guarantee is ten times better than Bs. b. Program Bs guarantee is ten times better than As. c. The stated properties do not tell us anything about the averagecase performance. 3) Find the worst-case complexity (Big-Oh) of the following function used to find the Kth smallest integer in an unordered array of integers. Justify your result. You do not need summations. int selectkth( int a[], int k, int n ) { int i, j, mini, tmp; for ( i = 0; i < k; i++ ) { mini = i; for ( j = i + 1; j < n; j++ ) if ( a[j] < a[mini] ) mini = j; tmp = a[i]; a[i] = a[min]; a[mini] = tmp; } return a[k-1]; } Q3 Answer: The outer loop iterates k times. The inner loop iterates O(n) times. So, the overall complexity is O(kn). If k = 1, the complexity is O(n) to find the smallest integer in the list. If k = n, the complexity is O(n2) to sort the entire list using selection sort.

4) For the typical algorithm that you use to multiply two N-digit integers (you, not a computer), determine the running time (Big-Oh). Justify your answer. Q4 Answer: Multiply each digit of the first number by the last digit of the second number: O(n), where n is the number of digits. Repeat this for every digit of the second number: n times, so there are O(n2) multiplications of digits. Then add n columns with n numbers in each column, which is also O(n2) . Therefore the overall complexity is O(n2). 5) Give a recursive algorithm to calculate XN for positive integer powers. Give the recurrence relation for your algorithm and solve the recurrence relation to find the Big-Oh running time. Include a proof of correctness that your solution is correct. Q5 Answer: Algorithm: power( x, n ) If n == 0 return 1 else return x * power( x, n-1 )

Recurrence Relation: T(0) = c T(n) = c + T( n 1 ) Solution: T(0) = c T(1) = c + T(0) = 2c T(2) = c + T(1) = 3c T(3) = c + T(2) = 4c T(n) = (n+1)c is O(n) Proof that Solution is Correct: Basis: Show T(n) = (n+1)c when n = 0 T(0) = c by the recurrence relation T(0) = (0+1)c = c same, so is correct

Inductive Assumption: Assume the solution is correct up to an arbitrary k: i.e., assume T(k) = (k+1)c Inductive Step: Show the solution is correct for the next value, i.e., show that T(k+1) = (k+2)c T(k+1) = c + T(k) = c + (k+1)c = (k+2)c by the recurrence relation by the inductive assumption so, is correct

6) For the tree given below, answer the following questions: 1. Which nodes are the leaves? Which node is the root? What is the height of this tree? 2. Is this tree a binary search tree? Why? Is this tree an AVL tree? Why? 3. Could this tree be a Red-Black tree? Why? If so, show how you would color it. 4. Could this tree be an AA tree? Why? If so, show how you would draw it. 5. Write the result of a preorder traversal. 6. Write the result of a postorder traversal. 7. Write the result of an inorder traversal. 8. Draw the tree after deleting the root using simple BST deletion. Is the new tree (after deleting the root) an AVL tree? Why? 5 3 2 1 Q6 Answers: 1. Root: 5; Leaves: 1, 4, 6, 8. The trees height is: 3 2. Yes, it is a binary search tree because it satisfies the search order property (i.e., the value at every node in the tree is less than or equal to 4 6 8 7 9

those in the nodes right subtree and larger than or equal to those in the nodes left subtree). It is also an AVL tree, because the height of the left and the right subtrees of every node do not differ by more than 1. 3. Yes, it could be a red-black tree because it can satisfy the red-black tree criteria (i.e., a BST with the root black, the same number of black nodes on every path from the root to a leaf and never two red nodes in a row). For it to be a red-black tree, nodes 3, 4, 5, 6, and 7 must be black. Either node 1 or 2 must be red and the other black. Either node 8 or 9 must be red and the other black. 4. No, it could not be an AA-Tree because an AA-Tree is not allowed to have left red children (or, equivalently, left horizontal children). On the path from the root to node 1, there is an extra node that must be colored red (or drawn horizontally on the same level as its parent). All of these links are to the left, so there is no way to color one of these nodes red (or draw it on the same level as its parent) without it being to the left of its parent. 5. Preorder traversal: 5, 3, 2, 1, 4, 7, 6, 9, 8 6. Postorder traversal: 1, 2, 4, 3, 6, 8, 9, 7, 5 7. Inorder traversal: 1, 2, 3, 4, 5, 6, 7, 8, 9 8. 6 3 2 1 4 8 7 9

The tree is no longer an AVL tree, because the height of the right subtree of 7 is more than the height of its left subtree by 2.

7) [4 marks] Using proof by induction, show that the maximum number of nodes in a binary tree of height H is 2H+1 1 Q7 Answer: Let NH = number of nodes in a full binary tree of height H Base Case: H = 0, N0 = 1 2H+1 1 = 21 1 = 1 = N0 Inductive Case: Inductive Assumption: Assume the theorem is true for H = 0, 1, 2, , k i.e., 2k+1 1 = Nk We need to show the theorem is true for H = k+1 i.e., 2k+2 1 = Nk+1 A tree of height k+1 has two sub-trees whose height is at most k. By the inductive assumption, these sub-trees can have at most 2k+1 1 nodes each, so together they have at most 2k+2 2 nodes. Adding the root gives us N= 2k+2 1 8) What is the appropriate table size if an open addressing collision resolution scheme is used and the number of items in a hash table is 10? Q8 Answer: The size of the table should be the smallest prime number greater than double the number of elements, so 23. 9) Given the input {51, 43, 83, 99, 77, 53, 89, 79}, a fixed table size of 10, and a hash function H(X) = X mod TABLE_SIZE, show the resulting probing hash table with offset F(i) = Ri, where R0 = 0, R1 = 3, R2 = 1, R3 = 4, R4 = 2, R5 = 8, R6 = 5, R7 = 9, R8 = 7, and R9 = 6. Q9 Answer: 0 1 2 3 4 5 6 7 8 9 79 51 89 43 53 83 77 99

10) Given the input {5471, 2423, 7273, 5299, 5444, 9789, 2089}, a fixed table of size of 10, and a hash function H(X) = X mod 10, show the resulting: A Linear probing hash table, with offset f( i ) = i to resolve collisions Q10A Answer: 0 1 2 3 4 5 6 7 8 9 9789 5471 2089 2423 7273 5444

5299

B Quadratic probing hash table, with offset f( i ) = i2 to resolve collisions Q10B Answer: 0 1 2 3 4 5 6 7 8 9 9789 5471 2423 7273 5444 2089 5299

11) If deletions are allowed with open addressing collision resolution, how must the design of the hash table be changed? Starting with the linear probing hash table developed in 15A, show the table that results after deleting 5299. Q11 Answer: Lazy deletion must be used, which means that an extra boolean field must be added to the table for each element in order to keep track of when an element has been deleted without breaking the search chain. When 5299 is deleted, its isActive field is set to false, but the element itself is not removed from the table.

0 1 2 3 4 5 6 7 8 9

9789 5471 2089 2423 7273 5444

T T T T T T

5299

12) Give the recurrence relation for the merge sort algorithm on page 29 and 32 of our text. Solve the recurrence relation using one of the methods show in class and prove that your solution is correct using proof by induction. Q12 Answer: Merge Sort Example posted in Lecture Slides, Week 2 13) How many null children (i.e., null pointers) are there in a binary tree of N nodes? How many are in an M-ary tree (i.e., a general tree) of N nodes? Q13 Answer: Binary Tree: A binary tree has 2N pointers (2 for each node). There are N-1 non-null pointers (edges) in a tree. 2N ( N 1 ) =N+1 (total pointers minus non-null pointers)

Therefore, there are N + 1 null pointers in a binary tree M-ary Tree: An M-ary tree has MN pointers (M for each node). There are N-1 non-null pointers (edges) in a tree. MN ( N 1 ) = MN N + 1 = (M-1)N + 1 null pointers in an M-ary tree

14) Draw all binary search trees that can result from inserting permutations of 1, 2, 3, and 4. How many trees are there? What are the probabilities of each trees occurring if all permutations are equally likely? Q14 Answer: There are 4! = 24 permutations of 4 digits: 1234, 1243, 1342, 1324, 1423, 1432, 2134, 2143, , so there are 24 possible insertion sequences. The first element inserted is the root, so we can look separately at each of the four groups of insertion sequences that start with each of the four digits. For instance, looking at the sequences that start with 1: The following four insertion sequences create four unique trees, not created by any other sequence: 1234, 1243, 1423, 1432 Therefore, the probability of each of these trees occurring is 1/24 The following two insertion sequences create the same tree: 1342, 1324 Therefore, the probability of this tree occurring is 2/24 = 1/12 Therefore, in the first goup of insertion sequences, all sequences that start with 1, there are 6 possible permutations, but only 5 possible trees. Continuing in this manner with the other four groups of insertion sequences, we could calculate the total number of different trees and the probability of each occurring. 15) Draw all AVL trees that can result from inserting permutations of 1, 2, and 3. How many trees are there? What are the probabilities of each trees occurring if all permutations are equally likely? Q15 Answer: There are 3! = 6 permutations of 3 digits: 123, 132, 213, 231, 312, 321, so there are 6 possible insertion sequences. Once rotations are carried out to balance to balance the AVL tree, all of these insertion sequences result in the same tree, so the probability of this trees occurring is 1.0: 2 / \ 1 3 16) What are the array indices for a hash table of size 11? Q16 Answer: 0, 1, 2, 10

?
c

?
d

b e f g

?
i

-2

-3

-5

17) Consider the game tree shown. Assume it is showing part of a game, with look-ahead of two levels and node a is the maximizing player. What move should he choose? Show why by evaluating the nodes using the minimax procedure. Q17 Answer: The value of node b is -2, since the opponent will pick the worst choice for a, the maximizing player. Similarly, the value of node c is -5. Therefore, the maximizing player will choose b = -2, so the value of a is -2.

Anda mungkin juga menyukai