Skip to content

Add Topological Sorting using DFS (Issue #6938)#7389

Open
Senrian wants to merge 2 commits intoTheAlgorithms:masterfrom
Senrian:feature/topological-sort-dfs
Open

Add Topological Sorting using DFS (Issue #6938)#7389
Senrian wants to merge 2 commits intoTheAlgorithms:masterfrom
Senrian:feature/topological-sort-dfs

Conversation

@Senrian
Copy link
Copy Markdown
Contributor

@Senrian Senrian commented Apr 19, 2026

Description

Implements topological sorting using Depth-First Search (DFS) for TheAlgorithms/Java repository, addressing issue #6938.

Algorithm

Given a Directed Acyclic Graph (DAG), the algorithm:

  1. Performs DFS traversal on all unvisited vertices
  2. Records each vertex in post-order (when all descendants have been explored)
  3. Reverses the recorded order to produce the topological ordering

Complexity

  • Time Complexity: O(V + E) where V = vertices, E = edges
  • Space Complexity: O(V + E) for adjacency list and recursion stack

Files

  • TopologicalSort.java - DFS-based topological sort implementation
  • TopologicalSortTest.java - Comprehensive tests

Closes #6938

Senrian added 2 commits April 1, 2026 08:34
Issue TheAlgorithms#7356: Add null check for the search value to prevent potential NullPointerException.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Topological Sorting using DFS

1 participant