test_voronoi.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import networkx as nx
  2. from networkx.utils import pairwise
  3. class TestVoronoiCells:
  4. """Unit tests for the Voronoi cells function."""
  5. def test_isolates(self):
  6. """Tests that a graph with isolated nodes has all isolates in
  7. one block of the partition.
  8. """
  9. G = nx.empty_graph(5)
  10. cells = nx.voronoi_cells(G, {0, 2, 4})
  11. expected = {0: {0}, 2: {2}, 4: {4}, "unreachable": {1, 3}}
  12. assert expected == cells
  13. def test_undirected_unweighted(self):
  14. G = nx.cycle_graph(6)
  15. cells = nx.voronoi_cells(G, {0, 3})
  16. expected = {0: {0, 1, 5}, 3: {2, 3, 4}}
  17. assert expected == cells
  18. def test_directed_unweighted(self):
  19. # This is the singly-linked directed cycle graph on six nodes.
  20. G = nx.DiGraph(pairwise(range(6), cyclic=True))
  21. cells = nx.voronoi_cells(G, {0, 3})
  22. expected = {0: {0, 1, 2}, 3: {3, 4, 5}}
  23. assert expected == cells
  24. def test_directed_inward(self):
  25. """Tests that reversing the graph gives the "inward" Voronoi
  26. partition.
  27. """
  28. # This is the singly-linked reverse directed cycle graph on six nodes.
  29. G = nx.DiGraph(pairwise(range(6), cyclic=True))
  30. G = G.reverse(copy=False)
  31. cells = nx.voronoi_cells(G, {0, 3})
  32. expected = {0: {0, 4, 5}, 3: {1, 2, 3}}
  33. assert expected == cells
  34. def test_undirected_weighted(self):
  35. edges = [(0, 1, 10), (1, 2, 1), (2, 3, 1)]
  36. G = nx.Graph()
  37. G.add_weighted_edges_from(edges)
  38. cells = nx.voronoi_cells(G, {0, 3})
  39. expected = {0: {0}, 3: {1, 2, 3}}
  40. assert expected == cells
  41. def test_directed_weighted(self):
  42. edges = [(0, 1, 10), (1, 2, 1), (2, 3, 1), (3, 2, 1), (2, 1, 1)]
  43. G = nx.DiGraph()
  44. G.add_weighted_edges_from(edges)
  45. cells = nx.voronoi_cells(G, {0, 3})
  46. expected = {0: {0}, 3: {1, 2, 3}}
  47. assert expected == cells
  48. def test_multigraph_unweighted(self):
  49. """Tests that the Voronoi cells for a multigraph are the same as
  50. for a simple graph.
  51. """
  52. edges = [(0, 1), (1, 2), (2, 3)]
  53. G = nx.MultiGraph(2 * edges)
  54. H = nx.Graph(G)
  55. G_cells = nx.voronoi_cells(G, {0, 3})
  56. H_cells = nx.voronoi_cells(H, {0, 3})
  57. assert G_cells == H_cells
  58. def test_multidigraph_unweighted(self):
  59. # This is the twice-singly-linked directed cycle graph on six nodes.
  60. edges = list(pairwise(range(6), cyclic=True))
  61. G = nx.MultiDiGraph(2 * edges)
  62. H = nx.DiGraph(G)
  63. G_cells = nx.voronoi_cells(G, {0, 3})
  64. H_cells = nx.voronoi_cells(H, {0, 3})
  65. assert G_cells == H_cells
  66. def test_multigraph_weighted(self):
  67. edges = [(0, 1, 10), (0, 1, 10), (1, 2, 1), (1, 2, 100), (2, 3, 1), (2, 3, 100)]
  68. G = nx.MultiGraph()
  69. G.add_weighted_edges_from(edges)
  70. cells = nx.voronoi_cells(G, {0, 3})
  71. expected = {0: {0}, 3: {1, 2, 3}}
  72. assert expected == cells
  73. def test_multidigraph_weighted(self):
  74. edges = [
  75. (0, 1, 10),
  76. (0, 1, 10),
  77. (1, 2, 1),
  78. (2, 3, 1),
  79. (3, 2, 10),
  80. (3, 2, 1),
  81. (2, 1, 10),
  82. (2, 1, 1),
  83. ]
  84. G = nx.MultiDiGraph()
  85. G.add_weighted_edges_from(edges)
  86. cells = nx.voronoi_cells(G, {0, 3})
  87. expected = {0: {0}, 3: {1, 2, 3}}
  88. assert expected == cells