Skip to content

Maximum bipartite matching#276

Open
mahdiehmalekian wants to merge 3 commits into
dwavesystems:mainfrom
mahdiehmalekian:max-bip-matching
Open

Maximum bipartite matching#276
mahdiehmalekian wants to merge 3 commits into
dwavesystems:mainfrom
mahdiehmalekian:max-bip-matching

Conversation

@mahdiehmalekian

@mahdiehmalekian mahdiehmalekian commented Jul 16, 2026

Copy link
Copy Markdown

Code for finding maximum bipartite matching via the Hopcroft-Karp algorithm.

AI use: I used AI to generate the code, checked the code myself.

@randomir randomir 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.

The implementation looks correct (when compared to the pseudo-code on Wikipedia), apart from a couple of pruning condition checks missing in your implementation, in the BFS and the DFS functions -- resulting in a greater runtime complexity in the worst case.

But my main question is what's the benefit of your implementation over the existing one in NetworkX, hopcroft_karp_matching()?

Comment thread dwave/graphs/algorithms/matching.py Outdated
Comment thread dwave/graphs/algorithms/matching.py Outdated
Comment thread dwave/graphs/algorithms/matching.py Outdated
returned; which one depends on iteration order.

Raises:
KeyError: If ``edges`` references a ``y`` that is not in ``Y``.

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.

What if it references x not in X?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed this and made it explicit that edges must have x in X and y in Y. dbda40f

Comment thread dwave/graphs/algorithms/matching.py Outdated
Comment thread dwave/graphs/algorithms/matching.py
mahdiehmalekian and others added 2 commits July 18, 2026 09:12
Co-authored-by: Radomir Stevanovic <radomir.stevanovic@gmail.com>
@mahdiehmalekian

Copy link
Copy Markdown
Author

Re. the missing pruning step, thank you for flagging it. I added it in dbda40f

Re. your main question, great point.
This is indeed the same algorithm as networkx.bipartite.hopcroft_karp_matching (except for the greedy warm-start and the BFS shortest-path prune added in dbda40f in this implementation). However, the main advantage of this implementation is for usage in the clique embedder in minorminer where this function is called in a hot loop where the inputs are already in the exact form this function needs. The two vertex sets are known by construction (vertical vs horizontal runs in the clique embedder), and the edges are already a plain adjacency dict. Using networkx.bipartite.hopcroft_karp_matching instead would require constructing a nx.Graph (node/edge insertion, attribute dicts) on every call, passing the bipartition we already know as top_nodes, and then inverting the symmetric result back into the one-directional {vertical run: horizontal run} map our caller expects. This custom version skips all of that and returns the exact contract the caller consumes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants