python - Randomized contraction algorithm for the Min Cuts in a graph -


i have tried write implementation of karger's algorithm solving min cut problem. here's code

def randomvertices(g):      v1 = g.keys() [random.randint(0,len(g)-1)]     v2 = g[v1] [random.randint(0,len(g[v1])-1)]     return v1, v2   def mergevertices(g):     v1,v2 = randomvertices(g)      g[v1].extend(g[v2])      x in g[v2]:         l = g[x]         in range(0,len(l)):             if l[i] == v2:                  l[i] = v1              while v1 in g[v1]:         g[v1].remove(v1)      del g[v2]  def mincut(g):      while len(g) > 2:          mergevertices(g)     return len(g[g.keys()[0]]) 

it's not commented should pretty self-explanatory. 'g' dictionary keys vertices of graph, each values vertices connected to. randomvertices gives me edge want contract, while mergevertices updates dictionary (and hence graph) deleting second vertex, updating values of keys linked vertex, , getting rid of self-loops. looks okay me if run on test graph keyerror @ l = g[x] stage, , can't understand why happening. hoping of may able me. thanks.

edit: code fine, error in way loaded file adjacent list. cobarzan, thank pointing out i'm not picking edges uniformly.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -