You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 56558d2
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: datastructure-algorithm-java-examples/src/main/java/com/hellokoding/datastructure/graph/ShortestPathByBFS.java
+5-6Lines changed: 5 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,12 @@ public class ShortestPathByBFS {
Copy file name to clipboardExpand all lines: datastructure-algorithm-java-examples/src/main/java/com/hellokoding/datastructure/graph/ShortestPathByBellmanFord.java
+21-14Lines changed: 21 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -10,13 +10,11 @@ static Object[] shortestPathByBellmanFord(GraphWeightedByAdjacencyList g, int so
10
10
int[] distances = newint[g.getV()];
11
11
int[] predecessors = newint[g.getV()];
12
12
13
-
distances[source] = 0;
14
13
for (intv = 0; v < g.getV(); v++) {
15
-
if (v != source) {
16
-
distances[v] = INFINITE;
17
-
predecessors[v] = UNDEFINED;
18
-
}
14
+
distances[v] = INFINITE;
15
+
predecessors[v] = UNDEFINED;
19
16
}
17
+
distances[source] = 0;
20
18
21
19
for (inti = 1; i < g.getV(); i++) {
22
20
for (intv = 0; v < g.getV(); v++) {
@@ -36,7 +34,8 @@ static Object[] shortestPathByBellmanFord(GraphWeightedByAdjacencyList g, int so
36
34
if (distances[v] == INFINITE) continue;
37
35
intalt = distances[v] + w.weight;
38
36
if (alt < distances[w.vertex]) {
39
-
thrownewRuntimeException("Graph contains a negative-weight cycle");
37
+
System.out.println("The input graph contains a negative-weight cycle");
38
+
returnnewObject[]{};
40
39
}
41
40
}
42
41
}
@@ -45,6 +44,8 @@ static Object[] shortestPathByBellmanFord(GraphWeightedByAdjacencyList g, int so
Copy file name to clipboardExpand all lines: datastructure-algorithm-java-examples/src/main/java/com/hellokoding/datastructure/graph/ShortestPathByDijkstra.java
+8-10Lines changed: 8 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -27,14 +27,12 @@ static Object[] shortestPathByDijkstra(GraphWeightedByAdjacencyList g, int sourc
Copy file name to clipboardExpand all lines: datastructure-algorithm-java-examples/src/main/java/com/hellokoding/datastructure/graph/ShortestPathByDijkstraWithMinHeap.java
+8-11Lines changed: 8 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -13,15 +13,12 @@ static Object[] shortestPathByDijkstra(GraphWeightedByAdjacencyList g, int sourc
Copy file name to clipboardExpand all lines: datastructure-algorithm-java-examples/src/main/java/com/hellokoding/datastructure/graph/ShortestPathByFloydWarshall.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ public static int[][] shortestPathByBFSFloydWarshall(int[][] g) {
17
17
}
18
18
19
19
if (distances[i][i] < 0) {
20
-
System.out.println("Graph contains a negative-weight cycle");
20
+
System.out.println("The input graph contains a negative-weight cycle");
0 commit comments