Question: JAVA CODE The three I am having trouble making work: public boolean isDirected( ) :check if the graph is a directed graph or not. Return
JAVA CODE
The three I am having trouble making work:
public boolean isDirected( ):check if the graph is a directed graph or not. Return true if the answer is yes, false, otherwise.
public ArrayList BFS(int start) : get the traversal sequence of the graph by using BFS from start index vertex and keep the edge sequence in an ArrayList
public void printAdjMatrix():print out the adjacency matrix if the number of vertices in the graph is less than 20
Edge.java:
public class Edge {
private int startVertex;
private int endVertex;
public Edge(int startVertex, int endVertex) {
super();
this.startVertex = startVertex;
this.endVertex = endVertex;
}
public int getStartVertex() {
return startVertex;
}
public void setStartVertex(int startVertex) {
this.startVertex = startVertex;
}
public int getEndVertex() {
return endVertex;
}
public void setEndVertex(int endVertex) {
this.endVertex = endVertex;
}
@Override
public String toString() {
return "Edge[" + startVertex + " -> " + endVertex + "]";
}
}
testClass.java:
import java.util.ArrayList;
public class testClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
double[][] testGraph = {{0, 80, 0, 25, 60, 0, 0, 0, 0, 0},
{80, 0, 25, 0, 0, 0, 21, 0, 0, 0},
{0, 25, 0, 40, 0, 20, 23, 0, 0, 0},
{25, 0, 40, 0, 55, 35, 0, 0, 0, 0},
{60, 0, 0, 55, 0, 0, 0, 0, 0, 0},
{0, 0, 20, 35, 0, 0, 0, 20, 0, 0},
{0, 21, 23, 0, 0, 0, 0, 25, 10, 0},
{0, 0, 0, 0, 0, 20, 25, 0, 0, 12},
{0, 0, 0, 0, 0, 0, 10, 0, 0, 30},
{0, 0, 0, 0, 0, 0, 0, 12, 30, 0}};
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(testGraph);
int testVertexU = 6;
int testVertexV = 3;
graph.printAdjMatrix();
System.out.println("*****************************************************************");
System.out.println("Edge Set: " + graph.edgeSet());
System.out.println("*****************************************************************");
System.out.println("Directed Graph: "+graph.isDirected());
System.out.println("numVertices:"+graph.numVertices()+"\tnumEdges:"+graph.numEdges());
System.out.println("TestVertex:"+testVertexV+"\tinDegree:"+graph.inDegree(testVertexV)+"\toutDegree:"+graph.outDegree(testVertexV));
System.out.print("TestVertex:"+testVertexV+"\tneighborList:");
System.out.println(graph.neighbors(testVertexV));
System.out.println("*****************************************************************");
graph.insertEdge(testVertexU, testVertexV, 10);
System.out.println("Vertex:"+testVertexU+" and Vertex:" +testVertexV+" are neighbors:" + graph.isNeighbors(testVertexU, testVertexV));
System.out.println("Successfully insert an edge: " + graph.insertEdge(testVertexU, testVertexV, 10));
graph.removeEdge(testVertexU,testVertexV);
System.out.println("Vertex:"+testVertexU+" and Vertex:" +testVertexV+" are neighbors:" + graph.isNeighbors(testVertexU, testVertexV));
System.out.println("Successfully remove an edge: " + graph.removeEdge(testVertexU,testVertexV));
System.out.println("*****************************************************************");
System.out.println("BFS Graph Traversal:");
System.out.println(graph.BFS(0));
//or System.out.println(graph.DFS());
System.out.println("*****************************************************************");
graph.printAdjMatrix();
}
}
AdjacencyMatrixGraph.java:
public class AdjacencyMatrixGraph{
private double[][] adjacencyMatrix;
//Methods Here
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
