Beispiel #1
0
    def setup_class(cls):
        G = nx.DiGraph()
        edges = [
            (1, 2),
            (1, 3),
            (2, 4),
            (3, 2),
            (3, 5),
            (4, 2),
            (4, 5),
            (4, 6),
            (5, 6),
            (5, 7),
            (5, 8),
            (6, 8),
            (7, 1),
            (7, 5),
            (7, 8),
            (8, 6),
            (8, 7),
        ]
        G.add_edges_from(edges, weight=2.0)
        cls.G = G.reverse()
        cls.G.alpha = 0.1
        cls.G.evc = [
            0.3289589783189635,
            0.2832077296243516,
            0.3425906003685471,
            0.3970420865198392,
            0.41074871061646284,
            0.272257430756461,
            0.4201989685435462,
            0.34229059218038554,
        ]

        H = nx.DiGraph(edges)
        cls.H = G.reverse()
        cls.H.alpha = 0.1
        cls.H.evc = [
            0.3289589783189635,
            0.2832077296243516,
            0.3425906003685471,
            0.3970420865198392,
            0.41074871061646284,
            0.272257430756461,
            0.4201989685435462,
            0.34229059218038554,
        ]
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 setup_method(cls):
        G = nx.Graph()
        G.add_nodes_from([0, 1], fish='one')
        G.add_nodes_from([2, 3], fish='two')
        G.add_nodes_from([4], fish='red')
        G.add_nodes_from([5], fish='blue')
        G.add_edges_from([(0, 1), (2, 3), (0, 4), (2, 5)])
        cls.G = G

        D = nx.DiGraph()
        D.add_nodes_from([0, 1], fish='one')
        D.add_nodes_from([2, 3], fish='two')
        D.add_nodes_from([4], fish='red')
        D.add_nodes_from([5], fish='blue')
        D.add_edges_from([(0, 1), (2, 3), (0, 4), (2, 5)])
        cls.D = D

        S = nx.Graph()
        S.add_nodes_from([0, 1], fish='one')
        S.add_nodes_from([2, 3], fish='two')
        S.add_nodes_from([4], fish='red')
        S.add_nodes_from([5], fish='blue')
        S.add_edge(0, 0)
        S.add_edge(2, 2)
        cls.S = S
Beispiel #4
0
 def test_path(self):
     G = nx.path_graph(10, create_using=nx.DiGraph())
     assert list(nx.clustering(G).values()) == [
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
     ]
     assert nx.clustering(G) == {
         0: 0.0,
         1: 0.0,
         2: 0.0,
         3: 0.0,
         4: 0.0,
         5: 0.0,
         6: 0.0,
         7: 0.0,
         8: 0.0,
         9: 0.0,
     }
def test_disconnected_graph():
    # https://github.com/networkx/networkx/issues/1144
    G = nx.Graph()
    G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
    assert not nx.is_tree(G)

    G = nx.DiGraph()
    G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
    assert not nx.is_tree(G)
Beispiel #6
0
 def setup_class(cls):
     cls.G = nx.path_graph(9)
     cls.DG = nx.path_graph(9, create_using=nx.DiGraph())
     cls.Gv = nx.to_undirected(cls.DG)
     cls.DGv = nx.to_directed(cls.G)
     cls.Rv = cls.DG.reverse()
     cls.graphs = [cls.G, cls.DG, cls.Gv, cls.DGv, cls.Rv]
     for G in cls.graphs:
         G.edges, G.nodes, G.degree
Beispiel #7
0
 def test_directed_find_cores(self):
     """core number had a bug for directed graphs found in issue #1959"""
     # small example where too timid edge removal can make cn[2] = 3
     G = nx.DiGraph()
     edges = [(1, 2), (2, 1), (2, 3), (2, 4), (3, 4), (4, 3)]
     G.add_edges_from(edges)
     assert nx.core_number(G) == {1: 2, 2: 2, 3: 2, 4: 2}
     # small example where too aggressive edge removal can make cn[2] = 2
     more_edges = [(1, 5), (3, 5), (4, 5), (3, 6), (4, 6), (5, 6)]
     G.add_edges_from(more_edges)
     assert nx.core_number(G) == {1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3}
