From 684ae997b3e289efd61d99ce5320479f73d1d2c9 Mon Sep 17 00:00:00 2001 From: Jan Tilly Date: Fri, 7 Aug 2015 13:42:39 -0400 Subject: [PATCH] Update README.md --- README.md | 84 +++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 16c25ab..06f3383 100644 --- a/README.md +++ b/README.md @@ -84,15 +84,15 @@ For additional examples, take a look at the [vignette](http://jtilly.io/matching ## Irving's Stable Roommate Algorithm: How does it work? -Preferences of potential roommates are summarized by an `n-1` by `n` dimensional matrix, e.g., if `n = 6`, +Preferences of potential roommates are summarized by an `n-1 \times n` dimensional matrix, e.g., if `n = 6`, ```{r} -pref = matrix(c(2, 5, 1, 4, 2, 4, - 3, 4, 3, 1, 0, 0, - 1, 3, 4, 2, 1, 2, - 5, 0, 0, 5, 3, 3, - 4, 2, 5, 0, 5, 1), nrow = 5, ncol = 6, byrow = TRUE) +pref = matrix(c(3, 6, 2, 5, 3, 5, + 4, 5, 4, 2, 1, 1, + 2, 4, 5, 3, 2, 3, + 6, 1, 1, 6, 4, 4, + 5, 3, 6, 1, 6, 2), nrow = 5, ncol = 6, byrow = TRUE) ``` -Column `i` represents the preferences of the `i`th roommate, and row `j` represents the ranking of the roommate whose index is encoded in that row. For example, in the above preference matrix, roommate `0` most prefers to be matched with roommate `2`, followed by `3`, followed by `1`. +Column `i` represents the preferences of the `i`th roommate, and row `j` represents the ranking of the roommate whose index is encoded in that row. For example, in the above preference matrix, roommate `1` most prefers to be matched with roommate `3`, followed by `4`, followed by `2`. The algorithm proceeds in two phases. @@ -102,54 +102,54 @@ In phase 1, potential roommates take turns sequentially proposing to the other r In the above example, -1. Roommate `0` begins by proposing to roommate `2`, he most preferred roommate. `2`, having no better offers, accepts. -2. `1` proposes to `5`, who accepts. -3. `2` proposes to `1`, who accepts. -4. `3` proposes to `4`, who accepts. -5. `4` proposes to `2`, who accepts. `2` cancels his proposal from `0`. -6. `0`, having no proposal, proposes to `3`, who accepts. -7. `5` proposes to `4`, who rejects, having a better proposal from `3`. -8. `5` proposes to `0`, who accepts. +1. Roommate `1` begins by proposing to roommate `3`, his most preferred roommate. `3`, having no better offers, accepts. +2. `2` proposes to `6`, who accepts. +3. `3` proposes to `2`, who accepts. +4. `4` proposes to `5`, who accepts. +5. `5` proposes to `3`, who accepts. `3` cancels his proposal from `1`. +6. `1`, having no proposal, proposes to `4`, who accepts. +7. `6` proposes to `5`, who rejects, having a better proposal from `4`. +8. `6` proposes to `1`, who accepts. ### Phase 2 -In phase 2, we begin by eliminating all potential roommate matches which are worse than the current proposals held. For example, in the above example, `2` has a proposal from `4`, and so we eliminate `0` and `5` from `2`'s column, and symmetrically eliminate `2` from `0` and `5`'s column. This results in the following 'reduced' preference listing: +In phase 2, we begin by eliminating all potential roommate matches which are worse than the current proposals held. For example, in the above example, `3` has a proposal from `5`, and so we eliminate `1` and `6` from `3`'s column, and symmetrically eliminate `3` from `1` and `6`'s column. This results in the following 'reduced' preference listing: ``` - 5, 1, 4, 2, -3, 4, 3, 1, 0 -1, 3, 4, 2, 1, -5, 0, 5, 3, 3 - 2, 0, 1 + 6, 2, 5, 3, +4, 5, 4, 2, 1 +2, 4, 5, 3, 2, +6, 1, 6, 4, 4 + 3, 1, 2 ``` -These preferences form what is called a 'stable' table, or, 's-table'. ('Stable' for short.) The defining characteristic of a stable table is that if `i` is the most preferred roommate on `j`s list, then `j` is the least preferred roommate on `i`s list. For example, `0` most prefers `3`, but `3` least prefers `0`. +These preferences form what is called a 'stable' table, or, 's-table'. ('Stable' for short.) The defining characteristic of a stable table is that if `i` is the most preferred roommate on `j`s list, then `j` is the least preferred roommate on `i`s list. For example, `1` most prefers `4`, but `4` least prefers `1`. The algorithm proceeds by finding and eliminating 'rotations'. A rotation is a sequence of pairs of roommates, such that there is a distinct roommate in the first position of each pair, the second roommate in each pair least prefers the roommate he is paired with, the first roommate in each pair most prefers the roommate he is paired with, and finally, the first roommate in each pair ranks the second roommate in the following pair second (modulo the number of pairs, that is, the first roommate in the last pair ranks the second roommate in the first pair second) Once a rotation has been identified, removing it results in another stable table. -For example, `(0, 3), (2, 1)` is a rotation in the above table, because `0` loves `3`, `2` loves `1`, `3` hates `0`, `1` hates `2`, `1` is second on `0`s list, and `3` is second on `2`'s list. Eliminating this rotation involves `1` rejecting `2`, `3` rejecting `0`, and then we remove every successive potential roommate as well to preserve the stable table property, resulting in +For example, `(1, 4), (3, 2)` is a rotation in the above table, because `1` loves `4`, `3` loves `2`, `4` hates `1`, `2` hates `3`, `2` is second on `1`s list, and `4` is second on `3`'s list. Eliminating this rotation involves `2` rejecting `3`, `4` rejecting `1`, and then we remove every successive potential roommate as well to preserve the stable table property, resulting in ``` - 5, 4, 2, - 4, 3, 1, 0 -1, 3, 4, 2, 1, -5, 0, 5, 3, 3 - 1 + 6, 5, 3, + 5, 4, 2, 1 +2, 4, 5, 3, 2, +6, 1, 6, 4, 4 + 2 ``` -A further rotation is `(0, 1), (1, 5), (3, 4)`. Eliminating it yields +A further rotation is `(1, 2), (2, 6), (4, 5)`. Eliminating it yields ``` - 2, - 4, 3, 1, 0 - 3, 4, 2, 1, -5, - + 3, + 5, 4, 2, 1 + 4, 5, 3, 2, +6, ``` -A final rotation is `(1, 4), (2, 3)`. Eliminating it yields +A final rotation is `(2, 5), (3, 4)`. Eliminating it yields ``` - 2, - 1, 0 - 3, 4, -5, - + 3, + 2, 1 + 4, 5, +6, + ``` -Therefore, a stable matching is for `0` and `5` to match, `1` and `3` to match, and `2` and `4` to match. +Therefore, a stable matching is for `1` and `6` to match, `2` and `4` to match, and `3` and `5` to match. ```{r} -results = onesided(pref = pref) +results = onesided(pref = pref - 1) +results ```