forked from joanaz/HireDot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stableMarriage.js
156 lines (132 loc) · 3.89 KB
/
stableMarriage.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function Person(name) {
var candidateIndex = 0;
this.name = name;
this.fiance = null;
this.candidates = [];
this.rank = function(p) {
for (i = 0; i < this.candidates.length; i++)
if (this.candidates[i] === p) return i;
return this.candidates.length + 1;
<<<<<<< HEAD
}
this.prefers = function(p) {
return this.rank(p) < this.rank(this.fiance);
}
=======
};
this.prefers = function(p) {
return this.rank(p) < this.rank(this.fiance);
};
>>>>>>> master
this.nextCandidate = function() {
if (candidateIndex >= this.candidates.length) return null;
return this.candidates[candidateIndex++];
<<<<<<< HEAD
}
=======
};
>>>>>>> master
this.engageTo = function(p) {
if (p.fiance) {
// console.log("engageTo",p.fiance.fiance.name)
p.fiance.fiance = null;
}
p.fiance = this;
if (this.fiance) this.fiance.fiance = null;
this.fiance = p;
// console.log(this.name, this.fiance.name)
<<<<<<< HEAD
}
=======
};
>>>>>>> master
this.swapWith = function(p) {
console.log("%s & %s swap partners", this.name, p.name);
var thisFiance = this.fiance;
var pFiance = p.fiance;
this.engageTo(pFiance);
p.engageTo(thisFiance);
<<<<<<< HEAD
}
=======
};
>>>>>>> master
}
function isStable(guys, gals) {
for (var i = 0; i < guys.length; i++)
for (var j = 0; j < gals.length; j++)
if (guys[i].prefers(gals[j]) && gals[j].prefers(guys[i]))
return false;
return true;
}
function engageEveryone(guys) {
var done;
do {
done = true;
for (var i = 0; i < guys.length; i++) {
var guy = guys[i];
<<<<<<< HEAD
console.log(guy.name)
=======
console.log(guy.name);
>>>>>>> master
if (!guy.fiance) {
done = false;
var gal = guy.nextCandidate();
<<<<<<< HEAD
console.log(gal.name)
if (!gal.fiance || gal.prefers(guy)) {
guy.engageTo(gal);
console.log("engage", guy.name, gal.name)
=======
console.log(gal.name);
if (!gal.fiance || gal.prefers(guy)) {
guy.engageTo(gal);
console.log("engage", guy.name, gal.name);
>>>>>>> master
}
}
// console.log(guy.name, guy.fiance.name)
}
} while (!done);
}
function doMarriage() {
var joanna = new Person("Joanna");
var frances = new Person("Frances");
var violet = new Person("Violet");
var google = new Person("Google");
var fb = new Person("Facebook");
// var twitter = new Person("Twitter");
// var linkedin=new Person('Linkedin');
joanna.candidates = [google, fb];
frances.candidates = [fb, google];
violet.candidates = [google, fb];
google.candidates = [frances, violet, joanna];
fb.candidates = [joanna, frances, violet];
// twitter.candidates = [frances, joanna, violet];
// linkedin.candidates=[joanna,frances,violet];
var people = [joanna, frances, violet];
var companies = [google, fb, ];
<<<<<<< HEAD
var smallerSet
if (people.length <= companies.length)
smallerSet = people
else
smallerSet = companies
engageEveryone(smallerSet)
=======
var smallerSet;
if (people.length <= companies.length)
smallerSet = people;
else
smallerSet = companies;
engageEveryone(smallerSet);
>>>>>>> master
for (var i = 0; i < smallerSet.length; i++) {
console.log("%s is engaged to %s", smallerSet[i].name, smallerSet[i].fiance.name);
}
console.log("Stable = %s", isStable(people, companies) ? "Yes" : "No");
joanna.swapWith(frances);
console.log("Stable = %s", isStable(people, companies) ? "Yes" : "No");
}
doMarriage();