Please complete the valence() method. here\'s part 1 of the question: https://ww
ID: 3820302 • Letter: P
Question
Please complete the valence() method.
here's part 1 of the question: https://www.chegg.com/homework-help/questions-and-answers/please-complete-completegraph-method-q20794417
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
The part 1 may not be required to code this method.
The source code is as follows:
public int valence(int vid) {
int count = 0;
for (int j = 0; j < numVerts(); j++) {
if (adjMat[vid][j]) {
count++;
}
}
return count;
}
Description:
The goal of this method is to find the number of neighbors of the given vertex.
The graph is having the adjacency matrix, so using that array, we can determine this count.
This is an unweighted graph.
And if adjMat[i][j] = true, then it means that there is a link from vertex i to j. This means j is a neighbor of i.
So, in this method, we need to count the number of vertices which has this value set to true in the array adjMat[ ][ ].
And the first index here is : 'vid' which is provided in this method argument.
And j us varying from 0 to number of vertex which can be found using the method numVerts() which is returning an int.
So, as it was used in the method print(), the same way for loop is used here.
And I am checking if the value is true at that particular location for 'vid' row, the count is added.
count was initialized to 0, and if the value is true, this count is added.
So, at the end of this for loop, the number of neighbors is found.
Then I am returning the count value.
Do comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.