Gale Shapley Java Program

Problem Description In this problem we will consider a version of the problem for professors and students and their fully ordered list of preferences. Please read through this document and the documentation in the starter code thoroughly before beginning The Stable Matching Problem, as discussed in the text, assumes that all men and women have a fully ordered list of preferences.

In this problem we will consider a version of the problem for professors and students and their fully ordered list of preferences. Note that ties in preference lists are not allowed. As before we have a set P of n professors and a set S of n students. Assume each professor and each student ranks the members of the opposite group.

Cached

  • The following Matlab project contains the source code and Matlab examples used for gale shapley stable marriage algorithm. Given N men and N women, and the preferences of each of them over the members of the opposite gender, a stable matching is a matching between the N men and women such that there is no man and woman who prefer each other over their respectively matched partners.
  • I think the following implements the Gale-Shapley algorithm where each person's preference ordering is given as an array of scores over the members of the opposite sex. As an aside, I just found out that David Gale passed away (see his Wikipedia entry — he will be missed).

Part 1: Implement a Brute Force Solution [30 points A brute force solution to this problem involves generating all possible permutations of men and women, and checking whether each one is a stable matching, until a stable matching is found. For this assignment, you are provided with Preferences.java class which includes the necessarry input structures for the problem. Please see the comments in Preferences.java file for details.

Gale-Shapley Algorithm Demonstration. Gale-Shapley provides a solution to the stable marriage problem.It can be used to pair items from two sets. In the stable marriage problem, boys are to be matched with girls, but obviously Gale-Shapley can be (and is) used in many different scenarios. Java & Programming Projects for $15. Stable matching gale shapley algorithm, program gale shapley algorithm, mysql implementation man day estimation. Here is the source code of the Java Program to Implement Gale Shapley Algorithm. The Java program is successfully compiled and run on a Windows system. Theses and Dissertations Available from Pro.

Gale Shapley Java Program - Cleverblast

You are also given Assignment1.java file where you will put your implementation for this part under stableMatchBruteForce) function which returns ArrayList. This ArrayList should contain information for matching information representing the index of student matched with a professor, -1 is not matched. For example, if ith element of the returned ArrayList is j, than professor i is matched with student j. There might be a stable matching which is neither professor nor student optimal. This is because in a brute force, you are trying to find all possible stable matches.

Programs

Part 2: Implement Gale-Shapley Algorithm [40 points In order to solve this matching problem more efficiently, you need to implement Gale-Shapley Al gorithm and give a solution for Professors Optimal Matching. For implementation, we provide you with again with Preferences.java and you will put your implementation to Assignment1.java file under stableMatchGaleShapley). This algorithm is discussed in the class, so you can use

Gale Shapley Java Programming


Code:
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 GaleShapley(String[] m, String[] w, String[][] mp, String[][] wp)
{
N = mp.length;
engagedCount = 0;
men = m;
women = w;
menPref = mp;
womenPref = wp;
menEngaged = new boolean[N];
womenPartner = new String[N];
calcMatches();
}
/** function to calculate all 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 boolean 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 Marriage Algorithmn');
/** list of men **/
String[] m = {'M1', 'M2', 'M3', 'M4', 'M5'};
/** list of women **/
String[] w = {'W1', 'W2', 'W3', 'W4', 'W5'};
/** men preference **/
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'}};
/** women preference **/
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 Shapley Marriage Algorithm
Couples are :
M4 W1
M2 W2
M5 W3
M3 W4
M1 W5
More Java Programs: