DaSt232 Assignment 2
DaSt232 Assignment 2
Data Structures
202.1.1031
Assignment No. 2
1 Assignment structure
1.1. Hands-on programming and Theoretical questions
• Design an efficient data structures for solving the given problem (elaborated in this document) using
the data structure taught in this course.
• Implement and test your data structure in Java programming language.
• Provide a time complexity analysis of your implementation.
1.2. Bonus
• Utilize your suggested data structure to solve a problem with a different setup. Provide input data
and detailed explanations. (5 points)
• Improve the time complexity of some specific methods. (5 points)
• class Container
• class DataStructure
• interface DT
The Point class contains three fields: Name, X, and Y. Indicating the flight’s serial number, X-axis coordinate,
and Y -axis coordinate, respectively. This class should NOT be changed!
The Container class initially contains a field Point data. You can add fields and methods to this
class as you wish, but the initial Point data field and the corresponding getData() method must
remain.
1
The DataStructure class will contain your implementation of the non-collision system. The methods and
run-time requirements for each method of the system are presented in the next section. Note that this class
implements the DT interface. Therefore, you must implement all of the interface’s methods. You must not
change DT! - including the signature of the methods. Some methods are called with Container and some
Point, you cannot modify the signatures of those methods. However, you are encouraged to add your own
methods to the class implementation.
6. The median value for a given sorted array of size n, indexed 0, . . . , n − 1, is the element at index ⌊ n2 ⌋.
For example, for the median value according to the Y -axis (axis=false):
Note that the Y -axis coordinate is the second element in each tuple.
7. You are allowed to use the classes Math, Object, Comparable, Comparator. You are also allowed to use
the static method Arrays.sort.
* You can assume that the method nearestPairInStrip will only be called with a Container that was
returned from the getMedian method.
2
4.1 Methods Description
For all methods, n is the number of planes in the system.
4.1.1 DataStructure()
Constructor. Creates an empty data structure with no planes.
Time complexity: O(1).
3
4.1.6 void narrowRange(int min, int max, Boolean axis)
The method deletes from the data structure all the points that have an axis value in the range of [−∞, min)
or in the range of (max, ∞](note that these are exclusive ranges, i.e., not including min and max values)
Parameters:
1. min - lower bound of the range.
2. max - upper bound of the range.
3. axis - the desired axis, true for X-axis and false for Y -axis.
Time complexity: O(|A|) where |A| is the number of points to be deleted.
Figure 1: nearestPairInStrip example. the black point represents the container point, and the red rectangle
represents the width
Parameters:
1. container - containing a point from the range (you may assume nearestPairInStrip will be called only
with container returned from the method getMedian(Section 4.1.8) with the parameter axis. that is
getMedian(axis).
2. width - the width of the strip.
3. axis - the desired axis, true for X-axis and false for Y -axis.
Time complexity: O min{n, |B| log |B|} where |B| is the number of points in the strip.
4
4.1.10 Point[ ] nearestPair()
The method returns the two closest points in the data structure. You may assume there are at least two points
in the structure.
Time complexity: You should calculate the time complexity of this method, and explain your calculation
in the theoretical questions part.
Guidance: The method performs the calculation using the following steps:
i If there are only two points return them. If there are less return empty set.
ii Find the largest axis axis.
iii Find the median in axis
iv Calculate the nearest pairs for all the points larger than the median recursively, and similarly (and recur-
sively) for all the points smaller than the median.
v Choose the closer pair between the two pairs from Item iv and calculate the distance between them minDist.
vi Check if the strip centered in the median with width 2 · minDist contains two points with a distance smaller
than minDist.
(a) If there exists such pair, return it
(b) Else, return the pair from Item v
5 Hands-on programming
The assignment includes several source files with:
• Point class and DT interface which you must not change.
• Container and DataStructure classes which you need to implement.
• GUI class you will use if you choose to implement the bonus (see Section 7)
You are allowed (and encouraged) to change Container class and add files and classes as you need, but you
must not change any existing code (i.e., class name, data field and getData method).
You are supplied with an empty implementation of the class DataStructure. You are required to implement
DataStructure so that it supports all the required methods described in Section 4.1 in the required run time.
You are allowed to add methods, constructors, and fields as you need. Please notice our tests will call only the
constructor and methods described in this file.
5
6 Theoretical questions
6.1. Describe your implementation shortly in a typed document. Explain which data structures you used and
describe with words the algorithms you used for implementing the methods of DS interface.
6.2. Explain shortly the run time of each of the methods (for most methods, 1-2 lines will be sufficient).
6.3. Give a pseudo-code for the function split(int value, Boolean axis) and analyze the run time of your solution.
All points will be given for a solution running in O(|C|) time where |C| is the number of points in the
smaller group in the partition. A solution running in O(n) time will get only some of the points, and slower
solutions won’t get points at all.
6.4. Determine and explain the run time of the method nearestPair() (your answer should be as optimal as
possible).
6.5. Bonus (up to 5 points) – Explain how can one build from an existing DataStructure instance, two
DataStructure instances, one containing all the points with X value larger than the median (included), and
one containing all the points with X value smaller than the median. The build should take O(n) time.
The instances must of course support all DataStructure methods correctly.
Optional – Explain how can one use the above for improving nearestPair() run time.
<name>;<x-coordinate>;<y-coordinate>
for example:
Moshe; 30; 55
You are encouraged to submit your own sets you used to validate your implementation, which doesn’t have to
match the plane’s problem.
A 5 points bonus will be given for submitting 2 original, meaningful, and different sets with at least 10
objects each (please note in your PDF file you submitted sets for bonus).
6
Figure 2: An example for GUI with planes on radar and selected areas
7
9. Don’t forget to sign the statement in Section 0. Your code will be checked for plagiarism using automated
tools and manually. The course faculty, CS department, and the university regard plagiarism with all
seriousness, and severe actions will be taken against anyone that was found to have plagiarized. A
submitted assignment without a signed statement will receive a grade of 0.
10. You can always assume the input is valid. You will not:
• Be asked to add an existing point.
• Be asked to delete points from an empty structure.
• Receive from our tests null as a parameter to any of the methods of the interface.
11. Please follow the submission guidelines in the VPL environment strictly. For testing, we will use our own
Point, GUI classes.
Good Luck!