473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to find equidistant hamming sequences?

1
Given N as the number of bits, how to find N sequences of (2^N/N) numbers each such that: given an arbitrary number n, there is always one number in each sequence that has hamming distance 1 from n.

As a reference hamming distance 1 means that two numbers have only one bit that is different.

For example given N=4, one of the possible solution to the aforementioned problem is:

s0 = [ 0000 0001 1110 1111 ]
s1 = [ 0010 0101 1101 1010 ]
s2 = [ 0100 0011 1011 1100 ]
s3 = [ 1000 0110 1001 0111 ]

If we consider for example s3 we always find a number that has hamming distance 1 from a complete sequence of numbers.

complete sequence : 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
relative s3 numbers : 1000 1001 0110 0111 0110 0111 0111 0111 1000 1001 1000 1001 1000 1001 0110 0111

I apologize in advance for posting the same question in another purely math forum with the same title.
I am re-posting my question here because I believe this forum might approach the answer from a more algorithmic point of view. Hopefully this is the appropriate place for this discussion.

So far I have been able to calculate those codes with N = 8 using a graph coloring algorithm, which does not scale well with the number of bits.

I am looking for an algorithm which has at most O(n^3) as time complexity, or a formula to calculate directly the sequences.

For N=8, one of the possible solutions is provided in the code below.
I wrote the code so anybody can easily test their idea, and it should work as long as you do not accept the challenge.

