Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement topological sort with DAG validation and cycle detection #6568

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
the-yash-rajput wants to merge 12 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from the-yash-rajput:TopologicalSort

Conversation

Copy link
Contributor

@the-yash-rajput the-yash-rajput commented Oct 1, 2025
edited
Loading

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Copy link

codecov-commenter commented Oct 1, 2025
edited
Loading

Codecov Report

❌ Patch coverage is 94.73684% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.22%. Comparing base (421ac6d) to head (88f2467).

Files with missing lines Patch % Lines
.../java/com/thealgorithms/graph/TopologicalSort.java 94.73% 2 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@
## master #6568 +/- ##
============================================
+ Coverage 75.19% 75.22% +0.03% 
- Complexity 5614 5625 +11 
============================================
 Files 690 691 +1 
 Lines 19407 19445 +38 
 Branches 3755 3763 +8 
============================================
+ Hits 14593 14628 +35 
- Misses 4247 4250 +3 
 Partials 567 567 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

the-yash-rajput and others added 11 commits October 1, 2025 16:22
Copy link
Member

alxkm commented Oct 1, 2025

Looks like this is already in the repository, but in the wrong package it is definitely related to the graph

https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/TopologicalSort.java

the-yash-rajput reacted with eyes emoji

Copy link
Contributor Author

the-yash-rajput commented Oct 1, 2025
edited
Loading

@alxkm How would you like to proceed?
Should we merge this PR or replace the previous implementation with the correct location?

@alxkm I think we can include both, as the previous algorithm is not more friendly to graphs.

Copy link
Member

alxkm commented Oct 1, 2025
edited
Loading

@siriak, @DenizAltunkapan, Could you please help clarify our approach here?

There are already several implementations of this algorithm in the repository:

The previous implementation follows a more traditional pattern as it operates directly on color values.
The current implementation also good and it has test coverage.

From my perspective, the optimal solution would be to:

  • Migrate the old implementation here
  • Make it generic
  • Clean up the code (e.g., implement specialized exceptions which already presented here)
  • Maintain test suites for both variants

What are your thoughts on this approach?

Copy link
Contributor Author

@alxkm Let’s first align on the right strategy and once confirmed, we can proceed with the necessary implementation.

Copy link
Member

alxkm commented Oct 3, 2025

@the-yash-rajput, you can proceed with the following approach as I described - creating a generic implementation of the algorithm.

  • Migrate the existing implementation to the correct package location
  • Refactor it to be generic and reusable
  • Implement the specialized exceptions that are already present in the your version
  • And generic tests for it with Integer and String values

This approach will provide the traditional algorithm that uses color values.

What do you think about this?

Copy link
Member

siriak commented Oct 3, 2025

@alxkm I agree with the proposed approach, I think both implementations should be stored together.

P.S. I reviewed this 2 days ago, but forgot to write a reply and then lost the tab -_-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@DenizAltunkapan DenizAltunkapan Awaiting requested review from DenizAltunkapan DenizAltunkapan is a code owner

@yanglbme yanglbme Awaiting requested review from yanglbme yanglbme is a code owner

@alxkm alxkm Awaiting requested review from alxkm alxkm is a code owner

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /