Question: public class Graph 1 { private boolean adjMatrix [ ] [ ] ; private int numVertices; public Graph 1 ( int numVertices ) { this.numVertices

public class Graph1{private boolean adjMatrix[][];
private int numVertices;
public Graph1(int numVertices){
this.numVertices = numVertices;
adjMatrix = new boolean[numVertices][numVertices];
}
// Add edges
public void addEdge(int i, int j){
adjMatrix[i][j]= true;
adjMatrix[j][i]= true;
}
public String toString(){
StringBuilder s = new StringBuilder();
for (int i =0; i < numVertices; i++){
s.append(i +": ");
{
);
}
}
return s.toString();
}
public static void main(String args[]){
Graph1 g = new Graph1(5);
g.addEdge(0,1);
g.addEdge(0,4);
g.addEdge(1,0);
g.addEdge(1,4);
g.addEdge(1,3);
g.addEdge(2,1);
g.addEdge(2,3);
g.addEdge(3,1);
g.addEdge(3,2);
g.addEdge(3,4);
g.addEdge(4,0);
g.addEdge(4,1);
g.addEdge(4,3);
System.out.print(g.toString());
}
}
Almost all the code for representing graph through adjacency matrix is provided. Only few objects are missing.
Your assignment:
Fill in the missing codes and program adjacency matrix to represent graph
Represent a graph having 7 vertices, you can choose your source and destination vertices
In a separate piece of paper explain the code and draw the graph that you have coded

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!