A matching game (also called a stable matching problem) models a situation
in a population of \(N\) suitors and \(N\) reviewers. Suitors and reviewers
rank their preferences and attempt to find a match.

Formally, a matching game of size \(N\) is defined by two disjoint sets \(S\)
and \(R\) of size \(N\). Associated to each element of \(S\) and \(R\) is a
preference list:

A matching \(M\) is any bijection between \(S\) and \(R\). If \(s \in S\) and
\(r \in R\) are matched by \(M\) we denote:

\[M(s) = r.\]

On any given matching game, one intends to find a matching that is stable.
In other words, so that no one individual has an incentive to break their
current match.

Formally, a stable matching is a matching that has no blocking pairs.
A blocking pair is any pair \((s, r)\) such that \(M(s) \neq r\) but \(s\)
prefers \(r\) to \(M(r)\) and \(r\) prefers \(s\) to \(M^{-1}(r)\).

To obtain the stable matching in Sage we use the solve method which
uses the extended Gale-Shapley algorithm [DI1989]:

sage: m.solve(){'J': 'A', 'K': 'C', 'L': 'D', 'M': 'B'}

Matchings have a natural representations as bipartite graphs:

sage: plot(m)Graphics object consisting of 13 graphics primitives

The above plots the bipartite graph associated with the matching.
This plot can be accessed directly:

It can be shown that the Gale-Shapley algorithm will return the stable
matching that is optimal from the point of view of the suitors and is in
fact the worst possible matching from the point of view of the reviewers.
To quickly obtain the matching that is optimal for the reviewers we
use the solve method with the invert=True option: