Question
Here is the original question: I need to make a more efficient algorithm for finding the common elements of a set of collections. Ideally, the
Here is the original question:
I need to make a more efficient algorithm for finding the common elements of a set of collections. Ideally, the goal is to achieve an algorithm that will only need to perform at most on the order of (k - 1)N comparisons. This can only be achieved if each element in the non-query collections only participates in at most 1 comparison (with a few exceptions).
The algorithm should satisfy the following criteria:
1. It should be able to accept as input 0 to k collections, stored as simple arrays. Were restricting the data structure to arrays since we havent covered higher order data structures yet.
2. The elements of the collections should all be of type Comparable, and they should all be derived from the same base class (not counting the Object class). Implementation of the Comparable interface is necessary since the elements must be compared to each other in order to determine commonality. They must all be derived from the same base class since comparisons between different data types is undefined.
3. Duplicate elements should be allowed; e.g., if there are M instances of the value, XYZ, inall the input collections, there should be M instances of the value, XYZ, in the collection of common elements.
4. The collections should be allowed to be of varying lengths; i.e., some collections may have more items than others.
5. One of the collections must be designated as the query collection, which is the collection containing the elements to which the elements in the other collections are compared.
6. The total number of element comparisons performed should be less than the value for the quadratic solution described above. That is, the total number of comparisons in the worst case should be less than (k - 1)N2. Do not be concerned about average performance or best case performance. Also, the total number of comparisons is defined, for this assignment, to be only those comparisons that are performed once the traversal of the query collection begins, and the other collections are checked for the presence of the elements in the query collection. Any comparisons performed to manipulate the data prior to searching for the common elements should be ignored.
The framework for your algorithm should satisfy the following criteria, for ease in testing:
1. Create a class called CommonElements, to contain your algorithm and associated methods and attributes.
2. In your CommonElements class, encapsulate your algorithm within a method called findCommonElements, that has the following signature:
public Comparable[] findCommonElements(Comparable[][] collections).
The argument to this method, collections, will be the set of k collections discussed earlier. Each collection will be represented as an array of objects of type Comparable. Note that in Java, a 2D array will support arrays of varying sizes provided it is initialized without first specifying the two dimensions. For example:
Comparable[][] collections = {{A}, {A, B}, {A, B, C}}; results in an array of 3 Comparable arrays of varying sizes. The following syntax also works:
Comparable[] col_1 = {A}; Comparable[] col_2 = {A, B}; Comparable[] col_3 = {A, B, C}; Comparable[][] collections = {col_1, col_2, col_3};
3. The value returned by your findCommonElements method should be a collection of Comparable elements that contains only the elements common to all the input collections.
4. Since you are being asked to evaluate your algorithm based on the number of comparisons performed, you will need to have your findCommonElements method maintain a running total of comparisons performed for each set of collections tested. You should create an attribute called comparisons in your CommonElements class to store the number of comparisons, and provide a getter method called getComparisons() to return this value. In order to keep a running total of comparisons, you will need to instrument your code by incrementing the comparisons attribute each time a comparison between two elements is made. Since element comparisons are typically performed in if statements, you may need to increment comparisons immediately before each comparison is actually performed. Although that may sound counter-intuitive, if you try to increment comparisons inside the if statement, after the element comparison has been made, you will miss all the comparisons that cause the condition inside the if statement to evaluate to false.
How can I get this algorithm to have a time complexity of (k - 1)N?
public static Comparable[] findCommonElements(Comparable[][] collections){ int n = collections[0].length; Comparable[] ret = new Comparable[n]; int j = 0;
// Sort all the Collection Array; for (int i = 0; i < collections.length; i++) collections[i] = Arrays.sort(collections[i]);
for (int i = 0; i < n; i++){ int count = 1; for (int k = 1; k < collections.length; k++){ // Do binary Search if (bin_seach(collections[0][i], collections[k]) == true) count += 1; }
// if the element exist in all the collections, show this in output if (count == collections.length) ret[j] = collections[0][i]; } return ret; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started