test_basic.py 4.2 KB

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