From bd8a16f90b5934483ba98e7953a79695f0ff543c Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 20:59:13 +0530 Subject: [PATCH 01/10] Add Topological Sort algorithm using DFS in GraphSearch --- .../graphsearch/TopologicalSort.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java new file mode 100644 index 000000000000..68e3bd124f39 --- /dev/null +++ b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java @@ -0,0 +1,83 @@ +package com.thealgorithms.graphsearch; + +import java.util.ArrayList; +import java.util.Stack; + +/** + * This class implements the Topological Sort algorithm using Depth-First Search + * for Directed Acyclic Graphs. It prints the nodes in a topological order. + */ + +public class TopologicalSort { + /** + * Main method to test the Topological Sort implementation. + * Creates a sample graph and prints its topological ordering. + * @param args command-line arguments(not used) + */ + + public static void main(String[] args) { + int v = 6; + ArrayList> adjList = new ArrayList<>(); + for(int i=0; i()); + } + + //sample DAG edges + adjList.get(5).add(2); + adjList.get(5).add(0); + adjList.get(4).add(0); + adjList.get(4).add(1); + adjList.get(2).add(3); + adjList.get(3).add(1); + + topoSort(v,adjList); + } + + /** + * Performs the topological sort on the given graph represented by adjacency list. + * Prints the nodes in topologically sorted order. + * + * @param v the number of vertices in the graph + * @param adjList adjacency list representing the graph + */ + + + public static void topoSort (int v, ArrayList> adjList){ + boolean [] visited = new boolean[v]; + Stack stack = new Stack<>(); + + for (int i = 0; i < v; i++) { + if(!visited[i]){ + topoSortUtil(i,visited,stack,adjList); + } + } + + System.out.println("Topological Sort:"); + while (!stack.isEmpty()){ + System.out.print(stack.pop()+" "); + } + System.out.println(); + } + + /** + * Utility method to perform DFS starting from a given node. + * Marks nodes as visited and pushes nodes to stack after visiting all their neighbors. + * + * @param node the current node being visited + * @param visited boolean array tracking visited nodes + * @param stack stack to store the topological order + * @param adjList adjacency list representing the graph + * + */ + + public static void topoSortUtil(int node, boolean[] visited, Stack stack, ArrayList> adjList){ + visited[node] = true; + + for(int neighbour : adjList.get(node)){ + if(!visited[neighbour]){ + topoSortUtil(neighbour,visited,stack,adjList); + } + } + stack.push(node); // add to stack after visiting all edges + } +} From 58f53b2c302a09733e23bc0af2a3b4cdf96cdd6f Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 21:08:22 +0530 Subject: [PATCH 02/10] Add Wikipedia reference link for Topological Sort --- .../java/com/thealgorithms/graphsearch/TopologicalSort.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java index 68e3bd124f39..2bf03f9b4d7f 100644 --- a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java +++ b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java @@ -1,5 +1,7 @@ package com.thealgorithms.graphsearch; +// Topological sort: https://en.wikipedia.org/wiki/Topological_sorting + import java.util.ArrayList; import java.util.Stack; From e050e09f576a39fe211a2abc26ecc3363555bbf1 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 21:39:46 +0530 Subject: [PATCH 03/10] Style: format TopologicalSort.java using clang-format --- .../graphsearch/TopologicalSort.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java index 2bf03f9b4d7f..7a5dc7bbd11b 100644 --- a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java +++ b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java @@ -20,11 +20,11 @@ public class TopologicalSort { public static void main(String[] args) { int v = 6; ArrayList> adjList = new ArrayList<>(); - for(int i=0; i()); } - //sample DAG edges + // sample DAG edges adjList.get(5).add(2); adjList.get(5).add(0); adjList.get(4).add(0); @@ -32,7 +32,7 @@ public static void main(String[] args) { adjList.get(2).add(3); adjList.get(3).add(1); - topoSort(v,adjList); + topoSort(v, adjList); } /** @@ -43,20 +43,19 @@ public static void main(String[] args) { * @param adjList adjacency list representing the graph */ - - public static void topoSort (int v, ArrayList> adjList){ - boolean [] visited = new boolean[v]; - Stack stack = new Stack<>(); + public static void topoSort(int v, ArrayList> adjList) { + boolean[] visited = new boolean[v]; + Stack stack = new Stack<>(); for (int i = 0; i < v; i++) { - if(!visited[i]){ - topoSortUtil(i,visited,stack,adjList); + if (!visited[i]) { + topoSortUtil(i, visited, stack, adjList); } } System.out.println("Topological Sort:"); - while (!stack.isEmpty()){ - System.out.print(stack.pop()+" "); + while (!stack.isEmpty()) { + System.out.print(stack.pop() + " "); } System.out.println(); } @@ -72,12 +71,12 @@ public static void topoSort (int v, ArrayList> adjList){ * */ - public static void topoSortUtil(int node, boolean[] visited, Stack stack, ArrayList> adjList){ + public static void topoSortUtil(int node, boolean[] visited, Stack stack, ArrayList> adjList) { visited[node] = true; - for(int neighbour : adjList.get(node)){ - if(!visited[neighbour]){ - topoSortUtil(neighbour,visited,stack,adjList); + for (int neighbour : adjList.get(node)) { + if (!visited[neighbour]) { + topoSortUtil(neighbour, visited, stack, adjList); } } stack.push(node); // add to stack after visiting all edges From 4cd729f88e07f326b002acb584abca71df0d84e2 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 22:22:16 +0530 Subject: [PATCH 04/10] Added Topological Sort algorithm with DFS implementation and Javadoc comments --- .../java/com/thealgorithms/graphsearch/TopologicalSort.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java index 7a5dc7bbd11b..0871b824894f 100644 --- a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java +++ b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java @@ -11,6 +11,11 @@ */ public class TopologicalSort { + + // Private constructor to prevent instantiation + private TopologicalSort(){ + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } /** * Main method to test the Topological Sort implementation. * Creates a sample graph and prints its topological ordering. From 0620b2ea65de00998add38c150094438b5c13b09 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 22:27:41 +0530 Subject: [PATCH 05/10] Update pom.xml as per project requirements --- pom.xml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index ae449cdfac31..7e41eec7a35a 100644 --- a/pom.xml +++ b/pom.xml @@ -71,13 +71,14 @@ maven-compiler-plugin 3.14.0 - 21 - 21 - - -Xlint:all - -Xlint:-auxiliaryclass - -Werror - + 21 + + + + + + + From dc43651401968a2446192b2548531ac7bc2e9e45 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 22:31:43 +0530 Subject: [PATCH 06/10] fix: add private constructor to hide utility class constructor in TopologicalSort --- .../java/com/thealgorithms/graphsearch/TopologicalSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java index 0871b824894f..c4738b9efc79 100644 --- a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java +++ b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java @@ -13,7 +13,7 @@ public class TopologicalSort { // Private constructor to prevent instantiation - private TopologicalSort(){ + private TopologicalSort() { throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /** From a0dd6d3e3ec9e4b916198fd31538ab780784ae30 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Fri, 6 Jun 2025 22:50:08 +0530 Subject: [PATCH 07/10] Fix: mark TopologicalSort class as final to satisfy Checkstyle --- .../java/com/thealgorithms/graphsearch/TopologicalSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java index c4738b9efc79..b285df0ac0f0 100644 --- a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java +++ b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java @@ -10,7 +10,7 @@ * for Directed Acyclic Graphs. It prints the nodes in a topological order. */ -public class TopologicalSort { +public final class TopologicalSort { // Private constructor to prevent instantiation private TopologicalSort() { From 771d5bed9a609b463d4d3fdbab8041a9a38dcc7d Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Sun, 8 Jun 2025 12:24:58 +0530 Subject: [PATCH 08/10] Removed graphsearch folder --- .../graphsearch/TopologicalSort.java | 89 ------------------- 1 file changed, 89 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java diff --git a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java b/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java deleted file mode 100644 index b285df0ac0f0..000000000000 --- a/src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.thealgorithms.graphsearch; - -// Topological sort: https://en.wikipedia.org/wiki/Topological_sorting - -import java.util.ArrayList; -import java.util.Stack; - -/** - * This class implements the Topological Sort algorithm using Depth-First Search - * for Directed Acyclic Graphs. It prints the nodes in a topological order. - */ - -public final class TopologicalSort { - - // Private constructor to prevent instantiation - private TopologicalSort() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - /** - * Main method to test the Topological Sort implementation. - * Creates a sample graph and prints its topological ordering. - * @param args command-line arguments(not used) - */ - - public static void main(String[] args) { - int v = 6; - ArrayList> adjList = new ArrayList<>(); - for (int i = 0; i < v; i++) { - adjList.add(new ArrayList<>()); - } - - // sample DAG edges - adjList.get(5).add(2); - adjList.get(5).add(0); - adjList.get(4).add(0); - adjList.get(4).add(1); - adjList.get(2).add(3); - adjList.get(3).add(1); - - topoSort(v, adjList); - } - - /** - * Performs the topological sort on the given graph represented by adjacency list. - * Prints the nodes in topologically sorted order. - * - * @param v the number of vertices in the graph - * @param adjList adjacency list representing the graph - */ - - public static void topoSort(int v, ArrayList> adjList) { - boolean[] visited = new boolean[v]; - Stack stack = new Stack<>(); - - for (int i = 0; i < v; i++) { - if (!visited[i]) { - topoSortUtil(i, visited, stack, adjList); - } - } - - System.out.println("Topological Sort:"); - while (!stack.isEmpty()) { - System.out.print(stack.pop() + " "); - } - System.out.println(); - } - - /** - * Utility method to perform DFS starting from a given node. - * Marks nodes as visited and pushes nodes to stack after visiting all their neighbors. - * - * @param node the current node being visited - * @param visited boolean array tracking visited nodes - * @param stack stack to store the topological order - * @param adjList adjacency list representing the graph - * - */ - - public static void topoSortUtil(int node, boolean[] visited, Stack stack, ArrayList> adjList) { - visited[node] = true; - - for (int neighbour : adjList.get(node)) { - if (!visited[neighbour]) { - topoSortUtil(neighbour, visited, stack, adjList); - } - } - stack.push(node); // add to stack after visiting all edges - } -} From 2c80f84bf1119ef2e1af037b8d0be69f60100332 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Sun, 8 Jun 2025 12:36:04 +0530 Subject: [PATCH 09/10] Removed graphsearch folder and added additional unit tests for TopologicalSort --- .../thealgorithms/sorts/TopologicalSortTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java b/src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java index de115b458fe7..d5588b2b968e 100644 --- a/src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java @@ -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; @@ -59,4 +60,18 @@ public void failureTest() { + "Back edge: 6 -> 2"; assertEquals(exception.getMessage(), expected); } + @Test + void testEmptyGraph() { + Graph graph = new Graph(); + LinkedList sorted = TopologicalSort.sort(graph); + assertTrue(sorted.isEmpty()); + } + @Test + void testSingleNode() { + Graph graph = new Graph(); + graph.addEdge("A", ""); + LinkedList sorted = TopologicalSort.sort(graph); + assertEquals(1, sorted.size()); + assertEquals("A", sorted.getFirst()); + } } From 99518b79615a04eef135843af2f2ff752d8385a1 Mon Sep 17 00:00:00 2001 From: Saipatnaik-23 Date: Mon, 9 Jun 2025 16:13:26 +0530 Subject: [PATCH 10/10] evert changes to pom.xml to keep original configuration --- pom.xml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 7e41eec7a35a..ae449cdfac31 100644 --- a/pom.xml +++ b/pom.xml @@ -71,14 +71,13 @@ maven-compiler-plugin 3.14.0 - 21 - - - - - - - + 21 + 21 + + -Xlint:all + -Xlint:-auxiliaryclass + -Werror +