Skip to content

Commit d4d5095

Browse files
authored
Add files via upload
1 parent 3555941 commit d4d5095

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class DijkstraTest {
11+
12+
@Test
13+
void testSingleNodeGraph() {
14+
// Test case where graph has a single node
15+
List<List<Dijkstra.Edge>> graph = new ArrayList<>();
16+
graph.add(new ArrayList<>());
17+
18+
Dijkstra dijkstra = new Dijkstra();
19+
int[] result = dijkstra.dijkstra(graph, 0);
20+
21+
// Since it's the only node, the distance to itself is 0
22+
assertArrayEquals(new int[]{0}, result);
23+
}
24+
25+
@Test
26+
void testDisconnectedGraph() {
27+
// Test case where graph is disconnected
28+
int V = 3; // 3 nodes in the graph
29+
List<List<Dijkstra.Edge>> graph = new ArrayList<>();
30+
for (int i = 0; i < V; i++) {
31+
graph.add(new ArrayList<>());
32+
}
33+
34+
Dijkstra dijkstra = new Dijkstra();
35+
int[] result = dijkstra.dijkstra(graph, 0);
36+
37+
// Node 0 can only reach itself, others should be unreachable (infinity)
38+
assertArrayEquals(new int[]{0, Integer.MAX_VALUE, Integer.MAX_VALUE}, result);
39+
}
40+
41+
@Test
42+
void testSimpleGraph() {
43+
// Test case for a simple connected graph
44+
int V = 4;
45+
List<List<Dijkstra.Edge>> graph = new ArrayList<>();
46+
for (int i = 0; i < V; i++) {
47+
graph.add(new ArrayList<>());
48+
}
49+
50+
// Add edges to the graph
51+
graph.get(0).add(new Dijkstra.Edge(1, 1));
52+
graph.get(0).add(new Dijkstra.Edge(2, 4));
53+
graph.get(1).add(new Dijkstra.Edge(2, 2));
54+
graph.get(1).add(new Dijkstra.Edge(3, 6));
55+
graph.get(2).add(new Dijkstra.Edge(3, 3));
56+
57+
Dijkstra dijkstra = new Dijkstra();
58+
int[] result = dijkstra.dijkstra(graph, 0);
59+
60+
// Expected shortest distances from node 0 to all other nodes
61+
assertArrayEquals(new int[]{0, 1, 3, 6}, result);
62+
}
63+
64+
@Test
65+
void testInvalidSourceNode() {
66+
int V = 3;
67+
List<List<Dijkstra.Edge>> graph = new ArrayList<>();
68+
for (int i = 0; i < V; i++) {
69+
graph.add(new ArrayList<>());
70+
}
71+
72+
Dijkstra dijkstra = new Dijkstra();
73+
74+
assertThrows(IndexOutOfBoundsException.class, () -> {
75+
dijkstra.dijkstra(graph, 5);
76+
});
77+
}
78+
79+
@Test
80+
void testCyclicGraph() {
81+
// Test case for a cyclic graph
82+
int V = 3;
83+
List<List<Dijkstra.Edge>> graph = new ArrayList<>();
84+
for (int i = 0; i < V; i++) {
85+
graph.add(new ArrayList<>());
86+
}
87+
88+
graph.get(0).add(new Dijkstra.Edge(1, 2));
89+
graph.get(1).add(new Dijkstra.Edge(2, 3));
90+
graph.get(2).add(new Dijkstra.Edge(0, 4));
91+
92+
Dijkstra dijkstra = new Dijkstra();
93+
int[] result = dijkstra.dijkstra(graph, 0);
94+
95+
assertArrayEquals(new int[]{0, 2, 5}, result);
96+
}
97+
}

0 commit comments

Comments
 (0)