def random_path(graph): """ Constructs a randomly-generated valid hyperpath. Parameters ------------ graph : Hypergraph Returns ---------- path : Hyperpath A randomly generated hyperpath. """ def random_path_edges(node): edges = [] if node.edges: edge = random.sample(node.edges, 1) edges.append(edge[0]) for t in edge[0].tail: edges += random_path_edges(t) return edges edges = random_path_edges(graph.root) return pydecode.Path(graph, edges)
def path(self): if self._status != pulp.LpStatusOptimal: raise Exception("No optimal solution.") else: path_edges = [ edge for edge in self.hypergraph.edges if pulp.value(self.edge_vars[edge.id]) == 1.0 ] return pydecode.Path(self.hypergraph, path_edges)
def outside_paths(graph, vertex): if vertex.id == graph.root.id: return [None] def has_vertex(path): for edge in path.edges: for tail in edge.tail: if vertex.id == tail.id: return True return False paths = [pydecode.Path(graph, list(edges)) for edges in _paths(graph.root, vertex)] paths = [path for path in paths if has_vertex(path)] return paths
def all_paths(graph): """ Constructs all possible hyperpaths through a hypergraph (for testing). Parameters ------------ graph : Hypergraph Returns ---------- paths : list of hyperpaths All hyperpaths in the graph. """ paths = [pydecode.Path(graph, list(edges)) for edges in _paths(graph.root)] return paths
def inside_paths(graph, vertex): paths = [pydecode.Path(graph, list(edges)) for edges in _paths(vertex)] return paths