Skip to content

Commit d4e9179

Browse files
committed
style: Apply Java code style guidelines
1 parent ef0c8eb commit d4e9179

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/main/java/com/thealgorithms/graph/TopologicalSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private TopologicalSort() {
5252
* @throws IllegalArgumentException if edges is null or contains invalid
5353
* vertices
5454
*/
55-
public static int[] sort(int numVertices, List<int[]> edges) {
55+
public static int[] sort(int numVertices, Iterable<int[]> edges) {
5656
if (edges == null) {
5757
throw new IllegalArgumentException("Edge list must not be null");
5858
}

src/test/java/com/thealgorithms/graph/TopologicalSortTest.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertThrows;
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88

9+
import java.util.ArrayList;
910
import java.util.Arrays;
1011
import java.util.List;
1112
import org.junit.jupiter.api.DisplayName;
@@ -16,16 +17,19 @@ class TopologicalSortTest {
1617
@Test
1718
@DisplayName("Simple DAG returns correct ordering")
1819
void simpleDAG() {
19-
List<int[]> edges = Arrays.asList(new int[] { 0, 1 }, new int[] { 0, 2 }, new int[] { 1, 3 },
20-
new int[] { 2, 3 });
20+
List<int[]> edges = new ArrayList<>();
21+
edges.add(new int[] { 0, 1 });
22+
edges.add(new int[] { 0, 2 });
23+
edges.add(new int[] { 1, 3 });
24+
edges.add(new int[] { 2, 3 });
2125
int[] result = TopologicalSort.sort(4, edges);
2226
assertArrayEquals(new int[] { 0, 1, 2, 3 }, result);
2327
}
2428

2529
@Test
2630
@DisplayName("Empty graph returns valid ordering")
2731
void emptyGraph() {
28-
List<int[]> edges = Arrays.asList();
32+
List<int[]> edges = new ArrayList<>();
2933
int[] result = TopologicalSort.sort(3, edges);
3034
assertEquals(3, result.length);
3135
// Any permutation is valid for empty graph
@@ -35,7 +39,10 @@ void emptyGraph() {
3539
@Test
3640
@DisplayName("Graph with cycle returns null")
3741
void graphWithCycle() {
38-
List<int[]> edges = Arrays.asList(new int[] { 0, 1 }, new int[] { 1, 2 }, new int[] { 2, 0 });
42+
List<int[]> edges = new ArrayList<>();
43+
edges.add(new int[] { 0, 1 });
44+
edges.add(new int[] { 1, 2 });
45+
edges.add(new int[] { 2, 0 });
3946
assertNull(TopologicalSort.sort(3, edges));
4047
}
4148

@@ -44,11 +51,11 @@ void graphWithCycle() {
4451
void coursePrerequisites() {
4552
// Example: Course prerequisites where edge [a,b] means course a must be taken
4653
// before b
47-
List<int[]> edges = Arrays.asList(new int[] { 1, 0 }, // Calculus I -> Calculus II
48-
new int[] { 2, 0 }, // Linear Algebra -> Calculus II
49-
new int[] { 1, 3 }, // Calculus I -> Differential Equations
50-
new int[] { 2, 3 } // Linear Algebra -> Differential Equations
51-
);
54+
List<int[]> edges = new ArrayList<>();
55+
edges.add(new int[] { 1, 0 }); // Calculus I -> Calculus II
56+
edges.add(new int[] { 2, 0 }); // Linear Algebra -> Calculus II
57+
edges.add(new int[] { 1, 3 }); // Calculus I -> Differential Equations
58+
edges.add(new int[] { 2, 3 }); // Linear Algebra -> Differential Equations
5259
List<Integer> result = TopologicalSort.sortAndDetectCycle(4, edges);
5360
// Either [1,2,0,3] or [2,1,0,3] is valid
5461
assertEquals(4, result.size());
@@ -62,8 +69,8 @@ void coursePrerequisites() {
6269
@Test
6370
@DisplayName("Invalid vertex throws exception")
6471
void invalidVertex() {
65-
List<int[]> edges = Arrays.asList(new int[] { 0, 5 } // Vertex 5 is invalid for a graph with 3 vertices
66-
);
72+
List<int[]> edges = new ArrayList<>();
73+
edges.add(new int[] { 0, 5 }); // Vertex 5 is invalid for a graph with 3 vertices
6774
assertThrows(IllegalArgumentException.class, () -> TopologicalSort.sort(3, edges));
6875
}
6976

@@ -76,7 +83,10 @@ void nullEdgeList() {
7683
@Test
7784
@DisplayName("sortAndDetectCycle throws exception for cyclic graph")
7885
void detectCycleThrowsException() {
79-
List<int[]> edges = Arrays.asList(new int[] { 0, 1 }, new int[] { 1, 2 }, new int[] { 2, 0 });
86+
List<int[]> edges = new ArrayList<>();
87+
edges.add(new int[] { 0, 1 });
88+
edges.add(new int[] { 1, 2 });
89+
edges.add(new int[] { 2, 0 });
8090
assertThrows(IllegalArgumentException.class, () -> TopologicalSort.sortAndDetectCycle(3, edges));
8191
}
8292
}

0 commit comments

Comments
 (0)