Skip to content

Commit 0033eb3

Browse files
committed
improves vertex validation in BFS and DFS
1 parent f220471 commit 0033eb3

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

src/main/java/edu/princeton/cs/algs4/BreadthFirstDirectedPaths.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,9 @@ private void validateVertices(Iterable<Integer> vertices) {
185185
int V = marked.length;
186186
for (Integer v : vertices) {
187187
if (v == null) {
188-
throw new IllegalArgumentException("vertex " + v + " is null");
189-
}
190-
if (v < 0 || v >= V) {
191-
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
188+
throw new IllegalArgumentException("vertex is null");
192189
}
190+
validateVertex(v);
193191
}
194192
}
195193

src/main/java/edu/princeton/cs/algs4/BreadthFirstPaths.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,9 @@ private void validateVertices(Iterable<Integer> vertices) {
248248
int V = marked.length;
249249
for (Integer v : vertices) {
250250
if (v == null) {
251-
throw new IllegalArgumentException("vertex " + v + " is null");
252-
}
253-
if (v < 0 || v >= V) {
254-
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
251+
throw new IllegalArgumentException("vertex is null");
255252
}
253+
validateVertex(v);
256254
}
257255
}
258256

src/main/java/edu/princeton/cs/algs4/DirectedDFS.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,9 @@ private void validateVertices(Iterable<Integer> vertices) {
121121
int V = marked.length;
122122
for (Integer v : vertices) {
123123
if (v == null) {
124-
throw new IllegalArgumentException("vertex " + v + " is null");
125-
}
126-
if (v < 0 || v >= V) {
127-
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
124+
throw new IllegalArgumentException("vertex is null");
128125
}
126+
validateVertex(v);
129127
}
130128
}
131129

0 commit comments

Comments
 (0)