gray-code

Gray code for all k element subset of {1,…,n}

我的未来我决定 提交于 2019-12-08 12:47:52
I am seeking for an algorithm which iterates through all k element subsets of an n element set. I do not want to generate all those subset explicitly. There is an easy algorithm to do this, namely sorting the corresponding bit vectors lexographically and then go from the current subset to the next one. Nevertheless, I seek for an algorithm which only switches 2 bits in each step. I have read that such a code is a called "gray-code" but I did not found an algorithm for my problem. Is there a straight forward implementation for this? This isn't going to be a complete answer, but it's also not

Efficient way to iterate over Gray code change positions

£可爱£侵袭症+ 提交于 2019-12-07 23:51:40
问题 There a number of ways iterating over n-bit Gray codes. Some are more efficient than others. However, I don't actually need the Gray codes and would like instead to iterate over the bit index that is changed in a Gray code list, not the actual Gray codes. For example, take this 3-bit Gray code list: 000, 001, 011, 010, 110, 111, 101, 100 I would like to output 3, 2, 3, 1, 3, 2, 3. This tells us we needed to change bits 3, 2, 3 etc. in order to get the list. Here I am indexing from 1 and from

Efficient way to iterate over Gray code change positions

折月煮酒 提交于 2019-12-06 11:56:39
There a number of ways iterating over n-bit Gray codes . Some are more efficient than others. However, I don't actually need the Gray codes and would like instead to iterate over the bit index that is changed in a Gray code list, not the actual Gray codes. For example, take this 3-bit Gray code list: 000, 001, 011, 010, 110, 111, 101, 100 I would like to output 3, 2, 3, 1, 3, 2, 3. This tells us we needed to change bits 3, 2, 3 etc. in order to get the list. Here I am indexing from 1 and from the left. One way to do this would be to compute the Gray codes in order and for each consecutive pair

Algorithm for generating “anti-Gray” on-demand combinations of k elements from n

只谈情不闲聊 提交于 2019-12-06 04:28:11
问题 I'm trying to implement an algorithm to get all combinations of k elements out of a set of n elements where the difference between two consecutive combinations are maximized (so kind of reverse Gray codes). In other words, the combinations should be ordered to avoid elements from appearing twice in a row, and so that no element is unnecessarily discriminated. Ideally, the algorithm would also NOT pre-calculate all combinations and store them into memory, but rather deliver combinations on

Generating gray codes.

为君一笑 提交于 2019-12-04 12:33:36
I tried generating gray codes in Python . This code works correctly. The issue is that I am initialising the base case ( n=1,[0,1] ) in the main function and passing it to gray_code function to compute the rest. I want to generate all the gray codes inside the function itself including the base case. How do I do that? def gray_code(g,n): k=len(g) if n<=0: return else: for i in range (k-1,-1,-1): char='1'+g[i] g.append(char) for i in range (k-1,-1,-1): g[i]='0'+g[i] gray_code(g,n-1) def main(): n=int(raw_input()) g=['0','1'] gray_code(g,n-1) if n>=1: for i in range (len(g)): print g[i], main()

Algorithm for generating “anti-Gray” on-demand combinations of k elements from n

六眼飞鱼酱① 提交于 2019-12-04 09:57:19
I'm trying to implement an algorithm to get all combinations of k elements out of a set of n elements where the difference between two consecutive combinations are maximized (so kind of reverse Gray codes). In other words, the combinations should be ordered to avoid elements from appearing twice in a row, and so that no element is unnecessarily discriminated. Ideally, the algorithm would also NOT pre-calculate all combinations and store them into memory, but rather deliver combinations on demand. I have searched extensively for this and found a few detailed answers such as https:/

How to find if two numbers are consecutive numbers in gray code sequence

风格不统一 提交于 2019-12-03 00:34:06
I am trying to come up with a solution to the problem that given two numbers, find if they are the consecutive numbers in the gray code sequence i.e., if they are gray code neighbors assuming that the gray code sequence is not mentioned. I searched on various forums but couldn't get the right answer. It would be great if you can provide a solution for this. My attempt to the problem - Convert two integers to binary and add the digits in both the numbers separately and find the difference between the sum of the digits in two numbers. If the difference is one then they are gray code neighbors.

Convert decimal to gray code in java

隐身守侯 提交于 2019-12-02 22:20:55
问题 Had a question come up recently which was: write the algorithm to convert a decimal number to an n-bit gray code. So for example: Using 1-bit (simplest): 0 -> 0 1 -> 1 Using 2-bit 0 -> 00 1 -> 01 2 -> 11 3 -> 10 Using 3-bit 0 -> 000 1 -> 001 2 -> 011 3 -> 010 4 -> 110 5 -> 111 6 -> 101 7 -> 100 回答1: Wrote the following and figured I'd share it as I don't see many Java implementations showing up on here: static String getGreyCode(int myNum, int numOfBits) { if (numOfBits == 1) { return String

Convert decimal to gray code in java

淺唱寂寞╮ 提交于 2019-12-02 12:46:02
Had a question come up recently which was: write the algorithm to convert a decimal number to an n-bit gray code. So for example: Using 1-bit (simplest): 0 -> 0 1 -> 1 Using 2-bit 0 -> 00 1 -> 01 2 -> 11 3 -> 10 Using 3-bit 0 -> 000 1 -> 001 2 -> 011 3 -> 010 4 -> 110 5 -> 111 6 -> 101 7 -> 100 Wrote the following and figured I'd share it as I don't see many Java implementations showing up on here: static String getGreyCode(int myNum, int numOfBits) { if (numOfBits == 1) { return String.valueOf(myNum); } if (myNum >= Math.pow(2, (numOfBits - 1))) { return "1" + getGreyCode((int)(Math.pow(2,

Fill matrix with binary numbers, regular and gray coded

故事扮演 提交于 2019-12-02 08:03:05
I have a matrix that holds 1:s or 0:s, creating binary numbers. Its width is n. For n = 2 and n = 3 it would look like: 00 000 01 001 10 010 11 011 100 101 110 111 and so on. Right now I'm using the following code to produce this. int row = (int) Math.pow(2, n); int col = n; int[][] matrix = new int[row][col]; for (int r = 0; r < row; r++) { String binaryNumber = String.format("%" + n + "s", Integer.toBinaryString(r)).replace(' ', '0'); for (int c = col - 1; c >= 0; c--) { matrix[r][c] = Integer.parseInt("" + binaryNumber.charAt(0)); binaryNumber = binaryNumber.substring(1); } } Now I need