Skip to content

Add topological sort algorithm#5492

Merged
rapids-bot[bot] merged 9 commits intorapidsai:mainfrom
ngokulakrish:fea-topo-sort
Apr 23, 2026
Merged

Add topological sort algorithm#5492
rapids-bot[bot] merged 9 commits intorapidsai:mainfrom
ngokulakrish:fea-topo-sort

Conversation

@ngokulakrish
Copy link
Copy Markdown
Contributor

This PR is to add topological sort algorithm to cuGraph.

@ngokulakrish ngokulakrish requested review from a team as code owners April 14, 2026 22:03
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented Apr 14, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@seunghwak seunghwak added feature request New feature or request non-breaking Non-breaking change labels Apr 14, 2026
Copy link
Copy Markdown
Contributor

@seunghwak seunghwak left a comment

Choose a reason for hiding this comment

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

Looks great except for minor cosmetic issues! We need to add tests and work on merging this.

Comment thread cpp/include/cugraph/algorithms.hpp Outdated
bool do_expensive_check = false);

/**
* @ingroup traversal_cpp
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.

This doesn't belong to the traversal group.

https://github.com/rapidsai/cugraph/blob/main/cpp/include/cugraph/algorithms.hpp#L31

We may create dag_group here. That place this algorithm in the dag_group.

@ChuckHastings @acostadon @alexbarghi-nv @rlratzel @BradReesWork Any thoughts on where should we place topological sort?

In networkX, topological sort is in dag.py (https://github.com/networkx/networkx/blob/main/networkx/algorithms/dag.py) along with

__all__ = [
    "descendants",
    "ancestors",
    "topological_sort",
    "lexicographical_topological_sort",
    "all_topological_sorts",
    "topological_generations",
    "is_directed_acyclic_graph",
    "is_aperiodic",
    "transitive_closure",
    "transitive_closure_dag",
    "transitive_reduction",
    "antichains",
    "dag_longest_path",
    "dag_longest_path_length",
    "dag_to_branching",
]

So, we initially placed this algorithm in the dag category.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That seems fine.

Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh
Comment thread cpp/src/dag/topological_sort_impl.cuh
Comment thread cpp/src/dag/topological_sort_mg_v32_e32.cu Outdated
Copy link
Copy Markdown
Contributor

@seunghwak seunghwak 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 and let's address few minor cosmetic issues; add a mechanism to avoid undesirable behaviors (possibly infinite looping) when user passed a graph with cycles (and set do_expensive_check = true); and finish SG/MG C++ tests (and we need to remove loops before passing the graph to topological_sort.

Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/include/cugraph/algorithms.hpp
Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh Outdated
Comment thread cpp/src/dag/topological_sort_impl.cuh
Comment thread cpp/tests/dag/mg_topological_sort_test.cpp Outdated
Comment thread cpp/tests/dag/topological_sort_test.cpp
Comment thread cpp/src/dag/topological_sort_impl.cuh
Comment thread cpp/tests/dag/topological_sort_test.cpp
Copy link
Copy Markdown
Contributor

@seunghwak seunghwak 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 except for few minor cosmetic issues (+ you need to clear the edge mask before attaching a new one if the graph already has an attached edge mask).

Once you make these updates, I will test with 2 GPUs, and we can work on merging this.

Comment thread cpp/include/cugraph/algorithms.hpp Outdated
Comment thread cpp/tests/dag/dag_test_utilities.hpp Outdated
Comment thread cpp/tests/dag/mg_topological_sort_test.cpp
Comment thread cpp/tests/dag/topological_sort_test.cpp
Comment thread cpp/tests/dag/mg_topological_sort_test.cpp Outdated
Copy link
Copy Markdown
Contributor

@seunghwak seunghwak left a comment

Choose a reason for hiding this comment

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

LGTM

@seunghwak seunghwak changed the title [WIP] Add topological sort algorithm Add topological sort algorithm Apr 21, 2026
@seunghwak
Copy link
Copy Markdown
Contributor

/ok to test 2115518

@ChuckHastings
Copy link
Copy Markdown
Collaborator

/ok to test 8887497

@ChuckHastings
Copy link
Copy Markdown
Collaborator

/merge

@rapids-bot rapids-bot Bot merged commit 5b7dcd7 into rapidsai:main Apr 23, 2026
141 of 143 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature request New feature or request non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants