示例#1
0
def k_truss(G, k):
    """
    Returns the K-Truss subgraph of a graph for a specific k.

    The k-truss of a graph is a subgraph where each edge is part of at least
    (k−2) triangles. K-trusses are used for finding tighlty knit groups of
    vertices in a graph. A k-truss is a relaxation of a k-clique in the graph
    and was define in [1]. Finding cliques is computationally demanding and
    finding the maximal k-clique is known to be NP-Hard.

    Parameters
    ----------
    G : cuGraph.Graph or networkx.Graph
        cuGraph graph descriptor with connectivity information. k-Trusses are
        defined for only undirected graphs as they are defined for
        undirected triangle in a graph.

    k : int
        The desired k to be used for extracting the k-truss subgraph.

    Returns
    -------
    G_truss : cuGraph.Graph or networkx.Graph
        A cugraph graph descriptor with the k-truss subgraph for the given k.
        The networkx graph will NOT have all attributes copied over
    """

    G, isNx = check_nx_graph(G)

    if isNx is True:
        k_sub = ktruss_subgraph(G, k)
        S = cugraph_to_nx(k_sub)
        return S
    else:
        return ktruss_subgraph(G, k)
示例#2
0
def maximum_spanning_tree(G,
                          weight=None,
                          algorithm="boruvka",
                          ignore_nan=False):
    """
    Returns a maximum spanning tree (MST) or forest (MSF) on an undirected
    graph

    Parameters
    ----------
    G : cuGraph.Graph or networkx.Graph
        cuGraph graph descriptor with connectivity information.
    weight : string
        default to the weights in the graph, if the graph edges do not have a
        weight attribute a default weight of 1 will be used.
    algorithm : string
        Default to 'boruvka'. The parallel algorithm to use when finding a
        maximum spanning tree.
    ignore_nan : bool
        Default to False
    Returns
    -------
    G_mst : cuGraph.Graph or networkx.Graph
        A graph descriptor with a maximum spanning tree or forest.
        The networkx graph will not have all attributes copied over
    """

    G, isNx = check_nx_graph(G)

    if isNx is True:
        mst = maximum_spanning_tree_subgraph(G)
        return cugraph_to_nx(mst)
    else:
        return maximum_spanning_tree_subgraph(G)
示例#3
0
def _convert_graph_to_output_type(G, input_type):
    """
    Given a cugraph.Graph, convert it to a new type appropriate for the
    graph algos in this module, based on input_type.
    """
    if is_nx_graph_type(input_type):
        return cugraph_to_nx(G)

    else:
        return G
示例#4
0
def _convert_graph_to_output_type(G, input_type):
    """
    Given a cugraph.Graph, convert it to a new type appropriate for the
    graph algos in this module, based on input_type.
    """
    if (nx is not None) and (input_type in [nx.Graph, nx.DiGraph]):
        return cugraph_to_nx(G)

    else:
        return G
示例#5
0
def minimum_spanning_tree(
    G, weight=None, algorithm="boruvka", ignore_nan=False
):
    """
    Returns a minimum spanning tree (MST) or forest (MSF) on an undirected
    graph

    Parameters
    ----------
    G : cuGraph.Graph or networkx.Graph
        cuGraph graph descriptor with connectivity information.

    weight : string
        default to the weights in the graph, if the graph edges do not have a
        weight attribute a default weight of 1 will be used.

    algorithm : string
        Default to 'boruvka'. The parallel algorithm to use when finding a
        minimum spanning tree.

    ignore_nan : bool
        Default to False

    Returns
    -------
    G_mst : 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_path / 'netscience.csv', delimiter='\t',
    ...                   dtype=['int32', 'int32', 'float32'], header=None)
    >>> G = cugraph.Graph()
    >>> G.from_cudf_edgelist(M, source='0', destination='1')
    >>> # cugraph.minimum_spanning_tree(G)

    """
    # FIXME: Uncomment out the above example

    G, isNx = ensure_cugraph_obj_for_nx(G)

    if isNx is True:
        mst = _minimum_spanning_tree_subgraph(G)
        return cugraph_to_nx(mst)
    else:
        return _minimum_spanning_tree_subgraph(G)
