Please complete the completeGraph() method. 1 import java .ut ArrayList 6 Generi
ID: 3820299 • Letter: P
Question
Please complete the completeGraph() method.
1 import java .ut ArrayList 6 Generic vertex class 7 class Vertex public T data public boolean visited 10e public Vertex data null visited false 130 public Vertex(T data data -data; visited false 16e public string tostring t return data. tos tring 19 Declare any additional vertex attribute you may need 20 22 public class Graph T 23 array of vertices 24 protected ArrayList verts 25 26 20 array representing adjacency matrix of an unweighted graph If adj Mat is true, there is an edge from verte i to j 27 otherwise the element adj Mat l is false. 28 29 protected boolean adj Mat 30 32 public Graph ArrayList tex> verts boolean -mat) check the validity of input parameters 33 int nverts J verts 34 adjacency matrix must be square matrix of NxN 35 if Jmat. ength nverts return 36 for (int i30; i nverts i++) 37 if -mat ength nverts return 38Explanation / Answer
As per the specs is concerened the given method should do the job well
public void completeGraph() {
adjMat = new boolean[numVerts()][numVerts()]; //create a new adjMat, better practice, though we could have used the old adjMat
for (int i = 0; i<numVerts(); i++)
for (int j = 0; j<numVerts(); j++)
if (i==j) //if i and j are equal, they represent self loop
adjMat[i][j] = false; // hence they are false indicatind that the edges are not present
else // and all other conditions represnts edges between any two distinct nodes
adjMat[i][j] = true; //hence they are true, representing presence of edge
}//end of function
I hope this helps you and solves your problem. I have commented almost every line of the code to make your life easy :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.