2019-05-09 19:32:54 +08:00
|
|
|
package DataStructures.Graphs;
|
|
|
|
|
2018-04-14 11:45:48 +08:00
|
|
|
public class MatrixGraphs {
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public static void main(String args[]) {
|
|
|
|
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(10);
|
|
|
|
graph.addEdge(1, 2);
|
|
|
|
graph.addEdge(1, 5);
|
|
|
|
graph.addEdge(2, 5);
|
|
|
|
graph.addEdge(1, 2);
|
|
|
|
graph.addEdge(2, 3);
|
|
|
|
graph.addEdge(3, 4);
|
|
|
|
graph.addEdge(4, 1);
|
|
|
|
graph.addEdge(2, 3);
|
|
|
|
System.out.println(graph);
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class AdjacencyMatrixGraph {
|
2020-10-24 18:23:28 +08:00
|
|
|
private int _numberOfVertices;
|
|
|
|
private int _numberOfEdges;
|
|
|
|
private int[][] _adjacency;
|
|
|
|
|
|
|
|
static final int EDGE_EXIST = 1;
|
|
|
|
static final int EDGE_NONE = 0;
|
|
|
|
|
|
|
|
public AdjacencyMatrixGraph(int givenNumberOfVertices) {
|
|
|
|
this.setNumberOfVertices(givenNumberOfVertices);
|
|
|
|
this.setNumberOfEdges(0);
|
|
|
|
this.setAdjacency(new int[givenNumberOfVertices][givenNumberOfVertices]);
|
|
|
|
for (int i = 0; i < givenNumberOfVertices; i++) {
|
|
|
|
for (int j = 0; j < givenNumberOfVertices; j++) {
|
|
|
|
this.adjacency()[i][j] = AdjacencyMatrixGraph.EDGE_NONE;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private void setNumberOfVertices(int newNumberOfVertices) {
|
|
|
|
this._numberOfVertices = newNumberOfVertices;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public int numberOfVertices() {
|
|
|
|
return this._numberOfVertices;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private void setNumberOfEdges(int newNumberOfEdges) {
|
|
|
|
this._numberOfEdges = newNumberOfEdges;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public int numberOfEdges() {
|
|
|
|
return this._numberOfEdges;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private void setAdjacency(int[][] newAdjacency) {
|
|
|
|
this._adjacency = newAdjacency;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private int[][] adjacency() {
|
|
|
|
return this._adjacency;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private boolean adjacencyOfEdgeDoesExist(int from, int to) {
|
|
|
|
return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public boolean vertexDoesExist(int aVertex) {
|
|
|
|
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public boolean edgeDoesExist(int from, int to) {
|
|
|
|
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
|
|
|
|
return (this.adjacencyOfEdgeDoesExist(from, to));
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method adds an edge to the graph between two specified vertices
|
|
|
|
*
|
|
|
|
* @param from the data of the vertex the edge is from
|
|
|
|
* @param to the data of the vertex the edge is going to
|
|
|
|
* @return returns true if the edge did not exist, return false if it already did
|
|
|
|
*/
|
|
|
|
public boolean addEdge(int from, int to) {
|
|
|
|
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
|
|
|
|
if (!this.adjacencyOfEdgeDoesExist(from, to)) {
|
|
|
|
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_EXIST;
|
|
|
|
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_EXIST;
|
|
|
|
this.setNumberOfEdges(this.numberOfEdges() + 1);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this method removes an edge from the graph between two specified vertices
|
|
|
|
*
|
|
|
|
* @param from the data of the vertex the edge is from
|
|
|
|
* @param to the data of the vertex the edge is going to
|
|
|
|
* @return returns false if the edge doesn't exist, returns true if the edge exists and is removed
|
|
|
|
*/
|
|
|
|
public boolean removeEdge(int from, int to) {
|
|
|
|
if (!this.vertexDoesExist(from) || !this.vertexDoesExist(to)) {
|
|
|
|
if (this.adjacencyOfEdgeDoesExist(from, to)) {
|
|
|
|
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_NONE;
|
|
|
|
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_NONE;
|
|
|
|
this.setNumberOfEdges(this.numberOfEdges() - 1);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this gives a list of vertices in the graph and their adjacencies
|
|
|
|
*
|
|
|
|
* @return returns a string describing this graph
|
|
|
|
*/
|
|
|
|
public String toString() {
|
|
|
|
String s = " ";
|
|
|
|
for (int i = 0; i < this.numberOfVertices(); i++) {
|
|
|
|
s = s + String.valueOf(i) + " ";
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
s = s + " \n";
|
|
|
|
|
|
|
|
for (int i = 0; i < this.numberOfVertices(); i++) {
|
|
|
|
s = s + String.valueOf(i) + " : ";
|
|
|
|
for (int j = 0; j < this.numberOfVertices(); j++) {
|
|
|
|
s = s + String.valueOf(this._adjacency[i][j]) + " ";
|
|
|
|
}
|
|
|
|
s = s + "\n";
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
}
|