Perfect matchings
perfect_matchings | |
Gives all perfect matchings of N objects | |
Other toolboxes required | none |
---|---|
Related functions | BrauerStates one_factorization |
Function category | Helper functions |
This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function. |
perfect_matchings is a function that returns all perfect matchings of a given list of objects. That is, it returns all ways of grouping an even number of objects into pairs.
Contents
Syntax
- PM = perfect_matchings(N)
Argument descriptions
- N: Either an even integer, indicating that you would like all perfect matchings of the integers $1, 2, \ldots, N$, or a vector containing an even number of distinct entries, indicating that you would like all perfect matchings of those entries.
Examples
Perfect matchings of four objects
The following code generates all perfect matchings of the numbers $1,2,3,4$:
>> perfect_matchings(4) ans = 1 2 3 4 1 3 2 4 1 4 3 2
The perfect matchings are read "naively" left-to-right. For example, the first row of the output above indicates that one valid perfect matching is $\{\{1,2\},\{3,4\}\}$. The second row says that another perfect matching is $\{\{1,3\},\{2,4\}\}$. Finally, the third row says that the third (and last) perfect matching is $\{\{1,4\},\{2,3\}\}$.
Notes
If $N = 2k$ then there are exactly $(2k)!/(k!\cdot 2^k)$ perfect matchings of $N$ objects. If $N$ is odd, there are no perfect matchings (and thus PM will have zero rows).
Source code
Click on "expand" to the right to view the MATLAB source code for this function.
%% PERFECT_MATCHINGS Gives all perfect matchings of N objects
% This function has one required argument:
% N: either an even natural number (the number of objects to be
% matched) or a vector containing an even number of distinct objects
% to be matched
%
% PM = perfect_matchings(N) is a matrix with each row corresponding to a
% perfect matching of N objects (or, if N is a vector, each row of PM is
% a pefect matching of the entries of N). Each perfect matching is read
% "naively": for each j, PM(j,1) is matched with PM(j,2), PM(j,3) is
% matched with PM(j,4), and so on.
%
% URL: http://www.qetlab.com/perfect_matchings
% requires: nothing
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% package: QETLAB
% last updated: November 6, 2014
function pm = perfect_matchings(n)
if(length(n) == 1)
n = 1:n;
end
sz = length(n);
% Base case, n = 2: only one perfect matching.
if(sz == 2)
pm = n;
return;
% There are no perfect matchings of an odd number of objects.
elseif(mod(sz,2) == 1)
pm = zeros(0,sz);
return;
end
% Recursive step: build perfect matchings from smaller ones.
% Only do the recursive step once instead of n-1 times: we will then tweak
% the output n-1 times.
lower_fac = perfect_matchings(n(3:end));
lfac_size = size(lower_fac,1);
pm = zeros(0,sz);
% Now build the perfect matchings we actually want.
for j = 2:sz
tlower_fac = lower_fac;
tlower_fac(tlower_fac==n(j)) = n(2);
pm = [pm;[ones(lfac_size,1)*[n(1),n(j)],tlower_fac]];
end