|
| 1 | +/** |
| 2 | + * The Gale-Shapley algorithm is used to find a stable matching between two sets |
| 3 | + * of equal size (e.g., donors and recipients) where no two elements prefer each |
| 4 | + * other over their current partners. It ensures a stable matching by proposing |
| 5 | + * from one side to the other until all are matched. |
| 6 | + * |
| 7 | + * Complexity: |
| 8 | + * Worst-case performance O(n^2) |
| 9 | + * Best-case performance O(n^2) |
| 10 | + * |
| 11 | + * Reference: |
| 12 | + * https://en.wikipedia.org/wiki/Stable_marriage_problem |
| 13 | + * https://www.youtube.com/watch?v=Qcv1IqHWAzg (Numberphile) |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @param {number[][]} donorPref Preferences of donors, where each donor has an ordered list of recipients. |
| 20 | + * @param {number[][]} recipientPref Preferences of recipients, where each recipient has an ordered list of donors. |
| 21 | + * @returns {number[]} Array where the index is the donor, and the value at the index is the matched recipient. |
| 22 | + * |
| 23 | + * @example |
| 24 | + * const donorPref = [[0, 1, 3, 2], [0, 2, 3, 1], [1, 0, 2, 3], [0, 3, 1, 2]]; |
| 25 | + * const recipientPref = [[3, 1, 2, 0], [3, 1, 0, 2], [0, 3, 1, 2], [1, 0, 3, 2]]; |
| 26 | + * stableMatching(donorPref, recipientPref); // Output: [1, 2, 3, 0] |
| 27 | + */ |
| 28 | +function stableMatching(donorPref, recipientPref) { |
| 29 | + // Initialize the number of donors and create a list of unmatched donors |
| 30 | + let n = donorPref.length |
| 31 | + let unmatchedDonors = Array.from({ length: n }, (_, i) => i) |
| 32 | + |
| 33 | + // Records of which recipient each donor is paired with and vice versa |
| 34 | + let donorRecord = Array(n).fill(-1) // Donor to recipient |
| 35 | + let recRecord = Array(n).fill(-1) // Recipient to donor |
| 36 | + |
| 37 | + // Array to track how many recipients each donor has proposed to |
| 38 | + let numDonations = Array(n).fill(0) |
| 39 | + |
| 40 | + // While there are unmatched donors |
| 41 | + while (unmatchedDonors.length > 0) { |
| 42 | + // Take the first unmatched donor |
| 43 | + let donor = unmatchedDonors[0] |
| 44 | + let donorPreference = donorPref[donor] |
| 45 | + |
| 46 | + // Find the next recipient this donor prefers |
| 47 | + let recipient = donorPreference[numDonations[donor]] |
| 48 | + numDonations[donor] += 1 |
| 49 | + |
| 50 | + // Get recipient's preference list and check the current match |
| 51 | + let recPreference = recipientPref[recipient] |
| 52 | + let prevDonor = recRecord[recipient] |
| 53 | + |
| 54 | + // If recipient is already matched, check if they prefer the new donor |
| 55 | + if (prevDonor !== -1) { |
| 56 | + if (recPreference.indexOf(prevDonor) > recPreference.indexOf(donor)) { |
| 57 | + // If the new donor is preferred, match them and unmatch the previous donor |
| 58 | + recRecord[recipient] = donor |
| 59 | + donorRecord[donor] = recipient |
| 60 | + unmatchedDonors.push(prevDonor) |
| 61 | + unmatchedDonors.splice(unmatchedDonors.indexOf(donor), 1) // Remove the current donor from unmatched |
| 62 | + } |
| 63 | + } else { |
| 64 | + // If the recipient is not matched, pair them with the current donor |
| 65 | + recRecord[recipient] = donor |
| 66 | + donorRecord[donor] = recipient |
| 67 | + unmatchedDonors.splice(unmatchedDonors.indexOf(donor), 1) // Remove the current donor from unmatched |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return donorRecord |
| 72 | +} |
| 73 | + |
| 74 | +// // Example usage: |
| 75 | +// const donorPref = [[0, 1, 3, 2], [0, 2, 3, 1], [1, 0, 2, 3], [0, 3, 1, 2]]; |
| 76 | +// const recipientPref = [[3, 1, 2, 0], [3, 1, 0, 2], [0, 3, 1, 2], [1, 0, 3, 2]]; |
| 77 | +// console.log(stableMatching(donorPref, recipientPref)); // Output: [1, 2, 3, 0] |
| 78 | +export { stableMatching } |
0 commit comments