I need help with my homework.
The task:
Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which is why ICEs are also eliminated), they let themselves be asked to take a plane on vacation. So you are now looking at the network spanned by airports and existing direct connections and wondering which airports are actually critical. A critical airport is an airport whose sudden disappearance would make it impossible to reach all other (remaining) airports from any (remaining) airport. For example, in the following example, the airports ICT and TXL are critical airports, but not BER:
So you decide to write a program that will calculate the amount of critical airports for you. You can assume the following assumptions:
At the beginning, all other airports can be reached from any
airport.
If a connection from airport
F1 to F2 airport will also consist of a link from F2 airport back
to F1.
In the ads.set3.airports package, write a CriticalAirportFinder
class and implement the following method:
/**
* Finds airports whose removal causes unreachable destinations. Thus, the
* result must be a set of airports that, if one is removed, cause at least
* one pair of other airports to have no path between them.
*
* @param airports set of airports.
* @return set of critical airports. Can be empty, but must not be {@code null}.
*/
public static Set<Airport> findCriticalAirports(Set<Airport> airports) {
// TODO Implement me!
}
The given Airport class:
package ads.set3.airports;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Models an airport. For them to be identifiable, all airports have
an
* <a
href="https://en.wikipedia.org/wiki/IATA_airport_code">IATA
code</a> and
* two airports are considered equal if their IATA codes match.
Since this class
* implements {@link #equals(Object)} and {@link #hashCode()}, it
can be used in
* hash-based data structures, such as {@link HashSet}.
*
* <p>
* Each airport has a (possibly empty) set of designations that can
be reached
* directly (that is, there are direct flights from this airport to
all
* designation airports). If there is a direct flight from this
airport to
* another airport, there will be a direct return flight as
well.
* </p>
*/
public final class Airport implements Comparable<Airport>
{
/** The airport's IATA designation. */
private final String iataDesignation;
/** Set of airports that can be reached from this
airport. */
private final Set<Airport> destinations = new
HashSet<>();
/**
* Creates a new airport with no connected
airports.
*
* @param iataCode the airport's non-{@code null} IATA
designation.
* @throw IllegalArgumentException if the code is
invalid.
*/
public Airport(String iataCode) {
ensureValidIataDesignation(iataCode);
this.iataDesignation =
iataCode;
}
/**
* Throws an {@link IllegalArgumentException} if the
given String is not a valid
* IATA designation.
*/
public static void ensureValidIataDesignation(String
iata) {
if (iata == null) {
throw new
IllegalArgumentException("IATA code cannot be null.");
}
if
(!Pattern.matches("[a-zA-Z]{3}", iata)) {
throw new
IllegalArgumentException("IATA codes must consist of three
characters.");
}
}
/**
* Returns this airport's IATA designation.
*/
public String getIataDesignation() {
return iataDesignation;
}
/**
* Returns the airport's set of destinations that can
be reached directly, to be
* modified at will.
*/
public Set<Airport> getDestinations() {
return destinations;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Airport) {
return
((Airport)
obj).getIataDesignation().equals(this.getIataDesignation());
} else {
return
false;
}
}
@Override
public int hashCode() {
return
getIataDesignation().hashCode();
}
@Override
public int compareTo(Airport o) {
return
iataDesignation.compareTo(o.iataDesignation);
}
}
My current solution (but it's far away from passing the automatic test):
package ads.set3.airports;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/**
* Program to find critical airports.
* @author Tobias.
*
*/
public class CriticalAirportFinder {
private static HashMap<Airport, Integer>
t_in;
private static HashMap<Airport, Integer>
t_out;
private static HashMap<Airport, Integer>
t_low;
private static HashSet<Airport>
visitedAirports;
private static HashSet<Airport>
criticalAirports;
private static HashSet<Airport> leaf;
private static int timer;
/**
* Finds airports whose removal causes unreachable
destinations. Thus, the
* result must be a set of airports that, if one is
removed, cause at least one
* pair of other airports to have no path between
them.
*
* @param airports
* set of airports.
* @return set of critical airports. Can be empty, but
must not be {@code null}.
*/
public static Set<Airport>
findCriticalAirports(Set<Airport> airports) {
visitedAirports = new
HashSet<Airport>();
criticalAirports = new
HashSet<Airport>();
leaf = new
HashSet<Airport>();
t_in = new HashMap<Airport,
Integer>();
t_out = new HashMap<Airport,
Integer>();
timer = 0;
for (Airport airport : airports)
{
if
(!visitedAirports.contains(airport))
DFS(airport, airport);
}
Set<Airport>
criticalAirports = new HashSet<Airport>();
for (Airport airport :
airports)
if
(criticalAirports.contains(airport) &&
!leaf.contains(airport))
criticalAirports.add(airport);
return criticalAirports;
}
/**
* DFS algorithm.
*
* @param v
* @param p
*/
private static void DFS(Airport v, Airport p) {
visitedAirports.add(v);
timer++;
t_in.put(v, timer);
t_out.put(v, timer);
if (v.getDestinations().size()
== 1)
leaf.add(v);
for (Airport to :
v.getDestinations()) {
if (to ==
p)
continue;
if
(visitedAirports.contains(to))
t_low.put(v, Math.min(t_low.get(v),
t_in.get(to)));
else {
DFS(to, v, airports); // here we have 3
parameters which doesn't work
t_low.put(v, Math.min(t_low.get(v),
t_low.get(to)));
if (t_low.get(to) > t_in.get(v)) {
criticalAirports.add(to);
criticalAirports.add(v);
}
}
}
}
}
Any help is much appreciated!
Code (using cut vertex finding algorihtm):
/ **
* Finds whose removal causes unrealistic destinations. Thus, the
* result must be a set of airports that, if removed, cause at least one pair of
* other airports to have no path between them.
*
* @param airports set of airports.
* @return set of critical airports. Can it be empty?
* airports without a path between them in the input. Must not be
* {@code null}.
* /
public static Set<Airport> findCriticalAirports (Set<Airport> airports) {
Set<Airport> used = new Set<Airport> ();
Set<Airport> list = new Set<Airport> ();
HashMap<Airport, Integer> in = new Map<Airport, Integer> ();
HashMap<Airport, Integer> min = new Map<Airport, Integer> ();
for(Airport x : airports) {
if(!used.contains(x))
DFS(x, x, 0, list, used, min, in);
}
return list;
}
private static void DFS(Airport f, Airport p, Integer cnt,
Set<Airport> list, Set<Airport> used,
HashMap<Airport, Integer> min, HashMap<Airport,
Integer> in) {
cnt++;
in.put(f,
cnt);
min.put(f,
cnt);
used.add(f);
int num =
0;
for(Airport to :
f.getDestinations()){
if(!to.equals(p)){
if(!used.contains(to)){
num++;
if(f.equals(p) && num > 1)
list.add(f);
DFS(to,
f);
min.put(f,
Math.min(min.get(to), min.get(f)));
if(!f.equals(p) && min.get(to) >= in.get(f))
list.add(f);
}
else
min.put(f,
Math.min(in.get(to), min.get(f)));
}
}
}
Comment down for any queries
Please give a thumb up
I need help with my homework. The task: Actually, you find flying very good, but you...
I need Help PLZ ( Java Code ).
Tomorrow 05.06.2019 I have to hand in my homework.
reachability
Actually, you find flying very good, but you do not
trust the whole new-fangled flying stuff and the infrastructure it
has built up. As a diehard medieval metal fan you prefer to travel
from A to B but rather the good old catapult. Since one can not
easily take the favorite cat on vacation with it (cats do not get
drafts, which...
I need Help PLZ ( Java Code ).
Today 14.06.2019 I have to hand in my homework.
reachability
Actually, you find flying very good, but you do not trust the
whole new-fangled flying stuff and the infrastructure it has built
up. As a diehard medieval metal fan you prefer to travel from A to
B but rather the good old catapult. Since one can not easily take
the favorite cat on vacation with it (cats do not get drafts, which...
Would someone help please, with code to finish. Iterator and entrySet, I need help. please ``` package coll.MapSet; import java.util.*; public class MapSet extends AbstractMap> implements Iterable { private Map> mapset = new HashMap<>(); /* Implement addValue such that calling this method adds the given value to the HashSet associated with the given key. This method must have the following signature: addValue(K key, V value) */ public void addValue(K key, V value) { // If the set has the key,...
(Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable { /** * Stores the number of objects instantiated from the {@code Triangle} class */ private static int numTriangles; /** * The shortest side of the triangle */ private final double a; /** * The side of medium length of the triangle */ private final double b;...
This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...
ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not? For example, "the" and "The" should be counted as...
I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...
Need some help on homework practice problems, Thank you in advance! Problem 1: Write a method, parallelSum(), that uses the fork-join framework to compute the sum of the elements of an array. Its skeleton as well as its Javadoc is provided in the file ParallelSum.java. Simple code for testing and timing your method is also provided in the main method. This problem is essentially the same as ParallelMax in structure. Therefore, follow the code for ParallelMax to complete ParallelSum. Problem...