







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Theory of Computation; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2002;
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








This is a closed-book, closed-notes, open-brain exam. If you brought anything with you besides writing instruments and your handwritten (^8 ) ′′ × 11 ′′^ cheat sheet, please leave it at the front of the classroom.
Print your name, netid, and alias in the boxes above. Circle U if you are an undergrad, or G if you are a grad student. Print your name at the top of every page (in case the staple falls out!).
The last few pages of this booklet are blank. Use that for a scratch paper. Please let us know if you need more paper.
If your cheat sheet if not hand written by yourself, or it is photocopied, please do not use it and leave it in front of the classroom.
Submit your cheat sheet together with your exam. An exam without your cheat sheet attached to it will not be checked.
If you are NOT using a cheat sheet you should indicate it in large friendly letters on this page.
Time limit: 180 minutes.
Relax. Breathe. It is almost over.
II. [20 Points] Suppose we are given two sorted arrays A[1 .. n] and B[1 .. n] and an integer k. Describe an algorithm, as fast as possible, to nd the kth smallest element in the union of A and B. (For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, our algorithm should return the median of A ∪ B.) You can assume that the arrays contain no duplicates. How fast is your algorithm?
III. [20 Points] You are given a tree T with n nodes, and root r. In the following, you can assume that one can compute the depth of a node of T in constant time, and that given a node v and k > 0 , one can compute the ancestor(v, k) = parent(k)(v) of v in constant time.
(a) [5 Points] Show how to preprocess T , such that given a query which is made out of two nodes u, v of T , one can decide, in O(1) time, if u is a child of v. (we denote this operation by IsChild(u, v).) (b) [5 Points] Using (a), describe an algorithm, as fast as possible, such that given a query made out of two nodes u, v of T , it computes the LCA (least common ancestor) of u and v in T. How fast is your algorithm as a function of n? How fast is it in term of h (h is the height of T )? (c) [3 Points] Assume that you are given an operation F indOne(x), which returns the location of the rst bit of x (x is a non-negative integer number) which is non-zero (starting from the least signi cant bit). For example F irstOne(6) = F irstOne(110 2 ) = 1 and F irstOne(5) = F irstOne(101 2 ) = 0, and F irstOne(16) = F irstOne(10000 2 ) = 4. Describe a function, such that given two positive integers a, b it computes the rst bit in which they di er. You can assume that you are allowed to use all standard bitwise operations, like and, or, xor, and not. (d) [6 Points] Assume that every node in T has at most two children. Show how to preprocess T , so that given two nodes u, v of T , one can compute their LCA in O(1) time. How much time does the preprocessing takes? (e) [1 Points] Why is the solution for (d) is unacceptable in practice?