[docs]defis_semi_directed_path(G,nodes):"""Returns True if and only if `nodes` form a semi-directed path in `G`. A *semi-directed path* in a graph is a nonempty sequence of nodes in which no node appears more than once in the sequence, each adjacent pair of nodes in the sequence is adjacent in the graph and where each pair of adjacent nodes does not contain a directed endpoint in the direction towards the start of the sequence. That is ``(a -> b o-> c <-> d -> e)`` is not a semi-directed path from ``a`` to ``e`` because ``d *-> c`` is a directed endpoint in the direction towards ``a``. Parameters ---------- G : graph A mixed-edge graph. nodes : list A list of one or more nodes in the graph `G`. Returns ------- bool Whether the given list of nodes represents a semi-directed path in `G`. Notes ----- This function is very similar to networkx's :func:`networkx.algorithms.simple_paths.is_simple_path` function. """# The empty list is not a valid path. Could also return# NetworkXPointlessConcept here.iflen(nodes)==0:returnFalse# If the list is a single node, just check that the node is actually# in the graph.iflen(nodes)==1:returnnodes[0]inG# check that all nodes in the list are in the graph, if at least one# is not in the graph, then this is not a semi-directed pathifnotall(ninGforninnodes):returnFalse# If the list contains repeated nodes, then it's not a semi-directed pathiflen(set(nodes))!=len(nodes):returnFalse# Test that each adjacent pair of nodes is adjacent and that there# is no directed endpoint towards the beginning of the sequence.foridxinrange(len(nodes)-1):u,v=nodes[idx],nodes[idx+1]ifG.has_edge(v,u,EdgeType.DIRECTED.value)orG.has_edge(v,u,EdgeType.BIDIRECTED.value):returnFalseelifnotG.has_edge(u,v):returnFalsereturnTrue
[docs]defall_semi_directed_paths(G,source:Node,target:Node,cutoff:int=None):"""Generate all semi-directed paths from source to target in G. A semi-directed path is a path from ``source`` to ``target`` in that no end-point is directed from ``target`` to ``source``. I.e. ``target *-> source`` does not exist. Parameters ---------- G : Graph The graph. source : Node The source node. target : Node The target node. cutoff : integer, optional Depth to stop the search. Only paths of length <= cutoff are returned. Notes ----- This algorithm is very similar to networkx's :func:`networkx.algorithms.simple_paths.all_simple_paths` function. This algorithm uses a modified depth-first search to generate the paths [1]_. A single path can be found in $O(V+E)$ time but the number of semi-directed paths in a graph can be very large, e.g. $O(n!)$ in the complete graph of order $n$. This function does not check that a path exists between `source` and `target`. For large graphs, this may result in very long runtimes. Consider using `has_path` to check that a path exists between `source` and `target` before calling this function on large graphs. References ---------- .. [1] R. Sedgewick, "Algorithms in C, Part 5: Graph Algorithms", Addison Wesley Professional, 3rd ed., 2001. """ifsourcenotinG:raisenx.NodeNotFound("source node %s not in graph"%source)iftargetinG:targets={target}else:try:targets=set(target)# type: ignoreexceptTypeError:raisenx.NodeNotFound("target node %s not in graph"%target)ifsourceintargets:return_empty_generator()ifcutoffisNone:cutoff=len(G)-1ifcutoff<1:return_empty_generator()ifcutoffisNone:cutoff=len(G)-1return_all_semi_directed_paths_graph(G,source,targets,cutoff)
def_all_semi_directed_paths_graph(G,source,targets,cutoff,directed_edge_name="directed",bidirected_edge_name="bidirected"):"""See networkx's all_simple_paths function. This performs a depth-first search for all semi-directed paths from source to target. """# memoize each node that was already visitedvisited={source:True}# iterate over neighbors of sourcestack=[iter(G.neighbors(source))]# if source has no neighbors, then prev_nodes should be Noneprev_nodes=[source]whilestack:# get the iterator through nbrs for the current nodenbrs=stack[-1]prev_node=prev_nodes[-1]nbr=next(nbrs,None)# The first condition guarantees that there is not a directed endpoint# along the path from source to target that points towards source.if(G.has_edge(nbr,prev_node,directed_edge_name)orG.has_edge(nbr,prev_node,bidirected_edge_name))andnbrnotinvisited:# If we've found a directed edge from child to prev_node,# that we haven't visited, then we don't need to continue down this pathcontinueelifnbrisNone:# once all children are visited, pop the stack# and remove the child from the visited setstack.pop()visited.popitem()prev_nodes.pop()eliflen(visited)<cutoff:ifnbrinvisited:continueifnbrintargets:# we've found a path to a targetyieldlist(visited)+[nbr]visited[nbr]=Trueiftargets-set(visited.keys()):# expand stack until find all targetsstack.append(iter(G.neighbors(nbr)))prev_nodes.append(nbr)else:visited.popitem()# maybe other ways to childelse:# len(visited) == cutoff:fortargetin(targets&(set(nbrs)|{nbr}))-set(visited.keys()):yieldlist(visited)+[target]stack.pop()visited.popitem()prev_nodes.pop()