Is there any way of finding efficiently (bitwise operations) the distance (not Hamming Distance!) of two 8-bit binary strings?
Each byte is guaranteed to have only one bit set.
Like:
a=0 0 0 0 0 0 0 1
b=0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 1 -> distance = 3
^^^^^
------
a=0 0 0 0 0 1 0 0
b=0 1 0 0 0 0 0 0
0 1 0 0 0 1 0 0 -> distance = 3
^^^^^
------
a=0 1 0 0 0 0 0 0
b=0 0 0 0 0 0 1 0
0 1 0 0 0 0 1 0 -> distance = 4
^^^^^^^
I could work with something like logarithms but that is not very efficient
"Efficient" can mean a different things here: e.g., asymptotic vs performance for a known range of inputs; time vs space; etc.
I'll assume you care about raw speed for the small bounded inputs you describe.
Baseline approach. Take the smaller bit, and left shift it until it's equal to the larger bit, counting shifts. While this is O(n), that sort of analysis doesn't matter here since n is bounded.
You might compare that baseline to either of the following approaches, which have better time complexity but may or may not be faster for your inputs.
Alternative 1. Put all the distances in a lookup matrix. O(1) time complexity, but O(n^2) space complexity.
Alternative 2. Have a lookup table for the logarithms, and return the difference log2(a) - log2(b), where a >= b. O(1) time complexity, O(n) space complexity. (Note that I'm assuming that dist(a, a) = 0, which is a off-by-one from what you describe above.)
I don't know in practice which of those will be faster, but the main point is not to assume that O(n) means that the algorithm is slower in absolute terms for your inputs.
You can use OR operation (logical summing) and then find a maximum amount of zeros, which goes one by one. Hope i get your question right.
Related
The problem I am solving is scheduling tasks from limited resources.
The way I thought about it is to use a two-dimensional array to identify resources.
I wonder how I can calculate efficiently because the operation speed is too long.
Using a binary tree is likely to be difficult. After calculation, there is a process of randomly exchanging indexes for the search process.
For example)
Factory's capacity : 4
A(2,2) B(3,2) C(1,1) \\\\task(processing time , required area)
Schedule : A-B-C ,1 means that there is space left, and 0 means that there is no space.
A task can only be allocated if the space required is continuously present.
The x-axis represents time and the y-axis represents capacity.
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 1 1 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 0 1 ㅡㅡㅡㅡㅡ 0 0 0 1
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 1 1 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 0 1 ㅡㅡㅡㅡㅡ 0 0 0 1
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 0 0 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 1 1 ㅡㅡㅡㅡㅡ 0 0 1 1
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 0 0 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 1 1 ㅡㅡㅡㅡㅡ 0 0 0 1
The good new is your problem looks very similar to well known Job shop scheduling problem. The bad new is Job shop scheduling is NP-hard.
I had referred to many articles and questions that answered how to solve a maze effectively but here I want to confirm what's going wrong in my code. Consider the maze:
2 1 0 0 3
0 1 0 1 1
0 1 0 0 1
0 1 1 0 0
0 0 0 0 0
where the 1's represent the walls and 0's represent the path.(source is 2 and destination is 3).
I have to output whether there is a path or not.
int y=0;
while(y==0)
{
robo1(n,m,maze);//this function adds 2 to any '0'/'3' in (i,j+1),(i+1,j),(i-1,j),(i,j-1) (if exists),where (i,j) is 2
robo2(n,m,k2,maze);//this function adds 3 to any '0'/'2' in (i,j+1),(i+1,j),(i-1,j),(i,j-1) (if exists), where (i,j) is 3
if(find5(n,m,maze)==1)//this function returns 1 if there is '5' in the maze
y++;
if(find0(n,m,maze)==0)//this function returns 0 if there are no '0' in the maze
break;
}
if(find0(n,m,maze)==0 && y==0)
printf("-1\n");//no path
else
printf("1\n");//there is a path
My idea is that if after any number of loops a five is found in the maze, then it would mean there is a path.
But while implementing this function in code I get wrong answers and sometimes run-time errors.
Is there any flaw in the above logic?
The general idea should almost work, but of course everything is in the details.
One case in which your approach will not work even if implemented correctly is however this:
2 1 0 0 0
1 1 0 1 1
0 0 0 1 3
i.e. if both 2 and 3 are "closed" by walls but there are 0s in the room. Your loop will never end because despite having 0s around neither of the two robo function will change anything.
A simple solution is returning 0/1 from robos if they actually changed at least a value in the matrix and quitting when this doesn't happen.
Note that this is not a very efficient way of solving a maze (your code will keep checking the same cells over and over many times).
How i can get the longest cycle in a undirected Graph (without BackTracking, it takes too long).
Example:
0 3 0 1 0
3 0 0 1 0
0 0 0 0 0
1 1 0 0 0
0 0 0 0 0
Solve: 3 + 3 + 1 => Out: 1 - 2 - 3 - 1.
If you can find the longest cycle, you can detect whether the graph has a Hamiltonian Cycle, which is an NP-complete problem, thus making your problem NP-hard.
That means no solution will be fundamentally better than backtracking unless P=NP.
iterate through a 3d matrix
i need to check every possible solution to a certain predicament.
i have a matrix[x][y][z] that represents possible nodes to travel through. I already finished a method that should give me a set of solutions (it disables a single path every iteration and recalculates the entire solution, disable priority is based on travel capacity of the last solution)
but i need to see how effective my method is in terms of total time taken to calculate a set. For this i require a method calculate the solution on every permutation of these paths.
currently it only has a single layer in between 2 main layers (L1) where 0 is a free path and 1 is a non accessible path.
This here is the starting layout where i can toggle the values on layer L1 from 0 to 1 to disable a path and the basis of my shortest path search algorithm.
L0 0 0 0 0 0 L1 0 1 0 1 0 L2 0 0 0 0 0
0 1 0 1 0 1 1 1 1 1 0 1 0 1 0
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
0 1 0 1 0 1 1 1 1 1 0 1 0 1 0
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
how can i iterate through every possible combination of disabling the free paths when the dimensions of the matrix are non constant (meaning they are already user defined on compile time and can be changed whenever)? there are 2^n solutions where n is the number of free path on all mediary layers.
(a quick explanation in C or C++ would be best, even pseudo code is good) since there currently 9 free path to make combinations with there should be about 2^9 solutions which i need to test with. I havent done any brute force algorithms before so i have no idea how to make one.
I'm not good in English, I can't ask it better, but please below:
if byte in binary is 1 0 0 0 0 0 0 0 then result is 1
if byte in binary is 1 1 0 0 0 0 0 0 then result is 2
if byte in binary is 1 1 1 0 0 0 0 0 then result is 3
if byte in binary is 1 1 1 1 0 0 0 0 then result is 4
if byte in binary is 1 1 1 1 1 0 0 0 then result is 5
if byte in binary is 1 1 1 1 1 1 0 0 then result is 6
if byte in binary is 1 1 1 1 1 1 1 0 then result is 7
if byte in binary is 1 1 1 1 1 1 1 1 then result is 8
But if for example the byte in binary is 1 1 1 0 * * * * then result is 3.
I would determine how many bit is set contiguous from left to right with one operation.
The results are not necessary numbers from 1-8, just something to distinguish.
I think it's possible in one or two operations, but I don't know how.
If you don't know a solution as short as 2 operations, please write that too, and I won't try it anymore.
Easiest non-branching solution I can think of:
y=~x
y|=y>>4
y|=y>>2
y|=y>>1
Invert x, and extend the lefttmost 1-bit (which corresponds to the leftmost 0-bit in the non-inverted value) to the right. Will give distinct values (not 1-8 though, but it's pretty easy to do a mapping).
110* ****
turns into
001* ****
001* **1*
001* 1*1*
0011 1111
EDIT:
As pointed out in a different answer, using a precomputed lookup table is probably the fastets. Given only 8 bits, it's probably even feasible in terms of memory consumption.
EDIT:
Heh, woops, my bad.. You can skip the invert, and do ands instead.
x&=x>>4
x&=x>>2
x&=x>>1
here
110* ****
gives
110* **0*
110* 0*0*
1100 0000
As you can see all values beginning with 110 will result in the same output (1100 0000).
EDIT:
Actually, the 'and' version is based on undefined behavior (shifting negative numbers), and will usually do the right thing if using signed 8-bit (i.e. char, rather than unsigned char in C), but as I said the behavaior is undefined and might not always work.
I'd second a lookup table... otherwise you can also do something like:
unsigned long inverse_bitscan_reverse(unsigned long value)
{
unsigned long bsr = 0;
_BitScanReverse(&bsr, ~value); // x86 bsr instruction
return bsr;
}
EDIT: Not that you have to be careful of the special case where "value" has no zeroed bits. See the documentation for _BitScanReverse.