Cracking the Code of Compatibility: Exploring the Gale-Shapley Algorithm for Stable Matching

Mohith J
8 min readMay 27, 2023

--

As a seasoned software developer, I have witnessed the importance of efficient algorithms in solving complex problems. In the realm of matching algorithms, the Gale-Shapley algorithm stands out as a groundbreaking solution for the Stable Matching problem. In this article, we will delve deep into the Gale-Shapley algorithm, examining its underlying principles, understanding its application in matching scenarios, exploring its significance in real-world scenarios where stability and fairness are crucial, and discussing the concept of blocking pairs.

Understanding the Gale-Shapley Algorithm

The Gale-Shapley algorithm, also known as the Deferred Acceptance algorithm, was introduced by mathematicians David Gale and Lloyd Shapley in 1962. It addresses the Stable Matching problem, which involves finding a stable and mutually acceptable pairing between two sets of participants, such as men and women seeking partners or employers and job applicants.

The algorithm begins with each participant proposing to their most preferred option from the opposite set, and then the receiving party either accepts the proposal or defers it for later consideration. The process continues iteratively until each participant ends up with a mutually acceptable match, resulting in a stable matching where no pair has an incentive to leave their current partner for another.

Blocking Pairs

During the execution of the Gale-Shapley algorithm, it is possible to encounter blocking pairs. A blocking pair consists of two participants who would both prefer to be matched with each other rather than their current partners.

When a blocking pair is identified, it signifies that the current matching is not stable, as there exists a potential improvement that would make both participants happier. The algorithm addresses this issue by allowing one participant involved in the blocking pair to reject their current match and accept the proposal from the other participant, leading to a new iteration of the algorithm.

By continuously identifying and resolving blocking pairs, the Gale-Shapley algorithm ensures that the resulting matching is not only acceptable but also stable, with no possible improvements that would satisfy all participants involved.

Pseudocode

function generateStableMatches(participants, preferences):
unmatchedParticipants = set of all participants
matches = empty dictionary

while unmatchedParticipants is not empty:
participant = select an arbitrary participant from unmatchedParticipants
participantPreferences = preferences[participant]

for preferredParticipant in participantPreferences:
currentMatch = matches[preferredParticipant]

if currentMatch is unassigned:
matches[preferredParticipant] = participant
matches[participant] = preferredParticipant
unmatchedParticipants.remove(participant)
break

elif preferredParticipant prefers participant over currentMatch:
matches[preferredParticipant] = participant
unmatchedParticipants.remove(participant)
unmatchedParticipants.add(currentMatch)
break

// If preferredParticipant prefers currentMatch over participant, continue to next iteration

return matches

In this pseudocode, participants represents the set of participants, and preferences is a dictionary mapping each participant to their preference list.

The algorithm starts by initialising the set of unmatched participants and an empty dictionary to store the matches.

Next, it enters a loop that continues until all participants are matched. In each iteration, it selects an arbitrary participant from the set of unmatched participants. The preference list of the selected participant is retrieved.

The algorithm then iterates over the preference list and checks the current match of each preferred participant. If the preferred participant is currently unmatched, the algorithm establishes a match between the participant and the preferred participant, removes both from the set of unmatched participants, and breaks out of the loop.

If the preferred participant is already matched, the algorithm compares the preferences of the preferred participant between the current match and the current participant. If the preferred participant prefers the current participant, the algorithm updates the matches, removes the current participant from the set of unmatched participants, adds the current match back to the set of unmatched participants, and breaks out of the loop.

If the preferred participant prefers the current match over the current participant, the algorithm continues to the next iteration without making any changes.

Finally, the algorithm returns the dictionary of matches.

Note: This pseudocode assumes that the preference lists are represented as arrays or ordered lists, and the comparison of preferences is based on their positions in the lists. The actual implementation may require adjustments based on the data structures and preferences representation used in your specific scenario.

Implementation in Java

import java.util.*;

