Docsity

CS161 Midterm Information, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS161: Data Structures and Algorithms Handout #12
Stanford University Tuesday, 30 April 2002
Lecture #9: Tuesday, 29 April 2002
Topics: Midterm information
Midterm Information
When: Tuesday, 7 May 2002, 9:30–10:45 a.m.
Where: Skilling 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 or CLRS).
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 CS161 Midterm Information and more Study notes Algorithms and Programming in PDF only on Docsity!

CS161: Data Structures and Algorithms Handout

Stanford University Tuesday, 30 April 2002

Lecture #9: Tuesday, 29 April 2002

Topics: Midterm information

Midterm Information

When: Tuesday, 7 May 2002, 9:30–10:45 a.m.

Where: Skilling 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 or CLRS).

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, 30 April 2002

Lecture #9: Tuesday, 29 April 2002

Topics: Midterm examination and solutions from Spring 1998

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

75 points.

• This exam is closed book. You may use one 8^12 ′′^ × 11 ′′^ 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 spend too much time on any problem. Read them all through first and

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

• Show your work, as partial credit will be given. You will be 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 above 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 first half of the lg^3 terms — or, to be precise, the first m = b log 23 nc terms — are each at least as big as n lg^3

n = (n lg^3 n)/ 8 — or, to be 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 appearing as exercise 4.4-2 on page 72 of CLR. It is also possible to solve this problem using the tree method. However, we should be careful to show both a lower and an upper asymptotic bound 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 be done by considering the cummulative cost of just half the tree levels, in a manner similar to our previous derivation using the iteration method. 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 does 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 be approximated as O(lg^3 n). A simple way to analyse this would be to observe that each term is O(lg^3 n) and there are O(lg n) such terms, yielding an upper bound of O(lg^4 n). Graded by Suresh.

(c) T 3 (n) = T 3 (bn/ 2 c) + T 3 (bn/ 4 c) + n [2 points]

Using the tree method 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 be unbalanced and that the subtree corresponding to T 3 (bn/ 2 c) will be deeper (i.e. it will have more levels). At all levels after the shallow subtree — the one corresponding to T 3 (bn/ 4 c) — becomes exhausted, each level will cost no more than

( 3 4

)i n. So we have

T 3 (n) ≤

∑^ ∞

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 loose. 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 above bound of O(n).

Graded by Suresh.

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

g(n) across the top, write O, Ω, or Θ in the appropriate box, depending on

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

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

The first line is a demo solution. [1 point per entry]

True False

The answer is True. In fact, the adversary does 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 depends on the internal, random selection of pivots; hence, it is not impossible (i.e. there is a positive probability) that the selection will be bad at every single recursive step, partitioning the array into subproblems of sizes n − 1 and 1. Also, if the input consists of n repetitions of the same number, then a Quick-Sort which uses the Partition procedure shown in class will definitely take Ω(n^2 ) steps, whether it’s deterministic or randomized. Graded by David.

(b) A max-based heap can be 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 be 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 procedure based on a new Heapify which, in turn, guarantees that every parent is no greater than its children. Although it is possible 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 possible 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 appears on the right side in the figure below. Reversing the contents of the array produces 4 , 1 , 5 which is not a min-based heap since the root, 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 be more precise, not necessarily. For example, the left heap in the figure below produces

5 , 4 , 1

during a preorder walk, while the right heap produces

5 , 1 , 4.

5

1 1 4

5

4

One counterexample suffices to show the fallacy of the claim. Another, more general way to prove that no tree walk (whether inorder, preorder, or postorder) can output the nodes 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 open 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 number of elements to be hashed is greater than the number of slots available, so we can’t use open addressing. Even when α doesn’t exceed 1 but it is close to 1 , chaining is much better since clustering will make open addressing very inefficient. Students who argued the inefficiency of open addressing for α close to 1 but didn’t say that open addressing fails for α > 1 lost only 1 point. Graded by David.

(e) If we know the ordered lists of keys produced 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 because 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 root of the tree before either subtree, then to reconstruct the tree we only need to do the following:

  1. Initialize the tree to be empty.
  2. Insert the elements in the order given by the preorder output. We’ll get the original tree because the root of the original tree (and every subtree recursively) is inserted into the new tree before 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 about 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 (corresponding) position from which it was deleted. Graded by David.
  • Create an array B[0,... , n] of booleans, all initialized to False. Go through all elements of A, extracting each A[i] bit by bit and interpreting it as a binary number; then set B[A[i]] to True. Finally, go through all elements of InA, and find 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 jth 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 jth 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 number; then XOR this number 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 numbers, in increasing order. Then look at the least significant bits of adjacent elements in the sorted array A. If you ever find 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 first and last elements of A against 00... 01 and 11... 10 , respectively.

Common errors:

  1. If n = 2k^ − 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 independent parameter. It is defined 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(kn) run in time O(n lg n), which is not linear.

RADIX-SORT does not run in “linear” time Given n input numbers that are d digits long, radix sort runs in time O(dn + kd), 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 points] Tree Operations

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

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

Describe an algorithm for implementing this operation whose worst-case running time

in terms of h, the height of the tree, and m, the number 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 modifications:

  • If key(x) < a then we do not visit lef t(x).
  • If key(x) > b then we do not visit right(x).

Assume that the hint is true. We will show that the running time is O(m + h). Partition the “visited” nodes according to whether key(x) < a, a ≤ key(x) ≤ b or key(x) > b. By definition, there are m nodes of the second type. By the hint, there are at most h nodes of the first type as

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

To prove the hint, assume that y and w are “visited” nodes with key(y), key(w) < a and suppose that their least common ancestor is z. Clearly, z 6 = y, w. Suppose that y is to the left of z. Then by the properties of the binary search tree, key(z) ≤ key(w) < a. But then we get a contradiction as our algorithm will not visit lef t(z) and therefore it will not visit y.

Graded by David.