Midterm Exam, CMSC 420, Fall 2002: Algorithm Analysis and Data Structures - Prof. Dana S. , Exams of Data Structures and Algorithms

The instructions and problems for a midterm exam in the computer science course cmsc 420, focusing on algorithm analysis and data structures. The exam includes questions on big o notation, worst-case and best-case analysis of algorithms, tridiagonal matrices, ternary trees, and dictionary ordering. Students are allowed open books and notes during the exam.

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-gqb
koofers-user-gqb 🇺🇸

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm Exam, CMSC 420, Fall 2002 Name:
Instructions:
Leave at least one empty seat between you and each of the other students.
This exam is open book, open notes (and closed neighbor :-)
Each question is worth 5 points, for a total of 100 points.
If you want any partial credit for wrong answers, you need to show your work.
Write only on the test sheets. If you run out of room, write on the back of the last page.
Problem 1. Professor Prune has created an algorithm that does some kind of computation on
an array. If the algorithm is called on an array of size n, then the number of comparison operations
done by the algorithm will vary depending on which array of size n, but it will always be a number
in the interval from 2n2nto n3+ 2n1, inclusive.
1. Let Cb est(n) and Cworst(n) be the worst-case and best-case values for the number of compar-
isons done by the algorithm. Below, circle each true statement:
Cbest(n) = O(n2)Cbest (n) = Ω(n2)Cbest (n) = Θ(n2)
Cbest(n) = O(n2lg n)Cbest (n) = Ω(n2lg n)Cbest (n) = Θ(n2lg n)
Cbest(n) = O(n3)Cbest (n) = Ω(n3)Cbest (n) = Θ(n3)
Cworst(n) = O(n2)Cworst(n) = Ω(n2)Cworst(n) = Θ(n2)
Cworst(n) = O(n2lg n)Cworst(n) = Ω(n2lg n)Cworst(n) = Θ(n2lg n)
Cworst(n) = O(n3)Cworst(n) = Ω(n3)Cworst(n) = Θ(n3)
2. Let Iworst (n) be the total number of computer operations (including both comparison oper-
ations and other operations) done by Professor Prune’s algorithm in the worst case on an
array of size n. Is it possible to have Iworst(n)6=O(Cworst(n))? Explain why or why not.
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Midterm Exam, CMSC 420, Fall 2002: Algorithm Analysis and Data Structures - Prof. Dana S. and more Exams Data Structures and Algorithms in PDF only on Docsity!

Midterm Exam, CMSC 420, Fall 2002 Name:

Instructions:

  • Leave at least one empty seat between you and each of the other students.
  • This exam is open book, open notes (and closed neighbor :-)
  • Each question is worth 5 points, for a total of 100 points.
  • If you want any partial credit for wrong answers, you need to show your work.
  • Write only on the test sheets. If you run out of room, write on the back of the last page.

Problem 1. Professor Prune has created an algorithm that does some kind of computation on an array. If the algorithm is called on an array of size n, then the number of comparison operations done by the algorithm will vary depending on which array of size n, but it will always be a number in the interval from 2n^2 − n to n^3 + 2n − 1, inclusive.

  1. Let Cbest(n) and Cworst(n) be the worst-case and best-case values for the number of compar- isons done by the algorithm. Below, circle each true statement:

Cbest(n) = O(n^2 ) Cbest(n) = Ω(n^2 ) Cbest(n) = Θ(n^2 )

Cbest(n) = O(n^2 lg n) Cbest(n) = Ω(n^2 lg n) Cbest(n) = Θ(n^2 lg n)

Cbest(n) = O(n^3 ) Cbest(n) = Ω(n^3 ) Cbest(n) = Θ(n^3 )

Cworst(n) = O(n^2 ) Cworst(n) = Ω(n^2 ) Cworst(n) = Θ(n^2 )

Cworst(n) = O(n^2 lg n) Cworst(n) = Ω(n^2 lg n) Cworst(n) = Θ(n^2 lg n)

Cworst(n) = O(n^3 ) Cworst(n) = Ω(n^3 ) Cworst(n) = Θ(n^3 )

  1. Let Iworst(n) be the total number of computer operations (including both comparison oper- ations and other operations) done by Professor Prune’s algorithm in the worst case on an array of size n. Is it possible to have Iworst(n) 6 = O(Cworst(n))? Explain why or why not.

Problem 2. A tridiagonal matrix is a square matrix T [1..n, 1 ..n] in which T [i, j] is nonzero only if |i − j| ≤ 1. You are to develop a sequential storage allocation strategy for tridiagonal matrices that only allocates storage to the nonzero entries.

  1. Draw a picture of the memory locations for a 4 × 4 tridiagonal matrix T [1.. 4 , 1 ..4]. At each memory location, write a note saying which T [i, j] it represents.
  2. How many storage locations will be needed to represent the nonzero elements of a tridiagonal matrix T [1..n, 1 ..n]? (Give an exact value, not a Θ or big-O value).
  3. Write a formula that gives the location of an element T [i, j] of a tridiagonal matrix T [1..n, 1 ..n].

Problem 3. A ternary tree is like a binary tree, except that each node p has three children: left(p), middle(p), and right(p).

  1. Give a formula for the number of nodes in a perfect ternary tree of height h.
  2. One way to represent a ternary tree is to encode it as a binary tree as shown in Fig. 4.10 on page 110. If T is a perfect ternary tree with n nodes and B is its binary-tree encoding, then how deep is the deepest node of B?
  3. One way to represent a complete ternary tree of n nodes is as a table T [0..n − 1], similar to the implicit representation of a binary tree described on pages 110 and 111. If i is the index of some node T [i], then give formulas for calculating left(i), middle(i), and right(i).

Problem 4. Suppose a dictionary has n elements whose keys are K 1 , K 2 ,... , Kn. Suppose that for each LookU p operation, the argument is Ki with probability 1/ 2 i. Then the optimal ordering for the keys is K 1 , K 2 ,... , Kn. Let Copt(n) be the expected number of comparisons for a successful search if the keys are stored in that order.

  1. When a LookU p occurs, what is the probability that the search is unsuccessful?
  2. What is Copt(4)?
  3. For n ≥ 3, let C(n) be the expected number of comparisons for a successful search if the keys are stored in the order K 2 , K 3 , K 1 , K 4 ,... , Kn− 1 , Kn. What is C(n) − Copt(n)?
  4. It can be proved that Copt(n) < 2 for every n. Use this to give a lower bound on C(n)/Copt(n).

Problem 4.

  1. Write the KMPSkipArray matrix for the string “ABABA”.
  2. Write the BMSkipArray matrix for the string ”ABABA”.

Problem 5. Consider a target string that contains 200 occurrences of the character A, followed by the characters ABABA. Suppose we are searching for ABABA.

  1. How many times will the Knuth-Morris-Pratt algorithm examine characters in the target?
  1. How many times will the Boyer-Moore algorithm examine characters in the target?

Problem 5. Professor Prune says, “Another way to do range searching is to use a binary search tree that is threaded. This way, if want to find all of the nodes in some range, then you only need to search for the first node in the range. Once you have found this node, you can follow the threads to find all of the other nodes in the range.”

  1. Which nodes will Professor Prune’s algorithm visit in the tree shown in Figure 9.10 of the book?
  2. Is Professor Prune’s algorithm any more efficient than ordinary range search? Explain your answer.