mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-26 19:43:33 +08:00
246 lines
15 KiB
HTML
246 lines
15 KiB
HTML
<!-- HTML header for doxygen 1.12.0-->
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.13.2"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>TheAlgorithms/C++: greedy_algorithms::stable_matching Namespace Reference</title>
|
|
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
|
|
<script type="text/javascript">
|
|
DoxygenAwesomeDarkModeToggle.init()
|
|
</script>
|
|
<script type="text/javascript" src="../../clipboard.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../cookie.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/javascript">
|
|
window.MathJax = {
|
|
options: {
|
|
ignoreHtmlClass: 'tex2jax_ignore',
|
|
processHtmlClass: 'tex2jax_process'
|
|
},
|
|
loader: {
|
|
load: ['[tex]/ams']
|
|
},
|
|
tex: {
|
|
macros: {},
|
|
packages: ['base','configmacros','ams']
|
|
}
|
|
};
|
|
</script>
|
|
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
|
|
<td id="projectalign">
|
|
<div id="projectname">TheAlgorithms/C++<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">All the algorithms implemented in C++</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.13.2 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() { codefold.init(1); });
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search',true);
|
|
$(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function(){initNavTree('dd/d9a/namespacegreedy__algorithms_1_1stable__matching.html','../../'); initResizable(true); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle"><div class="title">greedy_algorithms::stable_matching Namespace Reference</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p>Functions for the Gale-Shapley Algorithm.
|
|
<a href="#details">More...</a></p>
|
|
<table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a6d7e84df47dcf19e88f95f8f9040306c" id="r_a6d7e84df47dcf19e88f95f8f9040306c"><td class="memItemLeft" align="right" valign="top">std::vector< std::uint32_t > </td><td class="memItemRight" valign="bottom"><a class="el" href="#a6d7e84df47dcf19e88f95f8f9040306c">gale_shapley</a> (const std::vector< std::vector< std::uint32_t > > &secondary_preferences, const std::vector< std::vector< std::uint32_t > > &primary_preferences)</td></tr>
|
|
<tr class="memdesc:a6d7e84df47dcf19e88f95f8f9040306c"><td class="mdescLeft"> </td><td class="mdescRight">The main function that finds the stable matching between two sets of elements using the Gale-Shapley Algorithm. <br /></td></tr>
|
|
<tr class="separator:a6d7e84df47dcf19e88f95f8f9040306c"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p>Functions for the Gale-Shapley Algorithm. </p>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="a6d7e84df47dcf19e88f95f8f9040306c" name="a6d7e84df47dcf19e88f95f8f9040306c"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a6d7e84df47dcf19e88f95f8f9040306c">◆ </a></span>gale_shapley()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">std::vector< std::uint32_t > greedy_algorithms::stable_matching::gale_shapley </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const std::vector< std::vector< std::uint32_t > > &</td> <td class="paramname"><span class="paramname"><em>secondary_preferences</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const std::vector< std::vector< std::uint32_t > > &</td> <td class="paramname"><span class="paramname"><em>primary_preferences</em></span> )</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>The main function that finds the stable matching between two sets of elements using the Gale-Shapley Algorithm. </p>
|
|
<dl class="section note"><dt>Note</dt><dd>This doesn't work on negative preferences. the preferences should be continuous integers starting from 0 to number of preferences - 1. </dd></dl>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">primary_preferences</td><td>the preferences of the primary set should be a 2D vector </td></tr>
|
|
<tr><td class="paramname">secondary_preferences</td><td>the preferences of the secondary set should be a 2D vector </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>matches the stable matching between the two sets </dd></dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../db/d80/gale__shapley_8cpp_source.html#l00046">46</a> of file <a class="el" href="../../db/d80/gale__shapley_8cpp_source.html">gale_shapley.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 48</span> {</div>
|
|
<div class="line"><span class="lineno"> 49</span> std::uint32_t num_elements = secondary_preferences.size();</div>
|
|
<div class="line"><span class="lineno"> 50</span> std::vector<std::uint32_t> matches(num_elements, -1);</div>
|
|
<div class="line"><span class="lineno"> 51</span> std::vector<bool> is_free_primary(num_elements, <span class="keyword">true</span>);</div>
|
|
<div class="line"><span class="lineno"> 52</span> std::vector<std::uint32_t> proposal_index(</div>
|
|
<div class="line"><span class="lineno"> 53</span> num_elements,</div>
|
|
<div class="line"><span class="lineno"> 54</span> 0); <span class="comment">// Tracks the next secondary to propose for each primary</span></div>
|
|
<div class="line"><span class="lineno"> 55</span> </div>
|
|
<div class="line"><span class="lineno"> 56</span> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div>
|
|
<div class="line"><span class="lineno"> 57</span> <span class="keywordtype">int</span> free_primary_index = -1;</div>
|
|
<div class="line"><span class="lineno"> 58</span> </div>
|
|
<div class="line"><span class="lineno"> 59</span> <span class="comment">// Find the next free primary</span></div>
|
|
<div class="line"><span class="lineno"> 60</span> <span class="keywordflow">for</span> (std::uint32_t i = 0; i < num_elements; i++) {</div>
|
|
<div class="line"><span class="lineno"> 61</span> <span class="keywordflow">if</span> (is_free_primary[i]) {</div>
|
|
<div class="line"><span class="lineno"> 62</span> free_primary_index = i;</div>
|
|
<div class="line"><span class="lineno"> 63</span> <span class="keywordflow">break</span>;</div>
|
|
<div class="line"><span class="lineno"> 64</span> }</div>
|
|
<div class="line"><span class="lineno"> 65</span> }</div>
|
|
<div class="line"><span class="lineno"> 66</span> </div>
|
|
<div class="line"><span class="lineno"> 67</span> <span class="comment">// If no free primary is found, break the loop</span></div>
|
|
<div class="line"><span class="lineno"> 68</span> <span class="keywordflow">if</span> (free_primary_index == -1)</div>
|
|
<div class="line"><span class="lineno"> 69</span> <span class="keywordflow">break</span>;</div>
|
|
<div class="line"><span class="lineno"> 70</span> </div>
|
|
<div class="line"><span class="lineno"> 71</span> <span class="comment">// Get the next secondary to propose</span></div>
|
|
<div class="line"><span class="lineno"> 72</span> std::uint32_t secondary_to_propose =</div>
|
|
<div class="line"><span class="lineno"> 73</span> primary_preferences[free_primary_index]</div>
|
|
<div class="line"><span class="lineno"> 74</span> [proposal_index[free_primary_index]];</div>
|
|
<div class="line"><span class="lineno"> 75</span> proposal_index[free_primary_index]++;</div>
|
|
<div class="line"><span class="lineno"> 76</span> </div>
|
|
<div class="line"><span class="lineno"> 77</span> <span class="comment">// Get the current match of the secondary</span></div>
|
|
<div class="line"><span class="lineno"> 78</span> std::uint32_t current_match = matches[secondary_to_propose];</div>
|
|
<div class="line"><span class="lineno"> 79</span> </div>
|
|
<div class="line"><span class="lineno"> 80</span> <span class="comment">// If the secondary is free, match them</span></div>
|
|
<div class="line"><span class="lineno"> 81</span> <span class="keywordflow">if</span> (current_match == -1) {</div>
|
|
<div class="line"><span class="lineno"> 82</span> matches[secondary_to_propose] = free_primary_index;</div>
|
|
<div class="line"><span class="lineno"> 83</span> is_free_primary[free_primary_index] = <span class="keyword">false</span>;</div>
|
|
<div class="line"><span class="lineno"> 84</span> } <span class="keywordflow">else</span> {</div>
|
|
<div class="line"><span class="lineno"> 85</span> <span class="comment">// Determine if the current match should be replaced</span></div>
|
|
<div class="line"><span class="lineno"> 86</span> <span class="keyword">auto</span> new_proposer_rank =</div>
|
|
<div class="line"><span class="lineno"> 87</span> std::find(secondary_preferences[secondary_to_propose].begin(),</div>
|
|
<div class="line"><span class="lineno"> 88</span> secondary_preferences[secondary_to_propose].end(),</div>
|
|
<div class="line"><span class="lineno"> 89</span> free_primary_index);</div>
|
|
<div class="line"><span class="lineno"> 90</span> <span class="keyword">auto</span> current_match_rank =</div>
|
|
<div class="line"><span class="lineno"> 91</span> std::find(secondary_preferences[secondary_to_propose].begin(),</div>
|
|
<div class="line"><span class="lineno"> 92</span> secondary_preferences[secondary_to_propose].end(),</div>
|
|
<div class="line"><span class="lineno"> 93</span> current_match);</div>
|
|
<div class="line"><span class="lineno"> 94</span> </div>
|
|
<div class="line"><span class="lineno"> 95</span> <span class="comment">// If the new proposer is preferred over the current match</span></div>
|
|
<div class="line"><span class="lineno"> 96</span> <span class="keywordflow">if</span> (new_proposer_rank < current_match_rank) {</div>
|
|
<div class="line"><span class="lineno"> 97</span> matches[secondary_to_propose] = free_primary_index;</div>
|
|
<div class="line"><span class="lineno"> 98</span> is_free_primary[free_primary_index] = <span class="keyword">false</span>;</div>
|
|
<div class="line"><span class="lineno"> 99</span> is_free_primary[current_match] =</div>
|
|
<div class="line"><span class="lineno"> 100</span> <span class="keyword">true</span>; <span class="comment">// Current match is now free</span></div>
|
|
<div class="line"><span class="lineno"> 101</span> }</div>
|
|
<div class="line"><span class="lineno"> 102</span> }</div>
|
|
<div class="line"><span class="lineno"> 103</span> }</div>
|
|
<div class="line"><span class="lineno"> 104</span> </div>
|
|
<div class="line"><span class="lineno"> 105</span> <span class="keywordflow">return</span> matches;</div>
|
|
<div class="line"><span class="lineno"> 106</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../d2/d90/namespacegreedy__algorithms.html">greedy_algorithms</a></li><li class="navelem"><a class="el" href="../../dd/d9a/namespacegreedy__algorithms_1_1stable__matching.html">stable_matching</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|