networkx Pythonモジュールを使用して、2部グラフのマッチングを行うことを学んでいます。 maximal_matching
の名前を持つが、そのドキュメントが「それている状態をしていることnetworkx maximal_matching()が最大一致を返しません
nx.maximal_matching()
nx.bipartite.maxmum_matching()
注:グラフの最大基数マッチングを与えるモジュール内の2つの機能がありますグラフ内で最大のカーディナリティマッチングを見つける。
私のグラフは2部構成なので、これらの2つは同じ結果を出すと仮定します。しかし、私のコードは、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
私はそれを得ました。私はグラフに重みがないので、 'max_weight_matching'を調べていませんでした。それを明確にしてくれてありがとう。 – Jason