Skip to content

Add Hopcroft-Karp algorithm for maximum bipartite matching - #15293

Open
Clear20-22 wants to merge 4 commits into
TheAlgorithms:masterfrom
Clear20-22:add-hopcroft-karp
Open

Add Hopcroft-Karp algorithm for maximum bipartite matching#15293
Clear20-22 wants to merge 4 commits into
TheAlgorithms:masterfrom
Clear20-22:add-hopcroft-karp

Conversation

@Clear20-22

Copy link
Copy Markdown
Contributor

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Add the Hopcroft–Karp algorithm (graphs/hopcroft_karp.py) for finding maximum cardinality matchings in unweighted bipartite graphs in $O(|E|\sqrt{|V|})$ time using alternating BFS layering and DFS augmenting paths.

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".

Copilot AI lite review requested due to automatic review settings September 12, 2026 10:44
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed require tests Tests [doctest/unittest/pytest] are required labels Sep 12, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread graphs/hopcroft_karp.py Outdated
# distance_map stores the BFS level/distance from free vertices in U
distance_map: dict[T | None, float] = {}

def breadth_first_search() -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file graphs/hopcroft_karp.py, please provide doctest for the function breadth_first_search

Comment thread graphs/hopcroft_karp.py Outdated
# Termination condition: True if an augmenting path was found, False otherwise
return distance_map[None] != math.inf

def depth_first_search(left_vertex: T | None) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file graphs/hopcroft_karp.py, please provide doctest for the function depth_first_search

Copilot AI 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.

🟡 Changes recommended

The implementation has unresolved correctness and recursion-limit issues.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds a generic Hopcroft–Karp algorithm for maximum cardinality matching in unweighted bipartite graphs.

Changes:

  • Implements BFS layering and DFS augmentation.
  • Adds validation, documentation, reference link, and doctests.
File summaries
File Summary Findings
graphs/hopcroft_karp.py New Hopcroft–Karp implementation. Critical (3 votes): None sentinel can collide with valid vertices. Moderate (3 votes): recursive DFS can exceed Python’s recursion limit.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread graphs/hopcroft_karp.py Outdated
# pair_right[v] stores the vertex in U matched to v in V (or None if free)
pair_right: dict[T, T | None] = dict.fromkeys(right_vertices)
# distance_map stores the BFS level/distance from free vertices in U
distance_map: dict[T | None, float] = {}
Comment thread graphs/hopcroft_karp.py Outdated
# Augmentation Condition: Only step forward along the layered DAG
if distance_map.get(matched_left, math.inf) == distance_map[
left_vertex
] + 1.0 and depth_first_search(matched_left):
@algorithms-keeper algorithms-keeper Bot removed the require tests Tests [doctest/unittest/pytest] are required label Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants