如何使用 NetworKit/SNAP 获得最大匹配?

问题描述

我想得到一个图的最大匹配。

现在,我使用 Networkx 中的算法:nx.algorithms.bipartite.matching.hopcroft_karp_matching(G)

但是,我在 SNAPenter link description here 中没有找到类似的算法。

对于 NetworKit,我找到了这个页面enter link description here。但是不知道怎么用。

有什么想法吗?如何使用 NetworKit/SNAP 得到一个图的最大匹配?

解决方法

关于 NetworKit:尚未提供计算精确最大匹配的算法,但您可以使用 networkit.matching.PathGrowingMatcher,它实现了 Drake 和 Hougardy [1] 的最大匹配的线性时间 1/2 近似算法.您可以按如下方式使用它:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...