Whilst talking with an ex-colleague, a question came up on how to implement the Stable Marriage problem using a message passing approach. Naturally, I wanted to answer that question with Erlang!
Let’s first dissect the problem and decide what processes we need and how they need to interact with one another.
The stable marriage problem is commonly stated as:
Given n men and n women, where each person has ranked all members of the opposite sex with a unique number between 1 and n in order of preference, marry the men and women together such that there are no two people of opposite sex who would both rather have each other than their current partners. If there are no such people, all the marriages are “stable”. (It is assumed that the participants are binary gendered and that marriages are not same-sex).
From the problem description, we can see that we need:
- a module for man
- a module for woman
- a module for orchestrating the experiment
In terms of interaction between the different modules, I imagined something along the line of the following:
The proposal communication needs to be synchronous* as the man cannot proceed until he gets an answer for his proposal. But all other communications can be asynchronous.
(* remember, a synchronous call in OTP-sense is not the same as a synchronous call in Java/C# where the calling thread is blocked.
In this case the communication still happens via asynchronous message passing, but the calling process asynchronously waits for a reply before moving on)
From here, the implementation itself is pretty straight forward.
And using the test data from Rosetta Code, you can have a module that sets up the test, which I’ve called stable_marriage here.
Compile and run the code gives the following output:
You can get the full source code for this solution on github.
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Check out my new course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, callbacks, nested workflows, design patterns and best practices.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong