Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linear Regression

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12,

2010 Problem 1. In each of the following question, please specify if the statement is true or false. If the statement is true, explain why it is true. If it is false, explain what the correct answer is and why. (40 points. For each question, 2 points for the true/false answer and 3 points for the explanations.) 1. n1.5 = O(n log n). Ans:Flase Assume that n1.5 = O(n log n) i.e. there exist positive constants n0 and c such that n1.5 c(n log n) for all n n0 , then we can get greater than
n 1. 5 n log n n 1. 5 n log n

c. But there is no constant

. So the assumption leads to contradiction.

2. A complete binary tree with a height of h can have more nodes than a full binary tree with a height of h. Ans:Flase A complete binary tree with a height of h have 2h1 to 2h 1 nodes. A complete binary tree with a height of h have 2h 1 nodes.

3. A stack follows a FIFO (rst-in-rst-out) rule. Ans:False A stack follows a FILO (rst-in-last-out) rule.

4. When we use a max heap to implement a priority queue, the time complexity of both the add and delete operations are O(n). Ans:True The operation add() and delete() spend O( the height of min heap ). Because the min heap is a complete tree, the height of min heap log n. the time complexity of add() and delete() = O(log n) = O(n) 1

5. T (n) = T (n 1) + n, T (1) = 1. Then T (n) = O(n3 ). Ans:True

T(n)

=T(n-1)+n

T(n-1)=T(n-2)+n-1 ... + T(2) =T(1) +2

____________________________ T(n)=1+2+...+n=n(n+1)/2

Then we get T (n) = O(n2 ) = O(n3 ).

6. In a circular doubly linked list with 10 nodes, we will need to change 4 links if we want to delete a node other than the head node. Ans:False We can change the link elds of the nodes that precede and follow the node we want to delete. So we just need to change 2 links if we want to delete a node other than the head node.

7. If f1 (n) = (g1 (n)) and f2 (n) = (g2 (n)), then f1 (n) f2 (n) = (g1 (n) g2 (n)). Ans:True f1 (n) = (g1 (n)) there exist positive constants n1 and c1 such that f1 (n) c1 g1 (n) for all n n1 f2 (n) = (g2 (n)) there exist positive constants n2 and c2 such that f2 (n) c2 g2 (n) for all n n2

So we can nd positive constants c = c1 c2 and n0 = max{n1 , n2 } such that 2

f1 (n) f2 (n) cg1 (n) g2 (n) for all n n0 . Then f1 (n) f2 (n) = (g1 (n) g2 (n)).

8. When using linked list to perform insertion sort, each time we remove an element from the input list and insert it to the correct position in the linked list. Assume that we have n numbers to be sorted, the time complexity for sorting these numbers using the insertion sort algorithm is O(n2 ). Ans:True The worst case is that we have to search all numbers in the list before we insert a new number. 1 + 2 + .. + n = n(n 1)/2 The time complexity for sorting these numbers using the insertion sort algorithm is O(n2 ).

Problem 2. Fill the blank. (20 points. 5 points for each question.) 1. Please compute The failure function (in Knuth, Morris, and Pratts pattern matching algorithm) for the pattern string abaabaab in Table 1. 2. Please draw all the threads of the threaded tree shown in Figure 7. 3. Please complete Table 2 to show the progress of converting the inx expression 2+1-(4-3*1)*3 to its postx expression using a stack. 4. Please complete the following function to concatenate two circularly singly linked list into one circularly singly linked list. typedef struct listNode * listPointer; typedef struct listNode { int data; listPointer link; }; 3

listPointer concatenate(listPointer list1, listPointer list2) {

/* Produce a new list which contains list1 followed by list2. Return the pointer which points to the new list. The arguments list1 and list2 point to the tails of the lists.*/

listPointer temp;

if (list1==NULL) return list2; if (list2==NULL) return list1;

listPointer tmp = list1->link; list1->link = list2->link; list2->link = tmp;

return list2; } Problem 3. The Tower of Hanoi is a mathematical game. The game consists of 3 rods, and n disks of dierent sizes which can slide onto any rod. The puzzle starts with the disks stack in ascending order of size on rod number 1 (the smallest disk is on the top). The objective of the game is to move the entire stack to rod number 3, obeying the following rules: 1. Only one disk can be moved at a time. 2. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. 3. No disk may be placed on top of a smaller disk. 4

In this problem, please write an algorithm using the recursive technique to output a series of moves which solve the Tower of Hanoi problem. The input and output are as follows: Input: one single number which represents the number of disks on the rod Output: Each line of output consists of a pair (i, j ), which represents a move from rod number i to rod number j . The sequence of moves should move the entire stack from rod number 1 to rod number 3. Example: Input: 3 Output: (1,3) (1,2) (3,2) (1,3) (2,1) (2,3) (1,3)

(10 points)

Ans: hanoi(from, tmp, to, n){ if(n==1){ move(from,to); return; } hanoi(from, to, tmp, n-1); printf ( "(%d,%d)", from, to ); 5

hanoi(tmp, from, to, n-1); return; }

Problem 4. A lower triangular matrix is one in which all elements above the main diagonal of a square matrix are zero. Assume that we have a lower triangular matrix L with n rows (Figure 2). Please answer the following related questions: (10 points) 1. What is the total number of nonzero terms of L (3 points)? If each element can be stored in a 32-bit signed integer, how much memory would be needed to store all the nonzero elements of matrix L (2 points)? Ans: (a) 1 + 2+. . . +n = (b) 32
n(n+1) 2 n(n+1) 2

= 16(n2 + n) bits.

2. Since storing a triangular matrix as a two dimensional array wastes sapce, we would like to nd a way to store only the nonzero terms in the triangular matrix. Find an addressing formula for the elements Li,j to be stored in a one dimensional array a. In other words, where in the array a would you store the element Li,j ? (You only need to explain how to map each nonzero element to the one dimensional array. No need to write down the actual function.) (5 points) Ans: Pack the traiagular matrix into array a row by row like the following Fig 1

Figure 1: Stored into array a

1 + 2 + + (i 1) + j 1 =

i2 i (1 + i 1) (i 1) + (j 1) = + (j 1) 2 2 6

The minus one is because we store the rst element at a[0]. Problem 5. In the following questions, consider the list of numbers: 62, 31, 70, 91, 25, 11, 9, 61, 73, 6. (20 points) 1. Show the result of inserting the numbers in the list in the same order specied above into an initially empty minimum heap. Note that you need to show how the heap looks like after each number is inserted. (5 points) Ans: Here shows the 3 middle trees of the results. see Figure 3. 2. Show the result of inserting the numbers in the list in the same order specied above into an initially empty binary search tree. Note that you need to show how the binary search tree looks like after each number is inserted. (5 points) Ans: Here shows the 3 middle trees of the results. see Figure 4. 3. Use the binary search tree you created in question 2. What are the two possible binary search trees after 62 is deleted? (5 points) Ans: see Figure 5. 4. Explain how you can utilize a minimum heap to sort the list of numbers in descending order. (3 points) Let n be the number of elements in the list. What is the time complexity of your sorting algorithm? (2 points) Ans: (1) step1: Delete the root from the min heap and put it into stackA. O(1) step2: Restructure the min heap. O(log n) step3: Go step1 until the min heap is empty. n times step4: Pop items from stackA.(The result is in descending order.) O(n) (2) O(n log n) Problem 6. In this problem, we consider logic expressions with the following operands and operators: Operands: 0 and 1, which represents false and true, respectively. 7

Operators: & (and), | (or), and ! (not). Note that you also have to consider left and right parenthesis. The precedences of the three operators are ! > & > |. (25 points) 1. Draw the logical expression tree of the expression !(0&!1&0|0)&0. Since ! (not) is an unary operator, we ask you to put its only operand to its right child. (3 points) Write down its prex expression. (2 points) (a) See Figure 6 (b) Prex: & ! | & & 0 ! 1 0 0 0 2. Please write down an algorithm to take a string of logic expression and convert it to an expression tree. You can use any data structure you like to represent the tree. Since ! (not) is an unary operator, we ask you to put its only operand to its right child. However, please explain how your data structure relate to the tree logically in details. (Comment: this is the hardest question in this exam. Do this after you nish all other questions!) (10 points) (a) Observe that the higher precedence the operator is, the lower level the corresponding tree node will be. (b) Dene isp[] as in-stack-precedence and icp[] as incoming-precedence. (c) typedef struct ExpNode{ ExpNode *left, *right; op v; /*if the node represent digit, v is the value; if not, v is the operator*/ }; int icp[] = {7, 1, 5, 4, 3}; /*( ) ! & |*/ int isp[] = {2, 1, 5, 4, 3}; /*( ) ! & |*/

ToExpTree (Exp input){ Create Stack1 for operator; Create Stack2 for ExpNode; 8

//read the string left to right while(end of input is not reached) { read from input, store at v1 if(v1 == digit) { create ExpNode k for v1; push k into Stack2; }else /*v1 is & | ! ( )*/ { if(Stack1 is empty || the icp of v1 > isp of Stack1[top]) push v1 into Stack1; else { while(the icp of v1 <= isp of Stack1[top]) { if(Stack1[top] is "(") { pop Stack1; break; } op1 = pop Stack1; creat ExpNode t for op1; r = pop Stack2; t->right = t; if(op1 is & or |) { l = pop Stack2; t->left = l; } 9

push t into Stack2; } push v1 into Stack1; } } } } 3. Please write down an algorithm to evaluate the outcome of the logical expression using the tree generated by your algorithm in question 1. (5 points) (a) Postorder traversal (b) typedef struct ExpNode{ ExpNode *left, *right; int type; /*digit or operator*/ int value; }; cal(ExpNode *tree){ if(!tree){ cal(tree->left); cal(tree->right); if(tree->type is equal to operator & |) tree->value = (tree->left->value) op (tree->right->value); else if(tree->type is equal to operator !) tree->value = !(tree->right->value); } } 4. Please write down an algorithm to output the prex expression of the logic expression using the tree generated by your algorithm in question 1. (5 points) (a) Preorder traversal 10

(b) typedef struct ExpNode{ ExpNode *left, *right; op v; /*if the node represent digit, v is the value; if not, v is the operator*/ }; pre(ExpNode *tree){ if(!tree){ printf(tree->v); pre(tree->left); pre(tree->right); } } Problem 7. Please provide constructive suggestions to this course in terms of lectures, homeworks, or any other aspects. (5 points)

11

Figure 2: A Lower Triangular Matrix

Table 1: The failure function for the KMP algorithm b -1 a -1 b 0 c -1 b 0 c -1 b 0 a 1 b 2 c 3 b 4 a 1 b 2 c 3

Table 2: The progress of converting the inx expression 2+1-(4-3*1)*3 to its postx expression Token 2 + 1 ( 4 3 * 1 ) * 3 + + -( -( -(-(-(-* -(-* -* Stack content (bottom to top) Output so far 2 2 21 21+ 21+ 21+4 21+4 21+43 21+43 21+431 21+431*21+431*21+431*-3*-

12

25

31 31 70

11

11

61 82 62 91

62

70

25

61

31

70

25

91

73

62

Figure 3: 5-1

62

62

62

31 31 70

70

31

70

25 25 91 11

61

91

25

61

91

11

73

Figure 4: 5-2

61

70

31

70

31

91

25

91

25

61

73

11

73

11

Figure 5: 5-3

13

Figure 6: Expression Tree

Figure 7: Memory representation of a threaded tree

14

You might also like