示例#6
0
def subgraph(G, vertices):
    """
    Compute a subgraph of the existing graph including only the specified
    vertices.  This algorithm works with both directed and undirected graphs
    and does not actually traverse the edges, but instead simply pulls out any
    edges that are incident on vertices that are both contained in the vertices
    list.

    Parameters
    ----------
    G : cugraph.Graph
        cuGraph graph descriptor

    vertices : cudf.Series or cudf.DataFrame
        Specifies the vertices of the induced subgraph. For multi-column
        vertices, vertices should be provided as a cudf.DataFrame

    Returns
    -------
    Sg : cugraph.Graph
        A graph object containing the subgraph induced by the given vertex set.

    Examples
    --------
    >>> gdf = cudf.read_csv(datasets_path / 'karate.csv',
    ...                     delimiter = ' ',
    ...                     dtype=['int32', 'int32', 'float32'],
    ...                     header=None)
    >>> G = cugraph.Graph()
    >>> G.from_cudf_edgelist(gdf, source='0', destination='1')
    >>> verts = np.zeros(3, dtype=np.int32)
    >>> verts[0] = 0
    >>> verts[1] = 1
    >>> verts[2] = 2
    >>> sverts = cudf.Series(verts)
    >>> Sg = cugraph.subgraph(G, sverts)

    """

    G, isNx = ensure_cugraph_obj_for_nx(G)

    if G.renumbered:
        if isinstance(vertices, cudf.DataFrame):
            vertices = G.lookup_internal_vertex_id(vertices, vertices.columns)
        else:
            vertices = G.lookup_internal_vertex_id(vertices)

    result_graph = type(G)()

    df = subgraph_extraction_wrapper.subgraph(G, vertices)
    src_names = "src"
    dst_names = "dst"

    if G.renumbered:
        df, src_names = G.unrenumber(df, src_names, get_column_names=True)
        df, dst_names = G.unrenumber(df, dst_names, get_column_names=True)

    if G.edgelist.weights:
        result_graph.from_cudf_edgelist(df,
                                        source=src_names,
                                        destination=dst_names,
                                        edge_attr="weight")
    else:
        result_graph.from_cudf_edgelist(df,
                                        source=src_names,
                                        destination=dst_names)

    if isNx is True:
        result_graph = cugraph_to_nx(result_graph)

    return result_graph
示例#7
0
def k_core(G, k=None, core_number=None):
    """
    Compute the k-core of the graph G based on the out degree of its nodes. A
    k-core of a graph is a maximal subgraph that contains nodes of degree k or
    more. This call does not support a graph with self-loops and parallel
    edges.

    Parameters
    ----------
    G : cuGraph.Graph or networkx.Graph
        cuGraph graph descriptor with connectivity information. The graph
        should contain undirected edges where undirected edges are represented
        as directed edges in both directions. While this graph can contain edge
        weights, they don't participate in the calculation of the k-core.
    k : int, optional
        Order of the core. This value must not be negative. If set to None, the
        main core is returned.
    core_number : cudf.DataFrame, optional
        Precomputed core number of the nodes of the graph G containing two
        cudf.Series of size V: the vertex identifiers and the corresponding
        core number values. If set to None, the core numbers of the nodes are
        calculated internally.

        core_number['vertex'] : cudf.Series
            Contains the vertex identifiers
        core_number['values'] : cudf.Series
            Contains the core number of vertices

    Returns
    -------
    KCoreGraph : cuGraph.Graph
        K Core of the input graph

    Examples
    --------
    >>> gdf = cudf.read_csv('datasets/karate.csv', delimiter=' ',
    >>>                   dtype=['int32', 'int32', 'float32'], header=None)
    >>> G = cugraph.Graph()
    >>> G.from_cudf_edgelist(gdf, source='0', destination='1')
    >>> KCoreGraph = cugraph.k_core(G)
    """

    G, isNx = check_nx_graph(G)

    mytype = type(G)
    KCoreGraph = mytype()

    if mytype is not Graph:
        raise Exception("directed graph not supported")

    if core_number is not None:
        if G.renumbered is True:
            core_number = G.add_internal_vertex_id(core_number,
                                                   "vertex",
                                                   "vertex",
                                                   drop=True)
    else:
        core_number = core_number_wrapper.core_number(G)
        core_number = core_number.rename(columns={"core_number": "values"},
                                         copy=False)
    print(core_number)
    if k is None:
        k = core_number["values"].max()

    k_core_df = k_core_wrapper.k_core(G, k, core_number)

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

    if G.edgelist.weights:
        KCoreGraph.from_cudf_edgelist(k_core_df,
                                      source="src",
                                      destination="dst",
                                      edge_attr="weight")
    else:
        KCoreGraph.from_cudf_edgelist(k_core_df,
                                      source="src",
                                      destination="dst")

    if isNx is True:
        KCoreGraph = cugraph_to_nx(KCoreGraph)

    return KCoreGraph
def subgraph(G, vertices):
    """
    Compute a subgraph of the existing graph including only the specified
    vertices.  This algorithm works for both directed and undirected graphs,
    it does not actually traverse the edges, simply pulls out any edges that
    are incident on vertices that are both contained in the vertices list.

    Parameters
    ----------
    G : cugraph.Graph
        cuGraph graph descriptor
    vertices : cudf.Series
        Specifies the vertices of the induced subgraph

    Returns
    -------
    Sg : cugraph.Graph
        A graph object containing the subgraph induced by the given vertex set.

    Examples
    --------
    >>> gdf = cudf.read_csv('datasets/karate.csv',
                          delimiter = ' ',
                          dtype=['int32', 'int32', 'float32'],
                          header=None)
    >>> G = cugraph.Graph()
    >>> G.from_cudf_edgelist(gdf, source='0', destination='1')
    >>> verts = numpy.zeros(3, dtype=numpy.int32)
    >>> verts[0] = 0
    >>> verts[1] = 1
    >>> verts[2] = 2
    >>> sverts = cudf.Series(verts)
    >>> Sg = cugraph.subgraph(G, sverts)
    """

    null_check(vertices)

    G, isNx = check_nx_graph(G)

    if G.renumbered:
        vertices = G.lookup_internal_vertex_id(vertices)

    result_graph = type(G)()

    df = subgraph_extraction_wrapper.subgraph(G, vertices)

    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")

    if isNx is True:
        result_graph = cugraph_to_nx(result_graph)

    return result_graph