







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Data Structures and Algorithms Fall 2002
Fill in your Last Name, ID# and Section # above. Answer all questions in the space provided. Keep your answers BRIEF! This is a CLOSED book exam. Wherever possible, show your work, as you will get credit for partially worked solutions. Before you begin, check that your exam has all sheets including a last sheet for scratch work. The total point value for each question is given next to the question number in square brackets. You are encouraged to read the entire exam before begining, and to work on those questions you know before tackling the questions that you might be a little unfamiliar with. If you get stuck, do not waste time. Move on, and come back to the question if you have time at the end, as you only have a limited time to do all the questions. Once the exam begins, you will not be allowed to ask anyone (including myself or the TA’s) any questions. You must interpret and do each question with the knowledge that you have. Please use diagrams wherever you feel they will give a clearer and quicker answer.
Useful Information
Basic Operations that take 1 unit of time: +,-,*,/,=,comparison, reading from a memory location, changing the value of a pointer, reading or writing a basic type from standard input/output.
Basic types: bool, int, float, double, char, and pointers to them.
∑N
i=
i =
i=
Ai^ =
and
i=
2 i^ = 2N^ +1^ − 1
i=
iAi^ =
i=
i = Θ(log N ) and
i=
i^2
f or large N
(a) a > bk^ =⇒ T (n) = O(N logb^ a). (b) a = bk^ =⇒ T (n) = O(N k^ logb N ). (c) a < bk^ =⇒ T (n) = O(N k^ ).
Suppose that T (n) = aT (n − b) + O(nk^ ), then there are three cases.
(a) a > 1 =⇒ T (n) = O(aN/b). (b) a = 1 =⇒ T (n) = O(N k+1^ ). (c) a < 1 =⇒ T (n) = O(N k^ ).
O() can be replaced by Θ() everywhere or Ω() everywhere.
i=
log i = log N! = Θ(N log N )
)n ≤ F (n) ≤
)n
(b) 2n^2 + n = O(n log n) (c) 2n^2 = O(nlog^ n) (d) Suppose that f (n) = O(a(n)) and f (n) = O(b(n)). Then, it must be that i. a(n) = Θ(b(n)) ii. f (n) = O(2 ∗ a(n) − b(n)) (e) If f (n) = Θ(a(n)) and g(n) = Θ(b(n)). Then, it must be that f (n) ∗ g(n) = Θ(a(n) ∗ b(n)).
6 N log N 2100 log log N log^2 N 2 log^ N^22
3 N 0.^5 5 N 2 N log^2 N 2 N^ N log 4 N 4 N^ N 3 N 2 log N 4 log^ N^
log N
Justify your answer.
for(i=0;i<n;i++){ a[i]=i; if(a[i]>s[i]){ for(j=n;j>0;j--){ a[i]=2s[i]; s[i]=0; cout<<a[i]; } } else{ for(j=n;j>0;j--){ a[i]=s[i]s[i]; for(k=0;k<n;k++){ s[i]+=a[k]; } } } }
(a) [100] What is the maximum height of this tree. Prove your answer. (b) [200] What is the minimum height of this tree. Prove your answer.
(a) [100]Let the time to solve a problem of size n be T (n). Show that T (n) = T (n/2) + 10 (b) [100]Show that T (2) = T (1) + 10, T (4) = T (1) + 2 ∗ 10 , T (8) = T (1) + 3 ∗ 10 (c) [300]Suppose that n = 2k. Prove that T (n) = 5 + 10 ∗ k. [Hint: Use induction.] (d) [200]Use the Master theorem to derive a bound on the run time of this algorithm. Show that it agrees with your previous answer.
What you have shown that if you can solve a problem by solving a problem that is half the size and doing a little bit of extra work, then the run time is logarithmic in the size of the problem.
(a) [100] Give pseudo-code to swap two adjacent elements in a linked list if copying the objects stored in the lists is very expensive, but pointer operations are not. (b) [200] Given two linked lists, L 1 and L 2 , give pseudo-code to create a new linked list L 3 that has all the elements that are in L 1 or in L 2 , without any repetitions. Use only O(1) extra memory. What is the run time of your algorithm? (c) [200] If the two lists were initially sorted, give pseudo-code for a faster algorithm (that uses O(1) extra memory), and compute the run time. (d) [200] Given two linked lists, L 1 and L 2 , give pseudo-code to create a new linked list L 3 that has all the elements that are in both L 1 and L 2 , without any repetitions. Use only O(1) extra memory. What is the run time of your algorithm? (e) [200] If the two lists were initially sorted, give pseudo-code for a faster algorithm (that uses O(1) extra memory), and compute the run time. (f) [200] Give a pseudo-code algorithm to reverse the elements in a linked list that uses O(1) extra memory. What is the run time? (g) [200] Give a pseudo-code algorithm to reverse the elements in an array using only O(1) extra memory. What is the run time?
(a) [200] In the following game there are two players A and B. A random permutation of the integers 1 , 2 ,... , N is generated. These numbers are looked at sequentially. If the number 3 appears before both 1 and 2 have appeared, then player A wins. Otherwise, player B wins. Give a pseudo-code algorithm to compute the number of arrangements in which A wins, NA (for a given input N ). Note that the number of arrangements in which B wins, NB is then given by NB = N! − NA. Compute the run time of your algorithm. For example if N = 10, if the permutation were {1,6,4,3,9,8,7,5,10,2}, then A wins as 3 appeared befor both 1 and 2 appeared. On the other hand, {1,6,4,2,9,8,7,3,10,5} is a win for B. [Hint: The only thing that matters is the order of { 1 , 2 , 3 }.] (b) [400] A binary string consists of N bits. Give a pseudo-code algorithm to compute the number of such strings in which two 1’s never occur together. Make it as efficient as you can. Compute the run time of your algorithm. Example: i f N = 4, 1010 is allowed, but 1011 would not be. [Hint: Let B(N ) be the number we are after. Let F (N ) be the N th^ Fibonnaci number. Show that B(N ) = F (N ) and proceed from there.]
(a) [100] insert(10),insert(8),insert(6),insert(2),insert(4),insert(1),insert(7), delete(6) for a BST. (b) [200] insert(10),insert(8),insert(6),insert(2),insert(4),insert(1),insert(7), delete(6) for a min-Heap. Show the result both as a tree implementation and as an array implementation for the resulting heap. (c) [200] insert(10),insert(8),insert(6),insert(2),insert(4),insert(1),insert(7), for an AVL tree (note, no delete required here).
40
30
20
50
10
60
70
80
35
33
31
45
43
41
58
55 59 54
37 47
Whether or not it is an AVL tree, we can still perform the AVL delete operation on it. Show the result of performing AVL-delete on element with key 10.
dH^ + d − 2 d − 1
dH+1^ − 1 d − 1 [Hint: Use induction or sum a particular geometric series.]
(a) What data structure would you use to enable the train company to achieve its goal. (b) Assume that the company wants to store the schedule for the next 10 years, hence there are LOTS of trains. First of all the company needs to build the data structure that you have chosen. How long will this take to build? (c) Illustrate how you will use the data structure to implement all the tasks that the company needs, and discuss how long it will take to do each task in the worst case.
[Hint: You need constant access time to the next train (earliest departure).
operation LIST ARRAY BST AVL tree Min-Heap
find(X)
insert(X)
delete(X) O(log n)
build
rand access(i)
find min
find max
(b) [100] Given your table, would you ever use a BST over a List? Explain your answer. (c) [200] Construct an algorithm to find the median (middle element) of a set of n numbers, assuming that all the numbers are different and that n is odd. Analyse the run time of your algorithm.
(a) [100] Give pseudo-code to perform insert(X) and delete(X) on this tree. (b) [100] What is the worst case run time and why? (c) [200] What is the average case run time and why?
F (0) = 0 F (1) = 0
F (N ) =
i=
F (i) + F (N − 1 − i) N ≥ 2
[Hint: Use the recursive nature of trees like we did to get the average height of a BST.] Show that a solution to this recursion is
F (0) = 0 F (1) = 0
F (N ) =
[Hint: Use induction.] Hence show that the average number of leaves, L(N ), is given by
L(0) = 1 L(1) = 1
L(N ) =
[Hint: Look at the previous question part (c).]
D(0) = 0 D(1) = 0
i=
iD(i) +
[Hint: Use the recursive nature of trees like we did to get the average height of a BST.] and show that a solution to this recursion is
D(0) = 0 D(1) = 0
i=
N − i i + 1
[Hint: Use induction.] Hence that D(N ) = Θ(log N ), or that the average depth of a BST is O(log N ). What implication does this have on run time of operations on a BST. [Hint: You need to manipulate this summation. Show that the largest value in the summation numerator is N to get the O() bound. Show that the smallest value in the summation numerator for thefirst N/2 terms is N/2 and proceed to get the Ω() bound, and hence the Θ() bound. You will need to use one of the formulas in the front of this exam.]
T (0) = 0 T (1) = 0
i=
T (i) + N − 1 N ≥ 2
Obtain a solution to this recursion, and show that T (N ) = Θ(N log N ) [Hint: Use the answer to the previous part.]
(a) [300]Unlimited additional memory is available. (b) [300]O(1) additional memory is available.
(a) [200] Prove that ∑N
i=
iAi^ =
(b) [200]Prove by induction that the sum of the first n odd integers is n^2.
(c) [200]Prove by induction that
i=
i = Ω(n^2 ).
pow(n){ if(n==0) return 1; return 2*pow(n-1); }
Argue that the recursion for the run time is given by T (0) = 1 and T (n) = T (n − 1) + 2 for n > 0. Use the Master theorem to get a Θ() bound on the run time. Prove by induction that T (n) = 2n + 1 for n ≥ 0. Does this last result agree with the master theorem?