skm_tools.neighbors =================== .. py:module:: skm_tools.neighbors .. autoapi-nested-parse:: Neighbourhoods of nodes of interest. Functions --------- .. autoapisummary:: skm_tools.neighbors.neighborhood_nodes skm_tools.neighbors.get_neighborhood Module Contents --------------- .. py:function:: neighborhood_nodes(g, nodes, depth=1, direction='both') Nodes within `depth` steps of `nodes`. :param g: Graph to search. :type g: networkx.Graph :param nodes: Start node(s). Nodes not in `g` are ignored, with a warning. :type nodes: node or iterable of nodes :param depth: Number of steps to expand: a positive integer (default 1: first neighbours), or None for no limit (everything reachable). :type depth: int or None :param direction: For directed graphs: follow edges in both directions (default, as an undirected graph), only downstream (successors), or only upstream (predecessors). Ignored for undirected graphs. :type direction: {"both", "out", "in"} :returns: Node -> its distance (number of steps) from the closest start node, for the start nodes (distance 0) and every node reached. :rtype: dict .. rubric:: Examples >>> import networkx as nx >>> g = nx.DiGraph([("A", "B"), ("B", "C"), ("D", "A")]) >>> neighborhood_nodes(g, "A", depth=2, direction="out") {'A': 0, 'B': 1, 'C': 2} .. py:function:: get_neighborhood(g, nodes, depth=1, direction='both', induced=True, distance_attr='distance') Subgraph of `g` around `nodes` (a copy). :param g: Graph to search. :type g: networkx.Graph :param nodes: Start node(s). Nodes not in `g` are ignored, with a warning. :type nodes: node or iterable of nodes :param depth: Number of steps to expand (default 1: first neighbours; None: no limit). :type depth: int or None :param direction: For directed graphs: follow edges in both directions (default), only downstream, or only upstream. See :func:`neighborhood_nodes`. :type direction: {"both", "out", "in"} :param induced: If True (default), node-induced: every edge of `g` between the nodes found. If False, only the edges followed while expanding (each node reached through the edges from the previous step), so e.g. two second neighbours that happen to interact aren't linked. :type induced: bool :param distance_attr: Node attribute for each node's number of steps from the closest start node (default ``"distance"``; None: don't add it). :type distance_attr: str or None :returns: Same type as `g`, with node and edge attributes, and the distances (see `distance_attr`). :rtype: networkx.Graph .. rubric:: Examples >>> import networkx as nx >>> g = nx.DiGraph([("A", "B"), ("B", "C"), ("C", "D")]) >>> sorted(get_neighborhood(g, "B").nodes()) ['A', 'B', 'C']