Ejemplo n.º 1
0
def test_graph_disconnect():
    """Simple disconnected undirected graph, used for testing.
    10 nodes, 10 edges.
    """
    row = np.array([1, 2, 3, 4, 6, 6, 6, 7, 8, 9])
    col = np.array([1, 3, 2, 5, 4, 5, 7, 9, 9, 9])
    data = np.array([1, 2.5, 1, 2, 2, 1, 2, 2, 1.5, 2])
    adjacency = sparse.csr_matrix((data, (row, col)), shape=(10, 10))
    return directed2undirected(adjacency)
Ejemplo n.º 2
0
def edgelist2adjacency(edgelist: list,
                       undirected: bool = False) -> sparse.csr_matrix:
    """Build an adjacency matrix from a list of edges.

    Parameters
    ----------
    edgelist : list
        List of edges as pairs (i, j) or triplets (i, j, w) for weighted edges.
    undirected : bool
        If ``True``, return a symmetric adjacency.

    Returns
    -------
    adjacency : sparse.csr_matrix

    Examples
    --------
    >>> edgelist = [(0, 1), (1, 2), (2, 0)]
    >>> adjacency = edgelist2adjacency(edgelist)
    >>> adjacency.shape, adjacency.nnz
    ((3, 3), 3)
    >>> adjacency = edgelist2adjacency(edgelist, undirected=True)
    >>> adjacency.shape, adjacency.nnz
    ((3, 3), 6)
    >>> weighted_edgelist = [(0, 1, 0.2), (1, 2, 4), (2, 0, 1.3)]
    >>> adjacency = edgelist2adjacency(weighted_edgelist)
    >>> adjacency.dtype
    dtype('float64')
    """
    edges = np.array(edgelist)
    row, col = edges[:, 0].astype(np.int32), edges[:, 1].astype(np.int32)
    n = max(row.max(), col.max()) + 1
    if edges.shape[1] > 2:
        data = edges[:, 2]
    else:
        data = np.ones_like(row, dtype=bool)
    adjacency = sparse.csr_matrix((data, (row, col)), shape=(n, n))
    if undirected:
        adjacency = directed2undirected(adjacency)
    return adjacency
Ejemplo n.º 3
0
def test_graph():
    """Simple undirected graph, used for testing.
    10 nodes, 10 edges.
    """
    return directed2undirected(test_digraph(), weighted=True)