Question: PLEASE I NEED HELP COMPILING THIS CODE. THIS IS FOR A JAVA PROGRAM THAT BUILD A DIRECTED GRAPH. PLEASE PROVIDE SCREENSHOT FOR MORE EXPLANATION. THANKS
PLEASE I NEED HELP COMPILING THIS CODE. THIS IS FOR A JAVA PROGRAM THAT BUILD A DIRECTED GRAPH. PLEASE PROVIDE SCREENSHOT FOR MORE EXPLANATION. THANKS
Here starts the solution :-
GRAPH CLASS
public class Graph
private HashMap
private int numVertices;
private int numEdges;
private final String type = "directed";
private final boolean weighted = false;
public Graph() {
this.adjacencyMap = new HashMap<>();
}
/**
* Adds a vertex to the graph.
* @param vertex vertex to add.
* @return true if vertex was added successfully, false if otherwise.
* @
*/
public boolean addVertex(GraphNode
if(!this.adjacencyMap.containsKey(vertex)) {
this.adjacencyMap.put(vertex, new HashSet<>());
this.numVertices++;
return true;
}
return false;
}
/**
* Removes a specified vertex from the graph.
* @param vertex vertex to remove.
* @return true if vertex was removed successfully, false if otherwise.
* @
*/
public boolean removeVertex(GraphNode
if(this.adjacencyMap.containsKey(vertex)) {
this.adjacencyMap.remove(vertex);
for(Map.Entry
if(entry.getValue().contains(vertex)) {
this.adjacencyMap.get(entry.getKey()).remove(vertex);
}
}
this.numVertices--;
return true;
}
return false;
}
/**
* Adds an edge between two vertices to the graph.
* @param x source vertex of edge to add.
* @param y destination vertex of edge to add.
* @return true if the edge was added successfully, false if otherwise.
* @
*/
public boolean addEdge(GraphNode
if(this.adjacencyMap.containsKey(x)) {
if(!this.adjacencyMap.get(x).contains(y)) {
this.adjacencyMap.get(x).add(y);
this.numEdges++;
return true;
}
}
return false;
}
/**
* Removes a specified edge between two vertices from the graph, if it already exists.
* @param x source vertex of edge to remove.
* @param y destination vertex of edge to remove.
* @return true if the edge was removed successfully, false if otherwise.
* @
*/
public boolean removeEdge(GraphNode
if(this.adjacencyMap.containsKey(x)) {
if(this.adjacencyMap.get(x).contains(y)) {
this.adjacencyMap.get(x).remove(y);
this.numEdges--;
return true;
}
}
return false;
}
/**
* Determines if two vertices are adjacent (or, if an edge exists between them).
* @param x source vertex.
* @param y destination vertex.
* @return true if both vertices are adjacent, false if otherwise.
* @
*/
public boolean isAdjacent(GraphNode
HashSet
if(adjacencySet != null) {
if(adjacencySet.contains(y)) {
return true;
}
}
return false;
}
/**
* Determines if graph contains a given vertex or not.
* @param vertex vertex to search.
* @return true if the graph contains the vertex, false if otherwise.
* @
*/
public boolean containsVertex(GraphNode
if(this.adjacencyMap.containsKey(vertex)) {
return true;
}
return false;
}
/**
* Returns a HashSet containing all neighbors of a given vertex (or, all vertices with which the vertex shares an edge).
* @param vertex vertex to search.
* @return a HashSet containing all neighbors of the vertex.
* @
*/
public HashSet
return this.adjacencyMap.get(vertex);
}
/**
* Determines whether or not a path exists between two nodes, using Depth-First Search.
* Uses a wrapper method to initialize objects required for search traversal.
* @param source source node to be used in search.
* @param destination destination node to be used in search.
* @return true if a path exists between source and destination nodes, false if otherwise.
* @
*/
public boolean depthFirstSearch(GraphNode
if(!this.adjacencyMap.containsKey(source) || !this.adjacencyMap.containsKey(destination)) {
return false;
}
Stack
stack.push(source);
return depthFirstSearch(stack, destination);
}
private boolean depthFirstSearch(Stack
HashMap
while(!stack.isEmpty()) {
GraphNode
visited.put(current, VisitStatus.VISITING);
if(current.equals(destination)) {
return true;
}
for(GraphNode
if(visited.containsKey(neighbor)) {
if(visited.get(neighbor).equals(VisitStatus.UNVISITED)) {
stack.push(neighbor);
}
} else {
stack.push(neighbor);
}
}
visited.put(current, VisitStatus.VISITED);
}
return false;
}
/**
* Determines whether or not a path exists between two nodes, using Breadth-First Search.
* Uses a wrapper method to initialize objects required for search traversal.
* @param source source node to be used in search.
* @param destination destination node to be used in search.
* @return true if a path exists between source and destination nodes, false if otherwise.
* @
*/
public boolean breadthFirstSearch(GraphNode
if(!this.adjacencyMap.containsKey(source) || !this.adjacencyMap.containsKey(destination)) {
return false;
}
LinkedList
queue.addLast(source);
return breadthFirstSearch(queue, destination);
}
private boolean breadthFirstSearch(LinkedList
HashMap
while(!queue.isEmpty()) {
GraphNode
visited.put(current, VisitStatus.VISITING);
if(current.equals(destination)) {
return true;
}
for(GraphNode
if(visited.containsKey(neighbor)) {
if(visited.get(neighbor).equals(VisitStatus.UNVISITED)) {
queue.addLast(neighbor);
}
} else {
queue.addLast(neighbor);
}
}
visited.put(current, VisitStatus.VISITED);
}
return false;
}
/**
* Returns the number of vertices within the graph.
* @return an integer representing number of vertices contained within the graph.
* @
*/
public int getNumVertices() {
return this.numVertices;
}
/**
* Returns the number of edges within the graph.
* @return an integer representing number of edges contained within the graph.
* @
*/
public int getNumEdges() {
return this.numEdges;
}
}
GRAPH NODE CLASS
public class GraphNode
private T data;
public GraphNode() {}
public GraphNode(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
VISIT ENUM CLASS :-
public enum VisitStatus {
UNVISITED,
VISITING,
VISITED
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
