forked from TheAlgorithms/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixGraphs.java
More file actions
Latest commit
145 lines (124 loc) · 3.83 KB
/
Copy pathMatrixGraphs.java
File metadata and controls
145 lines (124 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
publicclassMatrixGraphs {
publicstaticvoidmain(Stringargs[]) {
AdjacencyMatrixGraphgraph = newAdjacencyMatrixGraph(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);
}
}
classAdjacencyMatrixGraph {
privateint_numberOfVertices;
privateint_numberOfEdges;
privateint[][] _adjacency;
staticfinalintEDGE_EXIST = 1;
staticfinalintEDGE_NONE = 0;
publicAdjacencyMatrixGraph(intgivenNumberOfVertices) {
this.setNumberOfVertices(givenNumberOfVertices);
this.setNumberOfEdges(0);
this.setAdjacency(newint[givenNumberOfVertices][givenNumberOfVertices]);
for (inti = 0; i < givenNumberOfVertices; i++) {
for (intj = 0; j < givenNumberOfVertices; j++) {
this.adjacency()[i][j] = AdjacencyMatrixGraph.EDGE_NONE;
}
}
}
privatevoidsetNumberOfVertices(intnewNumberOfVertices) {
this._numberOfVertices = newNumberOfVertices;
}
publicintnumberOfVertices() {
returnthis._numberOfVertices;
}
privatevoidsetNumberOfEdges(intnewNumberOfEdges) {
this._numberOfEdges = newNumberOfEdges;
}
publicintnumberOfEdges() {
returnthis._numberOfEdges;
}
privatevoidsetAdjacency(int[][] newAdjacency) {
this._adjacency = newAdjacency;
}
privateint[][] adjacency() {
returnthis._adjacency;
}
privatebooleanadjacencyOfEdgeDoesExist(intfrom, intto) {
return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);
}
publicbooleanvertexDoesExist(intaVertex) {
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
returntrue;
} else {
returnfalse;
}
}
publicbooleanedgeDoesExist(intfrom, intto) {
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
return (this.adjacencyOfEdgeDoesExist(from, to));
}
returnfalse;
}
/**
* 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
*/
publicbooleanaddEdge(intfrom, intto) {
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);
returntrue;
}
}
returnfalse;
}
/**
* 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
*/
publicbooleanremoveEdge(intfrom, intto) {
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);
returntrue;
}
}
returnfalse;
}
/**
* this gives a list of vertices in the graph and their adjacencies
*
* @return returns a string describing this graph
*/
publicStringtoString() {
Strings = newString();
s = " ";
for (inti = 0; i < this.numberOfVertices(); i++) {
s = s + String.valueOf(i) + " ";
}
s = s + " \n";
for (inti = 0; i < this.numberOfVertices(); i++) {
s = s + String.valueOf(i) + " : ";
for (intj = 0; j < this.numberOfVertices(); j++) {
s = s + String.valueOf(this._adjacency[i][j]) + " ";
}
s = s + "\n";
}
returns;
}
}