I want to read text file containing graph information and apply approximation on graph. Output would be graph containing maximum matching nodes information. Algorithm is based on maximum bipartite matching. I am using eclipse PyDev for this.
Here is code:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.read_edgelist('abc.text', create_using=nx.Graph(), nodetype=int)
def bipartiteMatch(G):
M = {}
for u in G:
for v in G[u]:
if v not in M:
M[v] = u
break
while 1:
preds = {}
unmatched = []
pred = dict([(u,unmatched) for u in G])
for v in M:
del pred[M[v]]
layer = list(pred)
while layer and not unmatched:
newLayer = {}
for u in layer:
for v in G[u]:
if v not in preds:
newLayer.setdefault(v,[]).append(u)
layer = []
for v in newLayer:
preds[v] = newLayer[v]
if v in M:
layer.append(M[v])
pred[M[v]] = v
else:
unmatched.append(v)
if not unmatched:
unlayered = {}
for u in G:
for v in G[u]:
if v not in preds:
unlayered[v] = None
return (M,list(pred),list(unlayered))
def recurse(v):
if v in preds:
L = preds[v]
del preds[v]
for u in L:
if u in pred:
pu = pred[u]
del pred[u]
if pu is unmatched or recurse(pu):
M[v] = u
return 1
return 0
for v in unmatched: recurse(v)
nx.draw(G)
plt.show()
abc.text file containing edge info:e.g.,
1 2
3 1
4 5
3 2
Problem: Code is able to read abc.text file and displaying output as graph but it is not applying bipartite algorithm on it. I am new on python. Please suggest where is problem in code so that algorithm apply on text file and give graph as output with max matching.