【问题标题】:How to use NetworKit/SNAP to get the maximum matching?如何使用 NetworKit/SNAP 获得最大匹配?
【发布时间】:2021-06-24 23:23:39
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: graph networkx matching snappy networkit


    【解决方案1】:

    关于 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

    【讨论】:

    • 您好,感谢您的帮助!但是当运行这个时:matcher = nk.matching.PathGrowingMatcher(g).run()。它显示“回溯(最近一次调用最后一次):文件“”,第 1 行,在 文件“networkit/base.pyx”,第 29 行,在 networkit.base.Algorithm.run IndexError:vector:: _M_range_check: __n (即 18446744073709551615) >= this->size() (即 10)"。
    • 你的图是定向的吗?如果是这样,请在使用匹配算法之前尝试将其转换为无向图,您可以使用g = nk.graphtools.toUndirected(g) 进行此操作。
    • 是的,图是有向图。我将它转换为无向二分,然后代码工作。非常感谢你的帮助!但是,匹配对的数量比networkx或igraph计算的要少,这真的很奇怪。
    • 如果您要与精确算法(或具有更强近似保证的算法)进行比较,这是预期的。 NetworKit 中实现的算法是 1/2 近似算法,这意味着它计算的匹配在最大匹配中具有至少一半的边。
    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多