Beispiel #8
0
    def setup_class(cls):
        cls.NXGraph = nx.DiGraph
        data_dir = os.path.expandvars("${GS_TEST_DIR}/ldbc_sample")
        cls.single_label_g = ldbc_sample_single_label(data_dir, True)
        cls.multi_label_g = ldbc_sample_multi_labels(data_dir, True)
        cls.duplicated_oid_g = ldbc_sample_with_duplicated_oid(data_dir, True)

        # FIXME: this is tricky way to create a str gs graph
        les_g = nx.les_miserables_graph()
        di_les_g = nx.DiGraph()
        di_les_g.add_edges_from(di_les_g.edges.data())
        cls.str_oid_g = g(di_les_g)
Beispiel #9
0
def gnm_random_graph(n, m, seed=None, directed=False):
    """Returns a $G_{n,m}$ random graph.

    In the $G_{n,m}$ model, a graph is chosen uniformly at random from the set
    of all graphs with $n$ nodes and $m$ edges.

    This algorithm should be faster than :func:`dense_gnm_random_graph` for
    sparse graphs.

    Parameters
    ----------
    n : int
        The number of nodes.
    m : int
        The number of edges.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    directed : bool, optional (default=False)
        If True return a directed graph

    See also
    --------
    dense_gnm_random_graph

    """
    if directed:
        G = nx.DiGraph()
    else:
        G = nx.Graph()
    G.add_nodes_from(range(n))

    if n == 1:
        return G
    max_edges = n * (n - 1)
    if not directed:
        max_edges /= 2.0
    if m >= max_edges:
        return complete_graph(n, create_using=G)

    nlist = list(G)
    edge_count = 0
    while edge_count < m:
        # generate random edge,u,v
        u = seed.choice(nlist)
        v = seed.choice(nlist)
        if u == v or G.has_edge(u, v):
            continue
        else:
            G.add_edge(u, v)
            edge_count = edge_count + 1
    return G
Beispiel #10
0
def gnp_random_graph(n, p, seed=None, directed=False):
    """Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph
    or a binomial graph.

    The $G_{n,p}$ model chooses each of the possible edges with probability $p$.

    The functions :func:`binomial_graph` and :func:`erdos_renyi_graph` are
    aliases of this function.

    Parameters
    ----------
    n : int
        The number of nodes.
    p : float
        Probability for edge creation.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    directed : bool, optional (default=False)
        If True, this function returns a directed graph.

    See Also
    --------
    fast_gnp_random_graph

    Notes
    -----
    This algorithm [2]_ runs in $O(n^2)$ time.  For sparse graphs (that is, for
    small values of $p$), :func:`fast_gnp_random_graph` is a faster algorithm.

    References
    ----------
    .. [1] P. Erdős and A. Rényi, On Random Graphs, Publ. Math. 6, 290 (1959).
    .. [2] E. N. Gilbert, Random Graphs, Ann. Math. Stat., 30, 1141 (1959).
    """
    if directed:
        edges = itertools.permutations(range(n), 2)
        G = nx.DiGraph()
    else:
        edges = itertools.combinations(range(n), 2)
        G = nx.Graph()
    G.add_nodes_from(range(n))
    if p <= 0:
        return G
    if p >= 1:
        return complete_graph(n, create_using=G)

    for e in edges:
        if seed.random() < p:
            G.add_edge(*e)
    return G
Beispiel #11
0
    def setup_method(self):

        G = nx.DiGraph()

        edges = [(1, 3), (1, 5), (2, 1), (3, 5), (5, 4), (5, 3), (6, 5)]

        G.add_edges_from(edges, weight=1)
        self.G = G
        self.G.a = dict(
            zip(sorted(G),
                [0.000000, 0.000000, 0.366025, 0.133975, 0.500000, 0.000000]))
        self.G.h = dict(
            zip(sorted(G),
                [0.366025, 0.000000, 0.211325, 0.000000, 0.211325, 0.211325]))
