Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MatchingModule.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/EdgeArray.h>
35 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/GraphCopy.h>
38 #include <ogdf/basic/Logger.h>
39 #include <ogdf/basic/Module.h>
40 #include <ogdf/basic/NodeArray.h>
42 
43 #include <tuple>
44 #include <unordered_set>
45 
46 namespace ogdf {
47 
48 using namespace matching_blossom;
49 
50 template<typename TWeight>
51 class MatchingModule : public Module, public Logger {
52 public:
53  virtual ~MatchingModule() { }
54 
63  bool minimumWeightPerfectMatching(const Graph& G, const EdgeArray<TWeight>& weights,
64  std::unordered_set<edge>& matching) {
65  return doCall(G, weights, matching);
66  }
67 
75  bool minimumWeightPerfectMatching(const GraphAttributes& GA, std::unordered_set<edge>& matching) {
76  return doCall(GA, matching);
77  }
78 
87  std::tuple<bool, TWeight> minimumWeightPerfectMatching(const Graph& G,
88  const EdgeArray<TWeight>& weights) {
89  std::unordered_set<edge> matching;
90  bool result = minimumWeightPerfectMatching(G, weights, matching);
91  TWeight weight = matchingWeight(matching, weights);
92  return std::make_tuple(result, weight);
93  }
94 
102  std::tuple<bool, TWeight> minimumWeightPerfectMatching(const GraphAttributes& GA) {
103  std::unordered_set<edge> matching;
104  bool result = minimumWeightPerfectMatching(GA, matching);
105  TWeight weight = matchingWeight(matching, GA);
106  return std::make_tuple(result, weight);
107  }
108 
118  std::unordered_set<edge>& matching) {
119  EdgeArray<TWeight> invertedWeights(G);
120  copyWeights(G, weights, invertedWeights, true);
121  return doCall(G, invertedWeights, matching);
122  }
123 
131  bool maximumWeightPerfectMatching(const GraphAttributes& GA, std::unordered_set<edge>& matching) {
132  const Graph& G = GA.constGraph();
133  EdgeArray<TWeight> weights(G);
134  copyWeights(G, GA, weights, true);
135  return doCall(G, weights, matching);
136  }
137 
146  std::tuple<bool, TWeight> maximumWeightPerfectMatching(const Graph& G,
147  const EdgeArray<TWeight>& weights) {
148  std::unordered_set<edge> matching;
149  bool result = maximumWeightPerfectMatching(G, weights, matching);
150  TWeight weight = matchingWeight(matching, weights);
151  return std::make_tuple(result, weight);
152  }
153 
161  std::tuple<bool, TWeight> maximumWeightPerfectMatching(const GraphAttributes& GA) {
162  std::unordered_set<edge> matching;
163  bool result = maximumWeightPerfectMatching(GA, matching);
164  TWeight weight = matchingWeight(matching, GA);
165  return std::make_tuple(result, weight);
166  }
167 
175  void maximumWeightMatching(const Graph& G, const EdgeArray<TWeight>& weights,
176  std::unordered_set<edge>& matching) {
177  doMaximumWeightMatching(G, weights, matching);
178  }
179 
186  void maximumWeightMatching(const GraphAttributes& GA, std::unordered_set<edge>& matching) {
187  doMaximumWeightMatching(GA.constGraph(), GA, matching);
188  }
189 
197  TWeight maximumWeightMatching(const Graph& G, const EdgeArray<TWeight>& weights) {
198  std::unordered_set<edge> matching;
199  maximumWeightMatching(G, weights, matching);
200  return matchingWeight(matching, weights);
201  }
202 
210  std::unordered_set<edge> matching;
211  maximumWeightMatching(GA, matching);
212  return matchingWeight(matching, GA);
213  }
214 
216  TWeight matchingWeight(const std::unordered_set<edge>& matching,
217  const EdgeArray<TWeight>& weights) {
218  return getMatchingWeight(matching, weights);
219  }
220 
222  TWeight matchingWeight(const std::unordered_set<edge>& matching, const GraphAttributes& GA) {
223  return getMatchingWeight(matching, GA);
224  }
225 
226 protected:
228  virtual bool doCall(const Graph& G, const EdgeArray<TWeight>& weights,
229  std::unordered_set<edge>& matching) = 0;
230 
232  virtual bool doCall(const GraphAttributes& GA, std::unordered_set<edge>& matching) = 0;
233 
234 private:
235  template<class WeightContainer>
236  void copyWeights(const Graph& G, const WeightContainer& weights, EdgeArray<TWeight>& copy,
237  bool invert = false) {
238  for (edge e : G.edges) {
239  copy[e] = (invert ? -1 : 1) * getWeight<TWeight>(e, weights);
240  }
241  }
242 
243  template<class WeightContainer>
244  TWeight getMatchingWeight(const std::unordered_set<edge>& matching,
245  const WeightContainer& weights) {
246  TWeight value = 0;
247  for (edge e : matching) {
248  value += getWeight<TWeight>(e, weights);
249  }
250  return value;
251  }
252 
253  template<class WeightContainer>
254  void doMaximumWeightMatching(const Graph& G, const WeightContainer& weights,
255  std::unordered_set<edge>& matching) {
256  // Duplicate the graph and connect all nodes with their copies with 0-weight edges
257  GraphCopySimple graphCopy(G);
258  EdgeArray<TWeight> weightsCopy(graphCopy, 0);
259  NodeArray<node> nodeMap(graphCopy);
260  for (node origNode : G.nodes) {
261  node v = graphCopy.copy(origNode);
262  node dup = graphCopy.newNode();
263  nodeMap[v] = dup;
264  graphCopy.newEdge(v, dup);
265  }
266  for (edge origEdge : G.edges) {
267  edge e = graphCopy.copy(origEdge);
268  edge newEdge = graphCopy.newEdge(nodeMap[e->source()], nodeMap[e->target()]);
269  weightsCopy[newEdge] = weightsCopy[e] = -getWeight<TWeight>(origEdge, weights);
270  }
271 
272  // Calculate the MinimumWeightPerfectMatching on the new graph
273  std::unordered_set<edge> matchingCopy;
274 #ifdef OGDF_DEBUG
275  bool result =
276 #endif
277  doCall(graphCopy, weightsCopy, matchingCopy);
278  OGDF_ASSERT(result);
279 
280  // Convert the matching to the original graph
281  for (edge e : matchingCopy) {
282  if (edge origEdge = graphCopy.original(e)) {
283  matching.insert(origEdge);
284  }
285  }
286  };
287 };
288 
289 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:66
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::MatchingModule::copyWeights
void copyWeights(const Graph &G, const WeightContainer &weights, EdgeArray< TWeight > &copy, bool invert=false)
Definition: MatchingModule.h:236
ogdf::MatchingModule::maximumWeightMatching
void maximumWeightMatching(const GraphAttributes &GA, std::unordered_set< edge > &matching)
Computes a maximum weight matching in GA.
Definition: MatchingModule.h:186
Graph.h
Includes declaration of graph class.
ogdf::MatchingModule::~MatchingModule
virtual ~MatchingModule()
Definition: MatchingModule.h:53
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::MatchingModule::maximumWeightMatching
TWeight maximumWeightMatching(const Graph &G, const EdgeArray< TWeight > &weights)
Computes a maximum weight matching in G.
Definition: MatchingModule.h:197
ogdf::MatchingModule::maximumWeightMatching
void maximumWeightMatching(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)
Computes a maximum weight matching in G.
Definition: MatchingModule.h:175
ogdf::MatchingModule::maximumWeightMatching
TWeight maximumWeightMatching(const GraphAttributes &GA)
Computes a maximum weight matching in G.
Definition: MatchingModule.h:209
ogdf::MatchingModule::minimumWeightPerfectMatching
std::tuple< bool, TWeight > minimumWeightPerfectMatching(const GraphAttributes &GA)
Computes a minimum weight perfect matching in G.
Definition: MatchingModule.h:102
ogdf::MatchingModule::doMaximumWeightMatching
void doMaximumWeightMatching(const Graph &G, const WeightContainer &weights, std::unordered_set< edge > &matching)
Definition: MatchingModule.h:254
ogdf::GraphCopySimple
Copies of graphs with mapping between nodes and edges.
Definition: GraphCopy.h:254
ogdf::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:217
Logger.h
Contains logging functionality.
ogdf::MatchingModule::getMatchingWeight
TWeight getMatchingWeight(const std::unordered_set< edge > &matching, const WeightContainer &weights)
Definition: MatchingModule.h:244
ogdf::MatchingModule::maximumWeightPerfectMatching
bool maximumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)
Computes a maximum weight perfect matching in G.
Definition: MatchingModule.h:117
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::MatchingModule::minimumWeightPerfectMatching
bool minimumWeightPerfectMatching(const GraphAttributes &GA, std::unordered_set< edge > &matching)
Computes a minimum weight perfect matching in GA.
Definition: MatchingModule.h:75
ogdf::MatchingModule::maximumWeightPerfectMatching
bool maximumWeightPerfectMatching(const GraphAttributes &GA, std::unordered_set< edge > &matching)
Computes a maximum weight perfect matching in GA.
Definition: MatchingModule.h:131
ogdf::MatchingModule::maximumWeightPerfectMatching
std::tuple< bool, TWeight > maximumWeightPerfectMatching(const GraphAttributes &GA)
Computes a maximum weight perfect matching in G.
Definition: MatchingModule.h:161
ogdf::Module
Base class for modules.
Definition: Module.h:47
EdgeArray.h
Declaration and implementation of EdgeArray class.
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::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::MatchingModule::matchingWeight
TWeight matchingWeight(const std::unordered_set< edge > &matching, const GraphAttributes &GA)
Calculate the weight of the matching with the weights given in GA.
Definition: MatchingModule.h:222
NodeArray.h
Declaration and implementation of NodeArray class.
ogdf::MatchingModule::matchingWeight
TWeight matchingWeight(const std::unordered_set< edge > &matching, const EdgeArray< TWeight > &weights)
Calculate the weight of the matching with the weights given in weights.
Definition: MatchingModule.h:216
utils.h
Utility functions and classes regarding map access and iteration.
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::MatchingModule::maximumWeightPerfectMatching
std::tuple< bool, TWeight > maximumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights)
Computes a maximum weight perfect matching in G.
Definition: MatchingModule.h:146
ogdf::MatchingModule::minimumWeightPerfectMatching
std::tuple< bool, TWeight > minimumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights)
Computes a minimum weight perfect matching in G.
Definition: MatchingModule.h:87
Module.h
Declares base class for all module types.
ogdf::Logger
Centralized global and local logging facility working on streams like std::cout.
Definition: Logger.h:100
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
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::MatchingModule::minimumWeightPerfectMatching
bool minimumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)
Computes a minimum weight perfect matching in G.
Definition: MatchingModule.h:63
ogdf::MatchingModule
Definition: MatchingModule.h:51
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