algos/gale-shapley.js
//keys will be individual names and values will be rank-ordered preference arrays
keys will be individual names and values will be rank-ordered preference arrays
keys will be individual names and values will be rank - ordered preference arrays
function stableMarriage(malePreference, femalePreference) {
  const malePref = cloneDeep(malePreference);
  const femalePref = cloneDeep(femalePreference);
  if (Object.keys(malePref).length !== Object.keys(femalePref).length) return Error;
  const proposalTable = {};
  for (const female in femalePref) proposalTable[female] = null;
  while (Object.values(proposalTable).includes(null)) {
    for (const male in malePref) {
      const [maleTopPref] = malePref[male];
      const targetFemalePrefList = femalePref[maleTopPref];
      if (!proposalTable[maleTopPref]) proposalTable[maleTopPref] = male;
      else if (
        targetFemalePrefList.indexOf(male) <
        targetFemalePrefList.indexOf(proposalTable[maleTopPref])
      ) {
        const replacedMale = proposalTable[maleTopPref];
        malePref[replacedMale] = malePref[replacedMale].slice(1);
        proposalTable[maleTopPref] = male;
      } else if (
        targetFemalePrefList.indexOf(male) >
        targetFemalePrefList.indexOf(proposalTable[maleTopPref])
      ) {
        malePref[male] = malePref[male].slice(1);
      }
    }
  }
  return proposalTable;
}
function stableMarriage ( malePreference , femalePreference ) { const malePref = cloneDeep ( malePreference ) ; const femalePref = cloneDeep ( femalePreference ) ; if ( Object . keys ( malePref ) . length !== Object . keys ( femalePref ) . length ) return Error ; const proposalTable = { } ; for ( const female in femalePref ) proposalTable [ female ] = null ; while ( Object . values ( proposalTable ) . includes ( null ) ) { for ( const male in malePref ) { const [ maleTopPref ] = malePref [ male ] ; const targetFemalePrefList = femalePref [ maleTopPref ] ; if ( ! proposalTable [ maleTopPref ] ) proposalTable [ maleTopPref ] = male ; else if ( targetFemalePrefList . indexOf ( male ) < targetFemalePrefList . indexOf ( proposalTable [ maleTopPref ] ) ) { const replacedMale = proposalTable [ maleTopPref ] ; malePref [ replacedMale ] = malePref [ replacedMale ] . slice ( 1 ) ; proposalTable [ maleTopPref ] = male ; } else if ( targetFemalePrefList . indexOf ( male ) > targetFemalePrefList . indexOf ( proposalTable [ maleTopPref ] ) ) { malePref [ male ] = malePref [ male ] . slice ( 1 ) ; } } } return proposalTable ; }
keys will be individual names and values will be rank-ordered preference arrays
keys will be individual names and values will be rank - ordered preference arrays
//create proposal table to store temp engagements //each male proposes to female he prefers most //each female is temp engaged to most preferred suitor and rejects all other proposals //if female has no temp proposal, then create one //else evaluate whether or not she prefers current proposal //compare index positions of current proposal to current engagement //male who will be replaced will need to adjust his pref list //if proposer got rejected, he will adjust his pref list