Add Hopcroft-Karp algorithm for maximum bipartite matching - #15293
Add Hopcroft-Karp algorithm for maximum bipartite matching#15293Clear20-22 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| # distance_map stores the BFS level/distance from free vertices in U | ||
| distance_map: dict[T | None, float] = {} | ||
|
|
||
| def breadth_first_search() -> bool: |
There was a problem hiding this comment.
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
| # 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: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
🟡 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.
| # 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] = {} |
| # 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): |
…tinel, iterative DFS, and tests
Describe your change
Add the Hopcroft–Karp algorithm ($O(|E|\sqrt{|V|})$ time using alternating BFS layering and DFS augmenting paths.
graphs/hopcroft_karp.py) for finding maximum cardinality matchings in unweighted bipartite graphs inChecklist