Beispiel #12
0
 def setup_class(cls):
     G = nx.DiGraph()
     edges = [
         (1, 2),
         (1, 3),
         # 2 is a dangling node
         (3, 1),
         (3, 2),
         (3, 5),
         (4, 5),
         (4, 6),
         (5, 4),
         (5, 6),
         (6, 4),
     ]
     G.add_edges_from(edges)
     cls.G = G
     cls.G.pagerank = dict(
         zip(
             sorted(G),
             [
                 0.03721197,
                 0.05395735,
                 0.04150565,
                 0.37508082,
                 0.20599833,
                 0.28624589,
             ],
         ))
     cls.dangling_node_index = 1
     cls.dangling_edges = {1: 2, 2: 3, 3: 0, 4: 0, 5: 0, 6: 0}
     cls.G.dangling_pagerank = dict(
         zip(
             sorted(G),
             [
                 0.10844518, 0.18618601, 0.0710892, 0.2683668, 0.15919783,
                 0.20671497
             ],
         ))
Beispiel #13
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 #14
0
 def test_bfs_edges_reverse(self):
     D = nx.DiGraph()
     D.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4)])
     edges = nx.builtin.bfs_edges(D, source=4, reverse=True)
     assert list(edges) == [(4, 2), (4, 3), (2, 1), (1, 0)]
Beispiel #15
0
def bfs_tree(G, source, reverse=False, depth_limit=None):
    T = nx.DiGraph()
    T.add_node(source)
    edges_gen = bfs_edges(G, source, reverse=reverse, depth_limit=depth_limit)
    T.add_edges_from(edges_gen)
    return T
Beispiel #16
0
def fast_gnp_random_graph(n, p, seed=None, directed=False):
    """Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph or
    a binomial graph.

    Parameters
    ----------
    n : int
        The number of nodes.
    p : float
        Probability for edge creation.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    directed : bool, optional (default=False)
        If True, this function returns a directed graph.

    Notes
    -----
    The $G_{n,p}$ graph algorithm chooses each of the $[n (n - 1)] / 2$
    (undirected) or $n (n - 1)$ (directed) possible edges with probability $p$.

    This algorithm [1]_ runs in $O(n + m)$ time, where `m` is the expected number of
    edges, which equals $p n (n - 1) / 2$. This should be faster than
    :func:`gnp_random_graph` when $p$ is small and the expected number of edges
    is small (that is, the graph is sparse).

    See Also
    --------
    gnp_random_graph

    References
    ----------
    .. [1] Vladimir Batagelj and Ulrik Brandes,
       "Efficient generation of large random networks",
       Phys. Rev. E, 71, 036113, 2005.
    """
    G = empty_graph(n)

    if p <= 0 or p >= 1:
        return nx.gnp_random_graph(n, p, seed=seed, directed=directed)

    w = -1
    lp = math.log(1.0 - p)

    if directed:
        G = nx.DiGraph(G)
        # Nodes in graph are from 0,n-1 (start with v as the first node index).
        v = 0
        while v < n:
            lr = math.log(1.0 - seed.random())
            w = w + 1 + int(lr / lp)
            if v == w:  # avoid self loops
                w = w + 1
            while v < n <= w:
                w = w - n
                v = v + 1
                if v == w:  # avoid self loops
                    w = w + 1
            if v < n:
                G.add_edge(v, w)
    else:
        # Nodes in graph are from 0,n-1 (start with v as the second node index).
        v = 1
        while v < n:
            lr = math.log(1.0 - seed.random())
            w = w + 1 + int(lr / lp)
            while w >= v and v < n:
                w = w - v
                v = v + 1
            if v < n:
                G.add_edge(v, w)
    return G
Beispiel #17
0
 def test_digraphs_equal(self):
     G = nx.path_graph(4, create_using=nx.DiGraph())
     H = nx.DiGraph()
     nx.add_path(H, range(4))
     self._test_equal(G, H)
Beispiel #18
0
 def setup_method(self):
     self.DG = nx.path_graph(9, create_using=nx.DiGraph())
     self.uv = nx.to_undirected(self.DG)
def test_dag_nontree():
    G = nx.DiGraph()
    G.add_edges_from([(0, 1), (0, 2), (1, 2)])
    assert not nx.is_tree(G)
    assert nx.is_directed_acyclic_graph(G)
def test_emptybranch():
    G = nx.DiGraph()
    G.add_nodes_from(range(10))
    assert nx.is_branching(G)
    assert not nx.is_arborescence(G)
Beispiel #21
0
 def setup_method(self):
     self.G = nx.path_graph(9, create_using=nx.DiGraph())
     self.rv = nx.reverse_view(self.G)
