Exemplo n.º 1
0
def ego_graph(G, n, radius=1, center=True, undirected=False, distance=None):
    """
    Compute the  induced subgraph of neighbors centered at node n,
    within a given radius.

    Parameters
    ----------
    G : cugraph.Graph, networkx.Graph, CuPy or SciPy sparse matrix
        Graph or matrix object, which should contain the connectivity
        information. Edge weights, if present, should be single or double
        precision floating point values.
    n : integer
        A single node
    radius: integer, optional
        Include all neighbors of distance<=radius from n.
    center: bool, optional
        Defaults to True. False is not supported
    undirected: bool, optional
        Defaults to False. True is not supported
    distance: key, optional
        Distances are counted in hops from n. Other cases are not supported.

    Returns
    -------
    G_ego : cuGraph.Graph or networkx.Graph
        A graph descriptor with a minimum spanning tree or forest.
        The networkx graph will not have all attributes copied over

    Examples
    --------
    >>> M = cudf.read_csv('datasets/karate.csv',
                          delimiter = ' ',
                          dtype=['int32', 'int32', 'float32'],
                          header=None)
    >>> G = cugraph.Graph()
    >>> G.from_cudf_edgelist(M, source='0', destination='1')
    >>> ego_graph = cugraph.ego_graph(G, seed, radius=2)

    """

    (G, input_type) = ensure_cugraph_obj(G, nx_weight_attr="weight")
    result_graph = type(G)()

    if G.renumbered is True:
        n = G.lookup_internal_vertex_id(cudf.Series([n]))

    df, offsets = egonet_wrapper.egonet(G, n, radius)

    if G.renumbered:
        df = G.unrenumber(df, "src")
        df = G.unrenumber(df, "dst")

    if G.edgelist.weights:
        result_graph.from_cudf_edgelist(
            df, source="src", destination="dst", edge_attr="weight"
        )
    else:
        result_graph.from_cudf_edgelist(df, source="src", destination="dst")
    return _convert_graph_to_output_type(result_graph, input_type)
Exemplo n.º 2
0
def batched_ego_graphs(G,
                       seeds,
                       radius=1,
                       center=True,
                       undirected=False,
                       distance=None):
    """
    Compute the  induced subgraph of neighbors for each node in seeds
    within a given radius.

    Parameters
    ----------
    G : cugraph.Graph, networkx.Graph, CuPy or SciPy sparse matrix
        Graph or matrix object, which should contain the connectivity
        information. Edge weights, if present, should be single or double
        precision floating point values.
    seeds : cudf.Series or list or cudf.DataFrame
        Specifies the seeds of the induced egonet subgraphs.
    radius: integer, optional
        Include all neighbors of distance<=radius from n.
    center: bool, optional
        Defaults to True. False is not supported
    undirected: bool, optional
        Defaults to False. True is not supported
    distance: key, optional
        Distances are counted in hops from n. Other cases are not supported.

    Returns
    -------
    ego_edge_lists : cudf.DataFrame or pandas.DataFrame
        GPU data frame containing all induced sources identifiers,
        destination identifiers, edge weights
    seeds_offsets: cudf.Series
        Series containing the starting offset in the returned edge list
        for each seed.
    """

    (G, input_type) = ensure_cugraph_obj(G, nx_weight_attr="weight")

    if G.renumbered is True:
        if isinstance(seeds, cudf.DataFrame):
            seeds = G.lookup_internal_vertex_id(seeds, seeds.columns)
        else:
            seeds = G.lookup_internal_vertex_id(cudf.Series(seeds))

    df, offsets = egonet_wrapper.egonet(G, seeds, radius)

    if G.renumbered:
        df = G.unrenumber(df, "src", preserve_order=True)
        df = G.unrenumber(df, "dst", preserve_order=True)

    return _convert_df_series_to_output_type(df, offsets, input_type)