Skip to content

Conversation

@codomposer
Copy link
Contributor

Summary

This PR adds Topological Sort algorithm implementation to the Graph algorithms collection. Topological Sort is a fundamental graph algorithm that produces a linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for every directed edge (u, v), vertex u comes before vertex v in the ordering.

Motivation and Context

Topological Sort is a well-established algorithm from reputable academic sources including:

  • "Introduction to Algorithms" (CLRS) by Cormen, Leiserson, Rivest, and Stein
  • "Algorithm Design Manual" by Steven Skiena

This algorithm was notably missing from the repository's graph algorithms collection and is essential for:

  • Build systems: Compile dependencies in correct order
  • Task scheduling: Execute tasks respecting dependencies
  • Course prerequisites: Order courses based on requirements
  • Package dependency resolution: Resolve dependency chains

Implementation Details

  • Two algorithm approaches:
    1. DFS-based: Uses depth-first search with post-order traversal
    2. Kahn's Algorithm: Uses in-degree counting and BFS-based processing
  • Time Complexity: O(V + E) for both approaches
  • Space Complexity: O(V)
  • Cycle Detection: Both methods detect cycles and throw appropriate exceptions for non-DAG graphs
  • Comprehensive documentation: Detailed inline comments explaining algorithm steps and concepts

Testing

  • Added 18 comprehensive test cases covering:
    • Simple linear graphs
    • Complex DAGs with multiple valid orderings
    • Disconnected graphs
    • Cycle detection (including self-loops)
    • Real-world scenarios (course prerequisites)
    • Single vertex graphs
    • Different data types (string and integer vertices)
  • All tests pass successfully ✅

Files Changed

  • Algorithms/Graph/TopologicalSort.cs - Algorithm implementation (290 lines)
  • Algorithms.Tests/Graph/TopologicalSortTests.cs - Comprehensive tests (573 lines)
  • README.md - Updated to include link to Topological Sort

Checklist

  • I have performed a self-review of my code
  • My code follows the style guidelines of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Comments in areas I changed are up to date
  • I have added comments to hard-to-understand areas of my code
  • I have made corresponding changes to the README.md

Contribution by Gittensor, learn more at https://gittensor.io/

@codomposer codomposer requested a review from siriak as a code owner November 11, 2025 01:16
@codecov
Copy link

codecov bot commented Nov 11, 2025

Codecov Report

❌ Patch coverage is 97.47899% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.89%. Comparing base (5bcbece) to head (19b9a64).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
Algorithms/Graph/TopologicalSort.cs 97.47% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master     #564    +/-   ##
========================================
  Coverage   96.89%   96.89%            
========================================
  Files         291      292     +1     
  Lines       12035    12154   +119     
  Branches     1740     1761    +21     
========================================
+ Hits        11661    11777   +116     
- Misses        237      239     +2     
- Partials      137      138     +1     

☔ 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.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@siriak siriak merged commit 887b6bd into TheAlgorithms:master Nov 11, 2025
4 checks passed
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.

2 participants