Quaternity
match.cpp
Go to the documentation of this file.
1 
7 #include <algorithm>
8 #include <vector>
9 
10 #include "match.h"
11 
16 bool dfs(const Graph &graph, int rnode, std::vector<bool> &seen, std::vector<int> &match)
17 {
18  for (size_t lnode = 0; lnode < graph.size(); lnode++) {
19  if (graph[lnode][rnode] && !seen[lnode]) {
20  seen[lnode] = true;
21  if (match[lnode] < 0 || dfs(graph, match[lnode], seen, match)) {
22  match[lnode] = rnode;
23  return true;
24  }
25  }
26  }
27  return false;
28 }
29 
35 bool match_exists(const Graph &graph)
36 {
37  std::vector<int> match = match_find(graph);
38  return std::find(match.begin(), match.end(), -1) == match.end();
39 }
40 
47 std::vector<int> match_find(const Graph &graph)
48 {
49  std::vector<int> match(graph.size(), -1); // FIXME Make match for left node, not for right.
50  for (int rnode = 0; rnode < (int)graph.size(); rnode++) {
51  std::vector<bool> seen(graph.size(), false);
52  dfs(graph, rnode, seen, match);
53  }
54  return match;
55 }
std::vector< std::vector< bool > > Graph
A bipartite graph where true booleans represent edges between a left node denoted as the row and a ri...
Definition: graph.h:19
std::vector< int > match_find(const Graph &graph)
Find a match of the given graph.
Definition: match.cpp:47
bool dfs(const Graph &graph, int rnode, std::vector< bool > &seen, std::vector< int > &match)
Depth first search recursive function which returns true if a matching for right node / rnode is poss...
Definition: match.cpp:16
bool match_exists(const Graph &graph)
Check if there exists a matching for all cards.
Definition: match.cpp:35
This file defines maximum matching algorithm function headers.