Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78f7706

Browse files
authored
Improve documentation forMatrixGraphs (TheAlgorithms#2808)
1 parent5f424ce commit78f7706

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
importjava.util.ArrayList;
66
importjava.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
publicclassMatrixGraphs {
916

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

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

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

67+
/**
68+
* Constructor
69+
*/
4270
publicAdjacencyMatrixGraph(intgivenNumberOfVertices) {
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
privatevoidsetNumberOfVertices(intnewNumberOfVertices) {
5487
this._numberOfVertices =newNumberOfVertices;
5588
}
5689

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

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

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

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

126+
/**
127+
* Getter for the adjacency matrix
128+
*
129+
* @return the adjacency matrix
130+
*/
73131
privateint[][]adjacency() {
74132
returnthis._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
privatebooleanadjacencyOfEdgeDoesExist(intfrom,intto) {
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
publicbooleanvertexDoesExist(intaVertex) {
82153
if (aVertex >=0 &&aVertex <this.numberOfVertices()) {
83154
returntrue;
@@ -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
publicbooleanedgeDoesExist(intfrom,intto) {
90168
if (this.vertexDoesExist(from) &&this.vertexDoesExist(to)) {
91169
return (this.adjacencyOfEdgeDoesExist(from,to));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp