0% found this document useful (0 votes)
51 views8 pages

Homework File: March 23, 2012

This homework document from Professor Yap's Data Structures course provides instructions for Homework 4. It includes 3 questions: 1) Implement a linked list using a "free list" method for memory management. Students must write methods to allocate and free nodes from a node array. 2) Implement removal and predecessor methods for a binary search tree. Students must also write self-tests for these methods. 3) Convert the binary search tree to a generic class BSTGeneric<T> and write self-tests using integers. Students must submit Makefile, README, and Java files for the three questions.

Uploaded by

taemini9214
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Homework File: March 23, 2012

This homework document from Professor Yap's Data Structures course provides instructions for Homework 4. It includes 3 questions: 1) Implement a linked list using a "free list" method for memory management. Students must write methods to allocate and free nodes from a node array. 2) Implement removal and predecessor methods for a binary search tree. Students must also write self-tests for these methods. 3) Convert the binary search tree to a generic class BSTGeneric<T> and write self-tests using integers. Students must submit Makefile, README, and Java files for the three questions.

Uploaded by

taemini9214
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures Course (CSCI-UA 102) Professor Yap Spring 2012

HOMEWORK FILE

March 23, 2012

Homework Instructions (H10 is new!)


H1. Please read carefully. There is penalty for non-compliance. H2. This homework le will contain all our homework assignments, listed in reverse chronological order. So, when printing, please print only what you need. H3. No late homework. If you have a valid delay, please obtain special permission in advance. H4. All homework must be submitted by email by midnite of due date. H5. I encourage you to work in small groups (two or three seems optimum). H6. Any submitted work must be your own work. You may not copy from anyone, not even from your group members. You also have responsibility not to show your work to anyone. (Have you read the Academic Integrity Statement in our webpage yet?) H7. Each homework must be emailed to [email protected]. It must be in a single TAR le called hwXXX-YYY-ZZZ.tar where XXX is the homework number, YYY is your last name (initial caps only), and ZZZ is your rst name (initial caps only). E.g., hw2-Yap-Chee.tar is my submission for homework 2, and hw3-Peng-Chang.tar is the TAs submission for homework 3. H8. If you need to re-submit your homework, you send us another complete set of les, as in the original submission. When we receive your re-submission, we discard the entire previous submission. Even if you modify ONE le, you must re-submit the other unmodied les. H9. What is inside the TAR le? It must always have a README le. Your solution for each question should be in its own le, and the two accepted formats are either pdf or jpg (if scanned). Name them either Q-NNN.pdf or Q-NNN.jpg for problem number NNN. E.g., Q-1.pdf, Q-13.jpg for questions 1 and 13. If there are two pages in (say) question 6, then Q-6-page1.pdf, Q-6-page2.pdf. Bobst Library basement and 4th oor has scanners. NOTE: DO NOT PUT SPACES IN FILE NAMES. Our convention is to use hyphens instead of spaces. H10. HINT for organizing homework: create in your home directory a directory called DataStruc. Put each homework in its own subdirectory in DataStruc. E.g., DataStruc/hw1, DataStruc/hw2, etc. When you need to create the tar le for hw2, go to its directory and type something like this: > tar -cvf hw2-Yap-Chee.tar Makefile README *.java Q-* H11. What is in the README? (1) Your name and NYU ID. (2) Your email address (+ optionally, phone). (3) Whom you have collaborated with (4) Anything else you like to tell us. E.g., it is nice if you can also give credit for ideas from your group members. Acknowledge sources and books, etc. This does NOT take away points from your solution it is proper scholarly practice. H12. Programming exercises are special: besides the Java programs, we expect to see a Makefile. Sometimes you might also have data les, representing input or output for the programs. DO NOT include any .class les. When we type make it should compile and run (with default arguments). H13. OTHER ISSUES: Both README and Makefile must be must be in plain ASCII format, NOT enriched formats which some editors introduce. Also do not give them the .txt extension. Scan your jpg les at 175 to 300 dpi, as we do not want huge les.

