Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puzzle: N persons sitting on round table. No of ways of handshakes without crossing any other handshakes

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.

like image 365
user2328404 Avatar asked Aug 06 '13 09:08

user2328404


People also ask

How many ways of handshakes without crossing any other handshake?

No of ways of handshakes without crossing any other handshakes 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.

How many ways can 6 people sit on a round table?

Solution (Method 1): Total no of ways in which 6 persons can sit on a round table is (6-1)! = 5! = 120. If we consider two same-named individuals as one person there are 5 persons who can sit in (5-1)! ways and these individuals can be seated together in 2! ways. Attention reader!

How to count the total number of handshakes possible?

Your task is to complete the function count () which takes an integer N as input parameters and returns an integer, the total number of handshakes possible so that no two handshakes cross each other. to report an issue.

How many people are standing in a circle?

The question states that there are 100 people standing in a circle The first person has a gun in his hand and he has to shoot the second person and then pass the gun to the third person. Now, the third person will kill the fourth person and gives the gun to the fifth person. This process continues until there is only one person left.


3 Answers

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.

like image 75
Buhb Avatar answered Jan 10 '23 00:01

Buhb


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)
like image 27
nneonneo Avatar answered Jan 10 '23 00:01

nneonneo


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

like image 32
Colonel Panic Avatar answered Jan 10 '23 00:01

Colonel Panic