Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
MatchingModule.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/Graph.h>
38#include <ogdf/basic/Logger.h>
39#include <ogdf/basic/Module.h>
40#include <ogdf/basic/basic.h>
42
43#include <tuple>
44#include <unordered_set>
45
46namespace ogdf {
47
48using namespace matching_blossom;
49
50template<typename TWeight>
51class MatchingModule : public Module, public Logger {
52public:
53 virtual ~MatchingModule() { }
54
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
226protected:
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
234private:
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}
Includes declaration of graph class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
Contains logging functionality.
Declares base class for all module types.
Basic declarations, included by all source files.
Class for the representation of edges.
Definition Graph_d.h:364
node target() const
Returns the target node of the edge.
Definition Graph_d.h:402
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
Stores additional attributes of a graph (like layout information).
const Graph & constGraph() const
Returns a reference to the associated graph.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition GraphCopy.h:191
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
edge copy(edge e) const override
Returns the edge in the graph copy corresponding to e.
Definition GraphCopy.h:294
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Centralized global and local logging facility working on streams like std::cout.
Definition Logger.h:102
bool maximumWeightPerfectMatching(const GraphAttributes &GA, std::unordered_set< edge > &matching)
Computes a maximum weight perfect matching in GA.
std::tuple< bool, TWeight > maximumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights)
Computes a maximum weight perfect matching in G.
std::tuple< bool, TWeight > minimumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights)
Computes a minimum weight perfect matching in G.
TWeight maximumWeightMatching(const GraphAttributes &GA)
Computes a maximum weight matching in G.
std::tuple< bool, TWeight > minimumWeightPerfectMatching(const GraphAttributes &GA)
Computes a minimum weight perfect matching in G.
TWeight matchingWeight(const std::unordered_set< edge > &matching, const GraphAttributes &GA)
Calculate the weight of the matching with the weights given in GA.
void doMaximumWeightMatching(const Graph &G, const WeightContainer &weights, std::unordered_set< edge > &matching)
TWeight getMatchingWeight(const std::unordered_set< edge > &matching, const WeightContainer &weights)
bool maximumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)
Computes a maximum weight perfect matching in G.
virtual bool doCall(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)=0
Main call function for the algorithm with an EdgeArray as weight container.
bool minimumWeightPerfectMatching(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)
Computes a minimum weight perfect matching in G.
TWeight maximumWeightMatching(const Graph &G, const EdgeArray< TWeight > &weights)
Computes a maximum weight matching in G.
virtual bool doCall(const GraphAttributes &GA, std::unordered_set< edge > &matching)=0
Main call function for the algorithm with GraphAttrubtes as weight container.
void maximumWeightMatching(const GraphAttributes &GA, std::unordered_set< edge > &matching)
Computes a maximum weight matching in GA.
std::tuple< bool, TWeight > maximumWeightPerfectMatching(const GraphAttributes &GA)
Computes a maximum weight perfect matching in G.
TWeight matchingWeight(const std::unordered_set< edge > &matching, const EdgeArray< TWeight > &weights)
Calculate the weight of the matching with the weights given in weights.
bool minimumWeightPerfectMatching(const GraphAttributes &GA, std::unordered_set< edge > &matching)
Computes a minimum weight perfect matching in GA.
void maximumWeightMatching(const Graph &G, const EdgeArray< TWeight > &weights, std::unordered_set< edge > &matching)
Computes a maximum weight matching in G.
void copyWeights(const Graph &G, const WeightContainer &weights, EdgeArray< TWeight > &copy, bool invert=false)
Base class for modules.
Definition Module.h:49
Class for the representation of nodes.
Definition Graph_d.h:241
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
Utility functions and classes regarding map access and iteration.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.