Beispiel #1
0
 def test_zero_personalization_vector(self):
     G = nx.complete_graph(4)
     personalize = {0: 0, 1: 0, 2: 0, 3: 0}
     pytest.raises(ZeroDivisionError,
                   nx.pagerank,
                   G,
                   personalization=personalize)
Beispiel #2
0
 def test_k5(self):
     G = nx.complete_graph(5, create_using=nx.DiGraph())
     assert list(nx.clustering(G, weight="weight").values()) == [1, 1, 1, 1, 1]
     assert nx.average_clustering(G, weight="weight") == 1
     G.remove_edge(1, 2)
     assert list(nx.clustering(G, weight="weight").values()) == [
         11.0 / 12.0,
         1.0,
         1.0,
         11.0 / 12.0,
         11.0 / 12.0,
     ]
     assert nx.clustering(G, [1, 4], weight="weight") == {1: 1.0, 4: 11.0 / 12.0}
     G.remove_edge(2, 1)
     assert list(nx.clustering(G, weight="weight").values()) == [
         5.0 / 6.0,
         1.0,
         1.0,
         5.0 / 6.0,
         5.0 / 6.0,
     ]
     assert nx.clustering(G, [1, 4], weight="weight") == {
         1: 1.0,
         4: 0.83333333333333337,
     }
Beispiel #3
0
    def test_is_at_free(self):
        is_at_free = nx.asteroidal.is_at_free

        cycle = nx.cycle_graph(6)
        assert not is_at_free(cycle)

        path = nx.path_graph(6)
        assert is_at_free(path)

        small_graph = nx.complete_graph(2)
        assert is_at_free(small_graph)

        petersen = nx.petersen_graph()
        assert not is_at_free(petersen)

        clique = nx.complete_graph(6)
        assert is_at_free(clique)
Beispiel #4
0
 def test_k5(self):
     G = nx.complete_graph(5)
     assert list(nx.triangles(G).values()) == [6, 6, 6, 6, 6]
     assert sum(nx.triangles(G).values()) / 3.0 == 10
     assert nx.triangles(G, 1) == 6
     G.remove_edge(1, 2)
     assert list(nx.triangles(G).values()) == [5, 3, 3, 5, 5]
     assert nx.triangles(G, 1) == 3
Beispiel #5
0
 def test_incomplete_personalization(self):
     G = nx.complete_graph(4)
     personalize = {3: 1}
     answer = {
         0: 0.22077931820379187,
         1: 0.22077931820379187,
         2: 0.22077931820379187,
         3: 0.3376620453886241,
     }
     p = nx.pagerank(G, alpha=0.85, personalization=personalize)
     for n in G:
         assert almost_equal(p[n], answer[n], places=4)
Beispiel #6
0
 def test_personalization(self):
     G = nx.complete_graph(4)
     personalize = {0: 1, 1: 1, 2: 4, 3: 4}
     answer = {
         0: 0.23246732615667579,
         1: 0.23246732615667579,
         2: 0.267532673843324,
         3: 0.2675326738433241,
     }
     p = nx.pagerank(G, alpha=0.85, personalization=personalize)
     for n in G:
         assert almost_equal(p[n], answer[n], places=4)
Beispiel #7
0
 def test_k5(self):
     G = nx.complete_graph(5)
     assert list(nx.clustering(G).values()) == [1, 1, 1, 1, 1]
     assert nx.average_clustering(G) == 1
     G.remove_edge(1, 2)
     assert list(nx.clustering(G).values()) == [
         5.0 / 6.0,
         1.0,
         1.0,
         5.0 / 6.0,
         5.0 / 6.0,
     ]
     assert nx.clustering(G, [1, 4]) == {1: 1.0, 4: 0.83333333333333337}
Beispiel #8
0
 def test_K5_unweighted(self):
     """Katz centrality: K5"""
     G = nx.complete_graph(5)
     alpha = 0.1
     b = nx.katz_centrality(G, alpha, weight=None)
     v = math.sqrt(1 / 5.0)
     b_answer = dict.fromkeys(G, v)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     nstart = dict([(n, 1) for n in G])
     b = nx.eigenvector_centrality_numpy(G, weight=None)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n], places=3)
Beispiel #9
0
 def test_K5(self):
     """Katz centrality: K5"""
     G = nx.complete_graph(5)
     alpha = 0.1
     b = nx.katz_centrality(G, alpha)
     v = math.sqrt(1 / 5.0)
     b_answer = dict.fromkeys(G, v)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     nstart = dict([(n, 1) for n in G])
     b = nx.katz_centrality(G, alpha, nstart=nstart)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Beispiel #10
0
    def test_K5(self):
        """Eigenvector centrality: K5"""
        G = nx.complete_graph(5)
        b = nx.eigenvector_centrality(G)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality(G, nstart=nstart)
        for n in sorted(G):
            assert almost_equal(b[n], b_answer[n])

        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert almost_equal(b[n], b_answer[n], places=3)
Beispiel #11
0
    def setup_method(self):
        # self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.K5 = nx.complete_graph(5)

        # F = nx.Graph()  # Florentine families
        # F.add_edge('Acciaiuoli', 'Medici')
        # F.add_edge('Castellani', 'Peruzzi')
        # F.add_edge('Castellani', 'Strozzi')
        # F.add_edge('Castellani', 'Barbadori')
        # F.add_edge('Medici', 'Barbadori')
        # F.add_edge('Medici', 'Ridolfi')
        # F.add_edge('Medici', 'Tornabuoni')
        # F.add_edge('Medici', 'Albizzi')
        # F.add_edge('Medici', 'Salviati')
        # F.add_edge('Salviati', 'Pazzi')
        # F.add_edge('Peruzzi', 'Strozzi')
        # F.add_edge('Peruzzi', 'Bischeri')
        # F.add_edge('Strozzi', 'Ridolfi')
        # F.add_edge('Strozzi', 'Bischeri')
        # F.add_edge('Ridolfi', 'Tornabuoni')
        # F.add_edge('Tornabuoni', 'Guadagni')
        # F.add_edge('Albizzi', 'Ginori')
        # F.add_edge('Albizzi', 'Guadagni')
        # F.add_edge('Bischeri', 'Guadagni')
        # F.add_edge('Guadagni', 'Lamberteschi')
        # self.F = F

        G = nx.DiGraph()
        G.add_edge(0, 5)
        G.add_edge(1, 5)
        G.add_edge(2, 5)
        G.add_edge(3, 5)
        G.add_edge(4, 5)
        G.add_edge(5, 6)
        G.add_edge(5, 7)
        G.add_edge(5, 8)
        self.G = G
Beispiel #12
0
 def test_k5(self):
     G = nx.complete_graph(5)
     assert list(nx.square_clustering(G).values()) == [1, 1, 1, 1, 1]
Beispiel #13
0
 def test_run_katz(self):
     G1 = nx.complete_graph(10)
     G = nx.Graph()
     G.add_edges_from(G1.edges, weight=1)
     alpha = 0.1
     nx.builtin.katz_centrality(G, alpha)
