|
| 1 | +public class MatrixGraphs { |
| 2 | + |
| 3 | + public static void main(String args[]) { |
| 4 | + AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(10); |
| 5 | + graph.addEdge(1, 2); |
| 6 | + graph.addEdge(1, 5); |
| 7 | + graph.addEdge(2, 5); |
| 8 | + graph.addEdge(1, 2); |
| 9 | + graph.addEdge(2, 3); |
| 10 | + graph.addEdge(3, 4); |
| 11 | + graph.addEdge(4, 1); |
| 12 | + graph.addEdge(2, 3); |
| 13 | + System.out.println(graph); |
| 14 | + } |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | +class AdjacencyMatrixGraph { |
| 19 | + private int _numberOfVertices; |
| 20 | + private int _numberOfEdges; |
| 21 | + private int[][] _adjacency; |
| 22 | + |
| 23 | + static final int EDGE_EXIST = 1; |
| 24 | + static final int EDGE_NONE = 0; |
| 25 | + |
| 26 | + public AdjacencyMatrixGraph(int givenNumberOfVertices) { |
| 27 | + this.setNumberOfVertices(givenNumberOfVertices); |
| 28 | + this.setNumberOfEdges(0); |
| 29 | + this.setAdjacency(new int[givenNumberOfVertices][givenNumberOfVertices]); |
| 30 | + for (int i = 0; i < givenNumberOfVertices; i++) { |
| 31 | + for (int j = 0; j < givenNumberOfVertices; j++) { |
| 32 | + this.adjacency()[i][j] = AdjacencyMatrixGraph.EDGE_NONE; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private void setNumberOfVertices(int newNumberOfVertices) { |
| 38 | + this._numberOfVertices = newNumberOfVertices; |
| 39 | + } |
| 40 | + |
| 41 | + public int numberOfVertices() { |
| 42 | + return this._numberOfVertices; |
| 43 | + } |
| 44 | + |
| 45 | + private void setNumberOfEdges(int newNumberOfEdges) { |
| 46 | + this._numberOfEdges = newNumberOfEdges; |
| 47 | + } |
| 48 | + |
| 49 | + public int numberOfEdges() { |
| 50 | + return this._numberOfEdges; |
| 51 | + } |
| 52 | + |
| 53 | + private void setAdjacency(int[][] newAdjacency) { |
| 54 | + this._adjacency = newAdjacency; |
| 55 | + } |
| 56 | + |
| 57 | + private int[][] adjacency() { |
| 58 | + return this._adjacency; |
| 59 | + } |
| 60 | + |
| 61 | + private boolean adjacencyOfEdgeDoesExist(int from, int to) { |
| 62 | + return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE); |
| 63 | + } |
| 64 | + |
| 65 | + public boolean vertexDoesExist(int aVertex) { |
| 66 | + if (aVertex >= 0 && aVertex < this.numberOfVertices()) { |
| 67 | + return true; |
| 68 | + } else { |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public boolean edgeDoesExist(int from, int to) { |
| 74 | + if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) { |
| 75 | + return (this.adjacencyOfEdgeDoesExist(from, to)); |
| 76 | + } |
| 77 | + |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * this method adds an edge to the graph between two specified |
| 83 | + * verticies |
| 84 | + * |
| 85 | + * @param from the data of the vertex the edge is from |
| 86 | + * @param to the data of the vertex the edge is going to |
| 87 | + * @return returns true if the edge did not exist, return false if it already did |
| 88 | + */ |
| 89 | + public boolean addEdge(int from, int to) { |
| 90 | + if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) { |
| 91 | + if (!this.adjacencyOfEdgeDoesExist(from, to)) { |
| 92 | + this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_EXIST; |
| 93 | + this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_EXIST; |
| 94 | + this.setNumberOfEdges(this.numberOfEdges() + 1); |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * this method removes an edge from the graph between two specified |
| 104 | + * verticies |
| 105 | + * |
| 106 | + * @param from the data of the vertex the edge is from |
| 107 | + * @param to the data of the vertex the edge is going to |
| 108 | + * @return returns false if the edge doesn't exist, returns true if the edge exists and is removed |
| 109 | + */ |
| 110 | + public boolean removeEdge(int from, int to) { |
| 111 | + if(!this.vertexDoesExist(from) || !this.vertexDoesExist(to)) { |
| 112 | + if (this.adjacencyOfEdgeDoesExist(from, to)) { |
| 113 | + this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_NONE; |
| 114 | + this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_NONE; |
| 115 | + this.setNumberOfEdges(this.numberOfEdges() - 1); |
| 116 | + return true; |
| 117 | + } |
| 118 | + } |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * this gives a list of verticies in the graph and their adjacencies |
| 124 | + * |
| 125 | + * @return returns a string describing this graph |
| 126 | + */ |
| 127 | + public String toString() { |
| 128 | + String s = new String(); |
| 129 | + s = " "; |
| 130 | + for (int i = 0; i < this.numberOfVertices(); i++) { |
| 131 | + s = s + String.valueOf(i) + " "; |
| 132 | + } |
| 133 | + s = s + " \n"; |
| 134 | + |
| 135 | + for (int i = 0; i < this.numberOfVertices(); i++) { |
| 136 | + s = s + String.valueOf(i) + " : "; |
| 137 | + for (int j = 0; j < this.numberOfVertices(); j++) { |
| 138 | + s = s + String.valueOf(this._adjacency[i][j]) + " "; |
| 139 | + } |
| 140 | + s = s + "\n"; |
| 141 | + } |
| 142 | + return s; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments