See an interesting article on the Internet "the key to the girl of discrete Mathematics"
Just use JS to write the above-mentioned pairing method:
First set the variable
//Boys Ideal List varMenpreference ={A: [1, 2, 3, 4, 5], B: [1, 3, 4, 2, 5], C: [3, 2, 1, 4, 5], D: [4, 5, 2, 3, 1], E: [5, 2, 1, 4, 3] }; //Girls Ideal List varWomempreference = { 1: [' A ', ' B ', ' C ', ' D ', ' E '], 2: [' B ', ' D ', ' A ', ' E ', ' C '], 3: [' E ', ' B ', ' C ', ' A ', ' D '], 4: [' B ', ' D ', ' E ', ' A ', ' C '], 5: [' B ', ' C ', ' A ', ' D ', ' E '] }; //Final Pairing varCouples = {}
Invoke the confession and pairing method in a recursive manner:
//Get stable Pairing varGetstablecouple =function(){ for(varIinchmenpreference) { varThe_one =Menpreference[i].shift (); Console.info ("Male" + i + "confession female" +The_one); if(i = = womempreference[the_one][0]) {Couples[i]=The_one; Console.info ("Male" + i + "successful hand holding female" +The_one); //Delete in original listremovecouple (The_one, i); } } varLen =Getattrlen (Couples); if(Len < 5) {getstablecouple (); }Else{console.info (couples); } };
In this case, after the pairing is complete, delete the data from the previous list for both sexes, as follows:
//Extended Array MethodArray.prototype.remove=function(DX) {if(IsNaN (dx) | | Dx> This. length) {return false;} for(vari=0,n=0;i< This. length;i++) {if( This[i]!= This[DX]) { This[n++]= This[i]}} This. length-=1}varRemovecouple =function(x, y) {DeleteWomempreference[x]; DeleteMenpreference[y]; for(varIinchwomempreference) { vararr =Womempreference[i]; for(varj=0; j<arr.length; J + +){ if(Y = =Arr[j]) Arr.remove (j); } } for(varIinchmenpreference) { vararr =Menpreference[i]; for(varj=0; j<arr.length; J + +){ if(x = =Arr[j]) Arr.remove (j); } } }
JS realization of "The Key of Discrete mathematics"