Skip to content

Add Topological Sort algorithm using DFS in GraphSearch #6263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<source>21</source>
<target>21</target>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Xlint:-auxiliaryclass</arg>
<arg>-Werror</arg>
</compilerArgs>
<release>21</release>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siriak what's your opinion on this one?

<!-- <source>21</source>-->
<!-- <target>21</target>-->
<!-- <compilerArgs>-->
<!-- <arg>-Xlint:all</arg>-->
<!-- <arg>-Xlint:-auxiliaryclass</arg>-->
<!--&lt;!&ndash; <arg>-Werror</arg>&ndash;&gt;-->
<!-- </compilerArgs>-->
</configuration>
</plugin>
<plugin>
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.thealgorithms.sorts.TopologicalSort.Graph;
import java.util.LinkedList;
Expand Down Expand Up @@ -59,4 +60,18 @@ public void failureTest() {
+ "Back edge: 6 -> 2";
assertEquals(exception.getMessage(), expected);
}
@Test
void testEmptyGraph() {
Graph graph = new Graph();
LinkedList<String> sorted = TopologicalSort.sort(graph);
assertTrue(sorted.isEmpty());
}
@Test
void testSingleNode() {
Graph graph = new Graph();
graph.addEdge("A", "");
LinkedList<String> sorted = TopologicalSort.sort(graph);
assertEquals(1, sorted.size());
assertEquals("A", sorted.getFirst());
}
}