def degree_centrality(network: Network, mode: str = 'degree') -> dict:
    """Calculates the degree centrality of all nodes.

    Parameters
    ----------
    network : Network

        The :py:class:`Network` object that contains the network

    mode : str

        Can be chose nas 'degree', 'indegree', or 'outdegree'. Determines
        whether to calculate undirected/total degrees, indegrees, or degrees

    Examples
    --------
    Compute degree centrality in a simple network

    >>> import pathpy as pp
    >>> net = pp.Network(directed=True)
    >>> net.add_edge('a', 'x')
    >>> net.add_edge('x', 'b')
    >>> c = pp.algorithms.centralities.degree_centrality(net)
    >>> c['a']
    1

    >>> c = pp.algorithms.centralities.degree_centrality(net, mode='indegree')
    >>> c['a']
    0

    """
    d: dict = dict()
    if mode not in set(['degree', 'indegree', 'outdegree']):
        LOG.error('Mode must be \'degree\', \'indegree\' or \'outdegree\'')
        raise KeyError

    for v in network.nodes.keys():
        if mode == 'indegree':
            d[v] = network.indegrees()[v]
        elif mode == 'outdegree':
            d[v] = network.outdegrees()[v]
        else:
            d[v] = network.degrees()[v]

    return d
Exemple #2
0
def local_clustering_coefficient(network: Network, v: str) -> float:
    """Calculates the local clustering coefficient of a node in a network.


    The local clustering coefficient of any node with an (out-)degree smaller
    than two is defined as zero. For all other nodes, it is defined as:

        cc(c) := 2*k(i)/(d_i(d_i-1))

        or

        cc(c) := k(i)/(d_out_i(d_out_i-1))

        in undirected and directed networks respectively.

    Parameters
    ----------
    network : Network

        The network in which to calculate the local clustering coefficient

    node : str

        The node for which the local clustering coefficient shall be calculated

    """
    lcc: float = 0.
    d = network.degrees()
    o = network.outdegrees()

    if network.directed and o[v] >= 2 or network.directed == False and d[
            v] >= 2:
        k: int = 0
        # compute set of closed triads
        closed = closed_triads(network, v)
        k = len(closed)

        if network.directed:
            return k / (o[v] * (o[v] - 1))
        else:
            return 2 * k / (d[v] * (d[v] - 1))
    else:
        return 0.
Exemple #3
0
def degree_assortativity(network: Network,
                         mode: str = 'total',
                         weight: Weight = None) -> float:
    """Calculates the degree assortativity coefficient of a network.

    Parameters
    ----------

    network : Network

        The network in which to calculate the Molloy-Reed fraction
    """
    A = network.adjacency_matrix(weight=weight)
    m = np.sum(A)

    d = network.degrees(weight)
    if network.directed and mode == 'in':
        d = network.indegrees(weight)
    elif network.directed and mode == 'out':
        d = network.outdegrees(weight)
    elif network.directed and mode == 'total':
        d = network.degrees(weight)
    elif not network.directed:
        m = m / 2.
    idx = network.nodes.index

    cov: float = 0.
    var: float = 0.
    for i in network.nodes.keys():
        for j in network.nodes.keys():
            cov += (A[idx[i], idx[j]] - (d[i] * d[j]) / (2 * m)) * d[i] * d[j]
            if i != j:
                var -= (d[i] * d[j]) / (2 * m) * d[i] * d[j]
            else:
                var += (d[i] - (d[i] * d[j]) / (2 * m)) * d[i] * d[j]
    return cov / var