Skip to content

fix: Remove defensive checks in bipartite graph functions - #15210

Open
tanishqraikwar83-eng wants to merge 1 commit into
TheAlgorithms:masterfrom
tanishqraikwar83-eng:fix-remove-defensive-checks-bipartite
Open

fix: Remove defensive checks in bipartite graph functions#15210
tanishqraikwar83-eng wants to merge 1 commit into
TheAlgorithms:masterfrom
tanishqraikwar83-eng:fix-remove-defensive-checks-bipartite

Conversation

@tanishqraikwar83-eng

Copy link
Copy Markdown

fix: Remove defensive checks in bipartite graph functions

This pull request removes the defensive checks in the bipartite graph functions (both DFS and BFS) that were causing the functions to return early when a node is not in the graph. According to the FIXME comments in the code, these checks should be removed to allow natural KeyError/TypeError exceptions for invalid inputs.

Fixes #15127

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Sep 6, 2026
@cclauss

cclauss commented Sep 6, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev priya-sundaram-dev 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.

Thanks for tackling #15127, @tanishqraikwar83-eng — the FIXME comments have been sitting there a while, so it's good to see someone take them on. I ran the branch, though, and unfortunately this can't merge as-is: removing the two guards breaks 13 of the existing doctests (CI will fail on check_bipatrite.py).

The reason is that the guards weren't only catching the two FIXME type/key cases — they were also making a very ordinary graph shape work: an adjacency dict where a node appears as a neighbor but is omitted as a key (i.e. a sink with no out-edges). For example the first BFS doctest:

>>> is_bipartite_bfs({0: [1, 2], 1: [0, 3], 2: [0, 4]})   # nodes 3 and 4 have no key

used to return True; with the guard gone it raises KeyError: 3. Quick repro on this branch:

$ python3 -m doctest graphs/check_bipatrite.py
... 13 failures (6 in is_bipartite_bfs, 7 in is_bipartite_dfs)

So before this can go in, the semantics question in #15127 needs a decision:

  1. If a missing key should be treated as "no out-edges" (a valid graph) — which is what the current doctests assume — then the guard should stay, and the fix for the FIXMEs is narrower: only the genuinely-invalid inputs (e.g. float keys used to index a list) should be allowed to raise. Deleting the guard outright is too broad.

  2. If the intent is that any node referenced but not keyed is an error — then those legitimate-looking doctests need to be rewritten to expect the exception (a Traceback ... block, or # doctest: +SKIP), and the two FIXME lines updated to match. But note that would be a behavior change users may rely on, so I'd flag it for a maintainer call rather than assume it.

My suggestion: go with (1) — keep the guards, and instead resolve the FIXMEs by normalizing the input at the top of each function (e.g. treat a missing key as []) so the "should fail" cases are handled deliberately rather than by an incidental KeyError. That keeps every current doctest green while still closing #15127.

Happy to look again once the doctests pass locally (python3 graphs/check_bipatrite.py should print All tests passed!). Thanks again for digging into this one!

— Priya Sundaram (AI agent)

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes A maintainer has requested changes to this PR enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants