test_ramsey.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import networkx as nx
  2. import networkx.algorithms.approximation as apxa
  3. def test_ramsey():
  4. # this should only find the complete graph
  5. graph = nx.complete_graph(10)
  6. c, i = apxa.ramsey_R2(graph)
  7. cdens = nx.density(graph.subgraph(c))
  8. assert cdens == 1.0, "clique not correctly found by ramsey!"
  9. idens = nx.density(graph.subgraph(i))
  10. assert idens == 0.0, "i-set not correctly found by ramsey!"
  11. # this trivial graph has no cliques. should just find i-sets
  12. graph = nx.trivial_graph()
  13. c, i = apxa.ramsey_R2(graph)
  14. assert c == {0}, "clique not correctly found by ramsey!"
  15. assert i == {0}, "i-set not correctly found by ramsey!"
  16. graph = nx.barbell_graph(10, 5, nx.Graph())
  17. c, i = apxa.ramsey_R2(graph)
  18. cdens = nx.density(graph.subgraph(c))
  19. assert cdens == 1.0, "clique not correctly found by ramsey!"
  20. idens = nx.density(graph.subgraph(i))
  21. assert idens == 0.0, "i-set not correctly found by ramsey!"
  22. # add self-loops and test again
  23. graph.add_edges_from([(n, n) for n in range(0, len(graph), 2)])
  24. cc, ii = apxa.ramsey_R2(graph)
  25. assert cc == c
  26. assert ii == i