Algorithms辅导、辅导Java语言编程

- 首页 >> Database
Data Structures and Algorithms: Assignment 2
Due: 7 June 2022, 11:59pm – 100 marks possible (20% of final grade)
When submitting, zip up your entire project into a folder with the following structure:
lastname_firstname_studentID.zip (using your own name and ID) and submit to the Assignment 2
folder on Canvas BEFORE the due date and time. Late submissions will incur a penalty. Anti-plagiarism
software will be used on all assignment submissions, so make sure you submit YOUR OWN work and
DO NOT SHARE your answer with others. Any breaches will be reported and will result in an automatic
failure and possible removal from the course.
Q1. File Sorting) Recommended readings 1.3, 3.2, 3.5) 25 marks)
There is often a big difference between an algorithm’s performance in internal memory and its
performance in external storage. For example, quick sort is considered very efficient for elements held
in memory but is less efficient with elements stored externally in a file, whereas merge sort can be
used more efficiently with sorting elements held externally in files.
Suppose the lines of a large text file are to be sorted, but the file is too large to all fit in memory at
one time. Prepare a class called FileSorter whose constructor accepts an integer limit (a maximum for
the number of Strings that will be held in memory at a time), and input and output text File objects.
The algorithm firstly performs the split stage, repeatedly reading each string from the input file line
by line, breaking the text file apart with at most “limit” strings (where a String is on its own single line
in the file). Once the limit has been reached, the algorithm should then quicksort the elements in
memory then output them back to a smaller temporary file (you may use an existing Java class for the
quicksort routine). The split stage continues until all lines of the input file are read in. After the split
stage, the algorithm then performs the merge stage. The merge stage uses your own merge sort
algorithm to read files together two at a time (deleting the temporary files), until a single sorted file
remains, the output file.
The FileSorter should be able to run as a separate thread, so any GUI program remains responsive to
the user. Add any necessary methods which can be used to notify the user of the progress of the
sorting algorithm.
Create a class called FileSorterGUI which can be used to run FileSorting tasks, it should have a button
to queue up a new task and another button to execute the task at the front of the queue. It should
also display progress bars to update the user on the progress of the currently running task. Feel free
to use the countries.txt and cities.txt files from Canvas to test your sorting application. An example
GUI is shown below:
_01000000 _01000000
_01000000 _01000000
Q2. Trie Data Structures) Recommended readings 4.1, 4.3, 5.2) 25 marks)
A Trie (or prefix-tree) is a tree data structure which usually holds string elements representing words.
Each node in the tree has links to children represented with individual characters that comprise the
string element. Each node can also have a string word (or null if no such word) which is made up of
the path of key characters from the root. To find an element in the tree simply look at the first
character of the target and traverse the child node with the same key character starting from the root
– repeating moving down the tree for each character in the target word. For efficiency child nodes
should be referenced from the parent using a map – using a character as the key, and the object node
as its value.
For example, using the tree below, if we were to see if the trie contains the word “then”, it involves a
traversal down the subtree, starting from the root and navigating the nodes which correspond to the
key characters ‘t’, ‘h’, ‘e’, ‘n’ - and checking whether the final node represented by the letter ‘n’ holds
the string element “then”.
_01000000 20089790
To add a word to the tree simply traverse the node that represents each character in the word. If a
node does not exist for that character, simply attach it as a new child node to the parent with a new
associated key character, the last node down the tree (associated with the last letter in the word)
should hold the newly added element. The trie should not deal with duplicate words.
_01000000 20089790
For example, when “hi” is added to the tree – because the ‘h’ and ‘I’ nodes already exist they are
traversed, and the node element associated with the key ‘i’ is set to “hi”. However, when “there” is
added to the tree, the tree is traversed down existing path ‘t’, ‘h’, ‘e’, but then new nodes with keys
‘r’ and ‘e’ need to be added respectively and attached to their parent and the last node associated
with key ‘e’ having its element set to the word “there”. Finally, if “seth” is added, straight away from
the root node there is no character key for ‘s’ – so the trie needs to build the path with ‘s’, ‘e’, ‘t’, ‘h’
before accommodating the word. The trie now looks like:
Trie data structures are commonly utilized in auto-complete algorithms. For example, if the user
enters the prefix “th” – this input would be used to obtain a list of suggestions back using tree
traversals and obtaining all word elements down the “th” subtree – “the”, “then”, “there”. Similarly,
if the user enters the prefix “hi” a list of suggestions would be “hi”, “him”, “hit”.
Removing elements can be a little trickier as the trie should clean up redundant nodes which are no
longer used for word paths to save memory and keep efficiency for word traversals. Again, the trie
should follow the path down the tree for the target element following the nodes associated with
character keys. The last node on the path should have its element set to null effectively removing the
target word from the tree. For example, as shown below, the removal of the words “he” and “the”,
the target elements are just removed from the trie after following their respective character keys, but
since the nodes are still used for further words down the tree path, they stay in place but now no
longer have a word associated with them. However, when “a” is removed from the tree it does not
have any child nodes so the key-value (character key ‘a’, with value node holding the word “a”) pair is
removed from its parent (the root). Similarly, when “then” is removed from the tree, the node with
key ‘n’ has no children (meaning it is no longer on the path to further nodes down the tree) it is
removed from its parent. This rule could recursively propagate back up the tree removing nodes that
are no longer used for words in the trie. The updated tree is shown below.
Finally, to illustrate the consecutive removal of nodes back up the tree, if we were to remove “there”
from the trie, because several characters on the path ‘h’, ‘e’, ‘r’, ‘e’ are no longer used for further
words down the tree, the nodes and character keys are repeatedly removed from their parent up until
the node associated with ‘t’ as shown below.
Using the UML, create a class called Trie which implements a trie data structure specifically for Strings.
It should maintain an innerclass called TrieNode which possibly holds a string word and a reference to
children using a map data structure where each child node is accessed using a Character key. Add any
other suitable private helper routines if required. Create a suitable toString and a simple main method
to test your implementation.
Trie
- root: TrieNode
+ Trie()
+ add(element : String) : boolean
+ remove(element : String) : boolean
+ contains(element : String) : boolean
+ removeAll(prefix : String) : boolean
+ startsWith(prefix : String) : boolean
+ suggestions(prefix : String) : Set
+ toString() : String
_01000000 20089790
Q3. HashSetWithChaining) Recommended readings 2.1, 5.1) 20 marks)
A set is a collection of elements with no repetitions and does not necessarily care about ordering of
its internal elements. A set can be described with the Set interface available in the Java Collections
API and the following UML. Create a class called HashSetWithChaining which implements an efficient
set using an underlying hash table, resolving any possible collisions by chaining. Feel free to look at
the Set Java documentation to get a full understanding of how these methods should operate. Make
sure you implement this class from scratch, creating your own hash table and Node class. Do not
encapsulate an existing Java data structure.
20089790
_01000000
_01000000
Your set should maintain a default load factor of 75%, otherwise it will have to expand capacity. A
custom load factor and initial capacity of the hash table can also be given as optional parameters
during construction. Create a test class with a suitable main method which “effectively” tests most of
the operations of your HashSetWithChaining class. For testing modify the toString method so that it
prints out the entire contents of the hash table showing chained elements, where necessary. See
example screenshot below of a simple test.
Q4. Journey Planner) Recommended readings 5.2, 6.1, 6.2) 30 marks)
Obtain the completed class called BusTrip, available from Canvas, which represents a single trip for a
bus travelling between two locations with associated departure and arrival times. A BusTrip has a cost
(given by static stage constants) and a unique bus route identifier. A bus leaves from a departLocation
on the departTime and arriving at the arrivalLocation on the arrivalTime. You can assume a BusTrip is
express, and only end to end with no location stops in between. All bus attribute information is passed
into its constructor. The class has getter methods for each attribute and a toString representation for
the object. A BusTrip is considered equal to another if the departure time, departure location, and
route identifier are the same.
_01000000 20089790
Part A: Using the following UML diagram below create a class called BusJourney that represents a
journey comprised of one or more BusTrip objects between multiple locations. The class encapsulates
a List data structure of BusTrip objects, keeping the dated order of the necessary trips which comprise
a complete journey. There are two constructors, firstly a default constructor creating a journey with
no initial trips, secondly a constructor that adds an existing list of BusTrip objects to the underlying
data structure.
BusJourney
- busList : List
+ BusJourney()
+ BusJourney(list : List)
+ addBus(bus : BusTrip) : boolean
+ removeLastTrip() : boolean
+ containsLocation(location : String) : boolean
+ getOrigin() : String
+ getDestination() : String
+ getOriginTime() : LocalTime
+ getDestinationTime() : LocalTime
+ cloneJourney() : BusJourney
+ getNumberOfBusTrips() : int
+ getTotalCost() : double
+ toString() : String
The addBus method should add a BusTrip to the journey, returning true and only adding the trip to
the current journey if it meets the following criteria:
• The journey’s end location is equal to the newly added BusTrip departure location (so the new
bus trip departs from the same location as journey’s current destination).
• The journey’s current destination time is earlier in the day (or equal) to the newly added
BusTrip parameter’s departure time (so the new bus trip cannot be added if it departs earlier
in the day to the journey’s current destination time).
• The journey so far does not already contain the parameters arrival location somewhere in its
busList (meaning the same location is never visited twice – no closed paths).
The containsLocation method should return true if the given location is in the journey so far. The
getter methods return the origin and destination location and dates (or null) of the Journey (so origin
location and time are the first BusTrip’s departure values, whereas the destination location and time
are the last BusTrip’s arrival values). The removeLastTrip method removes the lastly added BusTrip
from the current journey (if any). The getTotalCost returns the total cost of all the bus trips in this
journey. The getNumberOfTrips returns the number of bus trips which comprise this journey. The
toString method should print out a string representation of all trips in this journey and the total cost
in a nicely formatted way. Finally, the cloneJourney method returns a new BusJourney object,
passing its busList to the new instance by calling the second constructor.
_01000000 20089790
Part B: Using the following UML, create a class called JourneyPlanner. The class holds an efficient
Map data structure of unique locations as keys and a set of BusTrip instances which depart that
location as values. The add method is used to add a unique BusTrip to this map (care taken when the
departure location already exists in the locationMap).
The getPossibleJourneys method is used to return list of all the uniquely possible routes from the
start location and time, arriving at the end location before the end time. It does this by calling the
recursive method findPaths which uses a depth first search technique to build up all the possible
journeys that can be undertaken between the start and end location between the specified time
ranges (some journeys might not exist). It does this by building up a currentJourney parameter and
when the target location is found it will “clone” the current journey and add it to the List of
BusJourney values found for this search. Hint: Use BusJourney like a stack while recursively finding
paths.
JourneyPlanner
- locationMap : Map>
+ JourneyPlannner()
+ add(bus: BusTrip) : boolean
+ getPossibleJourneys(startLocation:String,startTime:LocalTime,
endLocation:String, endTime:LocalTime): List
-findPaths(currentLocation:String, currentTime:LocalTime,
endLocation:String, endTime:LocalTime
currentJourney:CruiseJourney, journeyList:List):void
Once developed, a suitable test class called JourneyPlannerTest (also available on Canvas) can be used
to find all BusJourney instances between any two locations within the specified time range. The test
class adds numerous BusTrip objects to the JourneyPlanner, essentially building a graph. The graph
below provides a visual representation showing the different bus trips travelling between locations.
The buses all have differing schedules starting from 6am to 11pm at night. Some routes have a more
frequent schedule (departing every 15 mins) whereas others less frequent (departing every two
hours). Some even have different bus route identifiers between the same two locations. Use this test
class to test different scenarios of travel. If you are having problems and for simple debugging, try
using only a small subset of these trips and build your own smaller graph before attempting this one.
However, make your own static buildGraph method, do not modify the existing one.
Once completed modify JourneyPlannerTest, so that once all the possible bus journeys have been
found, use the Collections sort with customized ordering so that the retrieved results are printed in
the order of destination time, so that journeys that arrive at the destination location earliest take
precedence.
Example output from graph: Leaving New Lynn at 8.30am and arrive at Ponsonby before 10am:
There are 7 possible journeys for your search from New Lynn (8:30) to Ponsonby (10:00)
Journey Option 1: TOTAL COST: $11.0!!!
> BUS: 11N LEAVING New Lynn (08:30) and ARRIVING Auckland CBD (09:00) $7.5
> BUS: 00P LEAVING Auckland CBD (09:00) and ARRIVING Ponsonby (09:15) $3.5
Journey Option 2: TOTAL COST: $13.0!!!
> BUS: 11N LEAVING New Lynn (08:30) and ARRIVING Auckland CBD (09:00) $7.5
> BUS: 02P LEAVING Auckland CBD (09:15) and ARRIVING Ponsonby (09:30) $5.5
Journey Option 3: TOTAL COST: $11.0!!!
> BUS: 11N LEAVING New Lynn (08:30) and ARRIVING Auckland CBD (09:00) $7.
> BUS: 00P LEAVING Auckland CBD (09:30) and ARRIVING Ponsonby (09:45) $3.5
Journey Option 4: TOTAL COST: $11.0!!!
> BUS: 10N LEAVING New Lynn (09:00) and ARRIVING Auckland CBD (09:30) $7.5
> BUS: 00P LEAVING Auckland CBD (09:30) and ARRIVING Ponsonby (09:45) $3.5
Journey Option 5: TOTAL COST: $13.0!!!
> BUS: 11N LEAVING New Lynn (08:30) and ARRIVING Auckland CBD (09:00) $7.5
> BUS: 02P LEAVING Auckland CBD (09:45) and ARRIVING Ponsonby (10:00) $5.5
Journey Option 6: TOTAL COST: $13.0!!!
> BUS: 10N LEAVING New Lynn (09:00) and ARRIVING Auckland CBD (09:30) $7.5
> BUS: 02P LEAVING Auckland CBD (09:45) and ARRIVING Ponsonby (10:00) $5.5
Journey Option 7: TOTAL COST: $13.0!!!
> BUS: 11N LEAVING New Lynn (09:15) and ARRIVING Auckland CBD (09:45) $7.5
> BUS: 02P LEAVING Auckland CBD (09:45) and ARRIVING Ponsonby (10:00) $5.5
Example: Leaving Papakura at 10.20am and arriving at Gulf Harbour before 3pm:
There are 0 possible journeys for your search from Papakura (10.20) to Gulf Harbour (15:00)
Example: Leaving Papakura at 10.00am (20 mins earlier) and arriving at Gulf Harbour before 3pm:
There is 1 possible journey for your search from Papakura (10.00) to Gulf Harbour (15:00)
Journey Option 1: TOTAL COST: $37.5!!!
> BUS: 88P LEAVING Papakura (10:15) and ARRIVING Manukau (10:45) $7.5
> BUS: 92M LEAVING Manukau (11:10) and ARRIVING Auckland CBD (11:55) $10.5
> BUS: 44B LEAVING Auckland CBD (12:00) and ARRIVING Takapuna (12:30) $7.5
> BUS: 99G LEAVING Takapuna (13:00) and ARRIVING Gulf Harbour (14:30) $12.0
Example: Leaving Auckland CBD at 1pm and arriving at Manukau before 3pm:
There are 5 possible journeys for your search from Auckland CBD (13.00) to Manukau (15:00)
Journey Option 1: TOTAL COST: $10.5!!!
> BUS: 92M LEAVING Auckland CBD (13:10) and ARRIVING Manukau (13:55) $10.5
Journey Option 2: TOTAL COST: $21.0!!!
> BUS: 11N LEAVING Auckland CBD (13:30) and ARRIVING New Lynn (14:00) $9.0
> BUS: 82M LEAVING New Lynn (14:00) and ARRIVING Manukau (14:50) $12.0
Journey Option 3: TOTAL COST: $21.0!!!
> BUS: 10N LEAVING Auckland CBD (13:15) and ARRIVING New Lynn (13:45) $9.0
> BUS: 82M LEAVING New Lynn (14:00) and ARRIVING Manukau (14:50) $12.0
Journey Option 4: TOTAL COST: $23.0!!!
> BUS: 22N LEAVING Auckland CBD (13:00) and ARRIVING Mt Albert (13:30) $5.5
> BUS: 23M LEAVING Mt Albert (13:30) and ARRIVING New Lynn (13:55) $5.5
> BUS: 82M LEAVING New Lynn (14:00) and ARRIVING Manukau (14:50) $12.0
Journey Option 5: TOTAL COST: $10.5!!!
> BUS: 92M LEAVING Auckland CBD (14:10) and ARRIVING Manukau (14:55) $10.5
Example: Leaving Auckland CBD at 8pm and arriving at Kingsland before 9pm:
There are 4 possible journeys for your search from Auckland CBD (20:00) to Kingsland (21:00)
Journey Option 1: TOTAL COST: $3.5!!!
> BUS: 21K LEAVING Auckland CBD (20:00) and ARRIVING Kingsland (20:20) $3.5
Journey Option 2: TOTAL COST: $7.0!!!
> BUS: 00P LEAVING Auckland CBD (20:00) and ARRIVING Ponsonby (20:15) $3.5
> BUS: 10P LEAVING Ponsonby (20:15) and ARRIVING Kingsland (20:30) $3.5
Journey Option 3: TOTAL COST: $3.5!!!
> BUS: 22K LEAVING Auckland CBD (20:15) and ARRIVING Kingsland (20:35) $3.5
Journey Option 4: TOTAL COST: $3.5!!!
> BUS: 21K LEAVING Auckland CBD (20:30) and ARRIVING Kingsland (20:50) $3.5
Example: Leaving Manukau 6am and arriving Ponsonby before 11pm (large time range):
There are 16804 possible journeys for your search from Manukau (6:00) to Ponsonby (23:00)!
Note: The possible journeys for all searches above are unique, although some may have the same
bus route number, the departure and arrival times are different. Also note that each consecutive
bus trip in a journey departs from the same location as the previous trips arrival location and cannot
depart earlier in time than the previous trips arrival time (departures and arrivals with the same
time are accepted). All times specified in 24-hour format.

站长地图