forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaleShapleyAlgorithm.cs
35 lines (32 loc) · 1.24 KB
/
GaleShapleyAlgorithm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// submitted by Julian Schacher (jspp) with great help by gustorn and Marius Becker
using System.Collections.Generic;
namespace StableMarriageProblem
{
public static class GaleShapleyAlgorithm<TFollow, TLead>
where TFollow : Person<TFollow, TLead>
where TLead : Person<TLead, TFollow>
{
public static void RunGaleShapleyAlgorithm(List<TFollow> follows, List<TLead> leads)
{
// All follows are lonely.
var lonelyFollows = new List<TFollow>(follows);
// Carry on until there are no lonely follows anymore.
while (lonelyFollows.Count > 0)
{
// Let every lonely follow propose to their current top choice.
foreach (var lonelyFollow in lonelyFollows)
{
lonelyFollow.ProposeToNext();
}
// Look which follows have a partner now and which don't.
var newLonelyFollows = new List<TFollow>();
foreach (var follow in follows)
{
if (follow.Partner == null)
newLonelyFollows.Add(follow);
}
lonelyFollows = newLonelyFollows;
}
}
}
}