-
Notifications
You must be signed in to change notification settings - Fork 20k
Add Unit Tests for Empty and Single-Node Graphs in TopologicalSort #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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd8a16f
Add Topological Sort algorithm using DFS in GraphSearch
Saipatnaik-23 58f53b2
Add Wikipedia reference link for Topological Sort
Saipatnaik-23 e050e09
Style: format TopologicalSort.java using clang-format
Saipatnaik-23 4cd729f
Added Topological Sort algorithm with DFS implementation and Javadoc …
Saipatnaik-23 0620b2e
Update pom.xml as per project requirements
Saipatnaik-23 dc43651
fix: add private constructor to hide utility class constructor in Top…
Saipatnaik-23 a0dd6d3
Fix: mark TopologicalSort class as final to satisfy Checkstyle
Saipatnaik-23 771d5be
Removed graphsearch folder
Saipatnaik-23 2c80f84
Removed graphsearch folder and added additional unit tests for Topolo…
Saipatnaik-23 99518b7
evert changes to pom.xml to keep original configuration
Saipatnaik-23 4447243
Merge branch 'master' into topological-sort
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/com/thealgorithms/graphsearch/TopologicalSort.java
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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<ArrayList<Integer>> 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<ArrayList<Integer>> adjList) { | ||
boolean[] visited = new boolean[v]; | ||
Stack<Integer> 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<Integer> stack, ArrayList<ArrayList<Integer>> 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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.