同步操作将从 编程语言算法集/Java 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package DataStructures.Graphs;public class MatrixGraphs {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);}}class AdjacencyMatrixGraph {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;}}}private void setNumberOfVertices(int newNumberOfVertices) {this._numberOfVertices = newNumberOfVertices;}public int numberOfVertices() {return this._numberOfVertices;}private void setNumberOfEdges(int newNumberOfEdges) {this._numberOfEdges = newNumberOfEdges;}public int numberOfEdges() {return this._numberOfEdges;}private void setAdjacency(int[][] newAdjacency) {this._adjacency = newAdjacency;}private int[][] adjacency() {return this._adjacency;}private boolean adjacencyOfEdgeDoesExist(int from, int to) {return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);}public boolean vertexDoesExist(int aVertex) {if (aVertex >= 0 && aVertex < this.numberOfVertices()) {return true;} else {return false;}}public boolean edgeDoesExist(int from, int to) {if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {return (this.adjacencyOfEdgeDoesExist(from, to));}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;}}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;}}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) + " ";}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;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。