Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 
43 template<typename EdgeContainer>
44 inline bool isMatching(const Graph& graph, const EdgeContainer& matching) {
45  NodeArray<bool> covered {graph, false};
46 
47  for (edge e : matching) {
48  for (node v : e->nodes()) {
49  if (covered[v]) {
50  return false;
51  }
52  covered[v] = true;
53  if (e->isSelfLoop()) {
54  break;
55  }
56  }
57  }
58 
59  return true;
60 }
61 
64 template<typename EdgeContainer>
65 bool isMaximal(const Graph& graph, const EdgeContainer& matching, edge& addable) {
66  addable = nullptr;
67 
68  EdgeArray<bool> covered {graph, false};
69 
70  for (edge e : matching) {
71  for (node v : e->nodes()) {
72  for (adjEntry adj : v->adjEntries) {
73  covered[adj->theEdge()] = true;
74  }
75  }
76  }
77 
78  for (edge e : graph.edges) {
79  if (!covered[e]) {
80  addable = e;
81  return false;
82  }
83  }
84 
85  return true;
86 }
87 
90 template<typename EdgeContainer>
91 inline bool isMaximal(const Graph& graph, const EdgeContainer& matching) {
92  edge ignored;
93  return isMaximal(graph, matching, ignored);
94 }
95 
98 template<typename EdgeContainer>
99 inline bool isMaximalMatching(const Graph& graph, const EdgeContainer& matching) {
100  return isMatching(graph, matching) && isMaximal(graph, matching);
101 }
102 
105 template<typename EdgeContainer>
106 inline bool isPerfect(const Graph& graph, const EdgeContainer& matching) {
107  return 2 * int(matching.size()) == graph.numberOfNodes();
108 }
109 
112 template<typename EdgeContainer>
113 inline bool isPerfectMatching(const Graph& graph, const EdgeContainer& matching) {
114  return isMatching(graph, matching) && isPerfect(graph, matching);
115 }
116 
119 OGDF_EXPORT void findMaximalMatching(const Graph& graph, ArrayBuffer<edge>& matching);
120 
133  const List<node>& V, EdgeArray<bool>& matching);
134 
135 }
136 }
ogdf::ArrayBuffer< edge >
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
Graph.h
Includes declaration of graph class.
ogdf::Matching::findMaximumCardinalityMatching
int findMaximumCardinalityMatching(const Graph &G, const List< node > &U, const List< node > &V, EdgeArray< bool > &matching)
Finds a maximum cardinality matching in the bipartite graph G = (U+V, E) in O(sqrt(|U+V|) * |E|) time...
ogdf::Matching::isMaximalMatching
bool isMaximalMatching(const Graph &graph, const EdgeContainer &matching)
Checks in O(|V| + |E|) time if matching is a maximal matching.
Definition: Matching.h:99
ogdf::Matching::findMaximalMatching
void findMaximalMatching(const Graph &graph, ArrayBuffer< edge > &matching)
Obtains a maximal matching in O(|E|) time.
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:153
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:974
ogdf::Matching::isMaximal
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:65
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: List.h:42
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::Matching::isMatching
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:44
ogdf::Matching::isPerfect
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:106
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:927
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::Matching::isPerfectMatching
bool isPerfectMatching(const Graph &graph, const EdgeContainer &matching)
Checks in O(|V| + size of matching) if matching is a perfect matching.
Definition: Matching.h:113
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709