[Repo Assist] perf(falsify): replace N×nx.ancestors calls with transitive_closure_dag in validate_pd#1606
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
…ag in validate_pd
For a graph with N nodes and E edges, the old implementation called
nx.ancestors() N times — one BFS/DFS per node, totalling O(N·(V+E))
Python function calls.
This commit replaces both paths:
adjacent_only=True:
Use list(causal_graph.edges()) directly — no ancestor enumeration
needed at all. O(E) instead of O(N·(V+E)).
adjacent_only=False (default):
Use nx.transitive_closure_dag(), which adds an edge (u,v) iff u
can reach v (u is an ancestor of v). A single optimised graph
traversal replaces N separate nx.ancestors() calls, reducing
Python-function-call overhead from O(N) to O(1). Correctness is
unchanged: the set of (ancestor, descendant) pairs is identical.
Also adds two lightweight unit tests that assert the correct N_TESTS
count for both code paths on a three-node chain DAG.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
58 tasks
47 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
validate_pdindowhy/gcm/falsify.pywas building the list of(ancestor, node)pairs by callingnx.ancestors()once per graph node:For a graph with N=100 nodes that is called repeatedly (e.g., during falsification), this meant 100 Python function calls and 100 separate graph traversals before any independence tests even started.
Changes
adjacent_only=True— skip ancestor enumeration entirely; direct edges are already available ascausal_graph.edges():adjacent_only=False(default) — usenx.transitive_closure_dag(), which adds edge(u, v)iffucan reachv(i.e.uis an ancestor ofv). A single optimised graph traversal replaces N separatenx.ancestors()calls:nx.transitive_closure_daghas been available since NetworkX 2.7, which is below the project's minimum of>=2.8.5.Impact
adjacent_only=Falseadjacent_only=Truesorted(old) == sorted(new)on test graphs)Test Status
Two new unit tests added in
tests/gcm/test_falsify.py:test_given_chain_dag_when_validate_pd_without_adjacent_only_then_tests_all_ancestor_pairs— verifiesN_TESTS=3for a 3-node chain (all ancestor pairs)test_given_chain_dag_when_validate_pd_with_adjacent_only_then_tests_only_direct_edges— verifiesN_TESTS=2for a 3-node chain (only direct edges)Full CI suite requires Poetry with all optional extras (not available in this environment). The optimisation is semantically equivalent:
sorted(old_pairs) == sorted(new_pairs)was verified with a standalone NetworkX 3.4.2 script.