Question

Implement the Gale Shapley algorithm using JsFiddle. Must have a sample data set of 5 males...

Implement the Gale Shapley algorithm using JsFiddle.

Must have a sample data set of 5 males and 5 females each with defined listings

  • Can treat people as indexes, Person 0 - Person 4 or whatever works.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Gale Shapley algorithm using JsFiddle:

Gale Shapley algorithm:

It is used for stable marriage problem(SMP). Algorithm as follows:

function stableMatching{

Initialize all m belongs to M and w belongs to W to free

while there is a man m who is free and hasn't proposed to every woman

choose such a man m

Let w be the highest-ranked woman in m's Preference list to whom m has not yet

If w is free then

(m,w) become engaged

Else w is currently engaged to m'

If w prefers m' to m then

m remains free

Else w prefers m to m'

(m,w) become engaged

m' becomes free

Endif

Endif

Endwhile

return the set S of engaged pairs

Java Program

import java.io.*;

public class Galeshapley{

private int N, engagedcount;

private String[][] menpref;

private String[][] womenpref;

private String[][] men;

private String[][] women

private String[][] womenpartner;

private boolean[] menengaged;

//constructor

public Galesshapley(String[] m, String[] w, String[][] mp, String[][] wp){

N =m.length;

engagedcount=0;

men=m;

women=w;

menpref=mp;

womenpref=wp;

menengaged = new boolean[N];

womenpartner =new String[N];

calcmatches();

}

//function calculate all the matches

private void calcMatches(){

while(engagedcount <N){

int free;

for(free =0; free<N;free++)

if(menengaged[free])

break;

for(int i=0;i<N && !menengaged[free];i++)

{

int index=womenIndexOf(menpref[free][i]);

if(womenpartner[index]==null){

womenpartner[index]=men[free];

menEngaged[free]=true;

engagedcount++;

}

else

{

String currentpartner =womenpartner[index];

if(morepreference(currentpartner, men[free], index))

{

womenpartner[index]=men[free];

menengaged[free]=true;

menengaged[menIndexOf(currentpartner)]=false;

}}}}

printcouples();

}

//function to check if women prefers new partner over old assigned partner

private boolen morepreference(String curpartner, String newpartner, int index){

for(int i=0;i<N;i++){

if(womenpref[index][i].equals(newpartner))

return true;

if(womenpref[index]][i].equals(curpartner)

return false;

}

return false;

}

//get men index

private int menIndexof(String str)

{

for(int i=0;i<N;i++)

if(men[i].equals(str))

return i;

return -1;

}

//get women index

private int womenIndexof(String str)

{

for(int i=0;i<N;i++)

if(women[i].equals(str))

return i;

return -1;

}

//print couples

public void printcouples()

{

System.out.println("Couples are:");

for(int i=0;i<N;i++)

{

System.out.println(womenpartner[i] +" "+women[i]);

}}

//main function

public static void main(String args[])

{

System.out.println("Gale Shapley Algorithm");

String[] m={"M1","M2","M3","M4","M5"};

String[] w={"W1","W2","W3","W4","W5"};

String[][] mp={{"W5","W2","W3","W4","W1"},

{"W2","W5","W1","W3","W4"},

{"W4","W3","W2","W1","W5"},

{ "W1","W2","W3","W4","W5"},

{"W5","W2","W3","W4","W1"}};

String[][] wp ={{"M5","M3","M4","M1","M2",},

{"M1","M2","M3","M5","M4"},

{"M4","M5","M3","M2","M1"},

{"M5","M2","M1","M4","M3"},

{"M2","M1","M4","M3","M5"}};

GaleShapley gs= new GaleShapley(m,w,mp,wp);

}}

OUTPUT:

Gale Shapely Algorithm

Couples are:

M4 W1

M2 W2

M5 W3

M3 W4

M1 W5

Add a comment
Know the answer?
Add Answer to:
Implement the Gale Shapley algorithm using JsFiddle. Must have a sample data set of 5 males...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the PLA algorithm using Python (Only language I know). Generate linearly separable data using at...

    Implement the PLA algorithm using Python (Only language I know). Generate linearly separable data using at least 20 points with two features, it is not required that the points be evenly divided between the positive and negative class. Run and test your code. Please show working code with explanations to help me understand how to implement my own. P.S. You guys are awesome Regarding Comment: "No information providing regarding dataset, features" It is a data set of nothing in particular,...

  • Researchers wondered if there was a difference between males and fema es in regard to some...

    Researchers wondered if there was a difference between males and fema es in regard to some common annoyances. They asked a random sample of maes and females, the following cuestion: "Are you annoyed by people who repeatedly check their mobile phones while having an In-person conversation?' Among the 580 males surveyed, 216 responded "Yes"; among the 520 females surveyed, 212 responded "Yes.'Does the evidence suggest a higher proportion of females are annoyed by this behavior? Complete parte (a) through (g)...

  • Researchers wondered if there was a difference between males and females in regard to some common...

    Researchers wondered if there was a difference between males and females in regard to some common annoyances. They asked a random sample of tales and ferrales, the following question: "Are you annoyed by people who repeatedly check their mobile phones while having aan in-person coriversaliun?" Ainung the 580 males surveyed 218 resurideu "Yes", argile 520 fernales surveyed, 212 responded Yes."Dues the eviderice sugges, a higher proportion of females are annoyed by this beliaviar? Cumplete paris (a) through (g) belun. (a)...

  • lots of parts please help! Researchers wondered if there was a difference between male and females...

    lots of parts please help! Researchers wondered if there was a difference between male and females in regard to some common annoyances They asked a random sample of males and femalesthe following question "Are you arvioyed by people who repeatedly check the mobile phones while having an in person conversation Among the 513 males surveyed. 194 responded "Yesamong the 551 females surveyed. 212 responded "Yes" Does the evidence suggest a Highet proportion of females we annoyed by this behavior? Complete...

  • Part 1- Additional Resources Allowed The data set to be analyzed in this assignment was collected...

    Part 1- Additional Resources Allowed The data set to be analyzed in this assignment was collected as part of the Stat 216 student projects during a previous semester A survey was conducted at the MSU Library and at Rendezvous Dining Hall. Students passing by were randomly given one of two Facebook status posts, then asked to answer the following questions: 1. How would you best describe the creator of this post: Trustworthy or Untrustworthy 2. What is your major? 3....

  • Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double...

    Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double hashing uses the idea of applying a second hash function to key when a collision occurs.   The software program will be based on the following requirements: Development Environment: If the software program is written in C++, its project must be created using Microsoft Visual Studio 2017. If the software program is written in Java, its project must be created using NetBeans v8.2. Algorithm: If...

  • MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as...

    MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as defined by the following skeleton: #define MAX_ITEMS 10 typedef char ItemType; class UnsortedList {        private:             int length; ItemType values[MAX_ITEMS]; int currentPos;        public:             SortedList( ); // default constructor: lenght=0, currentPos=-1             void MakeEmpty;    // let length=0             void InsertItem(ItemType x);   // insert x into the list                 void DeleteItem(ItemType x); // delete x from the list bool IsFull( );   // test...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Below is a example of a ID3 algorithm in Unity using C# im not sure how...

    Below is a example of a ID3 algorithm in Unity using C# im not sure how the ID3Example works in the whole thing can someone explain the whole thing in more detail please. i am trying to use it with this data set a txt file Alternates?:Bar?:Friday?:Hungry?:#Patrons:Price:Raining?:Reservations?:Type:EstWaitTime:WillWait? Yes:No:No:Yes:Some:$$$:No:Yes:French:0-10:True Yes:No:No:Yes:Full:$:No:No:Thai:30-60:False No:Yes:No:No:Some:$:No:No:Burger:0-10:True Yes:No:Yes:Yes:Full:$:Yes:No:Thai:10-30:True Yes:No:Yes:No:Full:$$$:No:Yes:French:>60:False No:Yes:No:Yes:Some:$$:Yes:Yes:Italian:0-10:True No:Yes:No:No:None:$:Yes:No:Burger:0-10:False No:No:No:Yes:Some:$$:Yes:Yes:Thai:0-10:True No:Yes:Yes:No:Full:$:Yes:No:Burger:>60:False Yes:Yes:Yes:Yes:Full:$$$:No:Yes:Italian:10-30:False No:No:No:No:None:$:No:No:Thai:0-10:False Yes:Yes:Yes:Yes:Full:$:No:No:Burger:30-60:True Learning to use decision trees We already learned the power and flexibility of decision trees for adding a decision-making component to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT