test_redundancy.py 919 B

12345678910111213141516171819202122232425262728293031323334353637
  1. """Unit tests for the :mod:`networkx.algorithms.bipartite.redundancy` module.
  2. """
  3. import pytest
  4. from networkx import NetworkXError, cycle_graph
  5. from networkx.algorithms.bipartite import complete_bipartite_graph, node_redundancy
  6. def test_no_redundant_nodes():
  7. G = complete_bipartite_graph(2, 2)
  8. # when nodes is None
  9. rc = node_redundancy(G)
  10. assert all(redundancy == 1 for redundancy in rc.values())
  11. # when set of nodes is specified
  12. rc = node_redundancy(G, (2, 3))
  13. assert rc == {2: 1.0, 3: 1.0}
  14. def test_redundant_nodes():
  15. G = cycle_graph(6)
  16. edge = {0, 3}
  17. G.add_edge(*edge)
  18. redundancy = node_redundancy(G)
  19. for v in edge:
  20. assert redundancy[v] == 2 / 3
  21. for v in set(G) - edge:
  22. assert redundancy[v] == 1
  23. def test_not_enough_neighbors():
  24. with pytest.raises(NetworkXError):
  25. G = complete_bipartite_graph(1, 2)
  26. node_redundancy(G)