0

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.

rahmed
  • 1
  • 3
  • Do you have any errors? There is a syntax error in you code (at least in the code you posted) - no indent after `def bipartiteMatch(G):`. If it's not the case, and you don't have this error in your actual program, fix the posted code please. – Andrew Che Mar 18 '17 at 22:19
  • I am not getting error with this code. You are right, no incident after def bipartiteMatch(G):. I want to apply def bipartiteMatch(G) on abc.txt file and produce graph as output. But nothing is happening with this. There is some mistake which I am not able to identify. Please suggest so i can get graph with maximum matching. – rahmed Mar 18 '17 at 22:50

0 Answers0