Beispiel #14
0
def watts_strogatz_graph(n, k, p, seed=None):
    """Returns a Watts–Strogatz small-world graph.

    Parameters
    ----------
    n : int
        The number of nodes
    k : int
        Each node is joined with its `k` nearest neighbors in a ring
        topology.
    p : float
        The probability of rewiring each edge
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    See Also
    --------
    newman_watts_strogatz_graph()
    connected_watts_strogatz_graph()

    Notes
    -----
    First create a ring over $n$ nodes [1]_.  Then each node in the ring is joined
    to its $k$ nearest neighbors (or $k - 1$ neighbors if $k$ is odd).
    Then shortcuts are created by replacing some edges as follows: for each
    edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest neighbors"
    with probability $p$ replace it with a new edge $(u, w)$ with uniformly
    random choice of existing node $w$.

    In contrast with :func:`newman_watts_strogatz_graph`, the random rewiring
    does not increase the number of edges. The rewired graph is not guaranteed
    to be connected as in :func:`connected_watts_strogatz_graph`.

    References
    ----------
    .. [1] Duncan J. Watts and Steven H. Strogatz,
       Collective dynamics of small-world networks,
       Nature, 393, pp. 440--442, 1998.
    """
    if k > n:
        raise nx.NetworkXError("k>n, choose smaller k or larger n")

    # If k == n, the graph is complete not Watts-Strogatz
    if k == n:
        return nx.complete_graph(n)

    G = nx.Graph()
    nodes = list(range(n))  # nodes are labeled 0 to n-1
    # connect each node to k/2 neighbors
    for j in range(1, k // 2 + 1):
        targets = nodes[j:] + nodes[0:j]  # first j nodes are now last in list
        G.add_edges_from(zip(nodes, targets))
    # rewire edges from each node
    # loop over all nodes in order (label) and neighbors in order (distance)
    # no self loops or multiple edges allowed
    for j in range(1, k // 2 + 1):  # outer loop is neighbors
        targets = nodes[j:] + nodes[0:j]  # first j nodes are now last in list
        # inner loop in node order
        for u, v in zip(nodes, targets):
            if seed.random() < p:
                w = seed.choice(nodes)
                # Enforce no self-loops or multiple edges
                while w == u or G.has_edge(u, w):
                    w = seed.choice(nodes)
                    if G.degree(u) >= n - 1:
                        break  # skip this rewiring
                else:
                    G.remove_edge(u, v)
                    G.add_edge(u, w)
    return G
Beispiel #15
0
 def test_k5(self):
     G = nx.complete_graph(5)
     assert nx.transitivity(G) == 1.0
     G.remove_edge(1, 2)
     assert nx.transitivity(G) == 0.875
Beispiel #16
0
 def test_simple_nx_to_gs(self):
     nx_g = nx.complete_graph(10, create_using=self.NXGraph)
     gs_g = g(nx_g)
     self.assert_convert_success(gs_g, nx_g)
Beispiel #17
0
def newman_watts_strogatz_graph(n, k, p, seed=None):
    """Returns a Newman–Watts–Strogatz small-world graph.

    Parameters
    ----------
    n : int
        The number of nodes.
    k : int
        Each node is joined with its `k` nearest neighbors in a ring
        topology.
    p : float
        The probability of adding a new edge for each edge.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Notes
    -----
    First create a ring over $n$ nodes [1]_.  Then each node in the ring is
    connected with its $k$ nearest neighbors (or $k - 1$ neighbors if $k$
    is odd).  Then shortcuts are created by adding new edges as follows: for
    each edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest
    neighbors" with probability $p$ add a new edge $(u, w)$ with
    randomly-chosen existing node $w$.  In contrast with
    :func:`watts_strogatz_graph`, no edges are removed.

    See Also
    --------
    watts_strogatz_graph()

    References
    ----------
    .. [1] M. E. J. Newman and D. J. Watts,
       Renormalization group analysis of the small-world network model,
       Physics Letters A, 263, 341, 1999.
       https://doi.org/10.1016/S0375-9601(99)00757-4
    """
    if k > n:
        raise nx.NetworkXError("k>=n, choose smaller k or larger n")

    # If k == n the graph return is a complete graph
    if k == n:
        return nx.complete_graph(n)

    G = empty_graph(n)
    nlist = list(G.nodes())
    fromv = nlist
    # connect the k/2 neighbors
    for j in range(1, k // 2 + 1):
        tov = fromv[j:] + fromv[0:j]  # the first j are now last
        for i in range(len(fromv)):
            G.add_edge(fromv[i], tov[i])
    # for each edge u-v, with probability p, randomly select existing
    # node w and add new edge u-w
    e = list(G.edges())
    for (u, v) in e:
        if seed.random() < p:
            w = seed.choice(nlist)
            # no self-loops and reject if edge u-w exists
            # is that the correct NWS model?
            while w == u or G.has_edge(u, w):
                w = seed.choice(nlist)
                if G.degree(u) >= n - 1:
                    break  # skip this rewiring
            else:
                G.add_edge(u, w)
    return G
Beispiel #18
0
 def test_run_eigenvector(self):
     G1 = nx.complete_graph(10)
     G = nx.Graph()
     G.add_edges_from(G1.edges, weight=1)
     nx.builtin.eigenvector_centrality(G)
Beispiel #19
0
 def test_k5(self):
     G = nx.complete_graph(5)
     assert nx.generalized_degree(G, 0) == {3: 4}
     G.remove_edge(0, 1)
     assert nx.generalized_degree(G, 0) == {2: 3}