cographs.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. r"""Generators for cographs
  2. A cograph is a graph containing no path on four vertices.
  3. Cographs or $P_4$-free graphs can be obtained from a single vertex
  4. by disjoint union and complementation operations.
  5. References
  6. ----------
  7. .. [0] D.G. Corneil, H. Lerchs, L.Stewart Burlingham,
  8. "Complement reducible graphs",
  9. Discrete Applied Mathematics, Volume 3, Issue 3, 1981, Pages 163-174,
  10. ISSN 0166-218X.
  11. """
  12. import networkx as nx
  13. from networkx.utils import py_random_state
  14. __all__ = ["random_cograph"]
  15. @py_random_state(1)
  16. @nx._dispatchable(graphs=None, returns_graph=True)
  17. def random_cograph(n, seed=None):
  18. r"""Returns a random cograph with $2 ^ n$ nodes.
  19. A cograph is a graph containing no path on four vertices.
  20. Cographs or $P_4$-free graphs can be obtained from a single vertex
  21. by disjoint union and complementation operations.
  22. This generator starts off from a single vertex and performs disjoint
  23. union and full join operations on itself.
  24. The decision on which operation will take place is random.
  25. Parameters
  26. ----------
  27. n : int
  28. The order of the cograph.
  29. seed : integer, random_state, or None (default)
  30. Indicator of random number generation state.
  31. See :ref:`Randomness<randomness>`.
  32. Returns
  33. -------
  34. G : A random graph containing no path on four vertices.
  35. See Also
  36. --------
  37. full_join
  38. union
  39. References
  40. ----------
  41. .. [1] D.G. Corneil, H. Lerchs, L.Stewart Burlingham,
  42. "Complement reducible graphs",
  43. Discrete Applied Mathematics, Volume 3, Issue 3, 1981, Pages 163-174,
  44. ISSN 0166-218X.
  45. """
  46. R = nx.empty_graph(1)
  47. for i in range(n):
  48. RR = nx.relabel_nodes(R.copy(), lambda x: x + len(R))
  49. if seed.randint(0, 1) == 0:
  50. R = nx.full_join(R, RR)
  51. else:
  52. R = nx.disjoint_union(R, RR)
  53. return R