1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/* Copyright (c) 1997-2024
Ewgenij Gawrilow, Michael Joswig, and the polymake team
Technische Universität Berlin, Germany
https://polymake.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
--------------------------------------------------------------------------------
*/
#pragma once
#include "polymake/Graph.h"
#include "polymake/Array.h"
#include "polymake/Set.h"
#include "polymake/graph/graph_iterators.h"
#include "polymake/graph/hungarian_method.h"
// TODO better representation for cycle
namespace polymake { namespace graph {
// This implementation for finding all perfect matchings in a bipartite graph
// broadly follows the algorithm described in the paper:
//
// Takeaki UNO - Algorithms for Enumerating All Perfect, Maximum and Maximal
// Matchings in Bipartite Graphs
class PerfectMatchings {
protected:
Graph<Directed> D;
Int dim;
Set<Array<Int>> matchings;
class CycleVisitor;
public:
PerfectMatchings(const Graph<Undirected>& graph, const Array<Int>& M)
: dim(graph.nodes()/2)
{
// some sanity checks first:
if (graph.nodes()%2 != 0)
throw std::runtime_error("Graph has odd number of nodes.");
if (graph.has_gaps())
throw std::runtime_error("Graph has gaps."); // TODO squeeze() instead?
for (Int i = 0; i < dim; ++i) {
for (auto n = entire(graph.adjacent_nodes(i)); !n.at_end(); ++n) {
if (*n < dim)
throw std::runtime_error("Graph not bipartite of the form {0..n-1}U{n..2n-1}.");
}
for (auto n = entire(graph.adjacent_nodes(i+dim)); !n.at_end(); ++n) {
if (*n >= dim)
throw std::runtime_error("Graph not bipartite of the form {0..n-1}U{n..2n-1}.");
}
}
for (Int i = 0; i < M.size(); ++i) {
if (!graph.edge_exists(M[i] + dim, i))
throw std::runtime_error("M not a matching of the given graph.");
}
if (M.size() != dim)
throw std::runtime_error("Matching not perfect.");
// build D(G,M):
Graph<Directed> dirgraph(graph.nodes());
for (Int i = 0; i < dim; i++) {
for (auto n = entire(graph.adjacent_nodes(i)); !n.at_end(); ++n) {
if (M[i] + dim == *n)
dirgraph.add_edge(*n, i);
else
dirgraph.add_edge(i, *n);
}
}
D = dirgraph;
}
Set<Array<Int>> get_matchings()
{
collect_matchings(D);
return matchings;
}
protected:
class CycleVisitor : public NodeVisitor<> {
using base_t = NodeVisitor<> ;
public:
bool cycle_found;
std::vector<Int> cycle;
protected:
std::vector<Int> parent; // encodes the search tree
std::vector<Int> child;
Set<Int> path_set; // nodes of the current branch of the search tree
Int path_head;
public:
CycleVisitor() {}
CycleVisitor(const Graph<Directed>& Din)
: base_t(Din)
, cycle_found(false)
, cycle(Din.dim(), -1)
, parent(Din.dim(), -1)
, child(Din.dim(), -1)
, path_set()
, path_head(-1)
{}
void clear(const Graph<Directed>&) {}
bool operator() (Int start_node)
{
if (cycle_found)
return false;
visited += start_node;
path_set.clear();
path_set += start_node;
path_head = start_node;
return true;
}
bool operator() (Int n_from, Int n_to)
{
if (cycle_found)
return false;
if (path_set.contains(n_to) && path_head == n_from) { // cycle found
cycle[0] = n_to;
for (Int i = n_to, k = 1; i != n_from; i = child[i], k++) {
cycle[k] = child[i];
}
cycle_found = true;
return false;
} else if (visited.contains(n_to)) {
return false;
} else {
while (path_head != n_from) { // deal with (potential) branching in the search tree
path_set -= path_head;
path_head = parent[path_head];
}
path_set += n_to;
path_head = n_to;
}
parent[n_to] = n_from;
child[n_from] = n_to;
visited += n_to;
return true;
}
};
std::vector<Int> find_cycle(const Graph<Directed>& graph)
{
DFSiterator<Graph<Directed>, VisitorTag<CycleVisitor>> iter(graph);
for (Int i = 0; i < dim; ++i) { // dfs per strongly connected component
if (iter.node_visitor().get_visited_nodes().contains(i))
continue;
iter.reset(i);
while (!iter.at_end()) {
++iter;
if (iter.node_visitor().cycle_found) {
return iter.node_visitor().cycle;
}
}
}
return std::vector<Int>();
}
Graph<Directed> augment(const Graph<Directed>& graph, std::vector<Int> cycle)
{
Graph<Directed> G(graph);
for (Int i = 0; i < Int(cycle.size()) && cycle[i] >= 0; ++i) {
Int n_to = i+1 < Int(cycle.size()) && cycle[i+1] >= 0 ? cycle[i+1] : cycle[0];
G.delete_edge(cycle[i], n_to);
G.add_edge(n_to, cycle[i]);
}
return G;
}
Array<Int> extract_matching(const Graph<Directed>& graph)
{
Array<Int> matching(dim, -1);
for (Int i = 0; i < dim; ++i)
matching[i] = graph.in_adjacent_nodes(i).front() - dim;
return matching;
}
void collect_matchings(const Graph<Directed>& graph)
{
// TODO trim unnecessary edges
std::vector<Int> c = find_cycle(graph);
if (c.empty()) {
matchings += extract_matching(graph);
} else {
// choose matching edge:
Int start_index = c[0] > c[1] ? 0 : 1;
std::pair<Int, Int> e(c[start_index], c[start_index+1]);
// build graph G1 = G\(E-adjacent edges):
Graph<Directed> g1(graph);
Int tmp;
for (auto n = entire(g1.in_adjacent_nodes(e.first)); !n.at_end();) {
tmp = *n;
++n;
g1.delete_edge(tmp, e.first);
}
for (auto n = entire(g1.out_adjacent_nodes(e.second)); !n.at_end();) {
tmp = *n;
++n;
g1.delete_edge(e.second, tmp);
}
// build graph G2 = augment(G)\E:
Graph<Directed> g2(augment(graph, c));
g2.delete_edge(e.second, e.first);
collect_matchings(g1);
collect_matchings(g2);
}
}
};
} }
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
// vim:shiftwidth=3:softtabstop=3:
|