Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

AuxGraph.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/EdgeArray.h>
36 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/GraphCopy.h>
38 #include <ogdf/basic/NodeArray.h>
45 
46 #include <unordered_set>
47 #include <vector>
48 
49 namespace ogdf {
50 namespace matching_blossom {
51 
52 template<class TWeight>
54 template<class TWeight>
55 class AuxEdge;
56 
57 template<class TWeight>
58 class AuxNode {
61 
64 
67 
70 
72  double m_delta = 0;
73 
80 
83  struct {
84  AuxEdge<TWeight>* edge = nullptr;
85  long iteration = -1;
86  } m_currentEdge;
87 
88 public:
90  : m_node(auxGraphNode), m_helper(helper), m_tree(m_helper, graphNode) {
91  OGDF_ASSERT(auxGraphNode->graphOf() != &m_helper.graph());
92  OGDF_ASSERT(graphNode->graphOf() == &m_helper.graph());
93  }
94 
95  /* Getters & setters */
96 
99  if (m_currentEdge.iteration == m_helper.currentIteration) {
100  return m_currentEdge.edge;
101  } else {
102  return nullptr;
103  }
104  };
105 
108  m_currentEdge.edge = edge;
109  m_currentEdge.iteration = m_helper.currentIteration;
110  }
111 
112  node graphNode() { return m_node; }
113 
115 
117 
119 
121 
122  double delta() { return m_delta; }
123 
124  /* End of getters & setters */
125 
127  double delta(node v) {
128  OGDF_ASSERT(m_tree.contains(v));
129  if (m_tree.isEven(v)) {
130  return m_delta;
131  } else {
132  return -m_delta;
133  }
134  }
135 
137  void addDelta(double delta) {
138  OGDF_ASSERT(delta >= 0);
139  m_delta += delta;
140  }
141 
144 
146  void addEvenEvenEdge(edge e) { m_evenEvenEdges.push(e, m_helper.getReducedWeight(e)); }
147 
149  void addEvenFreeEdge(edge e) { m_evenFreeEdges.push(e, m_helper.getReducedWeight(e)); }
150 };
151 
152 template<class TWeight>
153 class AuxEdge {
154 private:
156 
157  // the actual edge in the auxiliary graph
159 
161 
162  // edges between an even node in the source tree and an even node in the target tree
164  // edges between an even node in the source tree and an odd node in the target tree
166  // edges between an odd node in the source tree and an even node in the target tree
168 
170  void addEdgeToPQ(edge e, EdgePQ& pq) {
171  OGDF_ASSERT(e->graphOf() == &m_helper.graph());
172  pq.push(e, m_helper.getReducedWeight(e));
173  }
174 
175 public:
176  AuxEdge(edge e, BlossomVHelper<TWeight>& helper) : m_edge(e), m_helper(helper) {
177  OGDF_ASSERT(e->graphOf() != &m_helper.graph());
178  }
179 
180  /* Getters */
181 
182  edge graphEdge() { return m_edge; }
183 
185 
187 
189 
190  /* End getters */
191 
194  OGDF_ASSERT(m_edge->isIncident(auxNode->graphNode()));
195  if (m_edge->source() == auxNode->graphNode()) {
196  return m_evenOddEdges;
197  } else {
198  return m_oddEvenEdges;
199  }
200  }
201 
204 
208  }
209 
212  return (!m_evenOddEdges.empty() && m_helper.isEqualityEdge(m_evenOddEdges.topElement()))
213  || (!m_oddEvenEdges.empty() && m_helper.isEqualityEdge(m_oddEvenEdges.topElement()));
214  }
215 };
216 
217 template<class TWeight>
218 class AuxGraph {
220 
221  // the auxiliary graph
223 
230 
233  node orig = m_graph.original(v);
234  auto auxNode = new AuxNode<TWeight>(v, orig, m_helper);
236  m_nodeAuxNodeMap[orig] = auxNode;
237  return auxNode;
238  }
239 
242  auto auxEdge = new AuxEdge<TWeight>(e, m_helper);
244  return auxEdge;
245  }
246 
247 public:
249  : m_helper(helper)
250  , m_auxGraphNodeMap(m_graph, nullptr)
251  , m_auxGraphEdgeMap(m_graph, nullptr)
252  , m_nodeAuxNodeMap(m_helper.graph(), nullptr) {
253  reset();
254  }
255 
256  /* Getters */
257 
259 
261 
263 
266 
269 
272 
273  /* End of getters */
274 
276  void reset() {
277  m_graph.clear();
279 
280  // create all aux nodes
281  for (node v : m_helper.graph().nodes) {
282  if (!m_helper.matching(v)) {
284  }
285  }
286  // fill all edges
287  for (node auxGraphNode : m_graph.nodes) {
288  node u = m_graph.original(auxGraphNode);
289  for (auto adj : u->adjEntries) {
290  edge e = adj->theEdge();
291  node v = adj->twinNode();
292  if (m_graph.copy(v) != nullptr) {
293  if (m_graph.copy(e) == nullptr) {
295  auxEdge->addEvenEvenEdge(e);
296  }
297  } else {
298  auxNode(auxGraphNode)->addEvenFreeEdge(e);
299  }
300  }
301  }
302  }
303 
307  for (node v : e->nodes()) {
308  if (auto auxNode = treeAuxNode(v)) {
309  return auxNode;
310  }
311  }
312  return nullptr;
313  }
314 
317  auto auxNode = treeAuxNode(v);
318  return auxNode ? &auxNode->tree() : nullptr;
319  }
320 
324  if (auxNode->currentEdge() == nullptr) {
325  edge e = m_graph.newEdge(auxNode->graphNode(), current->graphNode());
326  auto auxEdge = createAuxEdge(e);
327  auxNode->setCurrentEdge(auxEdge);
328  return auxEdge;
329  } else {
330  return auxNode->currentEdge();
331  }
332  }
333 
336 
339  auto tree = auxNode->tree();
340  for (auto treeNodes : {tree.evenNodes, tree.oddNodes}) {
341  for (node v : treeNodes) {
342  m_helper.y(v) = m_helper.realY(v);
343  m_nodeAuxNodeMap[v] = nullptr;
344  }
345  }
346  for (auto adj : auxNode->graphNode()->adjEntries) {
347  AuxEdge<TWeight>* ae = auxEdge(adj->theEdge());
348  delete ae;
349  }
350  m_graph.delNode(auxNode->graphNode());
351  delete auxNode;
352  }
353 
359  void connectedComponents(std::vector<std::unordered_set<node>>& components) {
360  NodeArray<bool> visited(m_graph, false);
361  std::vector<node> stack;
362  for (node u : m_graph.nodes) {
363  if (!visited[u]) {
364  visited[u] = true;
365  stack.push_back(u);
366  std::unordered_set<node> component = {u};
367  while (!stack.empty()) {
368  node v = stack.back();
369  stack.pop_back();
370  for (adjEntry adj : v->adjEntries) {
371  node w = adj->twinNode();
372  if (!visited[w]) {
373  auto auxEdge = this->auxEdge(adj->theEdge());
374  if (auxEdge->hasEvenOddEqualityEdge()) {
375  component.insert(w);
376  visited[w] = true;
377  stack.push_back(w);
378  }
379  }
380  }
381  }
382  components.push_back(component);
383  }
384  }
385 #ifdef OGDF_DEBUG
386  // check that all nodes are in a component
387  int count = 0;
388  for (auto component : components) {
389  count += component.size();
390  }
391  OGDF_ASSERT(count == m_graph.numberOfNodes());
392 #endif
393  };
394 };
395 
396 }
397 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::matching_blossom::AuxGraph::m_graph
GraphCopySimple m_graph
Definition: AuxGraph.h:222
ogdf::matching_blossom::AlternatingTree
Definition: AlternatingTree.h:50
Graph.h
Includes declaration of graph class.
PQ.h
An extension of the standard priority queue with additional features.
ogdf::matching_blossom::AuxGraph::createAuxNode
AuxNode< TWeight > * createAuxNode(node v)
Creates an AuxNode for v and stores it in the appropriate maps.
Definition: AuxGraph.h:232
Pseudonode.h
Helper structure representing a pseudonode in the Blossom algorithm.
ogdf::matching_blossom::AuxNode::edge
AuxEdge< TWeight > * edge
Definition: AuxGraph.h:84
ogdf::matching_blossom::AuxGraph::assertCurrentEdge
AuxEdge< TWeight > * assertCurrentEdge(AuxNode< TWeight > *auxNode, AuxNode< TWeight > *current)
Creates an AuxEdge between auxNode and current if it does not exist and sets the currentEdge of auxNo...
Definition: AuxGraph.h:323
ogdf::matching_blossom::AuxNode::oddPseudonodes
NodePQ & oddPseudonodes()
Definition: AuxGraph.h:120
ogdf::GraphCopySimple::copy
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition: GraphCopy.h:288
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::matching_blossom::AuxGraph::treeOf
AlternatingTree< TWeight > * treeOf(node v)
Returns the tree which contains v, or nullptr if v is free.
Definition: AuxGraph.h:316
ogdf::matching_blossom::AuxEdge::evenEvenEdges
EdgePQ & evenEvenEdges()
Definition: AuxGraph.h:184
ogdf::matching_blossom::AuxNode::addEvenFreeEdge
void addEvenFreeEdge(edge e)
Add e to the list of even-free edges of this tree.
Definition: AuxGraph.h:149
ogdf::matching_blossom::AuxNode::evenEvenEdges
EdgePQ & evenEvenEdges()
Definition: AuxGraph.h:116
ogdf::matching_blossom::AuxGraph::deleteNode
void deleteNode(AuxNode< TWeight > *auxNode)
Removes auxNode and all incident edges from the auxiliary graph.
Definition: AuxGraph.h:338
ogdf::matching_blossom::AuxGraph::AuxGraph
AuxGraph(BlossomVHelper< TWeight > &helper)
Definition: AuxGraph.h:248
ogdf::matching_blossom::AuxGraph::m_auxGraphNodeMap
NodeArray< AuxNode< TWeight > * > m_auxGraphNodeMap
maps auxiliary graph nodes to their AuxNode objects
Definition: AuxGraph.h:225
ogdf::matching_blossom::AuxGraph::edges
const EdgeArray< AuxEdge< TWeight > * > & edges() const
Definition: AuxGraph.h:262
ogdf::matching_blossom::AuxEdge::m_edge
edge m_edge
Definition: AuxGraph.h:158
ogdf::matching_blossom::AuxEdge::oddEvenEdges
EdgePQ & oddEvenEdges()
Definition: AuxGraph.h:188
ogdf::matching_blossom::AuxGraph::auxEdge
AuxEdge< TWeight > * auxEdge(edge e)
Returns the AuxEdge corresponding to e, which must be an edge of the auxiliary graph.
Definition: AuxGraph.h:265
ogdf::matching_blossom::AuxNode::iteration
long iteration
Definition: AuxGraph.h:85
ogdf::matching_blossom::AuxNode::m_evenFreeEdges
EdgePQ m_evenFreeEdges
Edges between an even node in this tree and a free node.
Definition: AuxGraph.h:77
ogdf::GraphCopySimple
Copies of graphs with mapping between nodes and edges.
Definition: GraphCopy.h:254
ogdf::matching_blossom::AuxEdge::evenOddEdgesFromPerspective
EdgePQ & evenOddEdgesFromPerspective(AuxNode< TWeight > *auxNode)
Returns evenOddEdges or oddEvenEdges, depending on the perspective of auxNode (the even node).
Definition: AuxGraph.h:193
ogdf::GraphCopySimple::setOriginalGraph
void setOriginalGraph(const Graph *G) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
ogdf::matching_blossom::AuxNode::addOddPseudonode
void addOddPseudonode(node v)
Add v to the list of odd pseudonodes of this tree.
Definition: AuxGraph.h:143
ogdf::matching_blossom::BlossomVHelper
Helper class for the blossom matching algorithms.
Definition: AuxGraph.h:53
ogdf::matching_blossom::AuxNode::delta
double delta()
Definition: AuxGraph.h:122
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::matching_blossom::AuxGraph::treeAuxNode
AuxNode< TWeight > * treeAuxNode(node v)
Returns the AuxNode of whose tree the node v of the actual graph is a part of.
Definition: AuxGraph.h:271
ogdf::matching_blossom::AuxNode::setCurrentEdge
void setCurrentEdge(AuxEdge< TWeight > *edge)
Sets the current edge of this tree to edge and update the iteration to the current one.
Definition: AuxGraph.h:107
Cycle.h
Utility class representing a cycle in the Blossom algorithm.
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:153
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:209
ogdf::matching_blossom::AuxGraph::m_auxGraphEdgeMap
EdgeArray< AuxEdge< TWeight > * > m_auxGraphEdgeMap
maps auxiliary graph edges to their AuxEdge objects
Definition: AuxGraph.h:227
ogdf::matching_blossom::AuxNode::addDelta
void addDelta(double delta)
Add delta to this tree. delta must be non-negative.
Definition: AuxGraph.h:137
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:924
ogdf::EdgeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:433
ogdf::matching_blossom::AuxEdge
Definition: AuxGraph.h:55
ogdf::matching_blossom::AuxNode::evenFreeEdges
EdgePQ & evenFreeEdges()
Definition: AuxGraph.h:118
ogdf::matching_blossom::AuxEdge::m_evenEvenEdges
EdgePQ m_evenEvenEdges
Definition: AuxGraph.h:163
ogdf::matching_blossom::AuxEdge::graphEdge
edge graphEdge()
Definition: AuxGraph.h:182
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:974
ogdf::matching_blossom::AuxNode::m_oddPseudonodes
NodePQ m_oddPseudonodes
All odd pseudonodes in this tree.
Definition: AuxGraph.h:79
ogdf::matching_blossom::AuxGraph::createAuxEdge
AuxEdge< TWeight > * createAuxEdge(edge e)
Creates an AuxEdge for e and stores it in the appropriate maps.
Definition: AuxGraph.h:241
ogdf::matching_blossom::AuxGraph::auxNodeForEdge
AuxNode< TWeight > * auxNodeForEdge(edge e)
Returns the AuxNode of the auxiliary graph whose tree contains at least one endpoint of e....
Definition: AuxGraph.h:306
ogdf::matching_blossom::AuxGraph::connectedComponents
void connectedComponents(std::vector< std::unordered_set< node >> &components)
Calculates the connected components of the auxiliary graph and stores them in components....
Definition: AuxGraph.h:359
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
EdgeArray.h
Declaration and implementation of EdgeArray class.
ogdf::matching_blossom::AuxNode::m_tree
AlternatingTree< TWeight > m_tree
The alternating tree this node represents.
Definition: AuxGraph.h:69
ogdf::matching_blossom::AuxNode::tree
AlternatingTree< TWeight > & tree()
Definition: AuxGraph.h:114
ogdf::matching_blossom::AuxNode::currentEdge
AuxEdge< TWeight > * currentEdge()
The aux edge pointing to the current aux node.
Definition: AuxGraph.h:98
ogdf::matching_blossom::AuxNode::graphNode
node graphNode()
Definition: AuxGraph.h:112
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:391
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:185
GraphCopy.h
Declaration of graph copy classes.
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::matching_blossom::AuxNode::AuxNode
AuxNode(node auxGraphNode, node graphNode, BlossomVHelper< TWeight > &helper)
Definition: AuxGraph.h:89
ogdf::GraphCopyBase::delNode
void delNode(node v) override
Removes node v.
Definition: GraphCopy.h:200
ogdf::matching_blossom::AuxEdge::m_evenOddEdges
EdgePQ m_evenOddEdges
Definition: AuxGraph.h:165
ogdf::pq_internal::PrioritizedQueue::topElement
const E & topElement() const
Returns the topmost element in the queue.
Definition: PriorityQueue.h:284
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:168
ogdf::matching_blossom::AuxNode::delta
double delta(node v)
The delta of this tree for the given node.
Definition: AuxGraph.h:127
NodeArray.h
Declaration and implementation of NodeArray class.
ogdf::GraphCopySimple::clear
void clear() override
Removes all nodes and edges from this copy but does not break the link with the original graph.
ogdf::EdgeStandardType::tree
@ tree
for every hyperedge e a minimal subcubic tree connecting all hypernodes incident with e together is a...
ogdf::matching_blossom::AuxGraph::auxNode
AuxNode< TWeight > * auxNode(node v)
Returns the AuxNode corresponding to v, which must be a node of the auxiliary graph.
Definition: AuxGraph.h:268
ogdf::matching_blossom::AuxGraph::m_nodeAuxNodeMap
NodeArray< AuxNode< TWeight > * > m_nodeAuxNodeMap
maps normal graph nodes to the AuxNode whose tree they belong to
Definition: AuxGraph.h:229
ogdf::matching_blossom::AuxNode
Definition: AuxGraph.h:58
ogdf::matching_blossom::AuxGraph::m_helper
BlossomVHelper< TWeight > & m_helper
Definition: AuxGraph.h:219
ogdf::matching_blossom::AuxEdge::m_oddEvenEdges
EdgePQ m_oddEvenEdges
Definition: AuxGraph.h:167
ogdf::matching_blossom::AuxEdge::hasEvenOddEqualityEdge
bool hasEvenOddEqualityEdge()
Whether or not this edge connects two trees via an alternating equality edge.
Definition: AuxGraph.h:211
ogdf::matching_blossom::AuxEdge::addEdgeToPQ
void addEdgeToPQ(edge e, EdgePQ &pq)
Helper function to add an edge to a priority queue with its current reduced weight.
Definition: AuxGraph.h:170
ogdf::matching_blossom::AuxGraph::nodes
const NodeArray< AuxNode< TWeight > * > & nodes() const
Definition: AuxGraph.h:260
utils.h
Utility functions and classes regarding map access and iteration.
ogdf::matching_blossom::AuxEdge::m_helper
BlossomVHelper< TWeight > & m_helper
Definition: AuxGraph.h:160
ogdf::matching_blossom::BlossomPQ::push
void push(const E &element, const TWeight priority)
Adds a new element to the queue.
Definition: PQ.h:79
ogdf::matching_blossom::AuxEdge::AuxEdge
AuxEdge(edge e, BlossomVHelper< TWeight > &helper)
Definition: AuxGraph.h:176
AlternatingTree.h
Implementation of an Alternating Tree helper structure for the Blossom algorithm.
ogdf::matching_blossom::AuxGraph
Definition: AuxGraph.h:218
ogdf::matching_blossom::AuxEdge::addEvenOddEdgeFromPerspective
void addEvenOddEdgeFromPerspective(edge e, AuxNode< TWeight > *auxNode)
Adds e to evenOddEdges or oddEvenEdges depending on the perspective of auxNode.
Definition: AuxGraph.h:206
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::NodeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:337
ogdf::matching_blossom::AuxGraph::reset
void reset()
Rebuilds the auxiliary graph from the current graph.
Definition: AuxGraph.h:276
ogdf::matching_blossom::AuxNode::m_node
node m_node
The actual node in the auxiliary graph.
Definition: AuxGraph.h:63
ogdf::EdgeElement::isIncident
bool isIncident(node v) const
Returns true iff v is incident to the edge.
Definition: Graph_d.h:437
ogdf::matching_blossom::AuxEdge::evenOddEdges
EdgePQ & evenOddEdges()
Definition: AuxGraph.h:186
BlossomVHelper.h
Utility class for the Blossom V algorithm.
ogdf::matching_blossom::AuxNode::m_delta
double m_delta
The cummulated delta of this tree.
Definition: AuxGraph.h:72
ogdf::matching_blossom::AuxGraph::setAuxNode
void setAuxNode(node v, AuxNode< TWeight > *auxNode)
Sets the AuxNode of v to auxNode.
Definition: AuxGraph.h:335
ogdf::matching_blossom::AuxEdge::addEvenEvenEdge
void addEvenEvenEdge(edge e)
Adds e to evenEvenEdges.
Definition: AuxGraph.h:203
ogdf::matching_blossom::BlossomPQ< edge, TWeight >
ogdf::EdgeElement::nodes
std::array< node, 2 > nodes() const
Returns a list of adjacent nodes. If this edge is a self-loop, both entries will be the same node.
Definition: Graph_d.h:397
ogdf::matching_blossom::AuxNode::m_evenEvenEdges
EdgePQ m_evenEvenEdges
Eges between even nodes in this tree.
Definition: AuxGraph.h:75
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::GraphCopySimple::newEdge
edge newEdge(edge eOrig)
Creates a new edge in the graph copy with original edge eOrig.
Definition: GraphCopy.h:314
ogdf::matching_blossom::AuxNode::addEvenEvenEdge
void addEvenEvenEdge(edge e)
Add e to the list of even-even edges of this tree.
Definition: AuxGraph.h:146
ogdf::matching_blossom::AuxGraph::graph
GraphCopySimple & graph()
Definition: AuxGraph.h:258
ogdf::matching_blossom::AuxNode::m_currentEdge
struct ogdf::matching_blossom::AuxNode::@6 m_currentEdge
Structure representing the current edge of this tree. To avoid resetting all edges of all trees after...
ogdf::GraphCopyBase::original
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:98
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709
ogdf::matching_blossom::AuxNode::m_helper
BlossomVHelper< TWeight > & m_helper
The auxiliary graph this node belongs to.
Definition: AuxGraph.h:66