I need help implementing directed weighted graph in java using adjacency matrix. Not sure how to check if there are connected edges or how to remove, only know how to add edges.
// Implementation of directed weighted Graph using Adjacent Matrix
public class Graph {
private int size;
private int adjacentMatrix[][];
public Graph (int size) {
this.size = size;
adjacentMatrix = new int [size][size];
}
public void addEdge (int source, int destination, int weight) {
if (source < size && source >= 0 && destination < size && destination >= 0)
adjacentMatrix [source][destination] = weight;
}
// need help in this function for what to set second line equal to or new function in general
public void removeEdge (int source, int destination, int weight) {
if (source < size && source >= 0 && destination < size && destination >= 0)
adjacentMatrix [source][destination] = 0; //(not sure)
}
//need help with this entire function
//function to check if edges are connected
public boolean isEdge(int source, int destination) {
if(size >= 0 && size < size && destination >= 0 && destination < size) {
if(adjacentMatrix[source][destination] == 1)
return true;
else
return false;
}
}
}
}
// I'm not sure if I did this correct at all any help would be appreciated
removeEdge
need aweight
? – Sweeper