0

I want to get the maximum matching of a graph.

Now, I use the algorithm in Networkx: nx.algorithms.bipartite.matching.hopcroft_karp_matching(G)

However, I didn't find a similar algorithm in SNAPenter link description here.

And for NetworKit, I found this page:enter link description here. But I don't know how to use it.

Any ideas? How to use NetworKit/SNAP to get the maximum matching of a graph?

ANQI
  • 3
  • 1

1 Answers1

0

Concerning NetworKit: an algorithm to compute an exact maximum matching is not yet provided, but you can use networkit.matching.PathGrowingMatcher, which implements the linear time 1/2-approximation algorithm for maximum matching by Drake and Hougardy [1]. You can use it as follows:

import networkit as nk

# g is your graph

# Create and run the matching algorithm
matcher = nk.matching.PathGrowingMatcher(g).run()

# Get the matching, this returns an object of type networkit.matching.Matching
matching = matcher.getMatching()

# You can parse the computed matching by using
# matching.isMatched and matching.mate, for example:
for u in g.iterNodes():
    if matching.isMatched(u):
        print(f"Node {u} is matched with node {matching.mate(u)}")

[1] https://dl.acm.org/doi/10.1016/S0020-0190%2802%2900393-9

angriman
  • 345
  • 1
  • 10
  • Hi, thanks for your help! But when run this: matcher = nk.matching.PathGrowingMatcher(g).run(). It shows"Traceback (most recent call last): File "", line 1, in File "networkit/base.pyx", line 29, in networkit.base.Algorithm.run IndexError: vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 10)". – ANQI Mar 29 '21 at 20:46
  • Is your graph directed? If so, try to convert it to an undirected graph before using the matching algorithm, you can do this with `g = nk.graphtools.toUndirected(g)`. – angriman Mar 31 '21 at 07:21
  • Yes, the graph is a directed graph. I transformed it to an undirected bipartite, then the code works. Thanks so much for your help! However, it's really strange that the number of matched pairs is smaller than that is computed by networkx or igraph. – ANQI Mar 31 '21 at 14:08
  • That's expected if you are comparing against an exact algorithm (or an algorithm with stronger approximation guarantees). The algorithm implemented in NetworKit is a 1/2-approximation algorithm, meaning that it computes a matching that has at least half of the edges in a maximum matching. – angriman Mar 31 '21 at 20:49