TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
gale_shapley.cpp File Reference

Gale Shapley Algorithm More...

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <vector>
Include dependency graph for gale_shapley.cpp:

Go to the source code of this file.

Namespaces

namespace  greedy_algorithms
 for string class
 
namespace  greedy_algorithms::stable_matching
 Functions for the Gale-Shapley Algorithm.
 

Functions

std::vector< std::uint32_t > greedy_algorithms::stable_matching::gale_shapley (const std::vector< std::vector< std::uint32_t > > &secondary_preferences, const std::vector< std::vector< std::uint32_t > > &primary_preferences)
 The main function that finds the stable matching between two sets of elements using the Gale-Shapley Algorithm.
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Gale Shapley Algorithm

This implementation utilizes the Gale-Shapley algorithm to find stable matches.

Gale Shapley Algorithm aims to find a stable matching between two equally sized sets of elements given an ordinal preference for each element. The algorithm was introduced by David Gale and Lloyd Shapley in 1962.

Reference: Wikipedia Wikipedia

Author
B Karthik

Definition in file gale_shapley.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 152 of file gale_shapley.cpp.

152 {
153 tests(); // Run self-test implementations
154 return 0;
155}
static void tests()
Self-test implementations.

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 114 of file gale_shapley.cpp.

114 {
115 // Test Case 1
116 std::vector<std::vector<std::uint32_t>> primary_preferences = {
117 {0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
118 std::vector<std::vector<std::uint32_t>> secondary_preferences = {
119 {1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
121 secondary_preferences, primary_preferences) ==
122 std::vector<std::uint32_t>({0, 2, 1, 3}));
123
124 // Test Case 2
125 primary_preferences = {
126 {0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}};
127 secondary_preferences = {
128 {1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
130 secondary_preferences, primary_preferences) ==
131 std::vector<std::uint32_t>({0, 3, 1, 2}));
132
133 // Test Case 3
134 primary_preferences = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}};
135 secondary_preferences = {{1, 0, 2}, {2, 0, 1}, {0, 2, 1}};
137 secondary_preferences, primary_preferences) ==
138 std::vector<std::uint32_t>({0, 2, 1}));
139
140 // Test Case 4
141 primary_preferences = {};
142 secondary_preferences = {};
144 secondary_preferences, primary_preferences) ==
145 std::vector<std::uint32_t>({}));
146}
std::vector< std::uint32_t > gale_shapley(const std::vector< std::vector< std::uint32_t > > &secondary_preferences, const std::vector< std::vector< std::uint32_t > > &primary_preferences)
The main function that finds the stable matching between two sets of elements using the Gale-Shapley ...