@@ -23,114 +23,120 @@ class JohnsonsAlgorithmTest {
23
23
*/
24
24
@ Test
25
25
void testSimpleGraph () {
26
- // Test case for a simple graph without negative edges
27
26
double [][] graph = {{0 , 4 , INF , INF }, {INF , 0 , 1 , INF }, {INF , INF , 0 , 2 }, {INF , INF , INF , 0 }};
28
-
29
27
double [][] result = JohnsonsAlgorithm .johnsonAlgorithm (graph );
30
-
31
28
double [][] expected = {{0 , 4 , 5 , 7 }, {INF , 0 , 1 , 3 }, {INF , INF , 0 , 2 }, {INF , INF , INF , 0 }};
32
-
33
29
assertArrayEquals (expected , result );
34
30
}
35
31
36
32
/**
37
- * Tests Johnson's Algorithm on a graph with negative edges but no
38
- * negative weight cycles. Verifies the algorithm handles negative
39
- * edge weights correctly.
33
+ * Tests Johnson's Algorithm on a graph with negative edges but no negative weight cycles.
40
34
*/
41
35
@ Test
42
36
void testGraphWithNegativeEdges () {
43
- // Graph with negative edges but no negative weight cycles
44
37
double [][] graph = {{0 , -1 , 4 }, {INF , 0 , 3 }, {INF , INF , 0 }};
45
-
46
38
double [][] result = JohnsonsAlgorithm .johnsonAlgorithm (graph );
47
-
48
39
double [][] expected = {{0 , INF , 4 }, {INF , 0 , 3 }, {INF , INF , 0 }};
49
-
50
40
assertArrayEquals (expected , result );
51
41
}
52
42
53
43
/**
54
- * Tests the behavior of Johnson's Algorithm on a graph with a negative
55
- * weight cycle. Expects an IllegalArgumentException to be thrown
56
- * due to the presence of the cycle.
44
+ * Tests Johnson's Algorithm on a graph with a negative weight cycle.
57
45
*/
58
46
@ Test
59
47
void testNegativeWeightCycle () {
60
- // Graph with a negative weight cycle
61
48
double [][] graph = {{0 , 1 , INF }, {INF , 0 , -1 }, {-1 , INF , 0 }};
62
-
63
- // Johnson's algorithm should throw an exception when a negative cycle is detected
64
- assertThrows (IllegalArgumentException .class , () -> { JohnsonsAlgorithm .johnsonAlgorithm (graph ); });
49
+ assertThrows (IllegalArgumentException .class , () -> JohnsonsAlgorithm .johnsonAlgorithm (graph ));
65
50
}
66
51
67
52
/**
68
- * Tests Dijkstra's algorithm as a part of Johnson's algorithm implementation
69
- * on a small graph. Verifies that the shortest path is correctly calculated.
53
+ * Tests Dijkstra's algorithm on a small graph as part of Johnson's Algorithm.
70
54
*/
71
55
@ Test
72
56
void testDijkstra () {
73
- // Testing Dijkstra's algorithm with a small graph
74
57
double [][] graph = {{0 , 1 , 2 }, {INF , 0 , 3 }, {INF , INF , 0 }};
75
-
76
- double [] modifiedWeights = {0 , 0 , 0 }; // No reweighting in this simple case
77
-
58
+ double [] modifiedWeights = {0 , 0 , 0 };
78
59
double [] result = JohnsonsAlgorithm .dijkstra (graph , 0 , modifiedWeights );
79
60
double [] expected = {0 , 1 , 2 };
80
-
81
61
assertArrayEquals (expected , result );
82
62
}
83
63
84
64
/**
85
65
* Tests the conversion of an adjacency matrix to an edge list.
86
- * Verifies that the conversion process generates the correct edge list.
87
66
*/
88
67
@ Test
89
68
void testEdgeListConversion () {
90
- // Test the conversion of adjacency matrix to edge list
91
69
double [][] graph = {{0 , 5 , INF }, {INF , 0 , 2 }, {INF , INF , 0 }};
92
-
93
- // Running convertToEdgeList
94
70
double [][] edges = JohnsonsAlgorithm .convertToEdgeList (graph );
95
-
96
- // Expected edge list: (0 -> 1, weight 5), (1 -> 2, weight 2)
97
71
double [][] expected = {{0 , 1 , 5 }, {1 , 2 , 2 }};
98
-
99
- // Verify the edge list matches the expected values
100
72
assertArrayEquals (expected , edges );
101
73
}
102
74
103
75
/**
104
- * Tests the reweighting of a graph as a part of Johnson's Algorithm.
105
- * Verifies that the reweighted graph produces correct results.
76
+ * Tests the reweighting of a graph.
106
77
*/
107
78
@ Test
108
79
void testReweightGraph () {
109
- // Test reweighting of the graph
110
80
double [][] graph = {{0 , 2 , 9 }, {INF , 0 , 1 }, {INF , INF , 0 }};
111
- double [] modifiedWeights = {1 , 2 , 3 }; // Arbitrary weight function
112
-
81
+ double [] modifiedWeights = {1 , 2 , 3 };
113
82
double [][] reweightedGraph = JohnsonsAlgorithm .reweightGraph (graph , modifiedWeights );
114
-
115
- // Expected reweighted graph:
116
83
double [][] expected = {{0 , 1 , 7 }, {INF , 0 , 0 }, {INF , INF , 0 }};
117
-
118
84
assertArrayEquals (expected , reweightedGraph );
119
85
}
120
86
121
87
/**
122
- * Tests the minDistance method used in Dijkstra's algorithm to find
123
- * the vertex with the minimum distance that has not yet been visited.
88
+ * Tests the minDistance method used in Dijkstra's algorithm.
124
89
*/
125
90
@ Test
126
91
void testMinDistance () {
127
- // Test minDistance method
128
92
double [] dist = {INF , 3 , 1 , INF };
129
93
boolean [] visited = {false , false , false , false };
130
-
131
94
int minIndex = JohnsonsAlgorithm .minDistance (dist , visited );
132
-
133
- // The vertex with minimum distance is vertex 2 with a distance of 1
134
95
assertEquals (2 , minIndex );
135
96
}
97
+
98
+ /**
99
+ * Tests Johnson's Algorithm on a graph where all vertices are disconnected.
100
+ */
101
+ @ Test
102
+ void testDisconnectedGraph () {
103
+ double [][] graph = {{0 , INF , INF }, {INF , 0 , INF }, {INF , INF , 0 }};
104
+ double [][] result = JohnsonsAlgorithm .johnsonAlgorithm (graph );
105
+ double [][] expected = {{0 , INF , INF }, {INF , 0 , INF }, {INF , INF , 0 }};
106
+ assertArrayEquals (expected , result );
107
+ }
108
+
109
+ /**
110
+ * Tests Johnson's Algorithm on a fully connected graph.
111
+ */
112
+ @ Test
113
+ void testFullyConnectedGraph () {
114
+ double [][] graph = {{0 , 1 , 2 }, {1 , 0 , 1 }, {2 , 1 , 0 }};
115
+ double [][] result = JohnsonsAlgorithm .johnsonAlgorithm (graph );
116
+ double [][] expected = {{0 , 1 , 2 }, {1 , 0 , 1 }, {2 , 1 , 0 }};
117
+ assertArrayEquals (expected , result );
118
+ }
119
+
120
+ /**
121
+ * Tests Dijkstra's algorithm on a graph with multiple shortest paths.
122
+ */
123
+ @ Test
124
+ void testDijkstraMultipleShortestPaths () {
125
+ double [][] graph = {{0 , 1 , 2 , INF }, {INF , 0 , INF , 1 }, {INF , INF , 0 , 1 }, {INF , INF , INF , 0 }};
126
+ double [] modifiedWeights = {0 , 0 , 0 , 0 };
127
+ double [] result = JohnsonsAlgorithm .dijkstra (graph , 0 , modifiedWeights );
128
+ double [] expected = {0 , 1 , 2 , 2 };
129
+ assertArrayEquals (expected , result );
130
+ }
131
+
132
+ /**
133
+ * Tests Johnson's Algorithm with a graph where all edge weights are zero.
134
+ */
135
+ @ Test
136
+ void testGraphWithZeroWeights () {
137
+ double [][] graph = {{0 , 0 , 0 }, {0 , 0 , 0 }, {0 , 0 , 0 }};
138
+ double [][] result = JohnsonsAlgorithm .johnsonAlgorithm (graph );
139
+ double [][] expected = {{0 , INF , INF }, {INF , 0 , INF }, {INF , INF , 0 }};
140
+ assertArrayEquals (expected , result );
141
+ }
136
142
}
0 commit comments