Docsity

Midterm Information for CS161: Data Structures and Algorithms, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS161: Data Structures and Algorithms Handout #12
Stanford University Tuesday, 1 February 2000
Lecture #9: Tuesday, 1 February 2000
Topics: Midterm information
Midterm Information
When: Tuesday, 8 February 2000, 9:30–10:45 A.M.
Where: Braun Auditorium
Crib sheet: The midterm is closed book. You may, however, bring one 81
2
00
×1100 sheet
of paper as a crib sheet. Preparing a crib sheet can be a useful study aid, so take time
in selecting material for it. You may use both sides of the paper and write as small as
you like, but in any case, you are allowed only one sheet. Your must hand in your crib
sheet when you hand in your quiz.
Material covered: You are responsible for material covered in lectures, recitations,
and problem sets handed out by the midterm date. You are also responsible for the
material in the corresponding sections in the textbook (CLR).
Exam Conflict: If you are unable to attend the exam at the scheduled time, please
talk to the TAs in advance.
Sample test: Attached is a copy of the midterm and solutions from a previous year.
This should give you some sense about the types of questions that may be asked.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Information for CS161: Data Structures and Algorithms and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS161: Data Structures and Algorithms Handout

Stanford University Tuesday, 1 February 2000

Lecture #9: Tuesday, 1 February 2000

Topics: Midterm information

Midterm Information

When: Tuesday, 8 February 2000, 9:30–10:45 A.M.

Where: Braun Auditorium

Crib sheet: The midterm is closed book. You may, however, bring one 812

′′

× 11 ′′^ sheet

of paper as a crib sheet. Preparing a crib sheet can be a useful study aid, so take time

in selecting material for it. You may use both sides of the paper and write as small as

you like, but in any case, you are allowed only one sheet. Your must hand in your crib

sheet when you hand in your quiz.

Material covered: You are responsible for material covered in lectures, recitations,

and problem sets handed out by the midterm date. You are also responsible for the

material in the corresponding sections in the textbook (CLR).

Exam Conflict: If you are unable to attend the exam at the scheduled time, please

talk to the TAs in advance.

Sample test: Attached is a copy of the midterm and solutions from a previous year.

This should give you some sense about the types of questions that may be asked.

CS161: Data Structures and Algorithms Handout

Stanford University Tuesday, 12 May 1998

Lecture #13: Tuesday, 12 May 1998

Topics: Midterm examination solutions

 This exam ends at 10:45 A.M. It contains 4 problems. You have 75 minutes to earn

75 p oints.

 This exam is closed b o ok. You may use one 8 1200  1100 crib sheet. When the exam

is over, staple your crib sheet to the back of the exam.

 Write your solutions in the space provided. If you need more space, write on the

back of the sheet containing the problem. Do not put part of the answer to one

problem on the back of the sheet for another problem.

 Do not sp end to o much time on any problem. Read them all through rst and

attack them in the order that allows you to make the most progress.

 Show your work, as partial credit will b e given. You will b e graded not only on the

correctness of your answer, but also on the clarity with which you express it. Be

neat.

Problem Points Average Std dev

Total 75 45.29 9.

The last sum in the ab ove expression contains (lg n) terms, each no greater than n lg 3 n, and so T 2 (n) = O (n lg 4 n). On the other hand, the rst half of the lg 3 terms | or, to b e precise, the rst m = b log 2 3 nc terms | are each at least as big as n lg 3

p n = (n lg 3 n)= 8 | or, to b e precise again, at least as big as

n lg 3

n 3 m^1

:

So T 2 (n) = (n lg 4 n). Together we get T 2 (n) = (n lg 4 n). We can also get this result directly using the version of the Master theorem given in class, and app earing as exercise 4.4-2 on page 72 of CLR. It is also p ossible to solve this problem using the tree metho d. However, we should b e careful to show b oth a lower and an upp er asymptotic b ound of n lg 4 n. That is, arguing that the tree has depth at least log 3 n, with each level costing at most n lg 3 n successfully proves that T 2 (n) = O (n lg 4 n) | but we must also show that T 2 (n) = (n lg 4 n) to conclude that T 2 (n) = (n lg 4 n). This can b e done by considering the cummulative cost of just half the tree levels, in a manner similar to our previous derivation using the iteration metho d. Common error: We cannot apply the basic form of the Master theorem (page 63 of CLR) since

a = 3 b = 3 f (n) = n lg 3 n nlogb^ a^ = nlog^3 3 = n

and, even though n lg 3 n = (n), it is not (n1+^ ) for any  > 0. Why? Because if n lg 3 n = (n1+^ ), we could deduce that lg 3 n = (n^ ), which contradicts the formula on page 35 of CLR. So case 3 do es not apply. Common error 2: When we obtain the summation

n lg 3 n + n lg 3 (n=3) + n lg 3 (n=9) +    + n lg 3 (n=(n=3))

in the recurrence, this cannot b e approximated as O (lg 3 n). A simple way to analyse this would b e to observe that each term is O (lg 3 n) and there are O (lg n) such terms, yielding an upp er b ound of O (lg 4 n). Graded by Suresh.

(c) T 3 (n) = T 3 (bn= 2 c) + T 3 (bn= 4 c) + n [2 p oints]

Using the tree metho d we get the following tree

n

n/2 n/

n/4 n/8 n/8 n/

n

3n/

9n/

(3/4) ni

Note that the tree will b e unbalanced and that the subtree corresp onding to T 3 (bn= 2 c) will b e deep er (i.e. it will have more levels). At all levels after the shallow subtree | the one corresp onding to T 3 (bn= 4 c) | b ecomes exhausted, each level will cost no more than

4

i

n. So we have

T 3 (n) 

X^1

i=

3 4

i

n = O (n):

Now the top level costs n, and hence

T 3 (n)  n = (n):

Combining the two, we concude that

T 3 (n) = (n):

Common error: \Since there are O (lg n) levels, and each level costs O (n), the total cost is O (n lg n)". The problem with this argument is that it is lo ose. There are O (lg n) levels, but the cost at each level decreases. In fact, the cost at level i is n

3 4

i

. Summing up, we obtain the ab ove b ound of O (n).

Graded by Suresh.

(d) For each functionsf (n) along the left side of the table b elow and each function

g (n) across the top, write O , , or  in the appropriate b ox, dep ending on

whether f (n) = O (g (n)), f (n) = (g (n)), or f (n) = (g (n)). If there is

more than one relation b etween f (n) and g (n), write only the strongest one.

The rst line is a demo solution. [1 p oint p er entry]

True False The answer is True. In fact, the adversary do es not even have to think hard: any input s/he gives to randomized Quick-Sort has a non-zero probability of forcing the algorithm through (n^2 ) steps. The reason is that the running time of randomized Quick-Sort dep ends on the internal, random selection of pivots; hence, it is not imp ossible (i.e. there is a p ositive probability) that the selection will b e bad at every single recursive step, partitioning the array into subproblems of sizes n 1 and 1. Also, if the input consists of n rep etitions of the same numb er, then a Quick-Sort which uses the Partition pro cedure shown in class will de nitely take (n^2 ) steps, whether it's deterministic or randomized. Graded by David.

(b) A max-based heap can b e transformed into a min-based heap in linear time.

True False The answer is True. In fact, any array (i.e. not necessarily a heap) can b e turned into a min-based heap in linear time. The way to do this is to simply run Build-Heap on the array, which takes linear time. Here, of course, we are using a Build-Heap pro cedure based on a new Heapify which, in turn, guarantees that every parent is no greater than its children. Although it is p ossible to build a min-based heap by extracting the elements of the max-based heap in reverse sorted order, this algorithm takes (n lg n) steps. It is not p ossible to convert a max-based heap into a min-based heap by reversing the elements in the heap array. As a counterexample, consider the heap stored as

5 ; 1 ; 4

This heap app ears on the right side in the gure b elow. Reversing the contents of the array pro duces 4 ; 1 ; 5

which is not a min-based heap since the ro ot, 4, is bigger that its left child, 1. Graded by David.

(c) A preorder tree walk on a (max-)heap will output the values stored in the

heap in reverse sorted order.

True False The answer is False. Or, to b e more precise, not necessarily. For example, the left heap in the gure b elow pro duces

5 ; 4 ; 1

during a preorder walk, while the right heap pro duces

5 ; 1 ; 4 :

5

1 1 4

5

4

One counterexample suces to show the fallacy of the claim. Another, more general way to prove that no tree walk (whether inorder, preorder, or p ostorder) can output the no des in reverse sorted order is to simply observe that a heap guarantees only that a parent is no smaller that its children; there is no guarantee as to the relative ordering of the two children. Graded by David.

(d) Hashing with chaining is preferable to op en addressing, if the load factor

of the hash table is likely to get greater than 1.

True False The answer is True. If we have > 1 then the numb er of elements to b e hashed is greater than the numb er of slots available, so we can't use op en addressing. Even when do esn't exceed 1 but it is close to 1 , chaining is much b etter since clustering will make op en addressing very inecient. Students who argued the ineciency of op en addressing for close to 1 but didn't say that op en addressing fails for > 1 lost only 1 p oint. Graded by David.

(e) If we know the ordered lists of keys pro duced by a preorder and an inorder

traversal of a binary search tree T , then we have enough information to fully

reconstruct T.

True False The answer is True. In fact we don't even need the inorder output b ecause we have all the information we need to reconstruct the tree from the preorder output. (Actually, we could get the inorder output by sorting the preorder output, if we really wanted to.) Now, since a preorder traversal outputs the ro ot of the tree b efore either subtree, then to reconstruct the tree we only need to do the following:

  1. Initialize the tree to b e empty.
  2. Insert the elements in the order given by the preorder output. We'll get the original tree b ecause the ro ot of the original tree (and every subtree recursively) is inserted into the new tree b efore any of its original subtrees. Thus the structure of the new tree is exactly the same as that of the original tree. Another way to think ab out it is the following: imagine that every element we visit in a preorder traversal is deleted from the tree and inserted in a second (originally empty) tree. It will go exactly at the same (corresp onding) p osition from which it was deleted. Graded by David.

 Create an array B [0; : : : ; n] of b o oleans, all initialized to False. Go through all elements of A, extracting each A[i] bit by bit and interpreting it as a binary numb er; then set B [A[i]] to True. Finally, go through all elements of I nA, and nd the one, say it's B [j ], which is still False; the missing string is the binary representation of j.  Perform k passes over A. During pass j , extract the j th bit of all the elements of A, counting separately the zeros and the ones. If there are more (less) zeros than ones, conclude the pass by setting the j th bit of the missing string to one (zero).  Set the missing string M to 0. Go through all elements of A, extracting each A[i] bit by bit and interpreting it as a binary numb er; then X O R this numb er with M , storing the result back in M. When you are done, M contains the missing string.  Use a non-comparative sort to order the elements of A, interpreted as binary numb ers, in increasing order. Then lo ok at the least signi cant bits of adjacent elements in the sorted array A. If you ever nd two identical bits in adjacent elements A[i] and A[i + 1], this means that the missing string is A[i] + 1. To cover the cases where either the string 00 : : : 00 or the string 11 : : : 11 is missing, simply compare the rst and last elements of A against 00 : : : 01 and 11 : : : 10 , resp ectively.

Common errors:

  1. If n = 2 k^ 1 , then k = lg (n + 1) 6 = O (1). This is an error that nearly everyone made. It is essential to note here that k is not an indep endent parameter. It is de ned in terms of n, and since we can solve for k = lg (n + 1), we cannot claim that k is a constant. All algorithms that run in time O (k n) run in time O (n lg n), which is not linear.

RADIX-SORT do es not run in \linear" time Given n input numb ers that are d digits long, radix sort runs in time O (dn + k d), where k = O (2d^ ) is the range of each digit. In our case, d = lg n, hence RADIX sort, runs in time O (n lg n).

Graded by Suresh.

Problem 4. [21 p oints] Tree Op erations

We wish to augment binary search trees with the Tree-Enumerate(x; a; b) op eration,

which outputs all the keys k such that a  k  b in a binary search tree ro oted at x.

Describ e an algorithm for implementing this op eration whose worst-case running time

in terms of h, the height of the tree, and m, the numb er of keys that are output, is

O (m + h). Prove that the running time of your algorithm is O (m + h).

We do an inorder walk with the following mo di cations:

 If k ey (x) < a then we do not visit l ef t(x).  If k ey (x) > b then we do not visit r ig ht(x).

Assume that the hint is true. We will show that the running time is O (m + h). Partition the \visited" no des according to whether k ey (x) < a, a  k ey (x)  b or k ey (x) > b. By de nition, there are m no des of the second typ e. By the hint, there are at most h no des of the rst typ e as

they all lie in the same path. Similarly, there are at most h no des of the third typ e. Therefore, the running time is O (h + m).

To prove the hint, assume that y and w are \visited" no des with k ey (y ); k ey (w ) < a and supp ose that their least common ancestor is z. Clearly, z 6 = y ; w. Supp ose that y is to the left of z. Then by the prop erties of the binary search tree, k ey (z )  k ey (w ) < a. But then we get a contradiction as our algorithm will not visit l ef t(z ) and therefore it will not visit y.

Graded by David.