Docsity

Midterm - Data Structures and Introduction to Algorithms | CSCI 2300, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI 2300 RPI
Data Structures and Algorithms Fall 2002
MIDTERM
Last Name: Student ID#: Section:
Instructions
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.
When time is up, continuing to write will cost 5 points for every minute over.
1 2 3 4 5 6 TOTAL
250
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Midterm - Data Structures and Introduction to Algorithms | CSCI 2300 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSCI 2300 RPI

Data Structures and Algorithms Fall 2002

MIDTERM

Last Name: Student ID#: Section:

Instructions

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.

When time is up, continuing to write will cost 5 points for every minute over.

1 2 3 4 5 6 TOTAL

Useful Information

  1. 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.

  2. Basic types: bool, int, float, double, char, and pointers to them.

∑N

i=

i =

N (N + 1)

  1. For A 6 = 1, ∑N

i=

Ai^ =

AN^ +1^ − 1

A − 1

and

∑^ N

i=

2 i^ = 2N^ +1^ − 1

∑^ N

i=

iAi^ =

A

(1 − A)^2

N AN^ +1^ − (N + 1)AN^ + 1

∑N

i=

i = Θ(log N ) and

∑^ N

i=

i^2

f or large N

  1. Recursive algorithms and recursive functions. Suppose that T (n) = aT (n/b) + O(nk^ ), then there are three cases.

(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.

∑N

i=

log i = log N! = Θ(N log N )

  1. Fibonacci recursion: F (n) = F (n − 1) + F (n − 2) with F (0) = F (1) = 1 satisfies the following bounds. ( 7 5

)n ≤ F (n) ≤

)n

  1. Sorting will only allow the operations of assignment and comparison.

(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)).

  1. [200]Order the following list of functions in terms of big O notation, and group together (say by circling together) the ones that are big Theta of each other

6 N log N 2100 log log N log^2 N 2 log^ N^22

N √

N N 0.^01 N^1 4 N 3 /^2

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.

  1. [200]Compute upper and lower bounds for the running time, T (n), of the following algorithm as a function of n. Assume that s is a variable defined somewhere else and you have no idea what it is. Your answer should be of the form T (n) = O(?) and T (n) = Ω(n). Use the space on the side of the code.

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]; } } } }

  1. A d-ary tree is one in which each node can have at most d children. Consider a d-ary tree with N nodes.

(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.

  1. A recursive algorithm f (n) has the following properties (where n is the size of the input). The base case is when the input has size 1 and takes 5 seconds to compute. In other words f (1) takes 5 seconds to compute. Suppose that the recursive algorithm has the following property. To solve f (n), we solve a problem of size n/2 and do a little bit of extra work that takes 10 seconds. In other words, f (n) = f (n/2) + Extra W ork.

(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.

  1. In this problem, you are asked to design some algorithms. Try to make the algorithms as fast as you can. More credit will be awarded to more efficient algorithms. No credit will be awarded to algorithms that do not work.

(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?

  1. In this problem, you are asked to design some algorithms. Try to make the algorithms as fast as you can. More credit will be awarded to more efficient algorithms. No credit will be awarded to algorithms that do not work.

(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.]

  1. Show the FINAL result of the sequence of operations specified on an initially empty data structure of the type specified.

(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).

  1. (a) [100] For an AVL tree, we discussed in class, we discussed insert and remove operations that required elements stored in different nodes to have different keys. If we wanted to allow multiple values of the same key, describe a simple modification that would allow this and keep all the operations as we discussed before intact. (b) [200] For an AVL tree, describe using pseudo-code and pictures where more convenient, the algorithm for the insert operation. Make sure to take care of all cases. (c) [300] For an AVL tree, describe using pseudo-code and pictures where more convenient, the algorithm for the remove operation. Make sure to take care of all cases. (d) [400] Show that AVL insert and remove algorithms you gave above are guaranteed to end with a legitimate AVL tree. (e) [200] For a BST, describe using pseudo-code and pictures where more convenient, the algorithm for the delete operation. Make sure to take care of all cases. (f) [300] Show that the BST insert and remove algorithms you gave above are guaranteed to end with a legitimate BSTtree. (g) [300]Is the following binary tree an AVL tree? If so, why? If not, why not?

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.

  1. [300] A binary tree is a tree where every node has at most 2 children. A d-ary tree can have at most d children at each node. Let N be the number of nodes and H the height of a COMPLETE d-ary tree. Remember that in a complete tree, nodes are filled from left to right and top to bottom (leaves can be added only at depth H or higher. Show that the following inequalities hold

dH^ + d − 2 d − 1

≤ N ≤

dH+1^ − 1 d − 1 [Hint: Use induction or sum a particular geometric series.]

  1. [300] Consider the following problem for maintaining a schedule of trains from Albany to Boston. We can view each train in our schedule as a clock that tells you how long till it departs. The clock for each train gradually ticks down to zero at which time the train has departed. The “set of clocks” can be viewed as our train schedule, and knowing the time right now, we can get the time of departure for any particular train by adding the time on the clock for that train to the current time. For example: suppose that the time is noon, and there are 3 trains (clocks) in our schedule with times reading 2hr, 1hr, 3hr, then the departing times of these trains in our schedule would be 2pm, 1pm, and 3pm respectively. The train company would like to be able to do the following things.
    • Any time a customer calls to enquire when the next train to Boston departs, the operator must be able to give the answer, the quicker the better, to prevent conjestion of the phone lines.
    • When a train has departed (clock has reached zero), it needs to be taken off the schedule.
    • Trains (clocks) get added to the schedule and sometimes (very rarely), trains get cancelled and thus it is necessary to find that particular train and remove it.
    • trains (clocks) get delayed and sometimes (though rarely) they get advanced, and the schedule needs to updated accordingly.

(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).

  1. (a) [200] For lists and arrays, assume that insert(X) is only allowed at the begining. For binary trees, element (i) is the ith^ element in an in-order traversal. Complete the following table giving the worst case running times. Each entry should be of the form O(f (n)), where f(n) is as small as possible for the bound to hold, and n is the number of elements (an example is shown below).

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.

  1. Consider a BST with N nodes.

(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?

  1. [400] Let F (N ) be the average number of full nodes. Show that the average number of full nodes satisfies the following recursion

F (0) = 0 F (1) = 0

F (N ) =

N − 2

N

N

N∑ − 1

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 ) =

N − 2

N ≥ 2

[Hint: Use induction.] Hence show that the average number of leaves, L(N ), is given by

L(0) = 1 L(1) = 1

L(N ) =

N + 1

N ≥ 2

[Hint: Look at the previous question part (c).]

  1. [400] Let D(N ) be the average depth of the BST. Show that D(N ) satisfies the following recursion.

D(0) = 0 D(1) = 0

D(N ) =

N 2

N∑ − 1

i=

iD(i) +

N − 1

N

N ≥ 2

[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

D(N ) =

N

∑^ N

i=

N − i i + 1

N ≥ 2

[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.]

  1. [300] Using the previous part, show that the total depth of the nodes in a BST, T (N ), satisfies the following recursion

T (0) = 0 T (1) = 0

T (N ) =

N

N∑ − 1

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.]

  1. (a) [500]Give an exact expression for the minimum number of nodes in an AVL tree of height h. [Hint: Show that N (h) = N (h − 1) + N (h − 2) + 1. Use this to show that N (h) = F (h + 2) − 1, where F (N ) are the Fibonacci numbers, F (0) = 1, F (1) = 1, F (N ) = F (N − 1) + F (N − 2), so a closed form for F (N ) gives one for N (h).] (b) [300]What is this minimum number for h = 15? (c) [500]A tree is perfectly balanced if every node has a balance (HR − HL) of zero. Suppose that the keys 1, 2 ,... , 2 k^ − 1 are inserted in that order into an initially empty AVL tree. Prove that the resulting tree is perfectly balanced. [Hint: Use induction and the properties of AVL trees and the insert function.]
  2. [300] How many bits are needed to store the height (in each node) of an N -node AVL tree. Use this result to determine the smallest AVL tree (in terms of number of nodes) that will overflow an 8-bit height counter. [Hint: To overflow 8 bit, the height of some node must be ≥ 28 – which node has this height? Now use the answer to the previous question. Your final answer can be in terms of a Fibonnacci number.]
  3. (a) [200]For a binary tree, write pseudo code recursive functions to compute the number of nodes in the tree, the number of full nodes and the number leaves in the tree. The function should take as input a pointer to the root. Compute the run time of your functions. [Hint: Consider a traversal.] (b) [300]For an AVL tree, given the pointer to the root and the two keys k 1 < k 2 , write a function to print out all the elements between k 1 and k 2 in O(log N + K) worst case time (and show that it runs with this time bound) where N is the number of elements in the tree and K is the number of elements printed. [Hint: Modify a LR-inorder traversal and analyse.] (c) [300]Give pseudo-code for a recursive algorithm that returns a pointer to an AVL tree with height h that has the minimum number of nodes possible, N (h), and the entries 1,... , N (h) in the nodes. What is the run time of your algorithm. [Hint: Use the fact that N (h) = N (h − 1) + N (h − 2) + 1 to guide you. What is the base case?] (d) [300]Give pseudo-code for an algorithm to create a perfectly balanced BST of height h with the entries 1,... 2 h+1^ − 1 nodes. [Hint: Recursion.]
  4. An array contains N elements. Give an algorithm to determine a majority element if one exists. A majority element is one that occurs more than N/2 times in the list, and therefore must be unique. Solve the problem as efficiently as you can in the following two cases.

(a) [300]Unlimited additional memory is available. (b) [300]O(1) additional memory is available.

  1. Induction

(a) [200] Prove that ∑N

i=

iAi^ =

A

(1 − A)^2

N AN^ +1^ − (N + 1)AN^ + 1

(b) [200]Prove by induction that the sum of the first n odd integers is n^2.

(c) [200]Prove by induction that

∑N

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?