Question: Write a Graph method in the given class public int vertexOutDegree(int v) that tells the out - degree of a given vertex id (defined to

Write a Graph method in the given class public int vertexOutDegree(int v) that tells the out - degree of a given vertex id (defined to be the number of Edges exiting the vertex) by directly accessing the two - dimensional array.

To test the above code, add the following code to main(): theGraph.addEdge(3, 1); // DB theGraph.addEdge(3, 2); // DC theGraph.addEdge(3, 4); // DE theGraph.addEdge(4, 3); // ED theGraph.addEdge(2, 3); // CD

class Vertex

{

public char label; // label (e.g. A)

public boolean wasVisited;

public Vertex(char lab) // constructor

{

label = lab;

wasVisited = false;

}

} // end class Vertex

class Graph

{

private final int MAX_VERTS = 20;

private Vertex vertexList[]; // list of vertices

private int adjMat[][]; // adjacency matrix

private int nVerts; // current number of vertices

// -----------------------------------------------------------

public Graph() // constructor

{

vertexList = new Vertex[MAX_VERTS];

// adjacency matrix

adjMat = new int[MAX_VERTS][MAX_VERTS];

nVerts = 0;

for(int j=0; j

for(int k=0; k

adjMat[j][k] = 0;

} // end constructor

// -----------------------------------------------------------

public void addVertex(char lab)

{

vertexList[nVerts++] = new Vertex(lab);

}

// -----------------------------------------------------------

public void addEdge(int start, int end)

{

adjMat[start][end] = 1;

}

// ------------------------------------------------------------

public void displayVertex(int v)

{

System.out.print(vertexList[v].label);

}

public void displayVertices() {

// TODO Auto-generated method stub

for (int i=0; i

System.out.print(vertexList[i].label);

}

}

public void displayEdges() {

// TODO Auto-generated method stub

for (int i=0; i

for (int j=i; j

if(adjMat[i][j]==1) {

System.out.println(vertexList[i].label+"--"+vertexList[j].label);}

}

}

}

public void removeVertexEdges(int i) {

for (int j=0; j

adjMat[i][j]=0;

adjMat[j][i]=0;

}

}

// ------------------------------------------------------------

} // end class Graph

public class GraphApp1_9 {

public static void main(String[] args)

{

Graph theGraph = new Graph();

theGraph.addVertex('A'); // 0

theGraph.addVertex('B'); // 1

theGraph.addVertex('C'); // 2

theGraph.addVertex('D'); // 3

theGraph.addVertex('E'); // 4

theGraph.addEdge(0, 1); // AB

theGraph.addEdge(1, 2); // BC

theGraph.addEdge(0, 3); // AD

theGraph.addEdge(3, 4); // DE

System.out.print("Vertices: ");

theGraph.displayVertices();

System.out.println();

System.out.println("Edges:");

theGraph.displayEdges();

theGraph.removeVertexEdges(0);

System.out.println("After removing Vertex: A");

theGraph.displayEdges();

} // end main()

}

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 Databases Questions!