Docsity

Practice Midterm 1 - Algorithm Analysis and Design | ECS 122A, Exams of Computer Science

Toggle sidebar
bg1
ECS122A: Algorithm Analysis and Design
Practice Midterm 1
The midterm will be open notes, open book.
These questions are a little harder than the ones that will be on the midterm.
You should also go over all the algorithms and analyses we did in class and make sure you
understand the steps, and go over the homework solutions for homeworks 1 and 2 and make
sure you understand them (although you would be safe to assume that Pr. 1 from HW
1, and Pr. 4 from HW 2, are both difficult enough that they are unlikely to appear on
the midterm....). Know the definition of the expectation of a random variable, a universal
hash function, and big-O and big-Ω. Be sure you can do a proof by substitution to show
that a function T(n) is big-O of some other function f(n). Be sure you know where to find
important formulas in your notes or in the book.
1. We roll a single six-sided die four times in a row. Each time, the top number is one of
{1,2,3,4,5,6}, each with probability 1/6.
a) What is the probability that we roll the sequence exactly 2,3,2,2?
b) How many possible sequences of four numbers can be produced by the four rolls?
Position matters, that is, 2,3,2,2 is different from 2,2,3,2.
2. Give the best big-O upper bound you can for lg(nlg n). Give explicit constants c, n0in
the big-O bound.
1
pf3
pf4

Partial preview of the text

Download Practice Midterm 1 - Algorithm Analysis and Design | ECS 122A and more Exams Computer Science in PDF only on Docsity!

ECS122A: Algorithm Analysis and Design

Practice Midterm 1

The midterm will be open notes, open book.

These questions are a little harder than the ones that will be on the midterm.

You should also go over all the algorithms and analyses we did in class and make sure you understand the steps, and go over the homework solutions for homeworks 1 and 2 and make sure you understand them (although you would be safe to assume that Pr. 1 from HW 1, and Pr. 4 from HW 2, are both difficult enough that they are unlikely to appear on the midterm....). Know the definition of the expectation of a random variable, a universal hash function, and big-O and big-Ω. Be sure you can do a proof by substitution to show that a function T (n) is big-O of some other function f (n). Be sure you know where to find important formulas in your notes or in the book.

  1. We roll a single six-sided die four times in a row. Each time, the top number is one of { 1 , 2 , 3 , 4 , 5 , 6 }, each with probability 1/6. a) What is the probability that we roll the sequence exactly 2, 3 , 2 , 2?

b) How many possible sequences of four numbers can be produced by the four rolls? Position matters, that is, 2, 3 , 2 , 2 is different from 2, 2 , 3 , 2.

  1. Give the best big-O upper bound you can for lg(nlg^ n). Give explicit constants c, n 0 in the big-O bound.
  1. Let us say we are given a long thin rectangle of size k × 2. It contains 2k red points and 2k blue points, for a total of n = 4k. We are guaranteed that the distance between any two red points is at least 1, and also the distance between any two blue points is at least 1. Consider the following recursive algorithm for finding the closest pair of points such that one point is red and the other is blue. We divide the points into two sets, PL and PR, such that the x-coordinate of each of the n points of PL is ≤ the x-coordinate of each of the n points of PR. We recursively find the closest red-blue pair in PL and PR. Let δ be the distance between the closest red-blue pair found so far (which was the closest red-blue pair either in PL or PR). The only remaining possibility is that the closest red-blue pair lies in the small rectangle of height 2 and width 2δ around the vertical line separating PL from PR. An argument similar to the “orange packing” argument we used in class shows that with so many points packed into the box, δ has to be less than one. So you can assume δ < 1. a) Give the smallest upper bound you can on the number of points in this small rect- angle. No proof necessary.

b) Write down a recurrence relation describing the running time T (n) to find the clos- est red-blue pair in the set P.

c) Give the best solution you can for this recurrence relation, and prove it by substi- tution. By “best” here, we mean the smallest upper bound possible. Hint: You might want to look at the subsection titled “Subtleties” in Section 4.1.

  1. In Exercise 5.3-4, we showed that a function called PERMUTE-BY-CYCLIC(A) does not produce every permutation of input array A uniformly at random. One way to argue this is to show that there are many permutations of A which cannot be produced by PERMUTE-BY-CYCLIC. A nice way to do this involves counting.
    1. Array A has n elements. How many permutations of the elements of A are there?
  1. How many permutations of A are possible outputs of PERMUTE-BY-CYCLIC?

Consider a function very like the one we used as part of the universal hash function, f (x) = (ax + b) mod n. Here n is the size of array A, a is chosen randomly from { 1 ,... n − 1 } and b is chosen randomly from { 0 ,... , n − 1 }. When n is prime, we could use f () to produce a permutation B of A as follows:

for i = 0 to n − 1 B[f (i)] = A[i]

  1. Prove that when n is prime, B is a permutation of A. Hint: Prove that if i 6 = j, then f (i) 6 = f (j). Why would this imply that B is a permutation of A?

  2. Assume that n is prime. How many permutations of A can be produced by this function?

  3. Does this function produce every possible permutation of A?