@@ -15,45 +15,45 @@ public class DijkstraAlgorithmTest {
1515 @ BeforeEach
1616 void setUp () {
1717 graph = new int [][] {
18- { 0 , 4 , 0 , 0 , 0 , 0 , 0 , 8 , 0 },
19- { 4 , 0 , 8 , 0 , 0 , 0 , 0 , 11 , 0 },
20- { 0 , 8 , 0 , 7 , 0 , 4 , 0 , 0 , 2 },
21- { 0 , 0 , 7 , 0 , 9 , 14 , 0 , 0 , 0 },
22- { 0 , 0 , 0 , 9 , 0 , 10 , 0 , 0 , 0 },
23- { 0 , 0 , 4 , 14 , 10 , 0 , 2 , 0 , 0 },
24- { 0 , 0 , 0 , 0 , 0 , 2 , 0 , 1 , 6 },
25- { 8 , 11 , 0 , 0 , 0 , 0 , 1 , 0 , 7 },
26- { 0 , 0 , 2 , 0 , 0 , 0 , 6 , 7 , 0 },
18+ { 0 , 4 , 0 , 0 , 0 , 0 , 0 , 8 , 0 },
19+ { 4 , 0 , 8 , 0 , 0 , 0 , 0 , 11 , 0 },
20+ { 0 , 8 , 0 , 7 , 0 , 4 , 0 , 0 , 2 },
21+ { 0 , 0 , 7 , 0 , 9 , 14 , 0 , 0 , 0 },
22+ { 0 , 0 , 0 , 9 , 0 , 10 , 0 , 0 , 0 },
23+ { 0 , 0 , 4 , 14 , 10 , 0 , 2 , 0 , 0 },
24+ { 0 , 0 , 0 , 0 , 0 , 2 , 0 , 1 , 6 },
25+ { 8 , 11 , 0 , 0 , 0 , 0 , 1 , 0 , 7 },
26+ { 0 , 0 , 2 , 0 , 0 , 0 , 6 , 7 , 0 },
2727 };
2828
2929 dijkstraAlgorithm = new DijkstraAlgorithm (graph .length );
3030 }
3131
3232 @ Test
3333 void testRunAlgorithm () {
34- int [] expectedDistances = { 0 , 4 , 12 , 19 , 21 , 11 , 9 , 8 , 14 };
34+ int [] expectedDistances = {0 , 4 , 12 , 19 , 21 , 11 , 9 , 8 , 14 };
3535 assertArrayEquals (expectedDistances , dijkstraAlgorithm .run (graph , 0 ));
3636 }
3737
3838 @ Test
3939 void testGraphWithDisconnectedNodes () {
4040 int [][] disconnectedGraph = {
41- { 0 , 3 , 0 , 0 }, { 3 , 0 , 1 , 0 }, { 0 , 1 , 0 , 0 }, { 0 , 0 , 0 , 0 } // Node 3 is disconnected
41+ { 0 , 3 , 0 , 0 }, {3 , 0 , 1 , 0 }, {0 , 1 , 0 , 0 }, {0 , 0 , 0 , 0 } // Node 3 is disconnected
4242 };
4343
4444 DijkstraAlgorithm dijkstraDisconnected = new DijkstraAlgorithm (disconnectedGraph .length );
4545
4646 // Testing from vertex 0
47- int [] expectedDistances = { 0 , 3 , 4 , Integer .MAX_VALUE }; // Node 3 is unreachable
47+ int [] expectedDistances = {0 , 3 , 4 , Integer .MAX_VALUE }; // Node 3 is unreachable
4848 assertArrayEquals (expectedDistances , dijkstraDisconnected .run (disconnectedGraph , 0 ));
4949 }
5050
5151 @ Test
5252 void testSingleVertexGraph () {
53- int [][] singleVertexGraph = { { 0 } };
53+ int [][] singleVertexGraph = {{ 0 } };
5454 DijkstraAlgorithm dijkstraSingleVertex = new DijkstraAlgorithm (1 );
5555
56- int [] expectedDistances = { 0 }; // The only vertex's distance to itself is 0
56+ int [] expectedDistances = {0 }; // The only vertex's distance to itself is 0
5757 assertArrayEquals (expectedDistances , dijkstraSingleVertex .run (singleVertexGraph , 0 ));
5858 }
5959
@@ -67,17 +67,12 @@ void testInvalidSourceVertex() {
6767 void testLinearGraph () {
6868 // Linear graph: 0 - 1 - 2 - 3
6969 // with weights: 2 3 4
70- int [][] linearGraph = {
71- { 0 , 2 , 0 , 0 },
72- { 2 , 0 , 3 , 0 },
73- { 0 , 3 , 0 , 4 },
74- { 0 , 0 , 4 , 0 }
75- };
70+ int [][] linearGraph = {{0 , 2 , 0 , 0 }, {2 , 0 , 3 , 0 }, {0 , 3 , 0 , 4 }, {0 , 0 , 4 , 0 }};
7671
7772 DijkstraAlgorithm dijkstraLinear = new DijkstraAlgorithm (4 );
7873 int [] distances = dijkstraLinear .run (linearGraph , 0 );
7974
80- assertArrayEquals (new int [] { 0 , 2 , 5 , 9 }, distances );
75+ assertArrayEquals (new int [] {0 , 2 , 5 , 9 }, distances );
8176 }
8277
8378 @ Test
@@ -88,107 +83,79 @@ void testStarTopology() {
8883 // 3(4)-0-2(3)
8984 // |
9085 // 4(5)
91- int [][] starGraph = {
92- { 0 , 2 , 3 , 4 , 5 },
93- { 2 , 0 , 0 , 0 , 0 },
94- { 3 , 0 , 0 , 0 , 0 },
95- { 4 , 0 , 0 , 0 , 0 },
96- { 5 , 0 , 0 , 0 , 0 }
97- };
86+ int [][] starGraph = {{0 , 2 , 3 , 4 , 5 }, {2 , 0 , 0 , 0 , 0 }, {3 , 0 , 0 , 0 , 0 }, {4 , 0 , 0 , 0 , 0 }, {5 , 0 , 0 , 0 , 0 }};
9887
9988 DijkstraAlgorithm dijkstraStar = new DijkstraAlgorithm (5 );
10089 int [] distances = dijkstraStar .run (starGraph , 0 );
10190
102- assertArrayEquals (new int [] { 0 , 2 , 3 , 4 , 5 }, distances );
91+ assertArrayEquals (new int [] {0 , 2 , 3 , 4 , 5 }, distances );
10392 }
10493
10594 @ Test
10695 void testCompleteGraphK4 () {
10796 // Complete graph K4 with varying weights
108- int [][] completeGraph = {
109- { 0 , 1 , 2 , 3 },
110- { 1 , 0 , 4 , 5 },
111- { 2 , 4 , 0 , 6 },
112- { 3 , 5 , 6 , 0 }
113- };
97+ int [][] completeGraph = {{0 , 1 , 2 , 3 }, {1 , 0 , 4 , 5 }, {2 , 4 , 0 , 6 }, {3 , 5 , 6 , 0 }};
11498
11599 DijkstraAlgorithm dijkstraComplete = new DijkstraAlgorithm (4 );
116100 int [] distances = dijkstraComplete .run (completeGraph , 0 );
117101
118102 // Direct paths from 0 are shortest
119- assertArrayEquals (new int [] { 0 , 1 , 2 , 3 }, distances );
103+ assertArrayEquals (new int [] {0 , 1 , 2 , 3 }, distances );
120104 }
121105
122106 @ Test
123107 void testDifferentSourceVertex () {
124108 // Test running from different source vertices
125- int [][] simpleGraph = {
126- { 0 , 5 , 0 , 0 },
127- { 5 , 0 , 3 , 0 },
128- { 0 , 3 , 0 , 2 },
129- { 0 , 0 , 2 , 0 }
130- };
109+ int [][] simpleGraph = {{0 , 5 , 0 , 0 }, {5 , 0 , 3 , 0 }, {0 , 3 , 0 , 2 }, {0 , 0 , 2 , 0 }};
131110
132111 DijkstraAlgorithm dijkstra = new DijkstraAlgorithm (4 );
133112
134113 // From vertex 0
135114 int [] distFrom0 = dijkstra .run (simpleGraph , 0 );
136- assertArrayEquals (new int [] { 0 , 5 , 8 , 10 }, distFrom0 );
115+ assertArrayEquals (new int [] {0 , 5 , 8 , 10 }, distFrom0 );
137116
138117 // From vertex 2
139118 int [] distFrom2 = dijkstra .run (simpleGraph , 2 );
140- assertArrayEquals (new int [] { 8 , 3 , 0 , 2 }, distFrom2 );
119+ assertArrayEquals (new int [] {8 , 3 , 0 , 2 }, distFrom2 );
141120
142121 // From vertex 3
143122 int [] distFrom3 = dijkstra .run (simpleGraph , 3 );
144- assertArrayEquals (new int [] { 10 , 5 , 2 , 0 }, distFrom3 );
123+ assertArrayEquals (new int [] {10 , 5 , 2 , 0 }, distFrom3 );
145124 }
146125
147126 @ Test
148127 void testUnitWeightGraph () {
149128 // Graph with all unit weights (like BFS distance)
150- int [][] unitGraph = {
151- { 0 , 1 , 1 , 0 },
152- { 1 , 0 , 1 , 1 },
153- { 1 , 1 , 0 , 1 },
154- { 0 , 1 , 1 , 0 }
155- };
129+ int [][] unitGraph = {{0 , 1 , 1 , 0 }, {1 , 0 , 1 , 1 }, {1 , 1 , 0 , 1 }, {0 , 1 , 1 , 0 }};
156130
157131 DijkstraAlgorithm dijkstraUnit = new DijkstraAlgorithm (4 );
158132 int [] distances = dijkstraUnit .run (unitGraph , 0 );
159133
160- assertArrayEquals (new int [] { 0 , 1 , 1 , 2 }, distances );
134+ assertArrayEquals (new int [] {0 , 1 , 1 , 2 }, distances );
161135 }
162136
163137 @ Test
164138 void testTwoVertexGraph () {
165- int [][] twoVertexGraph = {
166- { 0 , 7 },
167- { 7 , 0 }
168- };
139+ int [][] twoVertexGraph = {{0 , 7 }, {7 , 0 }};
169140
170141 DijkstraAlgorithm dijkstraTwo = new DijkstraAlgorithm (2 );
171142 int [] distances = dijkstraTwo .run (twoVertexGraph , 0 );
172143
173- assertArrayEquals (new int [] { 0 , 7 }, distances );
144+ assertArrayEquals (new int [] {0 , 7 }, distances );
174145 }
175146
176147 @ Test
177148 void testShortcutPath () {
178149 // Graph where direct path is longer than indirect path
179150 // 0 --(10)--> 2
180151 // 0 --(1)--> 1 --(2)--> 2
181- int [][] shortcutGraph = {
182- { 0 , 1 , 10 },
183- { 1 , 0 , 2 },
184- { 10 , 2 , 0 }
185- };
152+ int [][] shortcutGraph = {{0 , 1 , 10 }, {1 , 0 , 2 }, {10 , 2 , 0 }};
186153
187154 DijkstraAlgorithm dijkstraShortcut = new DijkstraAlgorithm (3 );
188155 int [] distances = dijkstraShortcut .run (shortcutGraph , 0 );
189156
190157 // The shortest path to vertex 2 should be 3 (via vertex 1), not 10 (direct)
191- assertArrayEquals (new int [] { 0 , 1 , 3 }, distances );
158+ assertArrayEquals (new int [] {0 , 1 , 3 }, distances );
192159 }
193160
194161 @ Test
@@ -204,15 +171,11 @@ void testSourceToSourceDistanceIsZero() {
204171 @ Test
205172 void testLargeWeights () {
206173 // Graph with large weights
207- int [][] largeWeightGraph = {
208- { 0 , 1000 , 0 },
209- { 1000 , 0 , 2000 },
210- { 0 , 2000 , 0 }
211- };
174+ int [][] largeWeightGraph = {{0 , 1000 , 0 }, {1000 , 0 , 2000 }, {0 , 2000 , 0 }};
212175
213176 DijkstraAlgorithm dijkstraLarge = new DijkstraAlgorithm (3 );
214177 int [] distances = dijkstraLarge .run (largeWeightGraph , 0 );
215178
216- assertArrayEquals (new int [] { 0 , 1000 , 3000 }, distances );
179+ assertArrayEquals (new int [] {0 , 1000 , 3000 }, distances );
217180 }
218181}
0 commit comments