forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable_marriage.scala
65 lines (52 loc) · 1.65 KB
/
stable_marriage.scala
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import scala.collection.mutable._
object StableMarriage {
var bachelors = Queue[Man]()
case class Man(name: String, var preferences: List[Woman] = List()) {
def propose(): Unit = preferences match {
case woman :: remainingPreferences => {
if (woman.prefers(this)) {
bachelors ++= woman.fiance
woman.fiance = Some(this)
}
else
bachelors.enqueue(this)
preferences = remainingPreferences
}
case _ =>
}
}
case class Woman(name: String, var preferences: List[Man] = List(), var fiance: Option[Man] = None) {
def prefers(man: Man): Boolean =
fiance match {
case Some(otherMan) => preferences.indexOf(man) < preferences.indexOf(otherMan)
case _ => true //always prefer any man over nobody
}
}
def findStableMatches(men: Man*): Unit = {
bachelors = men.to[Queue]
while (bachelors.nonEmpty)
bachelors.dequeue.propose()
}
}
object StableMarriageExample {
val a = StableMarriage.Man("Adam")
val b = StableMarriage.Man("Bart")
val c = StableMarriage.Man("Colm")
val x = StableMarriage.Woman("Xena")
val y = StableMarriage.Woman("Yeva")
val z = StableMarriage.Woman("Zara")
a.preferences = List(y, x, z)
b.preferences = List(y, z, x)
c.preferences = List(x, z, y)
x.preferences = List(b, a, c)
y.preferences = List(c, a, b)
z.preferences = List(a, c, b)
def main(args: Array[String]): Unit = {
StableMarriage.findStableMatches(a, b, c)
List(x, y, z).foreach(
w => Console.println(
w.name
+ " is married to "
+ w.fiance.getOrElse(StableMarriage.Man("Nobody")).name))
}
}