Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

Matching.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 
36 namespace ogdf {
37 
39 namespace Matching {
40 
42 template<typename EdgeContainer>
43 inline bool isMatching(const Graph& graph, const EdgeContainer& matching) {
44  NodeArray<bool> covered{graph, false};
45 
46  for (edge e : matching) {
47  for (node v : e->nodes()) {
48  if (covered[v]) {
49  return false;
50  }
51  covered[v] = true;
52  if (e->isSelfLoop()) {
53  break;
54  }
55  }
56  }
57 
58  return true;
59 }
60 
63 template<typename EdgeContainer>
64 bool isMaximal(const Graph& graph, const EdgeContainer& matching, edge& addable) {
65  addable = nullptr;
66 
67  EdgeArray<bool> covered{graph, false};
68 
69  for (edge e : matching) {
70  for (node v : e->nodes()) {
71  for (adjEntry adj : v->adjEntries) {
72  covered[adj->theEdge()] = true;
73  }
74  }
75  }
76 
77  for (edge e : graph.edges) {
78  if (!covered[e]) {
79  addable = e;
80  return false;
81  }
82  }
83 
84  return true;
85 }
86 
88 template<typename EdgeContainer>
89 inline bool isMaximal(const Graph& graph, const EdgeContainer& matching) {
90  edge ignored;
91  return isMaximal(graph, matching, ignored);
92 }
93 
95 template<typename EdgeContainer>
96 inline bool isMaximalMatching(const Graph& graph, const EdgeContainer& matching) {
97  return isMatching(graph, matching) && isMaximal(graph, matching);
98 }
99 
101 template<typename EdgeContainer>
102 inline bool isPerfect(const Graph& graph, const EdgeContainer& matching) {
103  return 2 * int(matching.size()) == graph.numberOfNodes();
104 }
105 
107 template<typename EdgeContainer>
108 inline bool isPerfectMatching(const Graph& graph, const EdgeContainer& matching) {
109  return isMatching(graph, matching) && isPerfect(graph, matching);
110 }
111 
113 OGDF_EXPORT void findMaximalMatching(const Graph& graph, ArrayBuffer<edge>& matching);
114 
115 }
116 }
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
bool isPerfectMatching(const Graph &graph, const EdgeContainer &matching)
Checks in O(|V| + size of matching) if matching is a perfect matching.
Definition: Matching.h:108
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
bool isMaximal(const Graph &graph, const EdgeContainer &matching, edge &addable)
Checks in time O(|E|) if there are edges that could be added to matching.
Definition: Matching.h:64
void findMaximalMatching(const Graph &graph, ArrayBuffer< edge > &matching)
Obtains a maximal matching in O(|E|) time.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
Class for the representation of edges.
Definition: Graph_d.h:292
Class for the representation of nodes.
Definition: Graph_d.h:169
Includes declaration of graph class.
bool isMaximalMatching(const Graph &graph, const EdgeContainer &matching)
Checks in O(|V| + |E|) time if matching is a maximal matching.
Definition: Matching.h:96
bool isPerfect(const Graph &graph, const EdgeContainer &matching)
Checks in O(1) if matching (assuming it is a matching and the graph is simple and connected) is perfe...
Definition: Matching.h:102
The namespace for all OGDF objects.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
Class for adjacency list elements.
Definition: Graph_d.h:79
bool isMatching(const Graph &graph, const EdgeContainer &matching)
Checks in time O(|V| + size of matching) if the given set of edges represents a matching.
Definition: Matching.h:43