Homework 4: Due on Thu Mar 29

Q1 (25 Points) Free-list Implementation of Linked List Consider the generic List interface as given in Figure 3.9 (p.63, Chapter 3.3) of the textbook for this interface. So far we have studied two methods to implement this interface: using a linked List and using an array. Now consider a third method which (for lack of a better name) I will called free-list method. The idea is to simulate the linked list method using your own memory management. In linked list implementation, you rely on Javas memory allocation whenever you get a new node (by calling new Node()). To handle memory (i.e., node) allocation yourself, imagine you rst create an array of nodes (call it aNodes). Each time you need a node, you get from aNodes, and when you want to delete a node, you must explicitly free up that space (in Java, you do not have to do this because of Javas garbage collection). Hence you must keep track of which nodes in aNodes are free and which are not free. To do this, we will form a linked list of all the free nodes: this list is conventionally called the freelist. You must provide two routines: to get a new node from the freelist, and to return a node to freelist. Here is the signature of these two methods: int myNew(); -- gets a node from freelist void myOld(int x); -- returns node x to freelist We declare the Node class and aNodes array as follows: class Node int key; int next, prev; -- indices for aNodes array } static Node [] aNodes = new Node[100]; Instead of references to Nodes, we now use array indices of type int to refer to nodes. Thus next and prev above have type int. In the recitation, Chang has explained to you how we maintain the freelist. Actually, the freelist behaves more like a stack, and myNew() is like popping from a stack and myOld(int) is like pushing onto a stack. Part A: Use this free-list method to implement the small List interface given in Figure 3.9 (p.63, Chapter 3.3) of the textbook. The main class that you implement should be called FreeLinkedList. We basically require four methods. However, because this interface extends the Collection interface, it will force you to implement lots of other methods! The solution is to provide dummy implementations (for an example of dummy implementations, look the sample MyLinkedList.java from our Pickup Site). Part B: Please implement a self-test of your implementation to show that it is working correctly. Use a random number generator, and you must explain in your README le how the testing works. Modify your Makefile so that we can just type > make Q1 to compile and run it. Q2 (25 Points) BST Implementation The Pickup Site has a le Bst hw4.java that contains an implementation of Binary Search Trees. We only provided dummy implementations remove(int k) and pred(Node) (for predecessor). Please rename the le Bst.java implement the following: Part A: Please implement the pred(Node) method and the remove(int k) method. Your remove method must use a helper method called cut(Node u) which removes the node u which is guaranteed to be non-null and has at most one child. Part B: Please implement in the main method some self-tests for your remove and pred methods. Modify your Makefile so that we can just type > make Q2 to compile and run it. The README le must explain how this test is done. Q3 (15 Points) Generic BST Implementation Convert your Bst.java in Q2 into a Generic class, class BstGeneric <T>. When testing BstGeneric<T>, 2

please use T=Integer. Modify your Makefile so that we can just type > make Q3 to compile and run it. The README le must explain how this test is done. REMINDER: Please submit 5 les for this homework: Makefile, README, FreeLinkedList.java, Bst.java, BstGeneric.java.

Homework 3: Due on Thu Mar 1

Q1 (1 Point) Our Makele has various variables. For this question, assume the variables are p, arg, arg2. Suppose you want to have a target called parameters (or par for short) which will print all the default values the variable values. Please write the makele code for this target. HINT: use echo command but look up on the variation @echo. Q2 (8 Points) Assume n is a natural number (n N). Suppose T (n) = 1 T (n 1) + T (n 2) + T (n 3) if n 2 else.

(a) Prove by induction that T (n) 2n (ev.). (b) Prove by induction that T (n) 3n/3 (ev.). Q3 (30 Points) We have seen 2 solutions for Fibonacci numbers so far: Fib.java which uses int and runs in exponential time, and Fibonacci.java which uses BigInteger and runs in linear time. A further improvement is possible: Fibonacci.java uses linear space (O(n) space to compute Fn ). We now design a version that uses only constant space (O(1) space). Some of you already used this version in your homework. Here is the idea: consider the initial triple T0 = [n, 0, 1]. More generally, consider Ti = [n i, Fni , Fni+1 ] where i ranges from 0 to n 1. We can transform Ti to Ti+1 using the rule [m, a, b] [m 1, b, a + b] Thus applying this transformation n 1 times to T0 , we get Tn1 = [1, Fn1 , Fn ] and so the answer Fn is available. We want you to program this version. For this problem, we want to do some timing to see how these three methods perform. Let these 3 methods be called int fib(int), BigInteger fib2(int) and BigInteger fib3(int), respectively. Write a Java class called FibTime with these 3 methods and a main method to compare their timing for any range of values of n. Of course, fib(int) gives the wrong answer for large values of n, but that is not the point. Because fib() is exponential, do not test it for large n (I let you decide how big an n you want to test within a reasonable time). But you should test fib2(n) and fib3(n) for much larger values of n. Present us with a table of your timings (put it in a le called Timing.txt). Be sure that the le is clearly explained. NOTE: we will provide hints on how to do timing (for Q3) and also generating random numbers (for Q4) in our programming page. The web should can also give many suggestions. Q4 (15 Points) (a) Write a Java class called MyStack<T> that implements a generic stack. Please use MyArrayList which was provided earlier. You must have a main method to test your stack of Integers as follows: randomly generate and push 40 Integers, then print the stack contents, and nally pop 40 times (printing the popped contents). (b) Write a Java class called MyPostfixEval in which there is a method called eval that takes a string representing a postx expression over the four arithmetic operations, and outputs the value of the expression. The constants in the expression are machine doubles. The main program should use the MyStack class from part(a), and be able to read an input string from command line (but if there is no command line input, the default string is taken from rst line of this following table (i.e., 1 2 3 6 ). 4

Input: Eval:

* 6

+ 7

0.5

* 3

10

Note that there should be only ONE Makefile when you submit your homework. This rule applies to all homeworks. But how do we distinguish the programs of (Q3) and the two programs of (Q4)? Well, introduce another variable in your Makefile called w (which). If w=1 (the default), we should compile, and run, the program of Q3. If w=2, we will do the same for Q4(a). If w=3, we will do the same for Q4(b). E.g., > > > > > make make make make make cr cr cr cr cr w=1 w=2 w=3 w=1 arg=40 ----------this should compile and same as above this should compile and this should compile and presumably, you want to run FibTime.java run MyStack.java run MyPostfixEval.java compute F_40.

So, you need to modify your Makele to have this behavior.

Quiz 2: Feb 22

Q1. What is the big-Oh order of this code fragment: s=0; for (i=0; i<n; i++) for (j=0; j<m; j++) for (k=0; k<p; k++) s+= i*j*k; Q2. Write a simple Java Interface for a generic stack.

Homework 2: Due on Thu Feb 16

Q1. (4 Points each part) Consider the following function called crossProduct: int crossProduct(int[] A, int[] B){ int sum = 0; for (int i=0; i<A.length; i++) for (int j=0; j<B.length; j++) sum += A[i]*B[j]; return sum; } --1 --2 --3 --4 --5

(a) Let the complexity of this function be T (n, m) where the lengths of A and B are n and m, respectively. Determine the complexity T (n, m) of this function exactly, not just asymptotically. For this purpose, make reasonable assumptions about the cost of each step. (b) Next, state the -order of T (n, m) this means you write T (n, m) = (f (n, m)) where f (n, m) is as simple as possible. (c) Finally prove your statement in (b). (d) Write a class called CrossProduct that has the above method, and also a main method to test the function. Your main function should just test the following two arrays A= [1,2,3,4], B= [2,4,6]. But sure to initialize the arrays to these values in the most eective way (look up if you do not know). Your Makele must have a target called crossproduct (with short form cp) to compile AND run this class. 5

(e) Can you improve the complexity of the method by computing the result in a dierent way? Indicate how to do this and give a -order of the complexity if you can. Q2. (4 Points each part) The largest value of a Java int is 2, 147, 483, 647. This value is Integer.MAX_VALUE in the wrapper class Integer. (a) First, use our rule of thumb (210 is 1000) to show why this is a roughly what you expect. (b) Next, explain why it is exactly this value, and not 2, 147, 483, 646 or 2, 147, 483, 648 (c) Now, we want to determine the largest value of n such that Fn is less than M AX V ALU E. You are told that for n > 10, Fn is close to n where = (1 + 5)/2 1.618 is the golden ratio. You may use a pocket calculator this part, but DO NOT use a program that computes Fn . YOU MUST TELL US DETAILS ABOUT HOW YOU DETERMINE n. (d) Suppose we use the primitive type double. A Java double uses has a budget of 64 bits: 53 bits for mantissa, 1 bit for sign and 11 bits for exponent (that is a total of 65 not 64 bits the mysterious extra bit comes from the implicit leading bit in the mantissa). A double is represented by two parts: a 53 bit mantissa representing a number between 1 and 2, and an exponent e that can go to about 210 . The value of the double is the product of the mantissa and 2e . Because of the exponent, we can represent HUGE integers. But we want the largest integer M such that double can represent every integer between 0 and M . Using your rule of thumb, how many digits do you expect M to have? (e) (THIS PART IS NOT REQUIRED) Conrm your answer for (d) experimentally, and tell us your experiment. Q3. We have given you code for two classes MyArrayList and MyLinkedList, both implementing the List ADT. Suppose we want to introduce a new method to the List ADT and to modify an original method as follows: The new method public void append( List<T> alist ) will modies this list by appending the elements of alist to the end of itself, and alist becomes empty. E.g., if this list originally contains (1, 3, 5) and alist contains (a, b, c, d) then after calling append, this will contains (1, 3, 5, a, b, c, d). You need to implement this new method for MyLinkedList only, but rename your class to NewLinkedList. In MyArrayList, we want you to modify the behavior of remove() so that whenever the number of items in the list is less than half of the the length of the current array, we get a new array whose length is half the length of current array. Of course, it should not be less than the DEFAULT_CAPACITY Note that this choice half of the length of the current array is known to be a bad choice (why?), but it will serve our purpose. Rename your class to NewArrayList. You are to submit a Makele that has targets called app and rem to test the above two modications. (NOTE: submit only ONE Makele that combines your solution to Questions 1 and 3. We always need exactly ONE Makele per homework.)

Quiz: Feb 8
How many bits do you need to represent one billion? What is this is Java? Write a minimal class to illustrate the use of this. Your program should compile and run. Show that f (n) = 3n7 n3 is O(n7 ).

SOLUTION: One billion is 109 = (103 )3 < (210 )3 = 230 . So 30 bits will do. HARDER Question: Will 29 bits do? We know that 210 = 1024. So, 210 < 1.03(103 ). So 230 < (1.03)3 (109 ). If (1.03)3 > 2, then you may hope to save one bit. But (1.03)3 1.09. So you are still very much out of luck. 6

It seems that our computer memory is going up exponentially. Perhaps your childrens smartphone will 103k bytes of memory for some large k. This is < 210k bits. At what k will you be able to save one bit, and get 10k 1 instead of 10k bits? (My external disk of my rst Mac was a clunky box with 20 MB, and I was excited) this is a self-reference by an instance to itself, and is used within an instances methods and constructors. If an instance has a member x, then the methods of the class normally simply refer to this member by a plain x. However, it can also refer to this variable as this.x. Even though the use of this is redundant here, you might want to use this form if your super class also has a variable named x. Think of it as the pronoun I. I can refer to myself as I even though my name is Chee (but so can you, even though your name is not Chee). Most common usage is in constructors. Here is a simple class: class That { int x; That(int a){ x=a; } That(){ this(3); } }//That Show that f (n) = 3n7 n3 is O(n7 ). Note that f (n) 3n7 for all n 0. Thus f (n) Cn7 (ev.) if we let C = 3. But this is the denition of f (n) = O(n7 ) which we want to show.

Homework 1: Due on Wed Feb 1

Q1. (4 Points for each part) Recall the big-Oh and big-Theta notations (the books denition is in Section 2.1, which also has the big-Omega notation). Let f (n) = 2n 100. Show that f (n) = (n). In general, to show f (n) = (g(n)), you need to explicitly tell us the 3 implicit positive constants C, c, n0 in this notation. Show that f (n) = O(g(n)) and g(n) = O(h(n)) implies f (n) = O(h(n)). Note that showing f = O(g) only need 2 implicit constants, so it is a bit easier than showing f = (g). Show that it is NOT true that 2n = O(nk ), for any xed value of k. Note that a function f (n) is called polynomial if f (n) = O(nk ) for some constant k > 0. So this shows 2n is non-polynomial. Q2. (9 Points) In class, we described a Java class Fib that has a method f ib(n) to compute Fn , the nth Fibonacci number. Recall that F0 = 0, F1 = 1 and Fn+1 = Fn + Fn1 for n 1. This denition is slightly dierent than the books (p. 6), but the dierence is unimportant. Please download the le Fib.java from our pickup site to study this class. Let T (n) denote the number of steps used by f ib(n). You can interpret steps quite liberally, counting any constant time (i.e., O(1)) operation as one step. Observe (by studying the program f ib(n)) that T (n) 1 T (n 1) + T (n 2) if n 1 else. Let f (n) = n2 . Show that it is NOT true that f (n) = O(n).

(a) Show that T (n) 2n/2 . (b) Conclude that T (n) is non-polynomial. HINT: you may assume that T (n) T (m) whenever n m.

Q3. (30 Points) In class, we discussed two problems with the current implementation of f ib(n): (1) The use of int means that the output would be wrong for n > 60. The solution is to use the Java class Integer. (2) The fact that the recursive fib calls itself twice meant that this is an exponential time algorithm (see previous question). We can empirically see this by trying to compute F45 . We also sketched a version that only requires one recursive call (this one would be polynomial time). Please implement such an improved version that incorporates (1) and (2). The name of your Java class should be called Fibonacci, and it should have (at least) two methods: Integer fib(Integer n) that computes Fn , and a main method that reads an optional number n from the command line. If n is not given, then it defaults to 20. It should print the value f ib(n). Your main method should also accept a second optional number m, and this means you compute a consecutive sequence of m Fibonacci numbers, starting from f ib(n). The default value of m is 1. E.g., if we call java Fibonacci 4 2 then you should print F4 = 3 and F5 = 5. Download a Makefile from the pickup site, and put it in the same directory as Fibonacci.java. Modify it for your current assignment so that the following 12 behavior is achieved: > > > > > > > > > make make make make make compile c p=Fib c run r ------------------this this this this this same same same same should should should should should as as as as call "javac Fibonacci.java" be the same as "make compile" be the same as "make c" but using Fib.java call "java Fibonacci" call "make run"

make make p=Fib make arg=12 make arg=12 arg2=3

"make c; make r" "make" but using Fib.java "java Fibonacci 12" "java Fibonacci 12 3"

> make test > make test1 > make test2

--- this should call "java Fibonacci 20 10" --- this should call "java Fibonacci 30 10" --- this should call "java Fibonacci 80"

Note that we must use three variables in the Makele, p, arg, arg2. To do this, you should read up our tutorial on make, and in particular how to use variables. Also, our sample Makele already illustrates this. Here is one way to creat the TAR le to send us (either in Cygwin or in MacOS Windows or in Linux): > tar cvf hw1-Chee-Yap.tar README Q-1.pdf Q-2.pdf Makefile Fibonacci.java where > is just the command prompt (so do not type it!).

You might also like