Skip to content

Commit fe08e48

Browse files
committed
Added Topological Sort using DFS
1 parent 9355359 commit fe08e48

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* Implementation of Topological Sort using Depth-First Search (DFS).
9+
*
10+
* <p>Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering
11+
* of vertices such that for every directed edge u → v, vertex u comes before v
12+
* in the ordering.
13+
*/
14+
public final class TopologicalSortDFS {
15+
16+
// Private constructor to prevent instantiation
17+
private TopologicalSortDFS() {
18+
throw new AssertionError("Cannot instantiate utility class");
19+
}
20+
21+
/**
22+
* Performs topological sorting on a directed acyclic graph.
23+
*
24+
* @param vertices the number of vertices in the graph
25+
* @param adjacencyList the adjacency list representing the graph
26+
* @return a list containing vertices in topologically sorted order
27+
*/
28+
public static List<Integer> topologicalSort(int vertices, List<List<Integer>> adjacencyList) {
29+
boolean[] visited = new boolean[vertices];
30+
Stack<Integer> stack = new Stack<>();
31+
32+
for (int i = 0; i < vertices; i++) {
33+
if (!visited[i]) {
34+
dfs(i, visited, stack, adjacencyList);
35+
}
36+
}
37+
38+
List<Integer> result = new ArrayList<>();
39+
while (!stack.isEmpty()) {
40+
result.add(stack.pop());
41+
}
42+
return result;
43+
}
44+
45+
private static void dfs(int node, boolean[] visited, Stack<Integer> stack, List<List<Integer>> adjacencyList) {
46+
visited[node] = true;
47+
for (int neighbor : adjacencyList.get(node)) {
48+
if (!visited[neighbor]) {
49+
dfs(neighbor, visited, stack, adjacencyList);
50+
}
51+
}
52+
stack.push(node);
53+
}
54+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
/**
11+
* Unit tests for {@link TopologicalSortDFS}.
12+
*/
13+
public final class TopologicalSortDFSTest {
14+
15+
@Test
16+
void testSimpleGraph() {
17+
int vertices = 6;
18+
List<List<Integer>> adjacencyList = new ArrayList<>();
19+
for (int i = 0; i < vertices; i++) {
20+
adjacencyList.add(new ArrayList<>());
21+
}
22+
23+
adjacencyList.get(5).add(2);
24+
adjacencyList.get(5).add(0);
25+
adjacencyList.get(4).add(0);
26+
adjacencyList.get(4).add(1);
27+
adjacencyList.get(2).add(3);
28+
adjacencyList.get(3).add(1);
29+
30+
List<Integer> result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList);
31+
32+
// A valid topological order is one of the possible ones
33+
List<Integer> expected = Arrays.asList(5, 4, 2, 3, 1, 0);
34+
Assertions.assertTrue(result.containsAll(expected) && expected.containsAll(result));
35+
}
36+
37+
@Test
38+
void testEmptyGraph() {
39+
int vertices = 0;
40+
List<List<Integer>> adjacencyList = new ArrayList<>();
41+
List<Integer> result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList);
42+
Assertions.assertTrue(result.isEmpty());
43+
}
44+
}

0 commit comments

Comments
 (0)