Skip to content

Add DAG antichain width - #8654

Merged
dschult merged 12 commits into
networkx:mainfrom
pamedodji:add-dag-antichain-width
Jul 2, 2026
Merged

dschult merged 12 commits into
networkx:mainfrom
pamedodji:add-dag-antichain-width

Conversation

@pamedodji

Copy link
Copy Markdown
Contributor

This adds 'dag_antichain_width' which returns the size of a maximum antichain in a directed acyclic graph. The implementation uses Dilworth's theorem, the width of the partial order represented by the DAG is computed from a maximum bipartite matching on its transitive closure. This gives a polynomial-time way to compute the width of a DAG. Tests cover chains, independent nodes, a diamond-shaped DAG, and invalid inputs.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems reasonable and the main issue is whether it belongs in NetworkX.
The doc_string replaces the technical term "width of a partially ordered set" with other technical terms "size of a maximum antichain", or "minimum number of chains needed to cover all vertices". This then magically is computed using more technical terms: "a maximum bipartite matching on the transitive closure". Can you explain what a dag-antichain-width is more directly?

My understanding is that this is similar to our treewidth concept for undirected graphs. But that hasn't gotten much traction as far as we can tell. Can you help me with that by explaining why you are interested in dag-antichain-width? How did you come across it and what made you want to propose it for NetworkX?

Thanks!!

Comment thread networkx/algorithms/dag.py Resolved

@rossbar rossbar left a comment

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.

Another high-level question, how does this compare to:

>>> longest_ac = max(nx.antichains(G, key=len))
>>> len(longest_ac)

or, more concisely: len(max(nx.antichains(G, key=len))).

@pamedodji

pamedodji commented May 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback.

dag_antichain_width(G) returns the largest set of nodes such that no node in the set can reach another node in the set. For dependency DAGs, this is the largest group of tasks that are independent of each other with respect to the precedence constraints.
My interest comes from a school research project supervised by my advisor where computing the width of DAGs efficiently is needed. The algorithmic idea is not mine it was suggested by my advisor.

The reason I thought this could fit NetworkX is that NetworkX already has antichains(G), but if the user only needs the maximum size, enumerating antichains can be very expensive. In my use case, I first tried a related exact approach by building the co-comparability graph of the DAG and computing a maximum clique, for example with:

def clique_number_exact(G):
    return max((len(C) for C in nx.find_cliques(G)), default=0)

On a sparse DAG with around 2000 nodes this approach was not practical on my machine (hours for 500 nodes sparse DAGs) while the Dilworth/matching-based method returned in a few seconds.

About multigraphs, since the computation only depends on reachability parallel edges shouldn't change the result. I can add a test for MultiDiGraph to make that behavior explicit but if the preference is to keep the function limited to simple directed graphs I can also add @not_implemented_for("multigraph)

Yes ,python longest_ac = max(nx.antichains(G, key=len)) len(longest_ac) gives the same result but the difference is that nx.antichains(G) enumerates antichains and if only the maximum size is needed this can be much more expensive than necessary, since the number of antichains can be exponential in the number of nodes. For example, in an empty DAG on n nodes, every subset of nodes is an antichain so nx.antichains(G) has to enumerate 2**n antichains. This is the distinction that matters in my use case, I don’t need the actual antichain but only its size and enumeration-based approaches became impractical on larger DAGs. I agree this is a specialized graph parameter, so the main question is whether NetworkX wants to expose it directly but the efficiency difference made me think it could be worth proposing

@dschult

dschult commented May 16, 2026

Copy link
Copy Markdown
Member

Thanks for this description of where this PR is coming from.

Do you know why your adviser is interested in finding the width of DAGs?
Is their research focus on developing algorithms? Perhaps dag-width is of interest from a complexity perspective, or as one of a number of similar algorithms to study. Or is there an application -- a research problem where we want to use dag-width to answer a question. Perhaps finding dag-width is needed to get the answer to a question beyond the algorithm itself.

