Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
gale_shapley.cpp File Reference

Gale Shapley Algorithm More...

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

Namespaces

namespace  greedy_algorithms
 for std::vector
 
namespace  greedy_algorithms::stable_matching
 Functions for the Gale-Shapley Algorithm.
 

Functions

std::vector< std::uint32_tgreedy_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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
126 {
127 tests(); // Run self-test implementations
128 return 0;
129}
static void tests()
Self-test implementations.
Definition gale_shapley.cpp:100
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
100 {
101 // Test Case 1
102 std::vector<std::vector<std::uint32_t>> primary_preferences = {{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
103 std::vector<std::vector<std::uint32_t>> secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
104 assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({0, 2, 1, 3}));
105
106 // Test Case 2
107 primary_preferences = {{0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}};
108 secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
109 assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({0, 3, 1, 2}));
110
111 // Test Case 3
112 primary_preferences = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}};
113 secondary_preferences = {{1, 0, 2}, {2, 0, 1}, {0, 2, 1}};
114 assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({0, 2, 1}));
115
116 // Test Case 4
117 primary_preferences = {};
118 secondary_preferences = {};
119 assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<std::uint32_t>({}));
120}
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 ...
Definition gale_shapley.cpp:42
Here is the call graph for this function: