Question

I need help with my homework. The task: Actually, you find flying very good, but you...

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
I need help with my homework. The task: Actually, you find flying very good, but you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need Help PLZ ( Java Code ). Tomorrow 05.06.2019 I have to hand in my homework. reachability Actually, you find f...

    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...

    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...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    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...

    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...

    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 this. I need to create the hangman game using char arrays. I've...

    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();...

  • Can someone help me with this code, I not really understanding how to do this? import...

    Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...

  • I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHea

    I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHeap.Java package structures; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; public abstract class AbstractArrayHeap<P, V> {   protected final ArrayList<Entry<P, V>> heap;   protected final Comparator<P> comparator; protected AbstractArrayHeap(Comparator<P> comparator) {     if (comparator == null) {       throw new NullPointerException();     }     this.comparator = comparator;     heap = new ArrayList<Entry<P, V>>();   } public final AbstractArrayHeap<P, V> add(P priority, V value) {     if (priority == null || value...

  • Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, ...

    Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...

  • I have most of the code for this assignment but need some help getting the rest....

    I have most of the code for this assignment but need some help getting the rest. Go to bottom to see what I have. Please help me figure out the foreach loops and the put() statement. In this assignment, you will be using a new data structure, HashMaps, to repeat Assignment 8.3. Main Method (5 points) Declare and initialize an HashMap with values of type of type Employee, and keys of type integer. In a while loop, keep initializing objects...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT