5
5
import java .util .ArrayList ;
6
6
import java .util .LinkedList ;
7
7
8
+ /**
9
+ * Implementation of a graph in a matrix form
10
+ * Also known as an adjacency matrix representation
11
+ * [Adjacency matrix - Wikipedia](https://en.wikipedia.org/wiki/Adjacency_matrix)
12
+ *
13
+ * @author Unknown
14
+ */
8
15
public class MatrixGraphs {
9
16
10
17
public static void main (String args []) {
@@ -31,14 +38,35 @@ public static void main(String args[]) {
31
38
}
32
39
}
33
40
41
+ /**
42
+ * AdjacencyMatrixGraph Implementation
43
+ */
34
44
class AdjacencyMatrixGraph {
45
+ /**
46
+ * The number of vertices in the graph
47
+ */
35
48
private int _numberOfVertices ;
49
+
50
+ /**
51
+ * The number of edges in the graph
52
+ */
36
53
private int _numberOfEdges ;
54
+
55
+ /**
56
+ * The adjacency matrix for the graph
57
+ */
37
58
private int [][] _adjacency ;
38
59
60
+ /**
61
+ * Static variables to define whether or not an edge exists in the
62
+ * adjacency matrix
63
+ */
39
64
static final int EDGE_EXIST = 1 ;
40
65
static final int EDGE_NONE = 0 ;
41
66
67
+ /**
68
+ * Constructor
69
+ */
42
70
public AdjacencyMatrixGraph (int givenNumberOfVertices ) {
43
71
this .setNumberOfVertices (givenNumberOfVertices );
44
72
this .setNumberOfEdges (0 );
@@ -50,34 +78,77 @@ public AdjacencyMatrixGraph(int givenNumberOfVertices) {
50
78
}
51
79
}
52
80
81
+ /**
82
+ * Updates the number of vertices in the graph
83
+ *
84
+ * @param newNumberOfVertices the new number of vertices
85
+ */
53
86
private void setNumberOfVertices (int newNumberOfVertices ) {
54
87
this ._numberOfVertices = newNumberOfVertices ;
55
88
}
56
89
90
+ /**
91
+ * Getter for `this._numberOfVertices`
92
+ *
93
+ * @return the number of vertices in the graph
94
+ */
57
95
public int numberOfVertices () {
58
96
return this ._numberOfVertices ;
59
97
}
60
98
99
+ /**
100
+ * Updates the number of edges in the graph
101
+ *
102
+ * @param newNumberOfEdges
103
+ * */
61
104
private void setNumberOfEdges (int newNumberOfEdges ) {
62
105
this ._numberOfEdges = newNumberOfEdges ;
63
106
}
64
107
108
+ /**
109
+ * Getter for `this._numberOfEdges`
110
+ *
111
+ * @return the number of edges
112
+ */
65
113
public int numberOfEdges () {
66
114
return this ._numberOfEdges ;
67
115
}
68
116
117
+ /**
118
+ * Sets a new matrix as the adjacency matrix
119
+ *
120
+ * @param newAdjacency the new adjaceny matrix
121
+ */
69
122
private void setAdjacency (int [][] newAdjacency ) {
70
123
this ._adjacency = newAdjacency ;
71
124
}
72
125
126
+ /**
127
+ * Getter for the adjacency matrix
128
+ *
129
+ * @return the adjacency matrix
130
+ */
73
131
private int [][] adjacency () {
74
132
return this ._adjacency ;
75
133
}
76
134
135
+ /**
136
+ * Checks if two vertices are connected by an edge
137
+ *
138
+ * @param from the parent vertex to check for adjacency
139
+ * @param to the child vertex to check for adjacency
140
+ * @return whether or not the vertices are adjancent
141
+ */
77
142
private boolean adjacencyOfEdgeDoesExist (int from , int to ) {
78
143
return (this .adjacency ()[from ][to ] != AdjacencyMatrixGraph .EDGE_NONE );
79
144
}
80
145
146
+ /**
147
+ * Checks if a particular vertex exists in a graph
148
+ *
149
+ * @param aVertex the vertex to check for existence
150
+ * @return whether or not the vertex exists
151
+ */
81
152
public boolean vertexDoesExist (int aVertex ) {
82
153
if (aVertex >= 0 && aVertex < this .numberOfVertices ()) {
83
154
return true ;
@@ -86,6 +157,13 @@ public boolean vertexDoesExist(int aVertex) {
86
157
}
87
158
}
88
159
160
+ /**
161
+ * Checks if two vertices are connected by an edge
162
+ *
163
+ * @param from the parent vertex to check for adjacency
164
+ * @param to the child vertex to check for adjacency
165
+ * @return whether or not the vertices are adjancent
166
+ */
89
167
public boolean edgeDoesExist (int from , int to ) {
90
168
if (this .vertexDoesExist (from ) && this .vertexDoesExist (to )) {
91
169
return (this .adjacencyOfEdgeDoesExist (from , to ));
0 commit comments