### comparison env/lib/python3.9/site-packages/networkx/algorithms/bipartite/tests/test_basic.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2
3 import networkx as nx
4 from networkx.algorithms import bipartite
5
6
7 class TestBipartiteBasic:
8 def test_is_bipartite(self):
9 assert bipartite.is_bipartite(nx.path_graph(4))
10 assert bipartite.is_bipartite(nx.DiGraph([(1, 0)]))
11 assert not bipartite.is_bipartite(nx.complete_graph(3))
12
13 def test_bipartite_color(self):
14 G = nx.path_graph(4)
15 c = bipartite.color(G)
16 assert c == {0: 1, 1: 0, 2: 1, 3: 0}
17
18 def test_not_bipartite_color(self):
19 with pytest.raises(nx.NetworkXError):
20 c = bipartite.color(nx.complete_graph(4))
21
22 def test_bipartite_directed(self):
23 G = bipartite.random_graph(10, 10, 0.1, directed=True)
24 assert bipartite.is_bipartite(G)
25
26 def test_bipartite_sets(self):
27 G = nx.path_graph(4)
28 X, Y = bipartite.sets(G)
29 assert X == {0, 2}
30 assert Y == {1, 3}
31
32 def test_bipartite_sets_directed(self):
33 G = nx.path_graph(4)
34 D = G.to_directed()
35 X, Y = bipartite.sets(D)
36 assert X == {0, 2}
37 assert Y == {1, 3}
38
39 def test_bipartite_sets_given_top_nodes(self):
40 G = nx.path_graph(4)
41 top_nodes = [0, 2]
42 X, Y = bipartite.sets(G, top_nodes)
43 assert X == {0, 2}
44 assert Y == {1, 3}
45
46 def test_bipartite_sets_disconnected(self):
47 with pytest.raises(nx.AmbiguousSolution):
48 G = nx.path_graph(4)
50 X, Y = bipartite.sets(G)
51
52 def test_is_bipartite_node_set(self):
53 G = nx.path_graph(4)
54 assert bipartite.is_bipartite_node_set(G, [0, 2])
55 assert bipartite.is_bipartite_node_set(G, [1, 3])
56 assert not bipartite.is_bipartite_node_set(G, [1, 2])
58 assert bipartite.is_bipartite_node_set(G, [0, 2, 10])
59 assert bipartite.is_bipartite_node_set(G, [0, 2, 20])
60 assert bipartite.is_bipartite_node_set(G, [1, 3, 10])
61 assert bipartite.is_bipartite_node_set(G, [1, 3, 20])
62
63 def test_bipartite_density(self):
64 G = nx.path_graph(5)
65 X, Y = bipartite.sets(G)
66 density = float(len(list(G.edges()))) / (len(X) * len(Y))
67 assert bipartite.density(G, X) == density
68 D = nx.DiGraph(G.edges())
69 assert bipartite.density(D, X) == density / 2.0
70 assert bipartite.density(nx.Graph(), {}) == 0.0
71
72 def test_bipartite_degrees(self):
73 G = nx.path_graph(5)
74 X = {1, 3}
75 Y = {0, 2, 4}
76 u, d = bipartite.degrees(G, Y)
77 assert dict(u) == {1: 2, 3: 2}
78 assert dict(d) == {0: 1, 2: 2, 4: 1}
79
80 def test_bipartite_weighted_degrees(self):
81 G = nx.path_graph(5)
83 X = {1, 3}
84 Y = {0, 2, 4}
85 u, d = bipartite.degrees(G, Y, weight="weight")
86 assert dict(u) == {1: 1.1, 3: 2}
87 assert dict(d) == {0: 0.1, 2: 2, 4: 1}
88 u, d = bipartite.degrees(G, Y, weight="other")
89 assert dict(u) == {1: 1.2, 3: 2}
90 assert dict(d) == {0: 0.2, 2: 2, 4: 1}
91
93 scipy = pytest.importorskip("scipy")
94 G = nx.path_graph(5)
96 X = [1, 3]
97 Y = [0, 2, 4]
98 M = bipartite.biadjacency_matrix(G, X, weight="weight")
99 assert M[0, 0] == 2
100 M = bipartite.biadjacency_matrix(G, X, weight="other")
101 assert M[0, 0] == 4
102
104 scipy = pytest.importorskip("scipy")
105 tops = [2, 5, 10]
106 bots = [5, 10, 15]
107 for i in range(len(tops)):
108 G = bipartite.random_graph(tops[i], bots[i], 0.2)
109 top = [n for n, d in G.nodes(data=True) if d["bipartite"] == 0]