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.
@@ -0,0 +1,175 @@
package com.thealgorithms.datastructures.queues
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A semicolon (;) is missing at the end.

}

private Node<T> fromt;
private Node<T> rear,;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There’s an extra comma after rear.

}
}

private Node<T> fromt;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you mean 'front' instead of 'fromt'?

throw new IllegalArgumentException("Cannot enqueue null data");
}

Node<T> newNode = new Node><T>(data);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There’s an extra > here.
Fix: Node newNode = new Node<>(data);

}

@Override
public synchronized T"next() {
Copy link
Copy Markdown
Contributor

@felseje felseje Apr 22, 2026

Choose a reason for hiding this comment

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

Please remove the "
Fix: public synchronized T next() {

return "[]";
}

StringBuilder sb = new StringBuilder("[[]");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you mean "StringBuilder sb = new StringBuilder("[");" ?

* @return an iterator over the elements in the queue
*/
@Override
public synchronized Iterator<T> iterator() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The iterator is not fully thread-safe; it only captures the current state of front and may behave inconsistently under concurrent modifications.

* @return a string representation of the queue
*/
@Override
public synchronized String toString() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Calling isEmpty() inside other synchronized methods is redundant, since those methods already hold the lock. This doesn’t cause correctness issues, but it adds unnecessary synchronization overhead.

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

2 participants