生成汉明距离最大为2的所有无序比特串对

Generate all unordered pairs of bit strings with Hamming distance at most 2

本文关键字:无序 汉明距离      更新时间:2023-10-16

我正在寻找一种有效的方法来生成汉明距离最大为2的所有无序比特串对(表示为整数)。在这个答案中,示出了对于具有汉明距离1的对,这是如何非常有效地完成的。

换句话说,上面的答案给出了超立方体图的所有边。用这些术语来说,我正在寻找一种有效的方法来生成超立方体的正方形的边。

有没有一些很好的已知快速方法,也许类似地基于比特技巧?

对Nate Kohl的答案有一个简单的修改。

int n = 3;
// examine all vertices from 0...2^n-1
unsigned long long max = 1ULL << n;  
for (unsigned long long vertex = 0; vertex < max; ++vertex) {
  std::cout << vertex << ':';
  // print all vertices that differ from vertex by one bit
  unsigned long long mask = 1;
  for (int shift_amt = 0; shift_amt < n; ++shift_amt) {
    std::cout << ' ' << (vertex ^ (mask << shift_amt));
  }
  for (int shift_amt1 = 0; shift_amt1 < n; ++shift_amt1) {
    for (int shift_amt2 = 0; shift_amt2 < shift_amt1; ++shift_amt2) {
      std::cout << ' ' << (vertex ^ (mask << shift_amt1) ^ (mask << shift_amt2));
    }
  }
  std::cout << 'n';
}