networkx bipartite 그래프의 일부 일치를 수행하기 위해 파이썬 모듈을 사용하는 방법을 배우고 있습니다. maximal_matching
이름으로되지만, 그 문서는 "이것은 그 상태를 수행하는 것이networkx maximal_matching()이 최대 일치를 반환하지 않습니다.
nx.maximal_matching()
nx.bipartite.maxmum_matching()
주의 : 그래프의 최대 카디널리티 정합을 제공 모듈의 두 가지 기능이있다 그래프에서 일치하는 최대 카디널리티를 찾으십시오. "
그래프가 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의 구현에서이 버그가 있습니까? 아니면 여기서 잘못된 것이 있습니까?
추신 : 내 networkx이 this bug report에 대한 대답에 따라 버전 1.11
알겠습니다. 내 그래프에 가중치가 없으므로 'max_weight_matching'도 조사하지 않았습니다. 그 점을 명확히 해 주셔서 감사합니다. – Jason