skm_tools.paths =============== .. py:module:: skm_tools.paths .. autoapi-nested-parse:: Shortest paths between nodes of interest. Attributes ---------- .. autoapisummary:: skm_tools.paths.logger Functions --------- .. autoapisummary:: skm_tools.paths.get_paths skm_tools.paths.path_edges skm_tools.paths.path_subgraph Module Contents --------------- .. py:data:: logger .. py:function:: get_paths(g, sources, targets, directed=True, all_shortest=True, shortest_overall=False, max_paths=None) Shortest paths from each source to each target. :param g: Graph to search, any networkx graph type. Edge weights are not used: a path's length is its number of edges. :type g: networkx.Graph :param sources: Start node(s) of the paths. Nodes not in `g` are skipped, with a warning. :type sources: node or iterable of nodes :param targets: End node(s) of the paths. Nodes not in `g` are skipped, with a warning. :type targets: node or iterable of nodes :param directed: Follow edge direction (default). If False, search the undirected view of `g`. Ignored if `g` is undirected. :type directed: bool :param all_shortest: Return all shortest paths between each source and target (default), or only one of them. :type all_shortest: bool :param shortest_overall: If True, for each target keep only the paths that are as short as the shortest path from any of the sources (so a target reached in 2 steps from one source and 4 from another only keeps the 2-step paths). Default False: keep the shortest paths from every source. :type shortest_overall: bool :param max_paths: Stop after this many paths, with a warning (default: no limit). The number of shortest paths can grow very fast in dense networks. :type max_paths: int, optional :returns: Paths as lists of nodes, from source to target, by target and then by source (in the given order). Source/target pairs without a path are skipped. A node that is both a source and a target gives no path. :rtype: list of list .. rubric:: Examples >>> import networkx as nx >>> g = nx.DiGraph([("A", "B"), ("B", "C"), ("A", "D"), ("D", "C")]) >>> sorted(get_paths(g, "A", "C")) [['A', 'B', 'C'], ['A', 'D', 'C']] .. py:function:: path_edges(g, paths) The edges of `g` along each path. :param g: Graph the paths are in. :type g: networkx.Graph :param paths: Paths as lists of nodes, e.g. from :func:`get_paths`. :type paths: list of list :returns: Unique edges, in path order: ``(u, v)``, or ``(u, v, key)`` for every parallel edge if `g` is a multigraph. For an undirected search on a directed graph (``get_paths(..., directed=False)``), a step u-v gives whichever of ``(u, v)`` and ``(v, u)`` exist in `g`. :rtype: list of tuple .. rubric:: Examples >>> import networkx as nx >>> g = nx.DiGraph([("A", "B"), ("B", "C")]) >>> path_edges(g, [["A", "B", "C"]]) [('A', 'B'), ('B', 'C')] .. py:function:: path_subgraph(g, paths) Edge-induced subgraph of `g` made of the edges along `paths` (a copy). :param g: Graph the paths are in. :type g: networkx.Graph :param paths: Paths as lists of nodes, e.g. from :func:`get_paths`. :type paths: list of list :returns: Same type as `g`, with only the nodes and edges on the paths (and their attributes). :rtype: networkx.Graph