微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。