@pamedodji

Copy link
Copy Markdown
Contributor Author

My advisor doesn't need this alogirthm directly but needs it because we want to generate DAGs with a controlled width. So we need to be able to check the width quickly. In our case the width is a parameter of the graphs we want to generate. From what I understand it is connected to experiments on scheduling/precedence constraints and labeling problems.

@dschult

dschult commented May 19, 2026

Copy link
Copy Markdown
Member

I see, so that's the application -- to check whether a generated graph has a dag-antichain-width within a desired range.

Given that we have tree_width for undirected graphs, I'm leaning toward adding this. It makes sense that it is faster than an exhaustive search. And the width value itself makes sense as a quantity of interest for e.g. logistics or production pathways where the number of chains could relate to how many machines can work the production at the same time without duplicating or interfering. But this is just a guess.

@pamedodji

Copy link
Copy Markdown
Contributor Author

Yes, that's the idea. I can update the docstring to make this interpretation clearer and add a MultiDiGraph test to cover the multigraph case.

@rossbar

rossbar commented May 19, 2026

Copy link
Copy Markdown
Contributor

Given that we have tree_width for undirected graphs, I'm leaning toward adding this. It makes sense that it is faster than an exhaustive search.

Agreed, I'm in favor of adding as well - thanks @pamedodji for providing the additional context.

I'd propose to rename the function longest_antichain as that is (AIUI) a more precise term. There's also no need to add dag_ to the function name as it's already in the dag namespace, so the call signature will look like: nx.dag.longest_antichain(G)

@pamedodji

Copy link
Copy Markdown
Contributor Author

Thanks, that sounds good to me.

One small question about the name, since the current function returns only the size of a maximum antichain would antichain_width be clearer than longest_antichain? My concern is that longest_antichain
may suggest that the function returns an actual antichain.

@dschult

dschult commented May 20, 2026

Copy link
Copy Markdown
Member

It looks like wikipedia antichain is a good reference for this terminology. It talks about the names: "height" of a "longest chain" and "width" of a "largest antichain". I guess this is orienting the chains vertically -- and then the antichains are sets of nodes not connected via chains so they spread out horizontally. Obviously this is an arbitrary way to lay it out and the names are thus arbitrary. But that's ok. We should go with what the literature uses. Our namespace for dags means we don't need/want dag in the function name. So antichain_width sounds good.

Maybe a link to the wikipedia page for antichains could be added to the doc-string too. Actually, most of the functions in this module have no references(!!). So we could add links to wikipedia for many of them perhaps in a separate PR. That might help with the technical terminology too. So many definitions! :)

@pamedodji

Copy link
Copy Markdown
Contributor Author

Done. I renamed the function to antichain_width, updated the docstring, added the wikipedia reference and added a MultiDiGraph test.

Comment thread networkx/algorithms/dag.py Resolved

@rossbar rossbar left a comment

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.

I took the liberty of pushing up some minor tweaks/improvements but overall this LGTM. Thanks @pamedodji !

@rossbar

rossbar commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

The CI failures are unrelated and fixed on main

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good! Thanks @pamedodji !

I merged with main and changed "This" to "The width".
I think this is ready to merge.

@dschult
dschult merged commit 4d3dc2c into networkx:main Jul 2, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Jul 2, 2026
toastmt pushed a commit to toastmt/networkx that referenced this pull request Jul 2, 2026
* Add DAG antichain width

* format dag_antichain_width

* Renamed antichain width function

* Keep antichain_width in dag namespace

* DOC: ref guide alphabetical, for new fn at least.

* STY: update calling pattern in tests.

* DOC: Minor docstring tweaks.

* STY: rm nodes property.

* MAINT: node bipartite attr has no effect, so remove.

* TST: Minor test improvements.

* Use noun over pronon.

---------

Co-authored-by: AMEDODJIpierre <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants