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/Graph.h>
36 #include <ogdf/basic/GraphCopy.h>
37 #include <ogdf/basic/GraphList.h>
38 #include <ogdf/basic/basic.h>
41 
42 #include <array>
43 #include <unordered_set>
44 #include <vector>
45 
46 namespace ogdf {
47 namespace matching_blossom {
48 
49 template<class TWeight>
50 class AuxEdge;
51 template<class TWeight>
53 
54 template<class TWeight>
55 class AuxNode {
58 
61 
64 
67 
69  double m_delta = 0;
70 
77 
80  struct {
81  AuxEdge<TWeight>* edge = nullptr;
82  long iteration = -1;
83  } m_currentEdge;
84 
85 public:
87  : m_node(auxGraphNode), m_helper(helper), m_tree(m_helper, graphNode) {
88  OGDF_ASSERT(auxGraphNode->graphOf() != &m_helper.graph());
89  OGDF_ASSERT(graphNode->graphOf() == &m_helper.graph());
90  }
91 
92  /* Getters & setters */
93 
96  if (m_currentEdge.iteration == m_helper.currentIteration) {
97  return m_currentEdge.edge;
98  } else {
99  return nullptr;
100  }
101  };
102 
105  m_currentEdge.edge = edge;
106  m_currentEdge.iteration = m_helper.currentIteration;
107  }
108 
109  node graphNode() { return m_node; }
110 
112 
114 
116 
118 
119  double delta() { return m_delta; }
120 
121  /* End of getters & setters */
122 
124  double delta(node v) {
125  OGDF_ASSERT(m_tree.contains(v));
126  if (m_tree.isEven(v)) {
127  return m_delta;
128  } else {
129  return -m_delta;
130  }
131  }
132 
134  void addDelta(double delta) {
135  OGDF_ASSERT(delta >= 0);
136  m_delta += delta;
137  }
138 
141 
143  void addEvenEvenEdge(edge e) { m_evenEvenEdges.push(e, m_helper.getReducedWeight(e)); }
144 
146  void addEvenFreeEdge(edge e) { m_evenFreeEdges.push(e, m_helper.getReducedWeight(e)); }
147 };
148 
149 template<class TWeight>
150 class AuxEdge {
151 private:
153 
154  // the actual edge in the auxiliary graph
156 
158 
159  // edges between an even node in the source tree and an even node in the target tree
161  // edges between an even node in the source tree and an odd node in the target tree
163  // edges between an odd node in the source tree and an even node in the target tree
165 
167  void addEdgeToPQ(edge e, EdgePQ& pq) {
168  OGDF_ASSERT(e->graphOf() == &m_helper.graph());
169  pq.push(e, m_helper.getReducedWeight(e));
170  }
171 
172 public:
173  AuxEdge(edge e, BlossomVHelper<TWeight>& helper) : m_edge(e), m_helper(helper) {
174  OGDF_ASSERT(e->graphOf() != &m_helper.graph());
175  }
176 
177  /* Getters */
178 
179  edge graphEdge() { return m_edge; }
180 
182 
184 
186 
187  /* End getters */
188 
191  OGDF_ASSERT(m_edge->isIncident(auxNode->graphNode()));
192  if (m_edge->source() == auxNode->graphNode()) {
193  return m_evenOddEdges;
194  } else {
195  return m_oddEvenEdges;
196  }
197  }
198 
201 
205  }
206 
209  return (!m_evenOddEdges.empty() && m_helper.isEqualityEdge(m_evenOddEdges.topElement()))
210  || (!m_oddEvenEdges.empty() && m_helper.isEqualityEdge(m_oddEvenEdges.topElement()));
211  }
212 };
213 
214 template<class TWeight>
215 class AuxGraph {
217 
218  // the auxiliary graph
220 
227 
230  node orig = m_graph.original(v);
231  auto auxNode = new AuxNode<TWeight>(v, orig, m_helper);
233  m_nodeAuxNodeMap[orig] = auxNode;
234  return auxNode;
235  }
236 
239  auto auxEdge = new AuxEdge<TWeight>(e, m_helper);
241  return auxEdge;
242  }
243 
244 public:
246  : m_helper(helper)
247  , m_auxGraphNodeMap(m_graph, nullptr)
248  , m_auxGraphEdgeMap(m_graph, nullptr)
249  , m_nodeAuxNodeMap(m_helper.graph(), nullptr) {
250  reset();
251  }
252 
253  /* Getters */
254 
256 
258 
260 
263 
266 
269 
270  /* End of getters */
271 
273  void reset() {
274  m_graph.clear();
276 
277  // create all aux nodes
278  for (node v : m_helper.graph().nodes) {
279  if (!m_helper.matching(v)) {
281  }
282  }
283  // fill all edges
284  for (node auxGraphNode : m_graph.nodes) {
285  node u = m_graph.original(auxGraphNode);
286  for (auto adj : u->adjEntries) {
287  edge e = adj->theEdge();
288  node v = adj->twinNode();
289  if (m_graph.copy(v) != nullptr) {
290  if (m_graph.copy(e) == nullptr) {
292  auxEdge->addEvenEvenEdge(e);
293  }
294  } else {
295  auxNode(auxGraphNode)->addEvenFreeEdge(e);
296  }
297  }
298  }
299  }
300 
304  for (node v : e->nodes()) {
305  if (auto auxNode = treeAuxNode(v)) {
306  return auxNode;
307  }
308  }
309  return nullptr;
310  }
311 
314  auto auxNode = treeAuxNode(v);
315  return auxNode ? &auxNode->tree() : nullptr;
316  }
317 
321  if (auxNode->currentEdge() == nullptr) {
322  edge e = m_graph.newEdge(auxNode->graphNode(), current->graphNode());
323  auto auxEdge = createAuxEdge(e);
324  auxNode->setCurrentEdge(auxEdge);
325  return auxEdge;
326  } else {
327  return auxNode->currentEdge();
328  }
329  }
330 
333 
336  auto tree = auxNode->tree();
337  for (auto treeNodes : {tree.evenNodes, tree.oddNodes}) {
338  for (node v : treeNodes) {
339  m_helper.y(v) = m_helper.realY(v);
340  m_nodeAuxNodeMap[v] = nullptr;
341  }
342  }
343  for (auto adj : auxNode->graphNode()->adjEntries) {
344  AuxEdge<TWeight>* ae = auxEdge(adj->theEdge());
345  delete ae;
346  }
347  m_graph.delNode(auxNode->graphNode());
348  delete auxNode;
349  }
350 
356  void connectedComponents(std::vector<std::unordered_set<node>>& components) {
357  NodeArray<bool> visited(m_graph, false);
358  std::vector<node> stack;
359  for (node u : m_graph.nodes) {
360  if (!visited[u]) {
361  visited[u] = true;
362  stack.push_back(u);
363  std::unordered_set<node> component = {u};
364  while (!stack.empty()) {
365  node v = stack.back();
366  stack.pop_back();
367  for (adjEntry adj : v->adjEntries) {
368  node w = adj->twinNode();
369  if (!visited[w]) {
370  auto auxEdge = this->auxEdge(adj->theEdge());
371  if (auxEdge->hasEvenOddEqualityEdge()) {
372  component.insert(w);
373  visited[w] = true;
374  stack.push_back(w);
375  }
376  }
377  }
378  }
379  components.push_back(component);
380  }
381  }
382 #ifdef OGDF_DEBUG
383  // check that all nodes are in a component
384  int count = 0;
385  for (auto component : components) {
386  count += component.size();
387  }
388  OGDF_ASSERT(count == m_graph.numberOfNodes());
389 #endif
390  };
391 };
392 
393 }
394 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::matching_blossom::AuxGraph::m_graph
GraphCopySimple m_graph
Definition: AuxGraph.h:219
ogdf::matching_blossom::AlternatingTree
Definition: AlternatingTree.h:59
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:229
ogdf::matching_blossom::AuxNode::edge
AuxEdge< TWeight > * edge
Definition: AuxGraph.h:81
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:320
ogdf::matching_blossom::AuxNode::oddPseudonodes
NodePQ & oddPseudonodes()
Definition: AuxGraph.h:117
ogdf::GraphCopySimple::copy
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition: GraphCopy.h:295
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
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:313
ogdf::matching_blossom::AuxEdge::evenEvenEdges
EdgePQ & evenEvenEdges()
Definition: AuxGraph.h:181
ogdf::matching_blossom::AuxNode::addEvenFreeEdge
void addEvenFreeEdge(edge e)
Add e to the list of even-free edges of this tree.
Definition: AuxGraph.h:146
ogdf::matching_blossom::AuxNode::evenEvenEdges
EdgePQ & evenEvenEdges()
Definition: AuxGraph.h:113
ogdf::matching_blossom::AuxGraph::deleteNode
void deleteNode(AuxNode< TWeight > *auxNode)
Removes auxNode and all incident edges from the auxiliary graph.
Definition: AuxGraph.h:335
ogdf::matching_blossom::AuxGraph::AuxGraph
AuxGraph(BlossomVHelper< TWeight > &helper)
Definition: AuxGraph.h:245
ogdf::matching_blossom::AuxGraph::m_auxGraphNodeMap
NodeArray< AuxNode< TWeight > * > m_auxGraphNodeMap
maps auxiliary graph nodes to their AuxNode objects
Definition: AuxGraph.h:222
ogdf::matching_blossom::AuxGraph::edges
const EdgeArray< AuxEdge< TWeight > * > & edges() const
Definition: AuxGraph.h:259
ogdf::matching_blossom::AuxEdge::m_edge
edge m_edge
Definition: AuxGraph.h:155
ogdf::matching_blossom::AuxEdge::oddEvenEdges
EdgePQ & oddEvenEdges()
Definition: AuxGraph.h:185
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:262
ogdf::matching_blossom::AuxNode::iteration
long iteration
Definition: AuxGraph.h:82
ogdf::matching_blossom::AuxNode::m_evenFreeEdges
EdgePQ m_evenFreeEdges
Edges between an even node in this tree and a free node.
Definition: AuxGraph.h:74
ogdf::GraphCopySimple
Copies of graphs with mapping between nodes and edges.
Definition: GraphCopy.h:261
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:190
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:140
ogdf::matching_blossom::BlossomVHelper
Helper class for the blossom matching algorithms.
Definition: AuxGraph.h:52
ogdf::matching_blossom::AuxNode::delta
double delta()
Definition: AuxGraph.h:119
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
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:268
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:104
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:160
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:211
ogdf::matching_blossom::AuxGraph::m_auxGraphEdgeMap
EdgeArray< AuxEdge< TWeight > * > m_auxGraphEdgeMap
maps auxiliary graph edges to their AuxEdge objects
Definition: AuxGraph.h:224
ogdf::matching_blossom::AuxNode::addDelta
void addDelta(double delta)
Add delta to this tree. delta must be non-negative.
Definition: AuxGraph.h:134
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:932
ogdf::EdgeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:440
ogdf::matching_blossom::AuxEdge
Definition: AuxGraph.h:50
ogdf::matching_blossom::AuxNode::evenFreeEdges
EdgePQ & evenFreeEdges()
Definition: AuxGraph.h:115
ogdf::matching_blossom::AuxEdge::m_evenEvenEdges
EdgePQ m_evenEvenEdges
Definition: AuxGraph.h:160
ogdf::matching_blossom::AuxEdge::graphEdge
edge graphEdge()
Definition: AuxGraph.h:179
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:982
ogdf::matching_blossom::AuxNode::m_oddPseudonodes
NodePQ m_oddPseudonodes
All odd pseudonodes in this tree.
Definition: AuxGraph.h:76
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:238
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:303
GraphList.h
Decralation of GraphElement and GraphList classes.
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:356
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::matching_blossom::AuxNode::m_tree
AlternatingTree< TWeight > m_tree
The alternating tree this node represents.
Definition: AuxGraph.h:66
ogdf::matching_blossom::AuxNode::tree
AlternatingTree< TWeight > & tree()
Definition: AuxGraph.h:111
ogdf::matching_blossom::AuxNode::currentEdge
AuxEdge< TWeight > * currentEdge()
The aux edge pointing to the current aux node.
Definition: AuxGraph.h:95
ogdf::matching_blossom::AuxNode::graphNode
node graphNode()
Definition: AuxGraph.h:109
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:192
GraphCopy.h
Declaration of graph copy classes.
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::matching_blossom::AuxNode::AuxNode
AuxNode(node auxGraphNode, node graphNode, BlossomVHelper< TWeight > &helper)
Definition: AuxGraph.h:86
ogdf::GraphCopyBase::delNode
void delNode(node v) override
Removes node v.
Definition: GraphCopy.h:207
ogdf::matching_blossom::AuxEdge::m_evenOddEdges
EdgePQ m_evenOddEdges
Definition: AuxGraph.h:162
ogdf::pq_internal::PrioritizedQueue::topElement
const E & topElement() const
Returns the topmost element in the queue.
Definition: PriorityQueue.h:286
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
ogdf::matching_blossom::AuxNode::delta
double delta(node v)
The delta of this tree for the given node.
Definition: AuxGraph.h:124
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:265
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:226
ogdf::matching_blossom::AuxNode
Definition: AuxGraph.h:55
ogdf::matching_blossom::AuxGraph::m_helper
BlossomVHelper< TWeight > & m_helper
Definition: AuxGraph.h:216
ogdf::matching_blossom::AuxEdge::m_oddEvenEdges
EdgePQ m_oddEvenEdges
Definition: AuxGraph.h:164
ogdf::matching_blossom::AuxEdge::hasEvenOddEqualityEdge
bool hasEvenOddEqualityEdge()
Whether or not this edge connects two trees via an alternating equality edge.
Definition: AuxGraph.h:208
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:167
ogdf::matching_blossom::AuxGraph::nodes
const NodeArray< AuxNode< TWeight > * > & nodes() const
Definition: AuxGraph.h:257
basic.h
Basic declarations, included by all source files.
ogdf::matching_blossom::AuxEdge::m_helper
BlossomVHelper< TWeight > & m_helper
Definition: AuxGraph.h:157
ogdf::matching_blossom::BlossomPQ::push
void push(const E &element, const TWeight priority)
Adds a new element to the queue.
Definition: PQ.h:81
ogdf::matching_blossom::AuxEdge::AuxEdge
AuxEdge(edge e, BlossomVHelper< TWeight > &helper)
Definition: AuxGraph.h:173
AlternatingTree.h
Implementation of an Alternating Tree helper structure for the Blossom algorithm.
ogdf::matching_blossom::AuxGraph
Definition: AuxGraph.h:215
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:203
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::NodeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:344
ogdf::matching_blossom::AuxGraph::reset
void reset()
Rebuilds the auxiliary graph from the current graph.
Definition: AuxGraph.h:273
ogdf::matching_blossom::AuxNode::m_node
node m_node
The actual node in the auxiliary graph.
Definition: AuxGraph.h:60
ogdf::EdgeElement::isIncident
bool isIncident(node v) const
Returns true iff v is incident to the edge.
Definition: Graph_d.h:444
ogdf::matching_blossom::AuxEdge::evenOddEdges
EdgePQ & evenOddEdges()
Definition: AuxGraph.h:183
ogdf::matching_blossom::AuxNode::m_delta
double m_delta
The cummulated delta of this tree.
Definition: AuxGraph.h:69
ogdf::matching_blossom::AuxGraph::setAuxNode
void setAuxNode(node v, AuxNode< TWeight > *auxNode)
Sets the AuxNode of v to auxNode.
Definition: AuxGraph.h:332
ogdf::matching_blossom::AuxEdge::addEvenEvenEdge
void addEvenEvenEdge(edge e)
Adds e to evenEvenEdges.
Definition: AuxGraph.h:200
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:404
ogdf::matching_blossom::AuxNode::m_evenEvenEdges
EdgePQ m_evenEvenEdges
Eges between even nodes in this tree.
Definition: AuxGraph.h:72
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::GraphCopySimple::newEdge
edge newEdge(edge eOrig)
Creates a new edge in the graph copy with original edge eOrig.
Definition: GraphCopy.h:321
ogdf::matching_blossom::AuxNode::addEvenEvenEdge
void addEvenEvenEdge(edge e)
Add e to the list of even-even edges of this tree.
Definition: AuxGraph.h:143
ogdf::matching_blossom::AuxGraph::graph
GraphCopySimple & graph()
Definition: AuxGraph.h:255
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:105
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::matching_blossom::AuxNode::m_helper
BlossomVHelper< TWeight > & m_helper
The auxiliary graph this node belongs to.
Definition: AuxGraph.h:63