python双向最大匹配算法_python – 二分图的所有可能的最大匹配

匹配的边缘对于特定图形不是唯一的.

有没有办法找到所有最大匹配?

对于以下示例,下面的所有边可以是最大匹配:

{1:2,2:1}或{1:3,3:1}或{1:4,4:1}

import networkx as nx

import matplotlib.pyplot as plt

G = nx.MultiDiGraph()

edges = [(1,3), (1,4), (1,2)]

nx.is_bipartite(G)

True

nx.draw(G, with_labels=True)

plt.show()

不幸,

nx.bipartite.maximum_matching(G)

只返回

{1: 2, 2: 1}

有没有办法可以获得其他组合?

解决方法:

我读了Uno的工作并试图想出一个实现.下面是我非常冗长的代码和一个工作示例.在这个特殊的情况下,有4个“可行的”顶点(根据Uno的术语),所以用一个已经覆盖的顶点切换每个顶点,你们总共有2 ^ 4 = 16种不同的可能最大匹配.

我承认我对图论很新,我并没有完全遵循Uno的流程,存在细微差别,而且大多数情况下我都没有尝试进行任何优化.我确实很难理解论文,因为我认为解释并不完美,而且数字可能会有错误.所以请小心使用,如果你可以帮助优化它将是真正伟大的!

import networkx as nx

from networkx import bipartite

def plotGraph(graph):

import matplotlib.pyplot as plt

fig=plt.figure()

ax=fig.add_subplot(111)

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)

plt.show(block=False)

return

def formDirected(g,match):

'''Form directed graph D from G and matching M.

: undirected bipartite graph. Nodes are separated by their

'bipartite' attribute.

: list of edges forming a matching of .

Return : directed graph, with edges in pointing from set-0

(bipartite attribute ==0) to set-1 (bipartite attrbiute=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值