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
def setup_class(cls): # simple graph G = nx.Graph() G.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4)]) cls.G = G # simple graph, disconnected D = nx.Graph() D.add_edges_from([(0, 1), (2, 3)]) cls.D = D
def setup_method(self): # a tree G = nx.Graph() nx.add_path(G, [0, 1, 2, 3, 4, 5, 6]) nx.add_path(G, [2, 7, 8, 9, 10]) self.G = G # a disconnected graph D = nx.Graph() D.add_edges_from([(0, 1), (2, 3)]) nx.add_path(D, [2, 7, 8, 9, 10]) self.D = D
def setup_class(cls): # a tree G = nx.Graph() nx.add_path(G, [0, 1, 2, 3, 4, 5, 6]) nx.add_path(G, [2, 7, 8, 9, 10]) cls.G = G # a disconnected graph D = nx.Graph() D.add_edges_from([(0, 1), (2, 3)]) nx.add_path(D, [2, 7, 8, 9, 10]) cls.D = D
def test_bfs_tree_isolates(self): G = nx.Graph() G.add_node(1) G.add_node(2) T = nx.builtin.bfs_tree(G, source=1, depth_limit=10) assert sorted(T.nodes()) == [1] assert sorted(T.edges()) == []
def test_empty(self): numpy = pytest.importorskip("numpy") G = nx.Graph() assert nx.hits(G) == ({}, {}) assert nx.hits_numpy(G) == ({}, {}) assert nx.authority_matrix(G).shape == (0, 0) assert nx.hub_matrix(G).shape == (0, 0)
def test_lind_square_clustering(self): """Test C4 for figure 1 Lind et al (2005)""" G = nx.Graph( [ (1, 2), (1, 3), (1, 6), (1, 7), (2, 4), (2, 5), (3, 4), (3, 5), (6, 7), (7, 8), (6, 8), (7, 9), (7, 10), (6, 11), (6, 12), (2, 13), (2, 14), (3, 15), (3, 16), ] ) G1 = G.subgraph([1, 2, 3, 4, 5, 13, 14, 15, 16]) G2 = G.subgraph([1, 6, 7, 8, 9, 10, 11, 12]) assert nx.square_clustering(G, [1])[1] == 3 / 75.0 assert nx.square_clustering(G1, [1])[1] == 2 / 6.0 assert nx.square_clustering(G2, [1])[1] == 1 / 5.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)
def test_dfs_tree_isolates(self): G = nx.Graph() G.add_node(1) G.add_node(2) T = nx.dfs_tree(G, source=1) assert sorted(T.nodes()) == [1] assert sorted(T.edges()) == [] T = nx.dfs_tree(G, source=None) assert sorted(T.nodes()) == [1, 2] assert sorted(T.edges()) == []
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
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
def setup_method(self): self.edges = [(0, 1), (0, 2), (1, 2), (2, 3), (1, 4)] G = nx.Graph() G.add_edges_from(self.edges, weight=2) self.G = G
def test_empty_numpy(self): with pytest.raises(nx.NetworkXException): e = nx.eigenvector_centrality_numpy(nx.Graph())
def test_error_on_wrong_nx_type(self): g = self.single_label_g with pytest.raises(TypeError): nx_g = nx.Graph(g)
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)
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)
def setup_method(self): # simple graph G = nx.Graph() G.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4)]) self.G = G
def test_edges_with_data_not_equal(self): G = nx.Graph() G.add_nodes_from([1, 2, 3], color="red") H = nx.Graph() H.add_nodes_from([1, 2, 3], color="blue") self._test_not_equal(G.nodes(data=True), H.nodes(data=True))
def test_trivial(self): """Empty graph""" G = nx.Graph() assert nx.find_cores(G) == {}
def test_graphs_not_equal2(self): G = nx.path_graph(4) H = nx.Graph() nx.add_path(H, range(3)) self._test_not_equal(G, H)
def test_graphs_not_equal3(self): G = nx.path_graph(4) H = nx.Graph() nx.add_path(H, range(4)) H.name = "path_graph(4)" self._test_not_equal(G, H)
def test_exceptions(self): nxg = networkx.graphviews pytest.raises(nx.NetworkXNotImplemented, nxg.reverse_view, nx.Graph())
def test_bad_beta_numbe(self): with pytest.raises(nx.NetworkXException): G = nx.Graph([(0, 1)]) e = nx.katz_centrality_numpy(G, 0.1, beta="foo")
def test_bad_beta(self): with pytest.raises(nx.NetworkXException): G = nx.Graph([(0, 1)]) beta = {0: 77} e = nx.katz_centrality_numpy(G, 0.1, beta=beta)
def test_empty_scipy(self): scipy = pytest.importorskip("scipy") G = nx.Graph() assert nx.hits_scipy(G) == ({}, {})
def dynamic_property_graph(graphscope_session): with default_session(graphscope_session): g = nx.Graph() g.add_edges_from([(1, 2), (2, 3)], weight=3) yield g
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 dynamic_project_graph(graphscope_session): with default_session(graphscope_session): g = nx.Graph() g.add_edges_from([(1, 2), (2, 3)], weight=3) pg = g.project_to_simple(e_prop="weight") yield pg
def test_graphs_not_equal(self): G = nx.path_graph(4) H = nx.Graph() nx.add_cycle(H, range(4)) self._test_not_equal(G, H)
def test_empty(self): e = nx.katz_centrality(nx.Graph(), 0.1) assert e == {}