Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the National Football League, when teams are seeded for the playoffs, there is always the possibility that two or more teams may have the

In the National Football League, when teams are seeded for the playoffs, there is always the possibility that two or more teams may have the same won-lost-tied record. There is an ordered, step-by-step, tiebreaker process by which such ties are broken. If the first step in the process fails to break the tie, the next step is applied, and so on, until the tie is broken. This assignment focuses on one particular step, which happens to be the third step in the process: the won-lost-tied percentages in common games between the two teams are compared. Two games are considered common between two teams if each of those teams faced the same opponent. This problem can be abstracted into a more general problem: Given two collections of elements, A and B, generate a third collection, C, containing only those elements common in collections A and B, with duplicates allowed. Note that any type of collection (list, array, etc.) can be used, and no guarantees are made regarding the order of the elements in either collection. If we build a collection for each team in question that contains the name or identifier of the opponent that was faced in each game the team played, we end up with two separate collections of opponents. The goal is to generate a third collection that contains only the common opponents faced by both teams. For example, consider two teams from the AFC North Division from the 2011 season, the Pittsburgh Steelers and the Baltimore Ravens. Both teams finished the regular season with won-lost-tied records of 12-4-0. As it turns out, the Ravens won the tiebreaker not on the rule were describing in this assignment, but on the rule that the Ravens won both head-to-head matchups during the season. But lets assume the tiebreaker had to be resolved by the won-lost-tied percentage in common games. Table 1 shows the entire regular season schedules for both teams, and indicates common opponents in bold. Although in this example, most of the games are common;i.e., both teams faced common opponents, this is not a requisite condition for the problem. The result returned by an algorithm solving this problem should return a third collection containing only those teams which both the Steelers and the Ravens faced during the season. The order in which these common teams are present in this collection is not important, and duplicates should be included. Table 2 shows one possible correct result.

What You Need to Do: If we were only interested in two relatively small collections, as shown in Table 3, the quadratic algorithm described above is adequate. However, if N is very large, and/or there are many collections (i.e., k is very large), and/or the complexity of the comparison operation is high (i.e., the data are more complex than integers), the quadratic algorithm may be too slow to be useful. There are a number of strategies that can be employed to reduce the complexity below quadratic, and at this point we have covered enough topics to design a more efficient algorithm. Based on the material covered thus far in this course, your goal is to design and implement 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). Your 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, in all 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.

It is important that you adhere to the framework specification above. To facilitate testing of your program, I will use a test harness that will do the following: 1. Creates an instance of your CommonElements class. 2. Calls your findCommonElements method with a set of test collections as input. 3. Verifies that the collection that is returned by findCommonElements correctly contains the elements common to all the input collections. 4. Retrieves the number of comparisons that were performed, via your getComparisons() method. 5. Compares the number of comparisons performed to the target value stated in criterion #5 above for the algorithm. Thus, it is essential that you name your class and methods as described above, or my test harness will not work, and it will take longer to test your program.

Here is my code:

public class CommonElements implements Comparable { private int comparisons = 0; private Comparable[][] mArray; CommonElements() { } CommonElements(Comparable[][] collections) { mArray = collections; } public Comparable[] findCommonElements(Comparable[][] collections) { try { insertionSort(collections); } catch (NullPointerException e) { System.out.println("Error: Empty Array!"); } Comparable[] lCmmnCllctn; if (collections.length > 0) { lCmmnCllctn = new Comparable[collections[0].length]; Comparable[] lQryCllctn = createQuery(collections); Comparable[][] lOthrCllctn = createOther(collections); int lFll = 0; for (Comparable query : lQryCllctn) { boolean lMtch = true; for (int li = 0; li < lOthrCllctn.length; li++) { for (int lj = 0; lj < lOthrCllctn[li].length; lj++) { comparisons++; if (query.compareTo(lOthrCllctn[li][lj]) == 0) { lMtch = true; break; } else lMtch = false; } } if (lMtch == true) { lCmmnCllctn[lFll] = query; lFll++; } } } else { lCmmnCllctn = createQuery(collections); } return lCmmnCllctn; } public int getComparisons() { return comparisons; } public Comparable[][] getArray() { return mArray; } public void lStArry(Comparable[][] collections) { mArray = collections; } private Comparable[] createQuery(Comparable[][] collections) { Comparable[] lQryCllctn = new Comparable[collections.length]; for (int li = 0; li < collections[0].length; li++) { lQryCllctn[li] = collections[0][li]; } return lQryCllctn; } private Comparable[][] createOther(Comparable[][] collections) { Comparable[][] lOthrCllctn = new Comparable[collections.length - 1][]; for (int li = 1; li < collections.length; li++) { for (int lj = 0; lj < collections[li].length; lj++) { lOthrCllctn[li][lj] = collections[li][lj]; } } return lOthrCllctn; } private Comparable[][] insertionSort(Comparable[][] collections) { for (int lj = 0; lj < collections.length; lj++) { for (int li = 1; li < collections[lj].length; li++) { Comparable firstUnsorted = collections[lj][li]; int index = li - 1; while (index >= 0 && firstUnsorted.compareTo(collections[lj][index]) < 0) { collections[lj][index + 1] = collections[lj][index]; index--; } collections[lj][index + 1] = firstUnsorted; } } return collections; } public void toString(Comparable[][] collections) { for (int li = 0; li < collections.length; li++) { for (int lj = 0; lj < collections[li].length; lj++) { System.out.println("Set " + li + ": " + collections[li][lj]); } } } public void toString(Comparable[] collections) { for (int li = 0; li < collections.length; li++) { System.out.println("Set " + li + ": " + collections[li]); } } //Override @Override //Define a method public int compareTo(T other) { if (this.compareTo(other) < 0 || this.compareTo(other) > 0) return Integer.signum(this.compareTo(other)); else return 0; } }

And the following is my main with the 2 arrays supplied to me:

public class Module4 { public static void main(String[] agrs){ Object [] collections = new Object[2]; collections[0] = new String[]{ "Baltimore Ravens", "Seattle Seahawks", "Indianapolis Colts", "Houston Texans", "Tennessee Titans", "Jacksonville Jaguars", "Arizona Cardinals", "New England Patriots", "Baltimore Ravens", "Cincinnati Bengals", "bye week", "Kansas City Chiefs", "Cincinnati Bengals", "Cleveland Browns", "San Francisco 49ers", "St. Louis Rams", "Cleveland Browns" }; collections[1] = new String[]{ "Pittsburgh Steelers", "Tennessee Titans", "St. Louis Rams", "New York Jets", "bye week", "Houston Texans", "Jacksonville Jaguars", "Arizona Cardinals", "Pittsburgh Steelers", "Seattle Seahawks", "Cincinnati Bengals", "San Francisco 49ers", "Cleveland Browns", "Indianapolis Colts", "San Diego Chargers", "Cleveland Browns", "Cincinnati Bengals"}; } } public Comparable[] findCommonElements(Object[] collection)

What do i need to do to this code to get it to compile and display the column of commonalities between the arrays?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

a. Do team members trust each other?

Answered: 1 week ago