mycielski.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Functions related to the Mycielski Operation and the Mycielskian family
  2. of graphs.
  3. """
  4. import networkx as nx
  5. from networkx.utils import not_implemented_for
  6. __all__ = ["mycielskian", "mycielski_graph"]
  7. @not_implemented_for("directed")
  8. @not_implemented_for("multigraph")
  9. def mycielskian(G, iterations=1):
  10. r"""Returns the Mycielskian of a simple, undirected graph G
  11. The Mycielskian of graph preserves a graph's triangle free
  12. property while increasing the chromatic number by 1.
  13. The Mycielski Operation on a graph, :math:`G=(V, E)`, constructs a new
  14. graph with :math:`2|V| + 1` nodes and :math:`3|E| + |V|` edges.
  15. The construction is as follows:
  16. Let :math:`V = {0, ..., n-1}`. Construct another vertex set
  17. :math:`U = {n, ..., 2n}` and a vertex, `w`.
  18. Construct a new graph, `M`, with vertices :math:`U \bigcup V \bigcup w`.
  19. For edges, :math:`(u, v) \in E` add edges :math:`(u, v), (u, v + n)`, and
  20. :math:`(u + n, v)` to M. Finally, for all vertices :math:`u \in U`, add
  21. edge :math:`(u, w)` to M.
  22. The Mycielski Operation can be done multiple times by repeating the above
  23. process iteratively.
  24. More information can be found at https://en.wikipedia.org/wiki/Mycielskian
  25. Parameters
  26. ----------
  27. G : graph
  28. A simple, undirected NetworkX graph
  29. iterations : int
  30. The number of iterations of the Mycielski operation to
  31. perform on G. Defaults to 1. Must be a non-negative integer.
  32. Returns
  33. -------
  34. M : graph
  35. The Mycielskian of G after the specified number of iterations.
  36. Notes
  37. -----
  38. Graph, node, and edge data are not necessarily propagated to the new graph.
  39. """
  40. M = nx.convert_node_labels_to_integers(G)
  41. for i in range(iterations):
  42. n = M.number_of_nodes()
  43. M.add_nodes_from(range(n, 2 * n))
  44. old_edges = list(M.edges())
  45. M.add_edges_from((u, v + n) for u, v in old_edges)
  46. M.add_edges_from((u + n, v) for u, v in old_edges)
  47. M.add_node(2 * n)
  48. M.add_edges_from((u + n, 2 * n) for u in range(n))
  49. return M
  50. def mycielski_graph(n):
  51. """Generator for the n_th Mycielski Graph.
  52. The Mycielski family of graphs is an infinite set of graphs.
  53. :math:`M_1` is the singleton graph, :math:`M_2` is two vertices with an
  54. edge, and, for :math:`i > 2`, :math:`M_i` is the Mycielskian of
  55. :math:`M_{i-1}`.
  56. More information can be found at
  57. http://mathworld.wolfram.com/MycielskiGraph.html
  58. Parameters
  59. ----------
  60. n : int
  61. The desired Mycielski Graph.
  62. Returns
  63. -------
  64. M : graph
  65. The n_th Mycielski Graph
  66. Notes
  67. -----
  68. The first graph in the Mycielski sequence is the singleton graph.
  69. The Mycielskian of this graph is not the :math:`P_2` graph, but rather the
  70. :math:`P_2` graph with an extra, isolated vertex. The second Mycielski
  71. graph is the :math:`P_2` graph, so the first two are hard coded.
  72. The remaining graphs are generated using the Mycielski operation.
  73. """
  74. if n < 1:
  75. raise nx.NetworkXError("must satisfy n >= 0")
  76. if n == 1:
  77. return nx.empty_graph(1)
  78. else:
  79. return mycielskian(nx.path_graph(2), n - 2)