11

We have n persons sitting on a round table. Any person can do a handshake with any other person. In how many ways these n people can make handshakes so that no two handshakes crosses each other.

I found this puzzle in a technical interview forum, but no answer. One way i could think of was find all the permutations of handshakes and then check each permutation whether it satisfies or not.

Can anyone please sugget any other solution which is more efficient.

@edit: Clarification from comments: N would be even.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
user2328404
  • 423
  • 1
  • 4
  • 9
  • 1
    I'm assuming that the handshakes have to happen simultaneously. So if 4 people are sitting around the table, person 1 can handshake with person to the left of him, in which case at the same time the other 2 people can handshake. That's 1 combination. Then person 2 can handshake with person across, but that doesn't allow the other 2 to handshake. That's 2 combinations. Then he can handshake with the person to the right, which allows the other 2 to handshake. This is 3 combinations in total. Is this a correct understanding of the problem? – Lasse V. Karlsen Aug 06 '13 at 09:51
  • And then, if there's 5 people around the table, once the first handshake splits the table so that 3 people are "together", again we have multiple combinations in that group? – Lasse V. Karlsen Aug 06 '13 at 09:52
  • 4
    The solution is given by the Catalan numbers C(n/2) when `n` is even (and 0 when `n` is odd). See [OEIS A000108](http://oeis.org/A000108): "Ways of joining 2n points on a circle to form n nonintersecting chords." – nneonneo Aug 06 '13 at 10:12
  • @LasseV.Karlsen yes you are absolutely correct – user2328404 Aug 06 '13 at 10:13

8 Answers8

15

I would try a divide and conquer solution. if person 1 shakes hand with person x, it splits the rest of the people into two groups, that can be treated as sitting at two round tables.

Buhb
  • 7,088
  • 3
  • 24
  • 38
  • 2
    This can be made very efficient using memoization, requiring only `n` lookups (and the computation of values up to `n`). – nneonneo Aug 06 '13 at 10:01
  • 1
    Each sub group must contain an even number of people, or zero people. The tricky part is finding duplicated solutions efficently. – Skizz Aug 06 '13 at 10:07
  • @Skizz: There are no duplicate solutions! If you always pair person 1 with another person, it is easy to see there can be no duplicates. – nneonneo Aug 06 '13 at 10:13
  • 1
    @nneonneo: if you had 10 people (P1-P10) and did the top level divisions P1-P2, P1-P4, P1-P6, etc, and then did top level divisions P2-P3, P2-P5, etc, then when you get to P3 as the divider P3-P1 is the same as P1-P3 which has already been done. – Skizz Aug 06 '13 at 10:55
  • 1
    No, I don't follow. Can you give a specific example of a handshake pairing that would be counted twice? – nneonneo Aug 06 '13 at 10:57
12

The solution is quite easy given as a Python function (Python 3.3+):

@lru_cache(maxsize=None) # memoize
def num_handshakes(n):
    if n % 2 == 1: return 0
    elif n == 0: return 1
    res = 0
    for i in range(0, n, 2):
        res += num_handshakes(i) * num_handshakes(n-2-i)
    return res

Example:

>>> num_handshakes(8)
14

This basically implements @Buhb's divide-and-conquer approach. Another solution, once we know the answer is related to the Catalan numbers:

from math import factorial as fac
def catalan(n):
    return fac(2*n) // fac(n+1) // fac(n)

def num_handshakes(n):
    if n % 2 == 1: return 0
    return catalan(n//2)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
8

I would try a divide and conquer solution. if person 1 shakes hand with person x, it splits the rest of the people into two groups, that can be treated as sitting at two round tables.

@Buhb is right. That recurrence relation is

f(n) = sum( f(i-2) + f(n-i) for i in range(2, n))

Or in code

def f(n):
    if n == 0:
        # zero people can handshake
        return 1

    if n == 1:
        # it takes two to tango
        return 0

    ways = 0

    # what if person 1 shakes with person i ?
    for i in range(2, n+1):
        # splits table into independent sets 2 .. i-1 and i+1 .. n
        ways += f(i-2) * f(n-i)

    return ways

An odd number of people can't handshake, but the first few even-placed values of f are 1, 2, 5, 14, 42

Searching the encyclopedia of integer sequences, this looks like famous Catalan numbers http://oeis.org/A000108

Are the sequences really the same, or do they just start similarly? They are the same. Corroborated my a maths book—our recurrence relation that defines f holds for the Catalan numbers too https://en.wikipedia.org/wiki/Catalan_number#Properties

enter image description here

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • Can you please explain why do we multiply the counts of left sub table and right sub table ? Adding them makes more sense to me instead of multiplying. – Syed Jul 04 '20 at 21:13
0

What a bout backtracking?

  1. Start withe one handshake and search for still possible handshakes. if no more non crossing handshakes are possible, backtrack. Continue until no more branches of your search-tree can be extended by a non crossing handshake.
  2. All vertices of the resulting search-tree are non crossing handshakes.

Because your table is round (symmetry), you can optimize the problem by assuming that person 0 is always part of the top most handshake.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
0

I think this may be a solution for even n=2m.

Number the people around the circle from 1 to 2m.

In round j, 1≤j≤m, person j shakes hands with person j+1, and all other handshakes are "parallel" to this (so, j−1 with j+2, j−2 with j+3, and so on - throughout, the labels are interpreted modulo n, as necessary). At the end of these m rounds, everyone has shaken hands with everyone an odd number of people away.

In round m+j, 1≤j≤m, j shakes with j+2 and all other handshakes are parallel (so j−1 with j+3, j−2 with j+4, etc.). This handles all the pairs an even number of people apart. So the total is 2m rounds.

As noted in the problem statement, 2m−1 rounds is impossible, so 2m is the answer.

The odd case is even easier. In round j, person j sits out while j−1 greets j+1, j−2 greets j+2, etc., again using n rounds.

Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
0

Here Result follow the Catalan number Series. here is My code in c++

#include <bits/stdc++.h>
using namespace std;
long c[17] ;
void sieve(){
    c[0] = 1;
    c[1] = 1;
    for(int i =2;i<=15;i++){
        for(int j =0;j<i;j++){
            c[i] += c[j]*c[i-j-1];
        }
    }
}

int main(void){
    sieve();
    int t;
    scanf("%d",&t);
    while(t--){
        int n ;
        scanf("%d",&n);
        if(n%2!=0){
            cout<<"0"<<endl;
        }
        else{
            n = n>>1;
            cout<<c[n]<<endl;
        }
    }

    return 0;
}
HeadAndTail
  • 804
  • 8
  • 9
-1

people can make handshakes with (n-1)+(n-2)+.....+1 ways It's for linear

n ways for round table

DRAJI
  • 1,829
  • 8
  • 26
  • 45
  • So with 4 people around the table, how many possible combinations are valid? – Lasse V. Karlsen Aug 06 '13 at 09:50
  • 3+2+1=6. so totally 6 ways for 4 people – DRAJI Aug 06 '13 at 09:53
  • 1
    @DRAJI: not 6, but 2. Remember that handshakes can't cross. So the only two valid options are: Person A shakes with person to left and other two shake, and Person A shakes with person to right, and other two shake. Person A can't shake with person opposite as the other two would need to cross over. Choosing another person as Person A just replicates the two solutions already found. – Skizz Aug 06 '13 at 09:59
  • @DRAJI: in the case for 4 people around the table, yes, only two other people are valid, the third person being invalid due to cross over. – Skizz Aug 06 '13 at 10:06
  • Ok. Finally we get 4 shakes for 4 people. That means for n people n ways are possible @Skizz – DRAJI Aug 06 '13 at 10:10
-1

Since there are n people and 2 people would handshake at a time, and also if lets say A handshakes with B and B handshakes with A we cannot count it twice ie. AB and BA both cannot be counted which ultimately means that arrangement is not there ie. Permutations are not the case but Combination is...

So, applying combination formula it becomes : nC2 => (n*(n-1))/2 after solving.

So we can straight away apply this formula to the problem to get the answer.

Manish Khandelwal
  • 33
  • 1
  • 1
  • 10
  • This is not the right answer. For example, when N = 8 the answer is 14. But the combinations formula gives 28. – Syed Jul 04 '20 at 01:21