【问题标题】:networkx maximal_matching() does not return maximum matchingnetworkx maximal_matching() 不返回最大匹配
【发布时间】:2017-10-21 00:09:02
【问题描述】:

我正在学习使用 networkx python 模块对二分图进行一些匹配。模块中有两个函数可以给出图的最大基数匹配:

  1. nx.maximal_matching()
  2. nx.bipartite.maxmum_matching()

请注意,尽管名称为 maximal_matching,但其文档确实声明它“在图中找到最大基数匹配”。

由于我的图是二分图,我假设这两个图会给出相同的结果,至少两者的边数相同。然而,我的代码似乎表明nx.maximal_matching() 给出了错误的答案:正如nx.bipartite.maxmum_matching() 所暗示的那样,可能有更多优势。

下面是我的工作代码:

import networkx as nx
from networkx import bipartite    

def plotGraph(graph,ax,title):    
    pos=[(ii[1],ii[0]) for ii in graph.nodes()]
    pos_dict=dict(zip(graph.nodes(),pos))
    nx.draw(graph,pos=pos_dict,ax=ax,with_labels=True)
    ax.set_title(title)
    return   

if __name__=='__main__':    
    #---------------Construct the graph---------------
    g=nx.Graph()
    edges=[
            [(1,0), (0,0)],
            [(1,0), (0,1)],
            [(1,0), (0,2)],
            [(1,1), (0,0)],
            [(1,2), (0,2)],
            [(1,2), (0,5)],
            [(1,3), (0,2)],
            [(1,3), (0,3)],
            [(1,4), (0,3)],
            [(1,5), (0,2)],
            [(1,5), (0,4)],
            [(1,5), (0,6)],
            [(1,6), (0,1)],
            [(1,6), (0,4)],
            [(1,6), (0,6)]
            ]

    for ii in edges:
        g.add_node(ii[0],bipartite=0)
        g.add_node(ii[1],bipartite=1)

    g.add_edges_from(edges)

    #---------------Use maximal_matching---------------
    match=nx.maximal_matching(g)    
    g_match=nx.Graph()
    for ii in match:
        g_match.add_edge(ii[0],ii[1])

    #----------Use bipartite.maximum_matching----------
    match2=bipartite.maximum_matching(g)    
    g_match2=nx.Graph()
    for kk,vv in match2.items():
        g_match2.add_edge(kk,vv)

    #-----------------------Plot-----------------------
    import matplotlib.pyplot as plt
    fig=plt.figure(figsize=(10,8))

    ax1=fig.add_subplot(2,2,1)
    plotGraph(g,ax1,'Graph')

    ax2=fig.add_subplot(2,2,2)
    plotGraph(g_match,ax2,'nx.maximal_matching()')

    ax3=fig.add_subplot(2,2,3)
    plotGraph(g_match2,ax3,'bipartite.maximum_matching()')

    plt.show()

这是生成的图。如图所示,subplot-2 有 6 条边,而 3 有 7 条边。这是 networkx 实现中的错误还是我在这里做错了什么?

PS:我的networkx是1.11版

【问题讨论】:

    标签: python graph networkx matching bipartite


    【解决方案1】:

    networkx.maximal_matching 算法不会以您想要的方式给出 最大基数 匹配。它实现了一个简单的贪心算法,其结果是最大的,纯粹是因为不能添加额外的边。

    对于您想要的全局最大基数匹配,它的对应项是 networkx.max_weight_matching

    【讨论】:

    • 我明白了。我什至没有查看max_weight_matching,因为我的图表没有权重。感谢您为我澄清这一点。
    • 我现在明白两者的区别了,但我想知道最大匹配什么时候有用?有没有人有问题的例子,当它足以找到最大匹配时,不一定是最大匹配?
    【解决方案2】:

    根据this bug report的回答,返回的匹配是最大的,而不是最大的匹配。在他们的术语中(我不知道这有多普遍)意味着它给出了局部最优而不是全局最优。

    因此,对于 maximal_matching 返回的匹配,只能保证不会通过向该匹配添加边来使其变大(同时仍然是匹配)。但是,不能保证不存在具有更多边的匹配。

    【讨论】:

      【解决方案3】:

      用图论的说法,最大匹配和最大匹配是不同的(即使在二分图中)。正如 donkopotamus 所指出的那样,最大匹配仅仅意味着您不能再添加任何边缘。最大匹配意味着没有匹配有比它更多的边。这就是函数命名的原因。

      也就是说,图论中没有最大基数匹配这样的东西。但不幸的是,文档使用了“最大基数匹配”的措辞。这很容易让人感到困惑;或者更糟的是误解了算法的目的。

      【讨论】:

        猜你喜欢
        • 2021-05-01
        • 2013-01-14
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        相关资源
        最近更新 更多