voronoi.py 3.1 KB

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