If C is a hard for anybody I could rewrite the code in Python or Octave.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. #define ACCEPT_CHALLENGE 0
  6.  
  7. #if ACCEPT_CHALLENGE == 0
  8.  
  9. #define M 32        // the numbers in each base
  10. #define N 256      // the numbers in a complete sequence
  11. #define LOGN 8   // the number of bases
  12.  
  13. #else
  14.  
  15. #define M 4096
  16. #define N 65536
  17. #define LOGN 16
  18.  
  19. #endif 
  20.  
  21. void printbin( int a, int len ){
  22.    if ( a != -1 ){
  23.        for ( int i =1; i <= len ; i++ ){
  24.            printf("%d",(a>>(len-i)&1));
  25.        }
  26.    } else {
  27.        for ( int i =1; i <= len ; i++ ){
  28.            printf(" ");
  29.        }
  30.    }
  31. }
  32.  
  33. int to_grey(int a ){
  34.     a = ( a >> 1 ) ^ a ;
  35.     return a;
  36. }
  37.  
  38. int count_ones( int a , int len ){
  39.  
  40.    int ones;
  41.  
  42.    ones = 0;
  43.    for ( int i =1; i <= len ; i++ ){
  44.        ones += (a>>(len-i)&1) ;
  45.    }
  46.  
  47.    return ones;
  48. }
  49.  
  50.  
  51. void generate_bases( int ** bases ){
  52.  
  53.     bases[0][0]  =   0; bases[1][0]  =   1; bases[2][0]  =   2; bases[3][0]  =   3;
  54.     bases[0][1]  =   7; bases[1][1]  =   6; bases[2][1]  =   5; bases[3][1]  =   4;
  55.     bases[0][2]  =  25; bases[1][2]  =  24; bases[2][2]  =  27; bases[3][2]  =  26;
  56.     bases[0][3]  =  30; bases[1][3]  =  31; bases[2][3]  =  28; bases[3][3]  =  29;
  57.     bases[0][4]  =  42; bases[1][4]  =  43; bases[2][4]  =  40; bases[3][4]  =  41;
  58.     bases[0][5]  =  45; bases[1][5]  =  44; bases[2][5]  =  47; bases[3][5]  =  46; 
  59.     bases[0][6]  =  51; bases[1][6]  =  50; bases[2][6]  =  49; bases[3][6]  =  48;
  60.     bases[0][7]  =  52; bases[1][7]  =  53; bases[2][7]  =  54; bases[3][7]  =  55;
  61.     bases[0][8]  =  75; bases[1][8]  =  74; bases[2][8]  =  73; bases[3][8]  =  72;
  62.     bases[0][9]  =  76; bases[1][9]  =  77; bases[2][9]  =  78; bases[3][9]  =  79;
  63.     bases[0][10] =  82; bases[1][10] =  83; bases[2][10] =  80; bases[3][10] =  81;
  64.     bases[0][11] =  85; bases[1][11] =  84; bases[2][11] =  87; bases[3][11] =  86;
  65.     bases[0][12] =  97; bases[1][12] =  96; bases[2][12] =  99; bases[3][12] =  98;
  66.     bases[0][13] = 102; bases[1][13] = 103; bases[2][13] = 100; bases[3][13] = 101;
  67.     bases[0][14] = 120; bases[1][14] = 121; bases[2][14] = 122; bases[3][14] = 123;
  68.     bases[0][15] = 127; bases[1][15] = 126; bases[2][15] = 125; bases[3][15] = 124;
  69.     bases[0][16] = 255; bases[1][16] = 254; bases[2][16] = 253; bases[3][16] = 252;
  70.     bases[0][17] = 248; bases[1][17] = 249; bases[2][17] = 250; bases[3][17] = 251;
  71.     bases[0][18] = 230; bases[1][18] = 231; bases[2][18] = 228; bases[3][18] = 229;
  72.     bases[0][19] = 225; bases[1][19] = 224; bases[2][19] = 227; bases[3][19] = 226;
  73.     bases[0][20] = 213; bases[1][20] = 212; bases[2][20] = 215; bases[3][20] = 214;
  74.     bases[0][21] = 210; bases[1][21] = 211; bases[2][21] = 208; bases[3][21] = 209;
  75.     bases[0][22] = 204; bases[1][22] = 205; bases[2][22] = 206; bases[3][22] = 207;
  76.     bases[0][23] = 203; bases[1][23] = 202; bases[2][23] = 201; bases[3][23] = 200;
  77.     bases[0][24] = 180; bases[1][24] = 181; bases[2][24] = 182; bases[3][24] = 183;
  78.     bases[0][25] = 179; bases[1][25] = 178; bases[2][25] = 177; bases[3][25] = 176;
  79.     bases[0][26] = 173; bases[1][26] = 172; bases[2][26] = 175; bases[3][26] = 174;
  80.     bases[0][27] = 170; bases[1][27] = 171; bases[2][27] = 168; bases[3][27] = 169;
  81.     bases[0][28] = 158; bases[1][28] = 159; bases[2][28] = 156; bases[3][28] = 157;
  82.     bases[0][29] = 153; bases[1][29] = 152; bases[2][29] = 155; bases[3][29] = 154;
  83.     bases[0][30] = 135; bases[1][30] = 134; bases[2][30] = 133; bases[3][30] = 132;
  84.     bases[0][31] = 128; bases[1][31] = 129; bases[2][31] = 130; bases[3][31] = 131;
  85.  
  86.     bases[4][0]  =   8; bases[5][0]  =   9; bases[6][0]  =  10; bases[7][0]  =  11;
  87.     bases[4][1]  =  15; bases[5][1]  =  14; bases[6][1]  =  13; bases[7][1]  =  12;
  88.     bases[4][2]  =  17; bases[5][2]  =  16; bases[6][2]  =  19; bases[7][2]  =  18;
  89.     bases[4][3]  =  22; bases[5][3]  =  23; bases[6][3]  =  20; bases[7][3]  =  21;
  90.     bases[4][4]  =  34; bases[5][4]  =  35; bases[6][4]  =  32; bases[7][4]  =  33;
  91.     bases[4][5]  =  37; bases[5][5]  =  36; bases[6][5]  =  39; bases[7][5]  =  38;
  92.     bases[4][6]  =  59; bases[5][6]  =  58; bases[6][6]  =  57; bases[7][6]  =  56;
  93.     bases[4][7]  =  60; bases[5][7]  =  61; bases[6][7]  =  62; bases[7][7]  =  63;
  94.     bases[4][8]  =  67; bases[5][8]  =  66; bases[6][8]  =  65; bases[7][8]  =  64;
  95.     bases[4][9]  =  68; bases[5][9]  =  69; bases[6][9]  =  70; bases[7][9]  =  71;
  96.     bases[4][10] =  90; bases[5][10] =  91; bases[6][10] =  88; bases[7][10] =  89;
  97.     bases[4][11] =  93; bases[5][11] =  92; bases[6][11] =  95; bases[7][11] =  94;
  98.     bases[4][12] = 105; bases[5][12] = 104; bases[6][12] = 107; bases[7][12] = 106;   
  99.     bases[4][13] = 110; bases[5][13] = 111; bases[6][13] = 108; bases[7][13] = 109;
  100.     bases[4][14] = 112; bases[5][14] = 113; bases[6][14] = 114; bases[7][14] = 115;
  101.     bases[4][15] = 119; bases[5][15] = 118; bases[6][15] = 117; bases[7][15] = 116;
  102.     bases[4][16] = 247; bases[5][16] = 246; bases[6][16] = 245; bases[7][16] = 244;
  103.     bases[4][17] = 240; bases[5][17] = 241; bases[6][17] = 242; bases[7][17] = 243;
  104.     bases[4][18] = 238; bases[5][18] = 239; bases[6][18] = 236; bases[7][18] = 237;
  105.     bases[4][19] = 233; bases[5][19] = 232; bases[6][19] = 235; bases[7][19] = 234;
  106.     bases[4][20] = 221; bases[5][20] = 220; bases[6][20] = 223; bases[7][20] = 222;
  107.     bases[4][21] = 218; bases[5][21] = 219; bases[6][21] = 216; bases[7][21] = 217;
  108.     bases[4][22] = 196; bases[5][22] = 197; bases[6][22] = 198; bases[7][22] = 199;
  109.     bases[4][23] = 195; bases[5][23] = 194; bases[6][23] = 193; bases[7][23] = 192;
  110.     bases[4][24] = 188; bases[5][24] = 189; bases[6][24] = 190; bases[7][24] = 191;
  111.     bases[4][25] = 187; bases[5][25] = 186; bases[6][25] = 185; bases[7][25] = 184;
  112.     bases[4][26] = 165; bases[5][26] = 164; bases[6][26] = 167; bases[7][26] = 166;
  113.     bases[4][27] = 162; bases[5][27] = 163; bases[6][27] = 160; bases[7][27] = 161;
  114.     bases[4][28] = 150; bases[5][28] = 151; bases[6][28] = 148; bases[7][28] = 149;
  115.     bases[4][29] = 145; bases[5][29] = 144; bases[6][29] = 147; bases[7][29] = 146;
  116.     bases[4][30] = 143; bases[5][30] = 142; bases[6][30] = 141; bases[7][30] = 140;
  117.     bases[4][31] = 136; bases[5][31] = 137; bases[6][31] = 138; bases[7][31] = 139;  
  118.  
  119.     return;
  120.  
  121. }
  122.  
  123.  
  124. int main(){
  125.  
  126.    int i, j, k ;
  127.    int basei, basej, one_cnt, error, occurrance;
  128.    int ** bases;
  129.  
  130.    bases = (int **) malloc(LOGN*sizeof(int *));
  131.    for ( i = 0 ; i < LOGN ; i++ ) bases[i] = (int *) malloc(M*sizeof(int));
  132.  
  133.    // your soulution here
  134.    generate_bases( bases );
  135.  
  136.    //check hamming distance
  137.    error = 0;
  138.    for ( basei = 0; (basei < LOGN) && (!error); basei++ ){
  139.  
  140.        printf("checking base %4d\n",basei);
  141.  
  142.        for ( k = 0; (k < N) && (!error); k++ ){
  143.            error = 1;
  144.            for ( basej = 0; basej < M; basej++ ){
  145.                one_cnt = count_ones( k ^ bases[basei][basej], LOGN);
  146.                if (( one_cnt == 0 )||( one_cnt == 1 )){
  147.                     error = 0;
  148.                }
  149.            }
  150.            if ( error == 1 ){
  151.                printf("error in base %4d, counter example %4d ",basei,k);
  152.                printbin(k,LOGN);
  153.                printf("\n");
  154.  
  155.                // print base
  156.                for ( i = basei; i == basei ; i++ ){
  157.                    for ( j = 0; j < M ; j++ ){
  158.                        printbin(bases[i][j],LOGN);
  159.                        printf(" ");
  160.                    }
  161.                    printf("\n");
  162.                }
  163.                printf("\n");
  164.            }
  165.        }
  166.    }
  167.  
  168.    //check unicity
  169.    for ( i = 0; (i < N) && ( ! error ); i++ ){
  170.        occurrance = 0;
  171.        for ( basei = 0; (basei < LOGN) && (!error); basei++ ){
  172.            for ( basej = 0; basej < M; basej++ ){
  173.                if ( bases[basei][basej] == i ){
  174.                    occurrance ++;
  175.                }
  176.            }
  177.         }
  178.         if ( occurrance != 1 ){
  179.              error = 1;
  180.         }
  181.    }
  182.  
  183.    if ( error == 0 ){
  184.        // print bases
  185.        printf("\n\nvalid bases:\n");
  186.        for ( i = 0; i < LOGN ; i++ ){
  187.            for ( j = 0; j < M ; j++ ){
  188.                 printf("%4d, ",bases[i][j]);
  189.                 //printbin(bases[i][j],LOGN);
  190.                 printf(" ");
  191.                 if ( j == M/2-1){
  192.                     printf("\n");
  193.                 }
  194.            }
  195.            printf("\n\n");
  196.        }
  197.        printf("\n");
  198.    } else {
  199.        printf("not valid bases\n");
  200.    }
  201.  
  202.    for ( i = 0 ; i < LOGN ; i++ ) free(bases[i]) ;
  203.    free(bases);
  204.  
  205.    return 0;
  206.  
  207. }
  208.  
Nov 4 '22 #1
1 15509
dayamafor
1 Bit
i also had same question
Nov 22 '22 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
9
by: Bernd.Moos | last post by:
Given the following XML document: <text> <p> <w>Ronaldo</w> <w>scoredw> <w>the</w> <w>1</w> <c>:</c> <w>1</w>
9
by: KWSW | last post by:
Having settled the huffman encoding/decoding and channel modeling(thanks to the previous part on bitwise operation), the last part would be hamming encoding/decoding. Did some research as usual on...
21
by: KWSW | last post by:
Ok I am really confused now. Maybe its not my programming or something. Anyway just for background, the final part of my assignment is to encode a text with 8458 characters with huffman, encode it...
1
by: bigidybong | last post by:
I've been set this task below. Unlike my colleagues I am new to java. If anyone has the code for this then please let me know or send me an email if they can do it. Develop a program which...
3
by: mcoyle128 | last post by:
Where can I find the Hamming Code in Java in this site please?
8
by: godavemon | last post by:
I need to calculate the Hamming Distance of two integers. The hamming distance is the number of bits in two integers that don't match. I thought there'd be a function in math or scipy but i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.