Quaternity
Functions
match.cpp File Reference

This file defines maximum matching algorithm functions. More...

#include <algorithm>
#include <vector>
#include "match.h"

Go to the source code of this file.

Functions

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 possible. More...
 
bool match_exists (const Graph &graph)
 Check if there exists a matching for all cards. More...
 
std::vector< int > match_find (const Graph &graph)
 Find a match of the given graph. More...
 

Detailed Description

This file defines maximum matching algorithm functions.

Definition in file match.cpp.

Function Documentation

◆ dfs()

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 possible.

Definition at line 16 of file match.cpp.

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 }
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

◆ match_exists()

bool match_exists ( const Graph graph)

Check if there exists a matching for all cards.

See also
match_find

Definition at line 35 of file match.cpp.

36 {
37  std::vector<int> match = match_find(graph);
38  return std::find(match.begin(), match.end(), -1) == match.end();
39 }
std::vector< int > match_find(const Graph &graph)
Find a match of the given graph.
Definition: match.cpp:47

◆ match_find()

std::vector<int> match_find ( const Graph graph)

Find a match of the given graph.

Returns
A vector list with integers representing to which rnode the lnode at the given index is connected, -1 indicates a connection to none.

Definition at line 47 of file match.cpp.

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 }