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 #include <ogdf/basic/GraphList.h>
36 #include <ogdf/basic/basic.h>
37 
38 #include <array>
39 
40 namespace ogdf {
41 template<class E, class INDEX>
42 class ArrayBuffer;
43 template<class E>
44 class List;
45 
47 namespace Matching {
48 
51 template<typename EdgeContainer>
52 inline bool isMatching(const Graph& graph, const EdgeContainer& matching) {
53  NodeArray<bool> covered {graph, false};
54 
55  for (edge e : matching) {
56  for (node v : e->nodes()) {
57  if (covered[v]) {
58  return false;
59  }
60  covered[v] = true;
61  if (e->isSelfLoop()) {
62  break;
63  }
64  }
65  }
66 
67  return true;
68 }
69 
72 template<typename EdgeContainer>
73 bool isMaximal(const Graph& graph, const EdgeContainer& matching, edge& addable) {
74  addable = nullptr;
75 
76  EdgeArray<bool> covered {graph, false};
77 
78  for (edge e : matching) {
79  for (node v : e->nodes()) {
80  for (adjEntry adj : v->adjEntries) {
81  covered[adj->theEdge()] = true;
82  }
83  }
84  }
85 
86  for (edge e : graph.edges) {
87  if (!covered[e]) {
88  addable = e;
89  return false;
90  }
91  }
92 
93  return true;
94 }
95 
98 template<typename EdgeContainer>
99 inline bool isMaximal(const Graph& graph, const EdgeContainer& matching) {
100  edge ignored;
101  return isMaximal(graph, matching, ignored);
102 }
103 
106 template<typename EdgeContainer>
107 inline bool isMaximalMatching(const Graph& graph, const EdgeContainer& matching) {
108  return isMatching(graph, matching) && isMaximal(graph, matching);
109 }
110 
113 template<typename EdgeContainer>
114 inline bool isPerfect(const Graph& graph, const EdgeContainer& matching) {
115  return 2 * int(matching.size()) == graph.numberOfNodes();
116 }
117 
120 template<typename EdgeContainer>
121 inline bool isPerfectMatching(const Graph& graph, const EdgeContainer& matching) {
122  return isMatching(graph, matching) && isPerfect(graph, matching);
123 }
124 
127 OGDF_EXPORT void findMaximalMatching(const Graph& graph, ArrayBuffer<edge>& matching);
128 
141  const List<node>& V, EdgeArray<bool>& matching);
142 
143 }
144 }
ogdf::ArrayBuffer< edge >
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
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:107
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:142
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:160
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:982
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:73
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
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:52
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:114
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
basic.h
Basic declarations, included by all source files.
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:363
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:121
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716