Skip to content

Stable matching algorithm #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"flood_fill": "Flood Fill",
"cellular_automata": "Cellular Automata",
"create_maze": "Create Maze",
"magic_square": "Magic Square"
"magic_square": "Magic Square",
"stable_matching": "Stable Matching"
},
"name": "Uncategorized"
}
Expand Down
62 changes: 62 additions & 0 deletions algorithm/etc/stable_matching/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function init(rank) {
var o = {};
for (var k in rank) {
o[k] = {
key: k,
stable: false,
rank_keys: rank[k]
};
}
return o;
}

function extractUnstable(Q) {
for (var k in Q) {
if (Q[k].stable === false) {
return Q[k];
}
}
}

var A = init(ARank), B = init(BRank);
var a, b;

while ((a = extractUnstable(A)) != null) {

logTracer._print('Selecting ' + a.key)._wait();

var bKey = a.rank_keys.shift();
var b = B[bKey];

logTracer._print('--> Choicing ' + b.key)._wait();

if (b.stable === false) {

logTracer._print('--> ' + b.key + ' is not stable, stabilizing with ' + a.key)._wait();

a.stable = b;
b.stable = a;

tracerA._select(_aKeys.indexOf(a.key))._wait();
tracerB._select(_bKeys.indexOf(b.key))._wait();

} else {

var rank_a_in_b = b.rank_keys.indexOf(a.key);
var rank_prev_a_in_b = b.rank_keys.indexOf(b.stable.key);
if (rank_a_in_b < rank_prev_a_in_b) {

logTracer._print('--> ' + bKey + ' is more stable with ' + a.key + ' rather than ' + b.stable.key + ' - stabilizing again')._wait();

A[b.stable.key].stable = false;
tracerA._deselect(_aKeys.indexOf(b.stable.key))._wait();

a.stable = b;
b.stable = a;

tracerA._select(_aKeys.indexOf(a.key))._wait();
tracerB._select(_bKeys.indexOf(b.key))._wait();
}

}
}
23 changes: 23 additions & 0 deletions algorithm/etc/stable_matching/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var ARank = {
'Flavio' : [ 'Valentine', 'July', 'Summer', 'Violet' ],
'Stephen': [ 'Summer', 'July', 'Valentine', 'Violet' ],
'Albert' : [ 'July', 'Violet', 'Valentine', 'Summer' ],
'Jack' : [ 'July', 'Violet', 'Valentine', 'Summer' ]
};

var BRank = {
'July': [ 'Jack', 'Stephen', 'Albert', 'Flavio' ],
'Valentine': [ 'Flavio', 'Jack', 'Stephen', 'Albert' ],
'Violet': [ 'Jack', 'Stephen', 'Flavio', 'Albert' ],
'Summer': [ 'Stephen', 'Flavio', 'Albert', 'Jack' ],
};

var tracerA = new Array1DTracer('A');
var tracerB = new Array1DTracer('B');

var _aKeys = Object.keys(ARank);
var _bKeys = Object.keys(BRank);
tracerA._setData(_aKeys);
tracerB._setData(_bKeys);

var logTracer = new LogTracer ( 'Console' );
12 changes: 12 additions & 0 deletions algorithm/etc/stable_matching/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Stable Matching": "In mathematics, economics, and computer science, the stable marriage problem (also stable matching problem or SMP) is the problem of finding a stable matching between two equally sized sets of elements given an ordering of preferences for each element. A matching is a mapping from the elements of one set to the elements of the other set. A matching is not stable if: There is an element A of the first matched set which prefers some given element B of the second matched set over the element to which A is already matched, and also prefers A over the element to which B is already matched. In other words, a matching is stable when there does not exist any match (A, B) by which both A and B would be individually better off than they are with the element to which they are currently matched.",
"Complexity": {
"time": " $O(N^2)$"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Stable_marriage_problem'>Wikipedia</a>"
],
"files": {
"basic": "Stable Matching"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy