Distribute N apple and M orange among (N+M) children in such a way that, minimum numbers of pairs of children can have different fruits in a pair.

326 Views Asked by At

I have N apples and M oranges and few random pairs of (N+M) children.

Distribute N apple and M orange among (N+M) children in such a way that, minimum numbers of pairs of children can have different fruits in a pair.

For better understanding lets take an example:

Ex 1) 2 apples and 1 orange and children=(2+1)=3
Given pairs of children are: (1,2) (2,3)
Now if i give an apple to 1 and 3 and orange to 2, then i will have two pairs with different fruits in a pair. i.e (apple,orange) (orange,apple)

But if i give an apple to 1 and 2 and orange to 3, then i will have only one pair with different fruits in a pair. i.e (apple,apple) (apple,orange).

So answer here is 1 pair which is minimum.

Note:
Number of pairs can be anything but >0 and <= nCr(n=no. of children and r=2)

1

There are 1 best solutions below

0
On

Your problem is equal to the problem called graph partitioning.

Let every child be a vertex $v$ and every chosen pair be an edge $e$ in graph $G$.
To find apple and orange distribution between children is to find a partition of the graph vertex set $V(G)$ into 2 parts $V_1$ and $V_2$ such as $|V_1|=N$, $|V_2|=M$ and $V_1\cap V_2=\emptyset$,
while minimizing the number of edges between parts ( edges $(v,u)\in E : v \in V_1$ and $u \in V_2$ ).
The minimization of this edge number will minimize the number of pairs that have different fruits in it.

enter image description here

Also, the graph bisection problem which is known to be NP-hard can be reduced to your problem, so your problem is NP-hard too. (Proof: Let $N = M$)

So, if you want to find an algorithm for your problem you can use algorithms for graph partitioning. This survey can help you to find a better one for your needs.