The Gale-Shapley Algorithm is given by Lloyd Shapley, a famous mathematician and scientist. He contributed major researches in Game Theory and Graph Theory. Here in this algorithm he gave a solution for the marriage stable problem where there are same no of man and woman, with same preferences. In general this algorithm can be utilized for Pairing items of the given two sets.

This algorithm is also known as the solution of Marriage stable problem. This algorithm is also useful in solving the other problems too where optimal solution is required to develop where similar preferences are set and optimal selection or allocation has to be defined.

The Gale-Shapley Algorithm is an important part of Java.

**Pseudo code of the Algorithm**

Step 1 – Set all m belongs to M and w belongs to W are free or not engaged

While there is a man m who is single and has not selected any woman

Step 2- Choose such a man m

Let w be the highest-ranked woman in m’s preference list to whom m has not yet proposed

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 single

Else w prefers m to m’

(m,w) become engaged

m’ becomes free

Endif

Endif

Endwhile

Return the set S of engaged pairs

**Gale-shapley algorithm example**

Here we are giving solution of this algorithm in solving the College admission problem

This is the example of classical Gale shapley college Admissions where the 1000 students were required to match to the 400 colleges, where each college has two slots. By construction, 200 students will remain unmatched. In order to solve this problem the preferences of the students’ and colleges’ were set , uStudents and uColleges, respectively, by from a uniform distribution.

#set no of students

No of students: 1000

#set no of colleges

No of colleges : 400

#define the preferences

Ustudents = matrix(run if (ncolleges* n Students), n row = ncolleges, ncol = nstudents)

Ucolleges= matrix(run if (n students*n colleges), n row= n students , ncol= ncolleges)

#define student –college optimal matching

Result = gale.shapley.collegeadmission(student utils= ustudents, college utils= Ucolleges , slots =2)

Store(result)

## List of 4

## $ unmatched.students: num [1:200] 3 15 23 29 30 36 44 46 48 49 …

## $ unmatched.colleges: num(0)

## $ matched.colleges : num [1:400, 1:2] 576 887 28 551 926 775 456 553 402 917 …

## $ matched.students : num [1:1000, 1] 52 70 NA 210 155 170 238 16 371 391 …

# check if matching is stable

galeShapley.checkStability(uStudents, uColleges, results$matched.students, results$matched.colleges)

## [1] TRUE

**Java Implementation of the Algorithm**