voronoi.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Functions for computing the Voronoi cells of a graph."""
  2. import networkx as nx
  3. from networkx.utils import groups
  4. __all__ = ["voronoi_cells"]
  5. @nx._dispatchable(edge_attrs="weight")
  6. def voronoi_cells(G, center_nodes, weight="weight"):
  7. """Returns the Voronoi cells centered at `center_nodes` with respect
  8. to the shortest-path distance metric.
  9. If $C$ is a set of nodes in the graph and $c$ is an element of $C$,
  10. the *Voronoi cell* centered at a node $c$ is the set of all nodes
  11. $v$ that are closer to $c$ than to any other center node in $C$ with
  12. respect to the shortest-path distance metric. [1]_
  13. For directed graphs, this will compute the "outward" Voronoi cells,
  14. as defined in [1]_, in which distance is measured from the center
  15. nodes to the target node. For the "inward" Voronoi cells, use the
  16. :meth:`DiGraph.reverse` method to reverse the orientation of the
  17. edges before invoking this function on the directed graph.
  18. Parameters
  19. ----------
  20. G : NetworkX graph
  21. center_nodes : set
  22. A nonempty set of nodes in the graph `G` that represent the
  23. center of the Voronoi cells.
  24. weight : string or function
  25. The edge attribute (or an arbitrary function) representing the
  26. weight of an edge. This keyword argument is as described in the
  27. documentation for :func:`~networkx.multi_source_dijkstra_path`,
  28. for example.
  29. Returns
  30. -------
  31. dictionary
  32. A mapping from center node to set of all nodes in the graph
  33. closer to that center node than to any other center node. The
  34. keys of the dictionary are the element of `center_nodes`, and
  35. the values of the dictionary form a partition of the nodes of
  36. `G`.
  37. Examples
  38. --------
  39. To get only the partition of the graph induced by the Voronoi cells,
  40. take the collection of all values in the returned dictionary::
  41. >>> G = nx.path_graph(6)
  42. >>> center_nodes = {0, 3}
  43. >>> cells = nx.voronoi_cells(G, center_nodes)
  44. >>> partition = set(map(frozenset, cells.values()))
  45. >>> sorted(map(sorted, partition))
  46. [[0, 1], [2, 3, 4, 5]]
  47. Raises
  48. ------
  49. ValueError
  50. If `center_nodes` is empty.
  51. References
  52. ----------
  53. .. [1] Erwig, Martin. (2000),"The graph Voronoi diagram with applications."
  54. *Networks*, 36: 156--163.
  55. https://doi.org/10.1002/1097-0037(200010)36:3<156::AID-NET2>3.0.CO;2-L
  56. """
  57. # Determine the shortest paths from any one of the center nodes to
  58. # every node in the graph.
  59. #
  60. # This raises `ValueError` if `center_nodes` is an empty set.
  61. paths = nx.multi_source_dijkstra_path(G, center_nodes, weight=weight)
  62. # Determine the center node from which the shortest path originates.
  63. nearest = {v: p[0] for v, p in paths.items()}
  64. # Get the mapping from center node to all nodes closer to it than to
  65. # any other center node.
  66. cells = groups(nearest)
  67. # We collect all unreachable nodes under a special key, if there are any.
  68. unreachable = set(G) - set(nearest)
  69. if unreachable:
  70. cells["unreachable"] = unreachable
  71. return cells