test_centrality.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Unit tests for the :mod:`networkx.algorithms.community.centrality`
  2. module.
  3. """
  4. from operator import itemgetter
  5. import networkx as nx
  6. def set_of_sets(iterable):
  7. return set(map(frozenset, iterable))
  8. def validate_communities(result, expected):
  9. assert set_of_sets(result) == set_of_sets(expected)
  10. def validate_possible_communities(result, *expected):
  11. assert any(set_of_sets(result) == set_of_sets(p) for p in expected)
  12. class TestGirvanNewman:
  13. """Unit tests for the
  14. :func:`networkx.algorithms.community.centrality.girvan_newman`
  15. function.
  16. """
  17. def test_no_edges(self):
  18. G = nx.empty_graph(3)
  19. communities = list(nx.community.girvan_newman(G))
  20. assert len(communities) == 1
  21. validate_communities(communities[0], [{0}, {1}, {2}])
  22. def test_undirected(self):
  23. # Start with the graph .-.-.-.
  24. G = nx.path_graph(4)
  25. communities = list(nx.community.girvan_newman(G))
  26. assert len(communities) == 3
  27. # After one removal, we get the graph .-. .-.
  28. validate_communities(communities[0], [{0, 1}, {2, 3}])
  29. # After the next, we get the graph .-. . ., but there are two
  30. # symmetric possible versions.
  31. validate_possible_communities(
  32. communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
  33. )
  34. # After the last removal, we always get the empty graph.
  35. validate_communities(communities[2], [{0}, {1}, {2}, {3}])
  36. def test_directed(self):
  37. G = nx.DiGraph(nx.path_graph(4))
  38. communities = list(nx.community.girvan_newman(G))
  39. assert len(communities) == 3
  40. validate_communities(communities[0], [{0, 1}, {2, 3}])
  41. validate_possible_communities(
  42. communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
  43. )
  44. validate_communities(communities[2], [{0}, {1}, {2}, {3}])
  45. def test_selfloops(self):
  46. G = nx.path_graph(4)
  47. G.add_edge(0, 0)
  48. G.add_edge(2, 2)
  49. communities = list(nx.community.girvan_newman(G))
  50. assert len(communities) == 3
  51. validate_communities(communities[0], [{0, 1}, {2, 3}])
  52. validate_possible_communities(
  53. communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
  54. )
  55. validate_communities(communities[2], [{0}, {1}, {2}, {3}])
  56. def test_most_valuable_edge(self):
  57. G = nx.Graph()
  58. G.add_weighted_edges_from([(0, 1, 3), (1, 2, 2), (2, 3, 1)])
  59. # Let the most valuable edge be the one with the highest weight.
  60. def heaviest(G):
  61. return max(G.edges(data="weight"), key=itemgetter(2))[:2]
  62. communities = list(nx.community.girvan_newman(G, heaviest))
  63. assert len(communities) == 3
  64. validate_communities(communities[0], [{0}, {1, 2, 3}])
  65. validate_communities(communities[1], [{0}, {1}, {2, 3}])
  66. validate_communities(communities[2], [{0}, {1}, {2}, {3}])