[Prolog] The Three Jealous Men and Their Wives

Three men, travelling with their wives, came to a river which they wanted to cross. The one available boat would accomodate only two people. Since the husbands were very jealous, no woman could be with a man unless her own husband was present. Under these severe handicaps, how can they get across the river using the one boat?

(Source: Mr. P’s Math Page)

I haven’t had time to search for the solution on the internet so I am not very sure what kind of method that they have used, not to mention that if existing a solution that is similar to mine. So whatever, I will share it on here.

In this solution, I use the state space search method (I believe that every computer science student must have learnt this so I will not explain it again. If you, for some reason, do not know this method then please google about it on the internet for find some introductory books of artificial intelligence).

My state representation in Prolog is a list with below structure:

[Boat, [Husband1, Wife1], [Husband2, Wife2], [Husband3, Wife3]]

  • Boat: 0 if the boat is on the left bank, 1 if the boat is on the right bank.
  • Husband1: 0 if the Husband1 is on the left bank, 1 if the Husband1 is on the right bank.
  • Same goes for Wife1, Husband2, Wife2, Husband3, Wife3.

Before delving into further, I want to make some observations:

  • For variable T that can only hold 2 possible values 0 or 1: if T = 1, 1 – T = 0 ; if T = 0, 1 – T = 1.
  • People can only move from the left bank to the right bank if the boat is currently on the left bank. More conretely: If Boat = 0, Husband1 can use the boat to move to the opposite bank if and only if Husband1 = 0 ; in general, Husband1 can use the boat to move to the opposite bank if and only if Husband1 = Boat.

I also make some conventions:

  • B stands for Boat.
  • H stands for Husband.
  • W stands for Wife.
  • T stands for the tail of a list.

Possible actions to transfer from a state to another state are:

  • Let a couple be on the same boat to move to the opposite bank:

    However, that code line alone allows only the first couple to be on the same boat, how about the other couples? Let’s traverse the list:
  • Let 1 person be on the boat to move to the opposite bank, this one can be a husband or a wife:

    Gender = 1 means that person is male, Gender = 2 means that person is female.

We have been able to move a couple or a person to the opposite bank. Let’s combine this ability into one predicate:

Now comes the interesting part: Don’t you see that we lack one possible action of the state space? That is:

  • Let 2 people with same gender be on the same boat to move to the opposite bank:

But that is not enough, this situation can still happen: Husband1 & Wife2 can be on left bank while Husband2 is on right bank. We have to put a constrain on this. A little convention here: Z is always initialized with 2, then it will hold the value of 0 or 1 depending on the recursive process.

  • The wife is not with her husband on the bank Y:
  • Another husband is also on that bank Y:
  • The recursive process stops when it detects that there is a wife not being with her husband is on the same bank with other man:

Maybe there is nothing wrong with the first couple, still we have to traverse through the list for the other couples:

And the state is valid if such thing does not happen:

We have enough ingredients, the cooking part of f – the state transfer predicate should be easy:

Then comes the series of state space search predicates, I have an article about it here:

Full program in Prolog:

Run the program:

You may have realized that you can also try to solve this problem in size of n couples with this program. However, a friend of mine said that the version of this problem in size of four couples is unsolvable.


4 comments on “[Prolog] The Three Jealous Men and Their Wives”

  1. This is great, thank you so much!!! Very easy to follow and a great way to attack the problem.

    I am trying to train my machine using this program to progressively find simpler and simpler solutions to this. As I understand it, the simplest solutions have 11 steps, and the results I am getting from this program get up to over 20 steps.

    Any ideas or insight as to what queries to use or how to change the program to achieve this?

    Thanks again so much!

    1. It’s been a long time so I forgot how to use Prolog. I re-read what I wrote and I think *maybe* (I’m not sure) that you can get the shortest path by generating all solutions first. Maybe you can make a reference to the code block after the sentence “And maybe you want to count all the possible solution paths:” in this post. Hope it can help, though.

Leave a Reply

Your email address will not be published. Required fields are marked *