Compares the number of comparisons used by various data

Assignment Help Data Structure & Algorithms
Reference no: EM13346863

Compares the number of comparisons used by various data structures for a single algorithm. The algorithm is the one that is used to determine representation in the U.S. House of Representatives, and is described below.

The data structures are based on LinkedLists, ArrayLists, TreeSets, and PriorityQueues, as described below.

Specifically, the assignment's test class A1 assumes the existence of subclasses ArrayListApportioner, LinkedListApportioner, TreeSetApportioner, and PriorityQueueApportioner of an abstract class Apportioner. Define the last three of these subclasses according to the specifications below.

  • the Apportioner class
  • the ArrayListApportioner subclass,
  • a CountingComparator class described below,
  • the A1 test class, and
  • various files of test data

The Apportioner class and the apportionment algorithm

The apportionment problem takes as input a collection of states and their populations, as well as a total number of seats in the legislature. The algorithm that you are to use (and that the U.S. House uses) assigns each of these seats to one of the states by

1. first assigning each state one seat

2. then repeatedly assigning a seat to the state with the highest priority for it, until the seats are exhausted

Each state's priority is computed as the quotient of its population and a divisor computed from the number r of seats that it is currently assigned. For the algorithm that the U.S. House uses, and that you are to use, this divisor is the square root of r(r+1). Note that this value is approximately r + 0.5. Also note that every time a state gets a new representative, its divisor increases and thus its priority decreases.

The Apportioner class has a simple concrete apportion method whose code corresponds to the simple high-level algorithm given above. The class also provides a concrete method for computing the divisor in the priority function, and a concrete method for reading the name and population of a single state. Otherwise the apportion method is based on specific abstract methods that subclasses need to define concretely. One of these methods returns the apportionment as a list of elements of class Map.Entry<String, Integer>.

The Apportioner class also has two abstract methods for dealing with the number of comparisons of various types made during an apportionment. Its subclasses are to call these methods, as described below.

An apportionment example

In an example with a house size of 7 and three states Large, Medium, and Small, with respective populations 45, 32, and 18, the algorithm would iterate through steps corresponding to the rows of the table below.

Apportionment

Priority

Large

Medium

Small

Large

Medium

Small

1

1

1

32.8

21.6

12.7

2

1

1

18.4

21.6

12.7

2

2

1

18.4

13.1

12.7

3

2

1

13.0

13.1

12.7

3

3

1

-

-

-

The Apportioner subclasses

Each of your subclasses is to implement each of the abstract methods of the Apportioner class, based on a specified data type.

In addition, each subclass is to count the number of comparisons that it uses to peform apportionments, including those that it uses to initialize data structures. Here, comparisons include comparisons for equality -- do not use an equals method for these comparisons, or rely on a library method that does so. Each subclass is to count separately those comparisons used to update priorities, and those used for other purposes.

Specifically, the compare method for each of your CountingComparator subclasses is to increment the appropriate one of the two comparison counters. This method should return a negative number if the first argument has a larger priority than the second argument; this is particularly important for the priority queue data type. The resetComparisonCounters method of your Apportioner subclass is to set both of the class's counters to 0; the getComparisonCounters method is to return an array of size 2 that contains the two comparison counters, with the counter used for priorities appearing last.

The initializeMaps method of your Apportioner subclass is to assume that separate data structures are used to store each state's population, current number of seats, and current priority. It's convenient to call these data strucutres "maps", even though they will not implement Java's Map interface in each case. The method needn't initialize all maps in every subclass -- you may find it best to initialize some maps at definition, in a constructor, or in the readInput methods. If your subclasses have instance variables that aren't maps or comparison counters, you might want to initialize them in initializeMaps as well.

Note that, depending on the data type of the priority map and its entries, updating priorities in the assignRepAndUpdatePriority method may be possible by simply mutating an entry, or may need to be done by removing an entry and inserting an updated version of it into the map.

The getApportionment method is to return a list sorted by state. If an explicit sort is necessary, this sort is to be performed by a sorting algorithm that counts comparisons. This might be done by Collections.sort, or by using a TreeSet or TreeMap that has been constructed with a CountingComparator argument.

Each subclass may assume that the MiniScanner methods that appear in the superclass definition all work correctly.

For each subclass, the readInput method that takes a filename argument should check that the file of the given name is not empty. The readInput method that takes a list argument should check that the list is not empty. In each case, the method should throw an IOException with an informative message component. Neither method needs to handle any exceptions, or check for any other errors. In particular, you needn't check for repeated state names. The rest of the subclass's methods may assume that there is at least one state and that the population map is not empty.

The TreeSetApportioner class

In the TreeSetApportioner class, you are to represent the priority data as a TreeSet of Strings, constructed with a Comparator argument that compares strings by priority (where items of larger priority value precede items of smaller priority value). The population data and the apportionment data are to be represented as HashMaps from String to Long and Integer respectively. Note that here HashMaps are preferable to TreeMaps since the order of the state names is unimportant during apportionment.

The PriorityQueueApportioner class

In the PriorityQueueApportioner class, you are to represent priority data as a PriorityQueue of Strings that represent state names. Population data and apportionment data are to be represented as HashMaps as in theTreeSetApportioner case. You will need a Comparator class similar to that of the TreeSetApportioner class.

Reference no: EM13346863

Questions Cloud

Create a cost-benefit analysis to evaluate the projectthe : create a cost-benefit analysis to evaluate the projectthe state of massachusetts would like to replace a national guard
Q1 figure shows a scale drawing of the temperature profile : q1 figure shows a scale drawing of the temperature profile through a composite plane wall.i identify the layer with the
Experiment can be used to investigate heat transfer : experiment can be used to investigate heat transfer associated with flow past cylindrical tubes either in isolation or
1- a region is a geographical area which possesses certain : 1- a region is a geographical area which possesses certain characteristics e.g. political economic physical which give
Compares the number of comparisons used by various data : compares the number of comparisons used by various data structures for a single algorithm. the algorithm is the one
1harveys muffler offers a full refund to anyone who is not : 1.harveys muffler offers a full refund to anyone who is not satisfied with thenbsp replacement of mufflers. the owners
Based on the ppars projectapply the core is capabilities : based on the ppars projectapply the core is capabilities framework as described by feeny and willcocks 1998 to the
This sample webpages aim is to will serve most of the : this sample webpages aim is to will serve most of the audience including elder people and people with disabilities. it
1 in john stossels article in praise of price gouging : 1. in john stossels article in praise of price gouging stossel argues that a law banning price gouging would result in

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Using a backtracking algorithm

If the backtracking algorithm finds a dead end, it retraces its path until it reaches a position from which there is an untried path. The backtracking algorithm always tries all directions from any position, and always in the same order.

  Explain compression algorithms are often used in forensics

"Compression algorithms are often used in forensics. Suppose you are involved in a case and have been asked by the lawyer to explain, in general terms.

  Write a program to find average marks

Write a program to find average marks obtained by 10 students in a test along with algorithm and write a menu driven program using function to perform following operations on 1 d array?

  Explain an application level protocol

Create and explain an application level protocol to be used in an automatic teller machine and a bank's centralized computer. Your protocol should permit a user's card and password to be verified,

  Explain how to modify knuth-morris-pratt algorithm

Explain how to modify Knuth-Morris-Pratt algorithm to support patterns with these wild cards, and analyze modified algorithm. Your algorithm must find first substring in text which matches the pattern.

  Creating an effective physical design

Class, do IT database designers necessary to understand data volumes and number of users of database in order to create an effective physical design?

  Create unix shell scripts using dos commands

Suppose you are an experienced DOS programmer and you wish to create UNIX shell scripts using DOS commands.

  Calculate shortest path-djkstra-s shortest path algorithm

With indicated link costs, use Djkstra's shortest path algorithm to calculate shortest path from E to all network nodes. Illustrate how algorithm works by computing table.

  Creating visual studio asp .net web site

Make a Visual Studio 2008 ASP .NET Web Site with 2-Web Forms. Add a DropDownList server control and a Label server control to 1st Web Form.

  Singly linked list

Singly Linked List (SLL)Introduce a SLL class with the following functions. Please also introduce a main function that will invoke and verify whether the functions are implemented correctly

  Identifying flaws in the design

Identify flaws in design of the Report of Consumers that follows. What assumptions about users and tasks did you make in order to assess this design?

  Portfolio website containing thumbnail imagery

Explain in general terms what you think the role of good design is. Next, recognize 3-characteristics of an effective gallery website. Then find an example of a portfolio website containing thumbnail imagery.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd