diff --git a/algorithm/category.json b/algorithm/category.json index bc7eeef4..6255655d 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -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" } diff --git a/algorithm/etc/stable_matching/basic/code.js b/algorithm/etc/stable_matching/basic/code.js new file mode 100644 index 00000000..241c10a4 --- /dev/null +++ b/algorithm/etc/stable_matching/basic/code.js @@ -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(); + } + + } +} \ No newline at end of file diff --git a/algorithm/etc/stable_matching/basic/data.js b/algorithm/etc/stable_matching/basic/data.js new file mode 100644 index 00000000..5159f1c1 --- /dev/null +++ b/algorithm/etc/stable_matching/basic/data.js @@ -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' ); \ No newline at end of file diff --git a/algorithm/etc/stable_matching/desc.json b/algorithm/etc/stable_matching/desc.json new file mode 100644 index 00000000..9e91aa92 --- /dev/null +++ b/algorithm/etc/stable_matching/desc.json @@ -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": [ + "Wikipedia" + ], + "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