Javatpoint Logo
Javatpoint Logo

Stable Marriage Problem in C++

In combinatorial mathematics and computer science, the Stable Marriage Problem is a well-known Puzzle. It entails establishing a stable match between two sets of elements, such as men and women, in which each has distinct preferences for the individuals who make up the other group. If there are no elements that would both prefer to be matched with each other over their current partners, then the matching is said to be stable. Algorithms like the Gale-Shapley algorithm are frequently used in the solution of the problem.

Algorithm:

A popular method for resolving the Stable Marriage Problem is the Gale-Shapley algorithm, which was developed by David Gale and Lloyd Shapley.

Following are the steps in the algorithm:

  1. Every single man proposes to the woman he likes best out of those he hasn't yet.
  2. Each woman carefully weighs the offers she has received before tentatively accepting the one from her favourite male.
  3. When a woman rejects him, the male moves on to his next favourite woman and proposes.
  4. As long as no man is rejected or has used up all of his alternatives, the procedure is repeated.

Properties:

A stable matching is always discovered using the Gale-Shapley algorithm, and its termination is assured.

At each stage, the problem has a stable answer because if a guy proposes to a woman and she rejects him, it indicates that she prefers him over her present partner (if she has one) in that relationship.

Program:

Let's take an example to demonstrate a stable marriage problem in C++:

Output:

Enter the number of men and women: 3
Enter men's preferences (0-based indexing):
Preferences for Man 0: 1 0 2
Preferences for Man 1: 2 1 0
Preferences for Man 2: 0 2 1
Enter women's preferences (0-based indexing):
Preferences for Woman 0: 1 0 2
Preferences for Woman 1: 2 1 0
Preferences for Woman 2: 0 2 1
Stable Matches:
Man 0 matches with Woman 1
Man 1 matches with Woman 2
Man 2 matches with Woman 0
  1. First, men's and women's numbers are entered into the program.
  2. After that, the preferences of every man and woman are factored into the system. The preferences are entered as a list of indices with values indicating the priority to which they should be given.
  3. Stable matches between men and women are found using the Gale-Shapley algorithm, which is implemented by the stableMarriage function.
  4. Up until every man is matched, the algorithm iterates:
    • It identifies the first woman on each single man's list who hasn't received a marriage proposal.
    • A match develops between them if the woman is single.
    • Even though the woman is already matched, if she finds her current mate to be less appealing than the previous one, she swaps partners.
    • Until every man is matched, the algorithm keeps running.
    • The stable matches between men and women are printed by the program at the end.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA