Skip to content

Commit 78f7706

Browse files
authored
Improve documentation for MatrixGraphs (TheAlgorithms#2808)
1 parent 5f424ce commit 78f7706

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

DataStructures/Graphs/MatrixGraphs.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
import java.util.ArrayList;
66
import java.util.LinkedList;
77

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+
*/
815
public class MatrixGraphs {
916

1017
public static void main(String args[]) {
@@ -31,14 +38,35 @@ public static void main(String args[]) {
3138
}
3239
}
3340

41+
/**
42+
* AdjacencyMatrixGraph Implementation
43+
*/
3444
class AdjacencyMatrixGraph {
45+
/**
46+
* The number of vertices in the graph
47+
*/
3548
private int _numberOfVertices;
49+
50+
/**
51+
* The number of edges in the graph
52+
*/
3653
private int _numberOfEdges;
54+
55+
/**
56+
* The adjacency matrix for the graph
57+
*/
3758
private int[][] _adjacency;
3859

60+
/**
61+
* Static variables to define whether or not an edge exists in the
62+
* adjacency matrix
63+
*/
3964
static final int EDGE_EXIST = 1;
4065
static final int EDGE_NONE = 0;
4166

67+
/**
68+
* Constructor
69+
*/
4270
public AdjacencyMatrixGraph(int givenNumberOfVertices) {
4371
this.setNumberOfVertices(givenNumberOfVertices);
4472
this.setNumberOfEdges(0);
@@ -50,34 +78,77 @@ public AdjacencyMatrixGraph(int givenNumberOfVertices) {
5078
}
5179
}
5280

81+
/**
82+
* Updates the number of vertices in the graph
83+
*
84+
* @param newNumberOfVertices the new number of vertices
85+
*/
5386
private void setNumberOfVertices(int newNumberOfVertices) {
5487
this._numberOfVertices = newNumberOfVertices;
5588
}
5689

90+
/**
91+
* Getter for `this._numberOfVertices`
92+
*
93+
* @return the number of vertices in the graph
94+
*/
5795
public int numberOfVertices() {
5896
return this._numberOfVertices;
5997
}
6098

99+
/**
100+
* Updates the number of edges in the graph
101+
*
102+
* @param newNumberOfEdges
103+
* */
61104
private void setNumberOfEdges(int newNumberOfEdges) {
62105
this._numberOfEdges = newNumberOfEdges;
63106
}
64107

108+
/**
109+
* Getter for `this._numberOfEdges`
110+
*
111+
* @return the number of edges
112+
*/
65113
public int numberOfEdges() {
66114
return this._numberOfEdges;
67115
}
68116

117+
/**
118+
* Sets a new matrix as the adjacency matrix
119+
*
120+
* @param newAdjacency the new adjaceny matrix
121+
*/
69122
private void setAdjacency(int[][] newAdjacency) {
70123
this._adjacency = newAdjacency;
71124
}
72125

126+
/**
127+
* Getter for the adjacency matrix
128+
*
129+
* @return the adjacency matrix
130+
*/
73131
private int[][] adjacency() {
74132
return this._adjacency;
75133
}
76134

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+
*/
77142
private boolean adjacencyOfEdgeDoesExist(int from, int to) {
78143
return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);
79144
}
80145

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+
*/
81152
public boolean vertexDoesExist(int aVertex) {
82153
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
83154
return true;
@@ -86,6 +157,13 @@ public boolean vertexDoesExist(int aVertex) {
86157
}
87158
}
88159

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+
*/
89167
public boolean edgeDoesExist(int from, int to) {
90168
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
91169
return (this.adjacencyOfEdgeDoesExist(from, to));

0 commit comments

Comments
 (0)