public class GaleShapleyAlgorithm {

public static Map<String, String> match(Map<String, List<String>> menPreferences, Map<String, List<String>> womenPreferences) {
Map<String, String> matches = new HashMap<>();
Set<String> unmatchedMen = new HashSet<>(menPreferences.keySet());

while (!unmatchedMen.isEmpty()) {
String man = unmatchedMen.iterator().next();
List<String> preferences = menPreferences.get(man);

for (String woman : preferences) {
if (!matches.containsKey(woman)) {
matches.put(woman, man);
unmatchedMen.remove(man);
break;
} else {
String currentMan = matches.get(woman);
List<String> womanPreferences = womenPreferences.get(woman);

if (isPreferenceHigher(womanPreferences, man, currentMan)) {
matches.put(woman, man);
unmatchedMen.remove(man);
unmatchedMen.add(currentMan);
break;
}
}
}
}

return matches;
}

private static boolean isPreferenceHigher(List<String> preferences, String man1, String man2) {
return preferences.indexOf(man1) < preferences.indexOf(man2);
}

public static void main(String[] args) {
// Define preferences for men
Map<String, List<String>> menPreferences = new HashMap<>();
menPreferences.put("Alex", Arrays.asList("Emily", "Sophia", "Emma"));
menPreferences.put("Ben", Arrays.asList("Sophia", "Emma", "Emily"));
menPreferences.put("Charlie", Arrays.asList("Emily", "Emma", "Sophia"));

// Define preferences for women
Map<String, List<String>> womenPreferences = new HashMap<>();
womenPreferences.put("Emily", Arrays.asList("Alex", "Ben", "Charlie"));
womenPreferences.put("Sophia", Arrays.asList("Charlie", "Alex", "Ben"));
womenPreferences.put("Emma", Arrays.asList("Ben", "Charlie", "Alex"));

// Run the Gale-Shapley algorithm
Map<String, String> matches = match(menPreferences, womenPreferences);

// Print the resulting matches
for (Map.Entry<String, String> entry : matches.entrySet()) {
System.out.println(entry.getKey() + " matched with " + entry.getValue());
}
}
}

Output

In this implementation, we have two maps: menPreferences and womenPreferences, representing the preference lists for men and women, respectively. The match() method performs the Gale-Shapley algorithm, while the isPreferenceHigher() method compares preferences between two men.

In the main() method, we define the preferences for men and women, then call the match() method to obtain the matching pairs. Finally, we print the resulting matches.

Time and Space Complexity

Let’s dive into the time and space complexity of the Gale-Shapley algorithm in detail

Time Complexity

The Gale-Shapley algorithm has a time complexity of O(n²), where “n” represents the number of participants. This complexity arises from the two main operations in the algorithm: proposing and comparing preferences.

1. Proposing: Each participant proposes to every other participant. In the worst case, each participant needs to make n — 1 proposals. Therefore, the number of proposals in total is n * (n — 1), which simplifies to O(n²).

2. Comparing Preferences: For each proposal, there can be a comparison of preferences to determine the favored partner. Since each participant has a preference list of size n, the comparison operation takes O(n) time. As there can be a maximum of n² proposals, the overall time complexity for comparing preferences is O(n³).

Combining the proposing and preference comparison operations, the time complexity of the Gale-Shapley algorithm is O(n² + n³), which simplifies to O(n³). However, in practice, the algorithm often performs better than its worst-case time complexity due to early terminations when matches are made.

Space Complexity

The space complexity of the Gale-Shapley algorithm is O(n), where “n” represents the number of participants. The primary space consumption comes from storing the matches between participants. In the worst case, when all participants are matched, the matches data structure would have n entries, resulting in O(n) space usage.

Additional space may be required to store preference lists, unmatched participants, or auxiliary data structures. However, these additional space requirements typically remain proportional to the number of participants, resulting in a linear space complexity of O(n).

It’s important to note that the time and space complexity analysis assumes that the preference lists are preprocessed and readily available. If constructing preference lists or performing other operations require additional computations, the overall complexity may be affected.

Gale-Shapley algorithm exhibits a polynomial time complexity and linear space complexity, making it efficient for stable matching problems. However, the practical performance may vary depending on the specific problem instance and implementation details.

Real-World Applications

The Gale-Shapley algorithm has found practical applications in various domains:

  1. Stable Marriage Problem: The algorithm can be applied to solve the problem of stable matching in marriages or relationships, ensuring that each individual is paired with their most preferred partner.
  2. Job Matching: In scenarios where employers and job applicants need to be matched based on preferences, the Gale-Shapley algorithm can provide fair and stable matching outcomes.
  3. College Admissions: The algorithm can facilitate the assignment of students to colleges based on preferences, ensuring that the allocation is both fair and stable.
  4. Resource Allocation: The Gale-Shapley algorithm can be used to allocate limited resources, such as housing or public facilities, among applicants based on their preferences.

Advantages and Significance

The Gale-Shapley algorithm offers several advantages:

  1. Stability: The algorithm guarantees a stable matching, eliminating incentives for any participant to break their match and form a new one.
  2. Fairness: Participants have the opportunity to express their preferences, and the algorithm ensures that the resulting matching is as close to their preferences as possible.
  3. Efficiency: The algorithm has a time complexity of O(n²), where n is the number of participants, making it efficient even for large-scale matching scenarios.
  4. Practical Applicability: The Gale-Shapley algorithm has been successfully applied to real-world scenarios, providing tangible solutions in domains like marriage, job matching, and resource allocation.

Limitations

The Gale-Shapley algorithm, while being a powerful solution for stable matching problems, does have some limitations. Here are a few key limitations to consider:

  1. Biased Preferences: The algorithm assumes that participants have strict, consistent preferences when ranking their potential matches. However, in real-world scenarios, preferences can be complex and dynamic, often influenced by external factors. If preferences are biased or change over time, the resulting matches may not accurately reflect participants’ true desires.
  2. Asymmetry: The algorithm is asymmetric, meaning that the proposing party (e.g., men in the original formulation) tends to have more control and influence over the final matching outcome. This can introduce inherent biases or inequalities, especially in scenarios where one group has more power or resources than the other.
  3. Lack of Strategy Proofness: The Gale-Shapley algorithm is susceptible to manipulation by participants. They may strategically misrepresent their preferences or manipulate the order in which proposals are made to achieve a more favorable outcome. This can undermine the fairness and stability of the resulting matches.
  4. Dependency on Input Order: The algorithm’s outcome can depend on the order in which participants are processed. If the algorithm is rerun with the same preferences but a different processing order, it can yield different matchings. This sensitivity to input order can be a drawback, as it introduces potential unpredictability and instability.
  5. Efficiency for Large Datasets: While the Gale-Shapley algorithm has a polynomial time complexity, it can still be computationally expensive for large datasets. The algorithm’s time complexity of O(n²) can become a limitation when dealing with a large number of participants, as it may lead to increased execution time and resource consumption.

It’s important to consider these limitations when applying the Gale-Shapley algorithm in real-world scenarios and to explore alternative algorithms or modifications that address specific requirements and challenges.

Conclusion

The Gale-Shapley algorithm has revolutionised the field of matching algorithms, offering a principled and efficient solution to the Stable Matching problem. By incorporating the concept of blocking pairs, the algorithm ensures that the resulting matching is not only acceptable but also stable. Its applications in real-world scenarios have shown its significance in promoting stability, fairness, and efficiency.

As software architects and developers, understanding the Gale-Shapley algorithm equips us with a powerful tool for solving complex matching problems, enabling us to create solutions that balance the preferences and satisfaction of all participants involved. So, let’s harness the power of the Gale-Shapley algorithm and unlock the potential of stable matching in diverse domains.

--

--