Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The method public ArrayList<edge> edgeSet() is the one that I need help with. th

ID: 3706155 • Letter: T

Question

The method public ArrayList<edge> edgeSet() is the one that I need help with. the instructions say

8) public ArrayList<Edge> edgeSet( ): get the list of edges in the graph.

What is the code for this method?

import java.util.*;

public class AdjacencyMatrixGraph{

   private double[][] adjacencyMatrix;

private boolean isDirected;

   //initialize the graph based on a user given adjacency matrix

   public AdjacencyMatrixGraph(double[][] adjMatrix, boolean directed)

   {

adjacencyMatrix = adjMatrix;

isDirected = directed;

   }

  

   //check if the graph is a directed graph

   //public boolean isDirected(){

              

   //}

   //get the number of vertices

   public int numVertices() {

return adjacenyMatrix.length;

   }

   //get the number of edges

   public int numEdges() {

int edges = 0;

for(int i = 0; i < adjacenyMatrix.length; i++){

for(int j = 0; j < adjacenyMatrix[i].length; j++){

if(adjacencyMatrix[i][j] !=0) edges +=1;

}

}

if(isDirected){

return edges;

}

else return edges / 2;

   }

   //get the number of out degree for the vertex whose index is v

   public int outDegree(int v){

int out = 0;

for(int j = 0; j < adjacencyMatrix[v].length; j++){

if(adjacencyMatrix[v][j] !=0) out++;

}

return out;

   }

   //get the number of in degree for the vertex whose index is v

   public int inDegree(int v){

int in = 0;

for(int j=0; j < adjacencyMatrix.length; j++){

if(adjacencyMatrix[j][v] !=0) in++;

}

return in

   }

   //get the list of neighbor for the vertex whose index is v

   public ArrayList<Integer> neighbors(int v){

ArrayList<Integer> n = new ArrayList<Interger>();

for(int j = 0; j<adjacencyMatrix[v].length; j++){

if(adjacencyMatrix[v][j] !=0)n.add(j);

}

return n;

   }

  

   //get the set of edges ------------- THIS IS WHERE I AM HAVING PROBLEMS

   public ArrayList<Integer> edgeSet()

   {

      

   }

  

   //return true if vertex u and v are neighbors

   public boolean isNeighbors(int u, int v){

if(adjacencyMatrix[u][v] !=0){

return false;

}

return true;

   }

  

   //insert an edge between two vertices, return false if there is an edge between them already

   public boolean insertEdge(int u, int v, double weight){

if(adjacencyMatrix[u][v] !=0){

return false;

}

else {

adjacencyMatrix[u][v] = 1;

return true;

}

}

   //remove an edge between two vertices, return false if there is no edge between them

   public boolean removeEdge(int u, int v){

if(adjacencyMatrix[u][v] == 0){

return false;

}

else{

adjacencyMatrix[u][v] = 0;

return true;

}

      

   }

  

   //get the traversal sequence of the graph by using BFS and keep the sequence in an ArrayList

   public ArrayList<Integer> BFS()

   {

ArrayList<Integer> arr = new ArrayList<Integer>();

ArrayList<Integer> queue = new ArrayList<Integer>();

  

queue.add(0);

while(queue.size() !=0){

arr.add(queue.get(0));

for(int i = 0; i < adjacencyMatrix[queue.get(0)].length; i++){

if(adjacencyMatrix[queue.get(0)][i] == 1 && !arr.contains(i)){

queue.add(i);

}

}

queue.remove(0);

}

return arr;

   }

          

   // or DFS()

   /* get the traversal sequence of the graph by using DFS and keep the sequence in an ArrayList

     

   public ArrayList<Edge> DFS()

   {

   }*/

  

   //obtain the minimum spanning tree on a undirected graph, return null if the graph is directed

   //public ArrayList<Edge> MST(int start){

      

   //}

  

  

   //print out the adjacency matrix if the number of vertices in the graph is less than 20

   public void printAdjMatrix()

   {

if(adjacencyMatrix.length>=20) return;

for(int i = 0; i<adjacencyMatrix.length; i++){

for(int j=0; j<adjacencyMatrix[i].length; j++){

System.out.print(adjacencyMatrix[i][j] + " ");

}

System.out.println();

}

   }

}

Explanation / Answer

//the set of edges is defined as

public ArrayList<Integer> edgeSet(){

ArrayList<Integer> set = new ArrayList<Integer>();

for(i = 0; i<adjacencyMatrix.length; i++)

{

for(j = 0; j<adjacencyMatrix.length; j++)

{

if(adjacencyMatrix[i][j]==1) // Look for the edges

set.add(i, j); //adds pair of vertices, as edge in the list

}

return set;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote