Disclaimer This is for the semester project I'm working on right now. My question relates to detailing the level of implementation and is not part of the assessment framework. I am only writing this code as a way to test the theory that I am proposing for the paper I will write.
In addition, I have enjoyed reviewing the answers to this question , so please do not view this as a duplicate of this question
Problem :
I have a graph (G = (V, E)). At some point in my algorithm, I need to turn this into a hypergraph (in a sense) by “squeezing” several nodes (for example, v_1, v_2, ..., v_n ) into one node (say, v ). In the context of the problem, this means that I need to change the edges in E so that any edge of E between any of v_1, v_2, v_n and any other node u in v changed so that E now between u and v .
To fix that between any pair of nodes several different edges can exist, I need to make a unique identifier for each edge. I tried to do this using and ID, which at the moment I can not implement correctly.
This is what I tried :
class Edge: _ID = 0 def __init__(self, u, v, w, c,f=0): self.id = Edge._ID Edge._ID += 1 self.src = u self.dest = v self.weight = w self.capacity = c self.flow = f
However, when I try to instantiate a new edge, I get the following error:
>>> e = Edge(1,3,5,10,0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "UnsplittableFlow.py", line 14, in __init__ self.id = Edge._ID; Edge._ID += 1 UnboundLocalError: local variable '_ID' referenced before assignment
EDIT
With suggestions for some answers, I was able to fix the instant-time error. However, another error persists. Here is my code and errors:
class Edge: _ID = 0 def __init__(self, u, v, w, c,f=0): self.id = self._ID; self._ID += 1 self.src = u self.dest = v self.weight = w self.capacity = c self.flow = f
Error:
>>> e = Edge(1,3,5,10,0) >>> e.id 0 >>> Edge._ID 0 >>> f = Edge(2,3,5,10,0) >>> f.id 0 >>> Edge._ID 0
I would be grateful for any help
thanks