Skip to content

Commit 0152e2a

Browse files
Fix: apply google java format
1 parent b6d0f0e commit 0152e2a

File tree

2 files changed

+19
-67
lines changed

2 files changed

+19
-67
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/TopologicalSortDFS.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,28 @@
1010
/**
1111
* Topological Sort using Depth-First Search (DFS).
1212
*
13-
* <p>
14-
* A topological ordering of a directed acyclic graph (DAG) is a linear ordering
13+
* <p>A topological ordering of a directed acyclic graph (DAG) is a linear ordering
1514
* of its vertices such that for every directed edge u → v, vertex u appears
1615
* before vertex v in the ordering.
1716
*
18-
* <p>
19-
* This implementation uses DFS with a 3-state visited array:
17+
* <p>This implementation uses DFS with a 3-state visited array:
2018
* <ul>
21-
* <li>UNVISITED – node has not been visited yet</li>
22-
* <li>IN_PROGRESS – node is on the current DFS path (used for cycle
23-
* detection)</li>
24-
* <li>DONE – node and all its descendants are fully processed</li>
19+
* <li>UNVISITED – node has not been visited yet</li>
20+
* <li>IN_PROGRESS – node is on the current DFS path (used for cycle detection)</li>
21+
* <li>DONE – node and all its descendants are fully processed</li>
2522
* </ul>
2623
*
27-
* <p>
28-
* Time Complexity: O(V + E), where V = vertices, E = edges
24+
* <p>Time Complexity: O(V + E), where V = vertices, E = edges
2925
* Space Complexity: O(V + E) for the adjacency list and recursion stack
3026
*
31-
* @see <a href="https://en.wikipedia.org/wiki/Topological_sorting">Topological
32-
* Sorting (Wikipedia)</a>
27+
* @see <a href="https://en.wikipedia.org/wiki/Topological_sorting">Topological Sorting (Wikipedia)</a>
3328
*/
34-
public class TopologicalSortDFS {
29+
public final class TopologicalSortDFS {
3530

3631
public TopologicalSortDFS() {
3732
}
3833

39-
private enum VisitState {
40-
UNVISITED,
41-
IN_PROGRESS,
42-
DONE
43-
}
34+
private enum VisitState { UNVISITED, IN_PROGRESS, DONE }
4435

4536
private final Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
4637

@@ -69,8 +60,7 @@ public void addVertex(int v) {
6960
* Performs a topological sort of the graph using DFS.
7061
*
7162
* @return a {@link List} of vertices in topologically sorted order
72-
* @throws IllegalStateException if the graph contains a cycle (i.e., is not a
73-
* DAG)
63+
* @throws IllegalStateException if the graph contains a cycle (i.e., is not a DAG)
7464
*/
7565
public List<Integer> topologicalSort() {
7666
Map<Integer, VisitState> visitState = new HashMap<>();
@@ -107,16 +97,13 @@ private void dfs(int vertex, Map<Integer, VisitState> visitState, Deque<Integer>
10797
for (int neighbor : adjacencyList.get(vertex)) {
10898
VisitState state = visitState.get(neighbor);
10999
if (state == VisitState.IN_PROGRESS) {
110-
// Back edge found → cycle exists → not a DAG
111-
throw new IllegalStateException(
112-
"Graph contains a cycle. Topological sort is not possible.");
100+
throw new IllegalStateException("Graph contains a cycle. Topological sort is not possible.");
113101
}
114102
if (state == VisitState.UNVISITED) {
115103
dfs(neighbor, visitState, stack);
116104
}
117105
}
118106

119-
// All descendants processed — mark done and push to stack
120107
visitState.put(vertex, VisitState.DONE);
121108
stack.push(vertex);
122109
}

src/test/java/com/thealgorithms/datastructures/graphs/TopologicalSortDFSTest.java

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,19 @@
99

1010
/**
1111
* JUnit 5 tests for {@link TopologicalSortDFS}.
12-
*
13-
* Test cases cover:
14-
* - Standard DAG with a clear expected topological order
15-
* - Multiple valid orderings (validated by constraint checking)
16-
* - Graphs with cycles (must throw IllegalStateException)
17-
* - Single-node graph
18-
* - Disconnected graph
1912
*/
2013
class TopologicalSortDFSTest {
2114

22-
/**
23-
* Checks that every edge u → v satisfies: u appears before v in the result.
24-
*/
2515
private static void assertValidTopologicalOrder(List<Integer> order, int[][] edges) {
2616
for (int[] edge : edges) {
2717
int u = edge[0];
2818
int v = edge[1];
29-
assertTrue(
30-
order.indexOf(u) < order.indexOf(v),
31-
"Expected " + u + " to appear before " + v + " in topological order. Got: " + order);
19+
assertTrue(order.indexOf(u) < order.indexOf(v), "Expected " + u + " to appear before " + v + " in topological order. Got: " + order);
3220
}
3321
}
3422

3523
@Test
3624
void testSimpleLinearGraph() {
37-
// 0 → 1 → 2 → 3
3825
TopologicalSortDFS graph = new TopologicalSortDFS();
3926
graph.addEdge(0, 1);
4027
graph.addEdge(1, 2);
@@ -46,15 +33,9 @@ void testSimpleLinearGraph() {
4633

4734
@Test
4835
void testDAGWithMultiplePaths() {
49-
// Graph:
50-
// 5 → 2
51-
// 5 → 0
52-
// 4 → 0
53-
// 4 → 1
54-
// 2 → 3
55-
// 3 → 1
36+
// 5 → 2, 5 → 0, 4 → 0, 4 → 1, 2 → 3, 3 → 1
5637
TopologicalSortDFS graph = new TopologicalSortDFS();
57-
int[][] edges = { { 5, 2 }, { 5, 0 }, { 4, 0 }, { 4, 1 }, { 2, 3 }, { 3, 1 } };
38+
int[][] edges = {{5, 2}, {5, 0}, {4, 0}, {4, 1}, {2, 3}, {3, 1}};
5839
for (int[] edge : edges) {
5940
graph.addEdge(edge[0], edge[1]);
6041
}
@@ -66,12 +47,9 @@ void testDAGWithMultiplePaths() {
6647

6748
@Test
6849
void testBuildOrderDAG() {
69-
// Simulates build dependency: A depends on nothing, B depends on A, C depends
70-
// on A and B
71-
// A=0, B=1, C=2, D=3
7250
// 0 → 1, 0 → 2, 1 → 3, 2 → 3
7351
TopologicalSortDFS graph = new TopologicalSortDFS();
74-
int[][] edges = { { 0, 1 }, { 0, 2 }, { 1, 3 }, { 2, 3 } };
52+
int[][] edges = {{0, 1}, {0, 2}, {1, 3}, {2, 3}};
7553
for (int[] edge : edges) {
7654
graph.addEdge(edge[0], edge[1]);
7755
}
@@ -92,9 +70,8 @@ void testSingleVertex() {
9270

9371
@Test
9472
void testDisconnectedGraph() {
95-
// Two disconnected components: 0 → 1 and 2 → 3
9673
TopologicalSortDFS graph = new TopologicalSortDFS();
97-
int[][] edges = { { 0, 1 }, { 2, 3 } };
74+
int[][] edges = {{0, 1}, {2, 3}};
9875
for (int[] edge : edges) {
9976
graph.addEdge(edge[0], edge[1]);
10077
}
@@ -106,43 +83,31 @@ void testDisconnectedGraph() {
10683

10784
@Test
10885
void testSimpleCycleThrowsException() {
109-
// 0 → 1 → 2 → 0 (cycle)
11086
TopologicalSortDFS graph = new TopologicalSortDFS();
11187
graph.addEdge(0, 1);
11288
graph.addEdge(1, 2);
11389
graph.addEdge(2, 0);
11490

115-
assertThrows(
116-
IllegalStateException.class,
117-
graph::topologicalSort,
118-
"Expected IllegalStateException for cyclic graph");
91+
assertThrows(IllegalStateException.class, graph::topologicalSort, "Expected IllegalStateException for cyclic graph");
11992
}
12093

12194
@Test
12295
void testSelfLoopThrowsException() {
123-
// A self-loop is a trivial cycle: 0 → 0
12496
TopologicalSortDFS graph = new TopologicalSortDFS();
12597
graph.addEdge(0, 0);
12698

127-
assertThrows(
128-
IllegalStateException.class,
129-
graph::topologicalSort,
130-
"Expected IllegalStateException for self-loop");
99+
assertThrows(IllegalStateException.class, graph::topologicalSort, "Expected IllegalStateException for self-loop");
131100
}
132101

133102
@Test
134103
void testLargerCycleThrowsException() {
135-
// 0 → 1 → 2 → 3 → 4 → 2 (cycle at 2 → 3 → 4 → 2)
136104
TopologicalSortDFS graph = new TopologicalSortDFS();
137105
graph.addEdge(0, 1);
138106
graph.addEdge(1, 2);
139107
graph.addEdge(2, 3);
140108
graph.addEdge(3, 4);
141109
graph.addEdge(4, 2);
142110

143-
assertThrows(
144-
IllegalStateException.class,
145-
graph::topologicalSort,
146-
"Expected IllegalStateException for graph with cycle");
111+
assertThrows(IllegalStateException.class, graph::topologicalSort, "Expected IllegalStateException for graph with cycle");
147112
}
148113
}

0 commit comments

Comments
 (0)