(klass, matrix, mode="directed")
return klass(nvert, edges=edges, directed=(mode == "directed"))
Construct graph from sparse matrix, unweighted
Construct graph from sparse matrix, unweighted
Construct graph from sparse matrix unweighted
def _graph_from_sparse_matrix(klass, matrix, mode="directed"):
    """Construct graph from sparse matrix, unweighted"""
    # This function assumes there is scipy and the matrix is a scipy sparse
    # matrix. The caller should make sure those conditions are met.
    from scipy import sparse

    if not isinstance(matrix, sparse.coo_matrix):
        matrix = matrix.tocoo()

    nvert = max(matrix.shape)
    if min(matrix.shape) != nvert:
        raise ValueError("Matrix must be square")

    # Shorthand notation
    m = matrix

    mode = _convert_mode_argument(mode)

    if mode == "directed":
        edges = sum(
            ([(i, j)] * n for i, j, n in zip(m.row, m.col, m.data)),
            [],
        )

    elif mode in ("max", "plus"):
        fun = max if mode == "max" else add
        nedges = {}
        for i, j, n in zip(m.row, m.col, m.data):
            pair = (i, j) if i < j else (j, i)
            nedges[pair] = fun(nedges.get(pair, 0), n)

        edges = sum(
            ([e] * n for e, n in nedges.items()),
            [],
        )

    elif mode == "min":
        tmp = {(i, j): n for i, j, n in zip(m.row, m.col, m.data)}

        nedges = {}
        for pair, weight in tmp.items():
            i, j = pair
            if i == j:
                nedges[pair] = weight
            elif i < j:
                nedges[pair] = min(weight, tmp.get((j, i), 0))

        edges = sum(
            ([e] * n for e, n in nedges.items()),
            [],
        )

    elif mode == "upper":
        edges = sum(
            ([(i, j)] * n for i, j, n in zip(m.row, m.col, m.data) if j >= i),
            [],
        )

    elif mode == "lower":
        edges = sum(
            ([(i, j)] * n for i, j, n in zip(m.row, m.col, m.data) if j <= i),
            [],
        )

    else:
        raise ValueError("invalid mode")

    return klass(nvert, edges=edges, directed=(mode == "directed"))
def _graph_from_sparse_matrix ( klass , matrix , mode = "directed" ) : # This function assumes there is scipy and the matrix is a scipy sparse # matrix. The caller should make sure those conditions are met. from scipy import sparse if not isinstance ( matrix , sparse . coo_matrix ) : matrix = matrix . tocoo ( ) nvert = max ( matrix . shape ) if min ( matrix . shape ) != nvert : raise ValueError ( "Matrix must be square" ) # Shorthand notation m = matrix mode = _convert_mode_argument ( mode ) if mode == "directed" : edges = sum ( ( [ ( i , j ) ] * n for i , j , n in zip ( m . row , m . col , m . data ) ) , [ ] , ) elif mode in ( "max" , "plus" ) : fun = max if mode == "max" else add nedges = { } for i , j , n in zip ( m . row , m . col , m . data ) : pair = ( i , j ) if i < j else ( j , i ) nedges [ pair ] = fun ( nedges . get ( pair , 0 ) , n ) edges = sum ( ( [ e ] * n for e , n in nedges . items ( ) ) , [ ] , ) elif mode == "min" : tmp = { ( i , j ) : n for i , j , n in zip ( m . row , m . col , m . data ) } nedges = { } for pair , weight in tmp . items ( ) : i , j = pair if i == j : nedges [ pair ] = weight elif i < j : nedges [ pair ] = min ( weight , tmp . get ( ( j , i ) , 0 ) ) edges = sum ( ( [ e ] * n for e , n in nedges . items ( ) ) , [ ] , ) elif mode == "upper" : edges = sum ( ( [ ( i , j ) ] * n for i , j , n in zip ( m . row , m . col , m . data ) if j >= i ) , [ ] , ) elif mode == "lower" : edges = sum ( ( [ ( i , j ) ] * n for i , j , n in zip ( m . row , m . col , m . data ) if j <= i ) , [ ] , ) else : raise ValueError ( "invalid mode" ) return klass ( nvert , edges = edges , directed = ( mode == "directed" ) )