test_clique.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """Unit tests for the :mod:`networkx.algorithms.approximation.clique` module."""
  2. import networkx as nx
  3. from networkx.algorithms.approximation import (
  4. clique_removal,
  5. large_clique_size,
  6. max_clique,
  7. maximum_independent_set,
  8. )
  9. def is_independent_set(G, nodes):
  10. """Returns True if and only if `nodes` is a clique in `G`.
  11. `G` is a NetworkX graph. `nodes` is an iterable of nodes in
  12. `G`.
  13. """
  14. return G.subgraph(nodes).number_of_edges() == 0
  15. def is_clique(G, nodes):
  16. """Returns True if and only if `nodes` is an independent set
  17. in `G`.
  18. `G` is an undirected simple graph. `nodes` is an iterable of
  19. nodes in `G`.
  20. """
  21. H = G.subgraph(nodes)
  22. n = len(H)
  23. return H.number_of_edges() == n * (n - 1) // 2
  24. class TestCliqueRemoval:
  25. """Unit tests for the
  26. :func:`~networkx.algorithms.approximation.clique_removal` function.
  27. """
  28. def test_trivial_graph(self):
  29. G = nx.trivial_graph()
  30. independent_set, cliques = clique_removal(G)
  31. assert is_independent_set(G, independent_set)
  32. assert all(is_clique(G, clique) for clique in cliques)
  33. # In fact, we should only have 1-cliques, that is, singleton nodes.
  34. assert all(len(clique) == 1 for clique in cliques)
  35. def test_complete_graph(self):
  36. G = nx.complete_graph(10)
  37. independent_set, cliques = clique_removal(G)
  38. assert is_independent_set(G, independent_set)
  39. assert all(is_clique(G, clique) for clique in cliques)
  40. def test_barbell_graph(self):
  41. G = nx.barbell_graph(10, 5)
  42. independent_set, cliques = clique_removal(G)
  43. assert is_independent_set(G, independent_set)
  44. assert all(is_clique(G, clique) for clique in cliques)
  45. class TestMaxClique:
  46. """Unit tests for the :func:`networkx.algorithms.approximation.max_clique`
  47. function.
  48. """
  49. def test_null_graph(self):
  50. G = nx.null_graph()
  51. assert len(max_clique(G)) == 0
  52. def test_complete_graph(self):
  53. graph = nx.complete_graph(30)
  54. # this should return the entire graph
  55. mc = max_clique(graph)
  56. assert 30 == len(mc)
  57. def test_maximal_by_cardinality(self):
  58. """Tests that the maximal clique is computed according to maximum
  59. cardinality of the sets.
  60. For more information, see pull request #1531.
  61. """
  62. G = nx.complete_graph(5)
  63. G.add_edge(4, 5)
  64. clique = max_clique(G)
  65. assert len(clique) > 1
  66. G = nx.lollipop_graph(30, 2)
  67. clique = max_clique(G)
  68. assert len(clique) > 2
  69. def test_large_clique_size():
  70. G = nx.complete_graph(9)
  71. nx.add_cycle(G, [9, 10, 11])
  72. G.add_edge(8, 9)
  73. G.add_edge(1, 12)
  74. G.add_node(13)
  75. assert large_clique_size(G) == 9
  76. G.remove_node(5)
  77. assert large_clique_size(G) == 8
  78. G.remove_edge(2, 3)
  79. assert large_clique_size(G) == 7
  80. def test_independent_set():
  81. # smoke test
  82. G = nx.Graph()
  83. assert len(maximum_independent_set(G)) == 0