def disconnect(self, v, w): """Disconnect two vertices.""" if not v in self: raise ValueError if not w in self: raise ValueError if w == v: raise ValueError('{} and {} are the same vertex.'.format(v, w)) if not contains(self(v), w): raise ValueError('{} and {} are not connected.'.format(v, w)) # Only support undirected edges assert contains(self(w), v) self.neighborhoods[v] -= w self.neighborhoods[w] -= v
def components(graph): """Return a list of the connected components of in the graph.""" done = 0 for v in iterate(graph.vertices): if contains(done, v): continue component = sum(bfs(graph, v)) yield component done |= component
def trivial_case(N, left, right, v): # No neighbors if contains(left, N[v]): return True # Twins for u in iterate(left): if N[v] & right == subtract(N[u], v) & right: return True return False
def split(self, v, w): """Split edge between two vertices.""" if not v in self: raise ValueError if not w in self: raise ValueError if w == v: raise ValueError('{} and {} are the same vertex.'.format(v, w)) if contains(self(v), w): raise ValueError('{} and {} are not connected.'.format(v, w)) # Only support undirected edges assert contains(self(w), v) new = bit(size(self.vertices)) self.add(new) self.disconnect(v, w) self.connect(v, new) self.connect(w, new)
def __contains__(self, v): """Test if we contain vertex v.""" return contains(self.vertices, v)