Question

In: Computer Science

JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface...

JAVA code

Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface below for details.

EditableDigraph below:

import java.util.NoSuchElementException;

/**
* Implements an editable graph with sparse vertex support.
*
*
*/
public interface EditableDiGraph {
  
/**
* Adds an edge between two vertices, v and w. If vertices do not exist,
* adds them first.
*
* @param v source vertex
* @param w destination vertex
*/
void addEdge(int v, int w);

/**
* Adds a vertex to the graph. Does not allow duplicate vertices.
*
* @param v vertex number
*/
void addVertex(int v);

/**
* Returns the direct successors of a vertex v.
*
* @param v vertex
* @return successors of v
*/
Iterable getAdj(int v);
  
/**
* Number of edges.
*
* @return edge count
*/
int getEdgeCount();
  
/**
* Returns the in-degree of a vertex.
* @param v vertex
* @return in-degree.
* @throws NoSuchElementException exception thrown if vertex does not exist.
*/
int getIndegree(int v) throws NoSuchElementException;
  
/**
* Returns number of vertices.
* @return vertex count
*/
int getVertexCount();
  
/**
* Removes edge from graph. If vertices do not exist, does not remove edge.
*
* @param v source vertex
* @param w destination vertex
*/
void removeEdge(int v, int w);

/**
* Removes vertex from graph. If vertex does not exist, does not try to
* remove it.
*
* @param v vertex
*/
void removeVertex(int v);

/**
* Returns iterable object containing all vertices in graph.
*
* @return iterable object of vertices
*/
Iterable vertices();

/**
* Returns true if the graph contains at least one vertex.
*
* @return boolean
*/
boolean isEmpty();
  
/**
* Returns true if the graph contains a specific vertex.
*
* @param v vertex
* @return boolean
*/
boolean containsVertex(int v);
}

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

public class BetterDiGraph implements EditableDiGraph {

   private HashMap<Integer, HashSet<Integer>> adjacencyMap;
   private int numVertices;
   private int numEdges;

   public BetterDiGraph() {
       this.adjacencyMap = new HashMap<>();
   }

   @Override
   public void addEdge(int v, int w) {
       addVertex(v);
       addVertex(w);
       if (this.adjacencyMap.containsKey(v)) {
           if (!this.adjacencyMap.get(v).contains(w)) {
               this.adjacencyMap.get(v).add(w);
               this.numEdges++;
               return;
           }
       }
       return;

   }

   @Override
   public void addVertex(int v) {
       if (!this.adjacencyMap.containsKey(v)) {
           this.adjacencyMap.put(v, new HashSet<>());
           this.numVertices++;
           return;
       }
       return;

   }

   @Override
   public Iterable getAdj(int v) {
       // TODO Auto-generated method stub
       return this.adjacencyMap.get(v);
   }

   @Override
   public int getEdgeCount() {
       // TODO Auto-generated method stub
       return this.numEdges;
   }

   @Override
   public int getIndegree(int v) throws NoSuchElementException {

if (!containsVertex(v)) {
           throw new NoSuchElementException();
       }

       List<Integer> inList = new ArrayList<Integer>();
       for (Integer to : this.adjacencyMap.keySet()) {
           for (Integer e : this.adjacencyMap.get(to))
               if (e.equals(v))
                   inList.add(to);
       }
       return inList.size();

   }

   @Override
   public int getVertexCount() {
       // TODO Auto-generated method stub
       return this.numVertices;
   }

   @Override
   public void removeEdge(int v, int w) {
       if (this.adjacencyMap.containsKey(v) && this.adjacencyMap.containsKey(w)) {
           if (this.adjacencyMap.get(v).contains(w)) {
               this.adjacencyMap.get(v).remove(w);
               this.numEdges--;
               return;
           }
       }
       return;

   }

   @Override
   public void removeVertex(int vertex) {
       if (this.adjacencyMap.containsKey(vertex)) {
           this.adjacencyMap.remove(vertex);
           for (Map.Entry<Integer, HashSet<Integer>> entry : this.adjacencyMap.entrySet()) {
               if (entry.getValue().contains(vertex)) {
                   this.adjacencyMap.get(entry.getKey()).remove(vertex);
               }
           }
           this.numVertices--;
           return;
       }
       return;

   }

   @Override
   public Iterable vertices() {
       // TODO Auto-generated method stub

       List<Integer> inList = new ArrayList<Integer>();
       for (Integer to : this.adjacencyMap.keySet()) {
           if (!inList.contains(to))
               inList.add(to);
       }
       return inList;

   }

   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return this.numVertices == 0;
   }

   @Override
   public boolean containsVertex(int v) {
       if (this.adjacencyMap.containsKey(v)) {
           return true;
       }
       return false;
   }

}


Related Solutions

Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Using Java, write code as follows: Create an interface called Items. It should define at least...
Using Java, write code as follows: Create an interface called Items. It should define at least the following methods: public void add( Object item ) - adds a single object to the collection. public Object get( int index ) - returns an item at a specific index. public int size() - returns the number of items stored in the collection. public void addAll( Object[] items ) - adds all of the elements in the array to the collection. Write a...
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface...
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface Mathematical (in the Mathematical.java file). These should have the following definitions: public boolean isPrime(int n) You cannot change the signature for the method. This method is similar to the ones we've discussed in the lab and will return true or false depending on if the passed in integer values is prime or not, respectively. Return false if the invoker passes in a number less...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT