Skip to content

[WIP] Add topological sort algorithm#5492

Open
ngokulakrish wants to merge 4 commits intorapidsai:mainfrom
ngokulakrish:fea-topo-sort
Open

[WIP] Add topological sort algorithm#5492
ngokulakrish wants to merge 4 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.

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
});

frontier_vertices = std::move(new_frontier_vertices);
level++;
Copy link
Copy Markdown
Contributor

@seunghwak seunghwak Apr 14, 2026

Choose a reason for hiding this comment

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

Have you checked (with a small toy graph) what happens if the input graph has a cycle and do_expensive_check is set to false?

I guess in_degrees can have negative values and either we exits the loop pre-maturely or we fall into infinite loops.

We may accumulate the total aggregate frontier size and we may be able to detect pre-mature exit (if total < # vertices in the graph) or infinite loops (total > # vertices in the graph).

If there is no way to cheaply check this, we update documentation. Throws if the graph is symmetric or has cycles AND do_expensive_check is set to true. Otherwise, undefined behavior. If we can cheaply check this, we should check.

Comment thread cpp/src/dag/topological_sort_mg_v32_e32.cu Outdated
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.

2 participants