def edges(self): """ Returns all directed edges in this edge-weighted digraph. :return: all edges in this edge-weighted digraph :rtype: collections.iterable[DirectedEdge] """ edges = Bag() for v in range(self._V): for e in self._adj[v]: edges.add(e) return edges
def main(): """Unit tests the DirectedDFS data type.""" G = Digraph.from_stream(InStream(None)) sources = Bag() for i in range(1, len(sys.argv)): s = int(sys.argv[i]) sources.add(s) dfs = DirectedDFS(G, *sources) print("Reachable vertices:") for v in range(0, G.V()): if dfs.is_marked(v): print(v, end=" ") print()
def __init__(self, V, E): self._V = V self._E = E self._adj = [] for _ in range(V): self._adj.append(Bag())
def __init__(self, V): self._V = V self._E = 0 self._indegree = [0] * V self._adj = [None] * V for v in range(V): self._adj[v] = Bag()
def __init__(self, V): if V < 0: raise ValueError("Number of vertices must be nonnegative") self._V = V self._E = 0 self._adj = [] # adjacency lists for _ in range(V): self._adj.append(Bag()) # Initialize all lists to empty bags.
def __init__(self, V): """ Initializes an empty edge-weighted graph with V vertices and 0 edges. :param V: the number of vertices :raises IllegalArgumentException: if V < 0 """ if V < 0: raise IllegalArgumentException( "Number of vertices must be nonnegative") self._V = V self._E = 0 self._adj = [None] * V for v in range(V): self._adj[v] = Bag()
def __init__(self, V): """ Initializes an empty graph with V vertices and 0 edges. param V the number of vertices :param V: number of vertices :raises: ValueError if V < 0 """ if V < 0: raise ValueError("Number of vertices must be nonnegative") self._V = V # number of vertices self._E = 0 # number of edges self._adj = [] # adjacency lists for _ in range(V): self._adj.append(Bag()) # Initialize all lists to empty bags.
def edges(self): """ Returns all edges in this edge-weighted graph. :return: all edges in this edge-weighted graph """ edges = Bag() for v in range(self._V): self_loops = 0 for e in self.adj(v): if e.other(v) > v: edges.add(e) elif e.other(v) is v: if self_loops % 2 is 0: edges.add(e) self_loops += 1 return edges
def __init__(self, V): self._V = V self._E = 0 self._adj = [Bag() for _ in range(V)]
def recognizes(self, txt): """Returns True if the text is matched by the regular expression. :param txt: the text :returns: True if the text is matched by the regular expression; False otherwise """ regex = self.regex graph = self.graph m = self.m dfs = DirectedDFS(graph, 0) pc = Bag() for v in range(0, graph.V()): if dfs.is_marked(v): pc.add(v) # Compute possible NFA states for txt[i+1] for i in range(0, len(txt)): if txt[i] == "*" or txt[i] == "|" or txt[i] == "(" or txt[i] == ")": raise ValueError("text contains the metacharacter '{}'".format( txt[i])) match = Bag() for v in pc: if v == m: continue if regex[v] == txt[i] or regex[i] == ".": match.add(v + 1) dfs = DirectedDFS(graph, *match) pc = Bag() for v in range(0, graph.V()): if dfs.is_marked(v): pc.add(v) # Optimization if no states reachable if pc.size() == 0: return False for v in pc: if v == m: return True return False
def edges(self): edges = Bag() for v in range(self._V): for e in self._adj[v]: edges.add(e) return edges