# Illustrating the Deferred Acceptance Algorithm with R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The Deferred Acceptance Algorithm (DAA) goes back to Gale and Shapley (1962). They introduce a rather simple algorithm that finds a stable matching for example for college admissions or in a marriage market. In a marriage market where M men have preferences over W women, and men take the role of the proposing party, the DAA produces what is called the M-stable matching: each man strictly prefers the M-stable matching to any other potential matching. “Stable” means that no couple of a man and a woman could break the matching by choosing another mate. This is quite a strong result.**plausibel**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Variations of this algoritm are used in Hospital assignments in the USA, whereby recently graduated doctors submit preferences over hospitals, and hospitals submit preferences over graduates. Another application is the kidney exchange, where the algorithm is used to find the best match between a set of donors and a set of receivers. If you’re interested in this, check out Al Roth’s website.

Here I’m going to use R to make a little simulation of this. It’s not a spectacular movie, but it shows you how the algorithm works. So here goes: We have M men and W women in a marriage market, and each participant has a complete set of preferences (a ranking) over all participants of the other sex. In the demonstration I assumed strict preferences (no indifference between any 2 members) and that nobody wants to be single, but the algorithm can handle this.

- in round one all men are single. They propose to their highest preference-woman.
- Each woman who receives at least one proposal picks her highest preference from the set of proposers. Otherwise she stays single. Men who were rejected, go back to the set of singles.
- in Round two, all remaining singles propose to their second preference. All women who receive proposals accept the best mate. If there was a mate “deferentially accepted”, i.e. some man from round one, he is released back into the set of singles.
- the algorithm stops once we cycled over all W preferences, or if there are no more male singles left. At that point the current match is the one that gets implemented.

For this demonstration I used the animation package, which is a very nice way to just record multiple plots and wrap them into some output file of your choice. The code is below.

Demonstration:

Code on gist: https://gist.github.com/1628636

To

**leave a comment**for the author, please follow the link and comment on their blog:**plausibel**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.