This is a Typescript implementation of the Gale-Shapley algorithm, which finds a stable matching between two equally sized sets of elements given an ordering of preferences for each element.
To install this package:
npm install @colorstack/stable-matching --save
importStableMatching,{Pairing,PreferenceList,}from'@colorstack/stable-matching';constmentees: PreferenceList[]=[[1,3,0,2],[2,1,3,0],[2,3,0,1],[2,0,1,3],];constmentors: PreferenceList[]=[[1,3,2,0],[0,3,1,2],[0,2,3,1],[3,0,2,1],];constmatching: StableMatching=newStableMatching({
mentees,
mentors,});constpairings: Pairing[]=matching.run();// [[0, 1], [1, 3], [2, 2], [3, 0]]console.log(pairings);The "Stable Matching" problem is also called the "Stable Marriage" problem, and that's because for years, the example used to solve the problem was marrying a man to a woman. The man/woman terminology is not one that's inclusive so in my implementation we'll use the following:
mentee- Maps tomanin traditional example.mentor- Maps towomanin traditional example.