Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
AuxGraph.h
Go to the documentation of this file.
1
33#pragma once
34
35#include <ogdf/basic/Graph.h>
38#include <ogdf/basic/basic.h>
41
42#include <array>
43#include <unordered_set>
44#include <vector>
45
46namespace ogdf {
47namespace matching_blossom {
48
49template<class TWeight>
50class AuxEdge;
51template<class TWeight>
52class BlossomVHelper;
53
54template<class TWeight>
55class AuxNode {
58
61
64
67
69 double m_delta = 0;
70
77
80 struct {
82 long iteration = -1;
84
85public:
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
149template<class TWeight>
150class AuxEdge {
151private:
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
172public:
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
192 if (m_edge->source() == auxNode->graphNode()) {
193 return m_evenOddEdges;
194 } else {
195 return m_oddEvenEdges;
196 }
197 }
198
201
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
214template<class TWeight>
215class 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);
234 return auxNode;
235 }
236
243
244public:
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 }
389#endif
390 };
391};
392
393}
394}
Implementation of an Alternating Tree helper structure for the Blossom algorithm.
Includes declaration of graph class.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
An extension of the standard priority queue with additional features.
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
Class for the representation of edges.
Definition Graph_d.h:364
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition Graph_d.h:441
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:405
bool isIncident(node v) const
Returns true iff v is incident to the edge.
Definition Graph_d.h:445
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition GraphCopy.h:191
void delNode(node v) override
Removes node v.
Definition GraphCopy.h:206
const Graph & original() const
Returns a reference to the original graph.
Definition GraphCopy.h:104
Copies of graphs with mapping between nodes and edges.
Definition GraphCopy.h:260
edge newEdge(edge eOrig)
Creates a new edge in the graph copy with original edge eOrig.
Definition GraphCopy.h:320
void setOriginalGraph(const Graph *G) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
void clear() override
Removes all nodes and edges from this copy but does not break the link with the original graph.
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition GraphCopy.h:294
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition Graph_d.h:979
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:929
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition Graph_d.h:345
bool empty() const
Checks whether the queue is empty.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
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
AuxEdge(edge e, BlossomVHelper< TWeight > &helper)
Definition AuxGraph.h:173
void addEvenEvenEdge(edge e)
Adds e to evenEvenEdges.
Definition AuxGraph.h:200
EdgePQ & evenOddEdgesFromPerspective(AuxNode< TWeight > *auxNode)
Returns evenOddEdges or oddEvenEdges, depending on the perspective of auxNode (the even node).
Definition AuxGraph.h:190
BlossomVHelper< TWeight > & m_helper
Definition AuxGraph.h:157
void addEvenOddEdgeFromPerspective(edge e, AuxNode< TWeight > *auxNode)
Adds e to evenOddEdges or oddEvenEdges depending on the perspective of auxNode.
Definition AuxGraph.h:203
bool hasEvenOddEqualityEdge()
Whether or not this edge connects two trees via an alternating equality edge.
Definition AuxGraph.h:208
GraphCopySimple & graph()
Definition AuxGraph.h:255
AuxNode< TWeight > * createAuxNode(node v)
Creates an AuxNode for v and stores it in the appropriate maps.
Definition AuxGraph.h:229
const NodeArray< AuxNode< TWeight > * > & nodes() const
Definition AuxGraph.h:257
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
NodeArray< AuxNode< TWeight > * > m_nodeAuxNodeMap
maps normal graph nodes to the AuxNode whose tree they belong to
Definition AuxGraph.h:226
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
AuxEdge< TWeight > * auxEdge(edge e)
Returns the AuxEdge corresponding to e, which must be an edge of the auxiliary graph.
Definition AuxGraph.h:262
void setAuxNode(node v, AuxNode< TWeight > *auxNode)
Sets the AuxNode of v to auxNode.
Definition AuxGraph.h:332
NodeArray< AuxNode< TWeight > * > m_auxGraphNodeMap
maps auxiliary graph nodes to their AuxNode objects
Definition AuxGraph.h:222
AuxGraph(BlossomVHelper< TWeight > &helper)
Definition AuxGraph.h:245
BlossomVHelper< TWeight > & m_helper
Definition AuxGraph.h:216
void reset()
Rebuilds the auxiliary graph from the current graph.
Definition AuxGraph.h:273
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
AlternatingTree< TWeight > * treeOf(node v)
Returns the tree which contains v, or nullptr if v is free.
Definition AuxGraph.h:313
EdgeArray< AuxEdge< TWeight > * > m_auxGraphEdgeMap
maps auxiliary graph edges to their AuxEdge objects
Definition AuxGraph.h:224
AuxNode< TWeight > * auxNode(node v)
Returns the AuxNode corresponding to v, which must be a node of the auxiliary graph.
Definition AuxGraph.h:265
AuxEdge< TWeight > * createAuxEdge(edge e)
Creates an AuxEdge for e and stores it in the appropriate maps.
Definition AuxGraph.h:238
const EdgeArray< AuxEdge< TWeight > * > & edges() const
Definition AuxGraph.h:259
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
void deleteNode(AuxNode< TWeight > *auxNode)
Removes auxNode and all incident edges from the auxiliary graph.
Definition AuxGraph.h:335
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...
AlternatingTree< TWeight > m_tree
The alternating tree this node represents.
Definition AuxGraph.h:66
BlossomVHelper< TWeight > & m_helper
The auxiliary graph this node belongs to.
Definition AuxGraph.h:63
AlternatingTree< TWeight > & tree()
Definition AuxGraph.h:111
EdgePQ m_evenFreeEdges
Edges between an even node in this tree and a free node.
Definition AuxGraph.h:74
AuxEdge< TWeight > * edge
Definition AuxGraph.h:81
void addEvenFreeEdge(edge e)
Add e to the list of even-free edges of this tree.
Definition AuxGraph.h:146
AuxEdge< TWeight > * currentEdge()
The aux edge pointing to the current aux node.
Definition AuxGraph.h:95
node m_node
The actual node in the auxiliary graph.
Definition AuxGraph.h:60
void addOddPseudonode(node v)
Add v to the list of odd pseudonodes of this tree.
Definition AuxGraph.h:140
double delta(node v)
The delta of this tree for the given node.
Definition AuxGraph.h:124
void addDelta(double delta)
Add delta to this tree. delta must be non-negative.
Definition AuxGraph.h:134
EdgePQ m_evenEvenEdges
Eges between even nodes in this tree.
Definition AuxGraph.h:72
double m_delta
The cummulated delta of this tree.
Definition AuxGraph.h:69
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
void addEvenEvenEdge(edge e)
Add e to the list of even-even edges of this tree.
Definition AuxGraph.h:143
NodePQ m_oddPseudonodes
All odd pseudonodes in this tree.
Definition AuxGraph.h:76
AuxNode(node auxGraphNode, node graphNode, BlossomVHelper< TWeight > &helper)
Definition AuxGraph.h:86
void push(const E &element, const TWeight priority)
Adds a new element to the queue.
Definition PQ.h:81
Helper class for the blossom matching algorithms.
const E & topElement() const
Returns the topmost element in the queue.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.
@ tree
for every hyperedge e a minimal subcubic tree connecting all hypernodes incident with e together is a...