-2

Find n binary codes of length n such that distance between each pair is n/2 , where n is a even number , if possible?How to generate all codes? for example n=4 we have 1110,1101,1011,0111 each pair have distance 2. Distance between a pair of code means number of different bits in both code words. For example 1110 and 1101 ,only last two bits are different so distance between this pair is 2.

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • 1
    What does "distance" mean? What is a pair? – Kevin Sep 09 '15 at 16:47
  • @Kevin pair is not **that** abstract and distance is pretty clear from the question: a pair are two values like `(1110,1101)` and distance is the number of bits the two values differ by, like in the above pair: different bits are 0 and 1, thus distance=2 –  Sep 09 '15 at 16:56
  • Oh, I thought it might have been "difference in positioning of zeroes", which made me question the definition of "pair", because `1110` and `0111` have a "positional difference" of 3. – Kevin Sep 09 '15 at 17:05

2 Answers2

0

The main-difficulty with this question is simply that there are several solutions that produce the required solution. For example in your question:

  • 1110 , 1101 , 1011 , 0111

  • 1000 , 0100 , 0010 , 0001

  • 1100 , 1001 , 0011 , 0110 , 1010 , 0101 , 0000 (*1 note concerning 0000 bellow)

Would be all valid solution (order ignored).

In general we can assume that in any solution, each value has the same number of 0-bits. Proof:
Let a solution S for n consist of {a , b , ...}. Let's assume each value in S has exactly x 0-bits, except for one value (c'), which has x + 1 0-bits. Now the distance to one value will still be n/2, but for the rest the distance is n / 2 + 1, due to the additional 1-bit.

a = a1  a2  a3  a4  a5  ...
b = b1  b2  b3  b4  b5 ...
c = c1  c2  c3  c4  c5  ...
c'= c1' c2' c3' c4' c5' ...

if for example c3 != c3' and cx == cx' for any x != 3, the distance to any other value v, where v3 != c3' will always be n / 2 + 1 or if v3 == c3' n / 2 - 1. Solution S would be valid, if c is element of S. If c' is an element of S instead, S isn't a valid solution anymore.

Next step: The solution with most elements is the one that holds max(binomial_coefficient(n , x)), where x is the number of 0s. Thus the optimal solution would be:

  • x = n/2, if 2 | n

This solution will be a set of binomial_coefficient(n , n/2) + 1 values.

So basically we search for all permutations of x 1s in a n-bit value. Won't post any code here though, since that question has been answered pretty often.

*1: Solutions with x = n / 2 allow 0000 aswell as solution for any n. This is not the case, if x != n / 2.

Community
  • 1
  • 1
  • can you give any example for n=6 or 8? or give any previous link? – subrat singh Sep 09 '15 at 17:53
  • @subratsingh this answer applies to any `n`. and `n = 8` produces a set of 70 different values, so... –  Sep 09 '15 at 19:40
  • @subratsingh to simplify things: the relevant part of the answer is, that each value of the solution must have the same number of 0-bits and that the solution with the most bits will have `n/2` 0-bits per value and will produce `binomial_coefficient(n , n/2)` values –  Sep 09 '15 at 19:58
  • @Paul, see my results. Maybe I interpreted the question wrong, but some of my solutions break your same number of zeros rule. – agentp Sep 09 '15 at 20:12
  • @agentp what's the language? linq? there **are** solutions to which the 0-rule doesn't apply. But if you're searching for the solution with the most values, you'll have to rely on the 0-rule. Though i've forgotten to mention one special rule: for `n=4` my largest solution is missing `{0,0,0,0}` as additional value. might be I've missunderstood ops question just aswell... –  Sep 09 '15 at 20:19
  • @agentp ok. well, btw. what exactly is your code doing? if it'd simply calculate **all distinct** solutions it would produce by far more than 32 solutions. And these aren't the greatest solutions. My greatest solution has 7 values (after adding 0000, thanks btw. didn't check that special case). –  Sep 09 '15 at 20:26
  • added some explanation. My approach specifically looks for length=n solutions. – agentp Sep 09 '15 at 20:59
0

A totally brute force mathematica approach -- This unfortunately breaks my memory for n>4..

n = 4;
Select[ Subsets[Tuples[{0, 1}, {n}], {n}] ,
     And @@ Flatten@
      Outer[((# == n/2 || # == 0) &@Total@Abs@Subtract@##) & , # , #, 
          1 ] & ]

Some of the 32 solutions:

enter image description here

Edit, to explain a bit: Tuples[{0, 1}, {n}] gives all 2^n n-bit sequences. Subsets[ Tuples ,{n} ] gives all unique unordered length-n sets of the tuples ( 1820 for n=4 ) Select chooses from those the sets that match the criterea. Outer applies the test to every pair in the set ( note we have == n/2 or 0 because Outer tests each binary against itself with the self-test yielding zero )

Note each of the subsets can be reordered n! ways so for n=4 there should be 768 unique solutions if you consider order important.

agentp
  • 6,849
  • 2
  • 19
  • 37