Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Voronoi.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/ArrayBuffer.h>
35 #include <ogdf/graphalg/Dijkstra.h>
36 
37 #include <map>
38 
39 namespace ogdf {
40 
42 
45 template<typename T>
46 class Voronoi {
47 protected:
51  std::map<node, ArrayBuffer<node>> m_nodeList;
52  const Graph& m_graph;
54 
55  void computeVoronoiRegions(const Graph& G, const EdgeArray<T>& weights, const List<node>& seeds);
56 
57 public:
62  Voronoi(const Graph& G, const EdgeArray<T>& weights, const List<node>& seeds)
63  : m_seedOfNode(G), m_nodeList(), m_graph(G), m_seeds(seeds) {
64  computeVoronoiRegions(G, weights, seeds);
65  }
66 
68  const Graph& getGraph() const { return m_graph; }
69 
71  const List<node>& getSeeds() const { return m_seeds; }
72 
75  edge predecessorEdge(node v) const { return m_predecessor[v]; }
76 
78  node predecessor(node v) const {
79  edge tmp = predecessorEdge(v);
80  return tmp ? tmp->opposite(v) : nullptr;
81  }
82 
84  T distance(node v) const { return m_distance[v]; }
85 
87  node seed(node v) const { return m_seedOfNode[v]; }
88 
91  return m_nodeList.find(seed(v))->second;
92  }
93 };
94 
96 
97 template<typename T>
99  const List<node>& seeds) {
100  Dijkstra<T> sssp;
101  sssp.call(G, weights, seeds, m_predecessor, m_distance);
102 
103  // extract Voronoi seeds for each node and Voronoi regions for each seed
104  NodeArray<bool> processed(G, false);
105  for (node seed : seeds) {
106  processed[seed] = true;
107  m_seedOfNode[seed] = seed;
108  m_nodeList[seed].push(seed);
109  }
110 
111  for (node u : G.nodes) {
112  ArrayBuffer<node> foundNodes;
113  node v;
114  for (v = u; !processed[v]; v = predecessor(v)) {
115  processed[v] = true;
116  foundNodes.push(v);
117  }
118 
119  for (node passedNode : foundNodes) {
120  m_seedOfNode[passedNode] = m_seedOfNode[v];
121  m_nodeList[m_seedOfNode[v]].push(passedNode);
122  }
123  }
124 }
125 
126 }
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:46
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ArrayBuffer.h
Declaration and implementation of ArrayBuffer class.
ogdf::Voronoi::m_seedOfNode
NodeArray< node > m_seedOfNode
Definition: Voronoi.h:50
ogdf::Voronoi::getGraph
const Graph & getGraph() const
Returns a reference to the graph the Voronoi region has been computed for.
Definition: Voronoi.h:68
ogdf::Voronoi
Computes Voronoi regions in an edge-weighted graph.
Definition: Voronoi.h:46
ogdf::Voronoi::m_nodeList
std::map< node, ArrayBuffer< node > > m_nodeList
Definition: Voronoi.h:51
ogdf::Voronoi::m_graph
const Graph & m_graph
Definition: Voronoi.h:52
ogdf::Voronoi::predecessorEdge
edge predecessorEdge(node v) const
Returns the edge incident to v and its predecessor. Note that the predecessor of a terminal is nullpt...
Definition: Voronoi.h:75
ogdf::Voronoi::nodesInRegion
const ArrayBuffer< node > & nodesInRegion(node v) const
Returns the list of nodes in the Voronoi region of node v.
Definition: Voronoi.h:90
ogdf::Dijkstra
Dijkstra's single source shortest path algorithm.
Definition: Dijkstra.h:53
ogdf::Voronoi::getSeeds
const List< node > & getSeeds() const
Returns a reference to the list of seeds the Voronoi region has been computed for.
Definition: Voronoi.h:71
ogdf::Dijkstra::call
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false, bool arcsReversed=false, node target=nullptr, T maxLength=std::numeric_limits< T >::max())
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
Definition: Dijkstra.h:246
ogdf::ArrayBuffer::push
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:209
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: List.h:42
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::Voronoi::computeVoronoiRegions
void computeVoronoiRegions(const Graph &G, const EdgeArray< T > &weights, const List< node > &seeds)
Definition: Voronoi.h:98
ogdf::Voronoi::distance
T distance(node v) const
Returns the distance between v and its Voronoi seed.
Definition: Voronoi.h:84
ogdf::Voronoi::Voronoi
Voronoi(const Graph &G, const EdgeArray< T > &weights, const List< node > &seeds)
Build data structure to query Voronoi regions of edge-weighted graph G with given Voronoi seeds.
Definition: Voronoi.h:62
ogdf::EdgeElement::opposite
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:406
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
Dijkstra.h
Implementation of Dijkstra's single source shortest path algorithm.
ogdf::Voronoi::m_predecessor
NodeArray< edge > m_predecessor
Definition: Voronoi.h:48
ogdf::Voronoi::m_seeds
const List< node > & m_seeds
Definition: Voronoi.h:53
ogdf::Voronoi::predecessor
node predecessor(node v) const
Returns the nearest node to v on the shortest path to its Voronoi seed.
Definition: Voronoi.h:78
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::Voronoi::seed
node seed(node v) const
Returns the Voronoi seed of node v.
Definition: Voronoi.h:87
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709
ogdf::Voronoi::m_distance
NodeArray< T > m_distance
Definition: Voronoi.h:49