Beispiel #22
0
    def setup_class(cls):
        G = nx.DiGraph()

        edges = [
            (1, 2),
            (1, 3),
            (2, 4),
            (3, 2),
            (3, 5),
            (4, 2),
            (4, 5),
            (4, 6),
            (5, 6),
            (5, 7),
            (5, 8),
            (6, 8),
            (7, 1),
            (7, 5),
            (7, 8),
            (8, 6),
            (8, 7),
        ]

        G.add_edges_from(edges, weight=2.0)
        cls.G = G.reverse()
        cls.G.evc = [
            0.25368793,
            0.19576478,
            0.32817092,
            0.40430835,
            0.48199885,
            0.15724483,
            0.51346196,
            0.32475403,
        ]

        H = nx.DiGraph()

        edges = [
            (1, 2),
            (1, 3),
            (2, 4),
            (3, 2),
            (3, 5),
            (4, 2),
            (4, 5),
            (4, 6),
            (5, 6),
            (5, 7),
            (5, 8),
            (6, 8),
            (7, 1),
            (7, 5),
            (7, 8),
            (8, 6),
            (8, 7),
        ]

        G.add_edges_from(edges)
        cls.H = G.reverse()
        cls.H.evc = [
            0.25368793,
            0.19576478,
            0.32817092,
            0.40430835,
            0.48199885,
            0.15724483,
            0.51346196,
            0.32475403,
        ]
Beispiel #23
0
 def test_error_on_wrong_nx_type(self):
     g = self.single_label_g
     with pytest.raises(TypeError):
         nx_g = nx.DiGraph(g)
Beispiel #24
0
 def setup_method(cls):
     cls.P4 = nx.path_graph(4)
     cls.D = nx.DiGraph()
     cls.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
     cls.S = nx.Graph()
     cls.S.add_edges_from([(0, 0), (1, 1)])
def test_path():
    G = nx.DiGraph()
    nx.add_path(G, range(5))
    assert nx.is_branching(G)
    assert nx.is_arborescence(G)
Beispiel #26
0
 def test_triangle_and_edge(self):
     G = nx.cycle_graph(3, create_using=nx.DiGraph())
     G.add_edge(0, 4, weight=2)
     assert nx.clustering(G)[0] == 1.0 / 6.0
     assert nx.clustering(G, weight="weight")[0] == 1.0 / 12.0
Beispiel #27
0
 def test_clustering(self):
     G = nx.DiGraph()
     assert list(nx.clustering(G).values()) == []
     assert nx.clustering(G) == {}
Beispiel #28
0
def random_uniform_k_out_graph(n, k, self_loops=True, with_replacement=True, seed=None):
    """Returns a random `k`-out graph with uniform attachment.

    A random `k`-out graph with uniform attachment is a multidigraph
    generated by the following algorithm. For each node *u*, choose
    `k` nodes *v* uniformly at random (with replacement). Add a
    directed edge joining *u* to *v*.

    Parameters
    ----------
    n : int
        The number of nodes in the returned graph.

    k : int
        The out-degree of each node in the returned graph.

    self_loops : bool
        If True, self-loops are allowed when generating the graph.

    with_replacement : bool
        If True, neighbors are chosen with replacement and the
        returned graph will be a directed multigraph. Otherwise,
        neighbors are chosen without replacement and the returned graph
        will be a directed graph.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    NetworkX graph
        A `k`-out-regular directed graph generated according to the
        above algorithm. It will be a multigraph if and only if
        `with_replacement` is True.

    Raises
    ------
    ValueError
        If `with_replacement` is False and `k` is greater than
        `n`.

    See also
    --------
    random_k_out_graph

    Notes
    -----
    The return digraph or multidigraph may not be strongly connected, or
    even weakly connected.

    If `with_replacement` is True, this function is similar to
    :func:`random_k_out_graph`, if that function had parameter `alpha`
    set to positive infinity.

    """
    if with_replacement:
        create_using = nx.MultiDiGraph()

        def sample(v, nodes):
            if not self_loops:
                nodes = nodes - {v}
            return (seed.choice(list(nodes)) for i in range(k))

    else:
        create_using = nx.DiGraph()

        def sample(v, nodes):
            if not self_loops:
                nodes = nodes - {v}
            return seed.sample(nodes, k)

    G = nx.empty_graph(n, create_using)
    nodes = set(G)
    for u in G:
        G.add_edges_from((u, v) for v in sample(u, nodes))
    return G