Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An algorithm to determine if a number belongs to a group or not

Tags:

algorithm

I'm not even sure if this is possible but I think it's worth asking anyway.

Say we have 100 devices in a network. Each device has a unique ID.

I want to tell a group of these devices to do something by broadcasting only one packet (A packet that all the devices receive). For example, if I wanted to tell devices 2,5,75,116 and 530 to do something, I have to broadcast this : 2-5-75-116-530 But this packet can get pretty long if I wanted (for example) 95 of the devices to do something!!! So I need a method to reduce the length of this packet.

After thinking for a while, I came up with an idea:

what if I used only prime numbers as device IDs? Then I could send the product of device IDs of the group I need, as the packet and every device will check if the remainder of the received number and its device ID is 0.

For example if I wanted devices 2,3,5 and 7 to do something, I would broadcast 2*3*5*7 = 210 and then each device will calculate "210 mod self ID" and only devices with IDs 2,3,5 and 7 will get 0 so they know that they should do something.

But this method is not efficient because the 100th prime numbers is 541 and the broadcasted number may get really big and the "mod" calculation may get really hard.(the devices have 8bit processors).

So I just need a method for the devices to determine if they should do something or ignore the received packet. And I need the packet to be as short as possible.

I tried my best to explain the question, If its still vague, please tell me to explain more.

like image 666
Mehrad Moein Avatar asked Nov 20 '15 00:11

Mehrad Moein


2 Answers

You can just use a bit string in which every bit represents a device. Then, you just need a bitwise AND to tell if a given machine should react.

You'd need one bit per device, which would be, for example, 32 bytes for 256 devices. Admittedly, that's a little wasteful if you only need one machine to react, but it's pretty compact if you need, say, 95 devices to respond.

You mentioned that you need the device id to be <= 4 bytes, but that's no problem: 4 bytes = 32 bits = enough space to store 2^32 device ids. For example, the device id for the 101st machine (if you start at 0) could just be 100 (0b01100100) = 1 byte. You would just need to use that to figure out which byte of the packet to use (ceil(100 / 8) = the 13th) and bitwise AND that byte against 100 % 8 = 4 = 0b00000100.


As cobarzan said, you also can use a hybrid scheme allowing for individual addressing. In that scenario, you could use the first bit as a signal to indicate multiple- or single-machine addressing. As cobarzan said, that requires more processing, and it means the first byte can only store 7 machine signals, rather than 8.

like image 175
elixenide Avatar answered Nov 01 '22 18:11

elixenide


Like Ed Cottrell suggested, a bit string would do the job. If the machines are labeled {1,..,n}, there are 2n-1 possible subsets (assuming you do not send requests with no intended target). So you need a data structure able to hold every possible signature of such a subset, whatever you decide the signature to be. And n bits (one for each machine) is the best one can do regarding the size of such a data structure. The evaluation performed on the machines takes constant time (on machine with label l just look at the lth bit).

But one could go for some hybrid scheme. Say you have a task for one device only, then it would be a pity to send n bits (all 0s, except one). So you can take one additional bit T which indicates the type of packet. The value of T is set to 0 if you are sending a bit string of length n as described above or set to 1 if you are using a more appropriate scheme (i.e. less bits). In the case of just one machine that needs to perform the task, you could send directly the label of the machine (which is O(log n) bits long). This approach reduces the size of the packet if you have less than O(n/log n) machines you need to perform the task. Evaluation on the machines is more expensive though.

like image 30
cobarzan Avatar answered Nov 01 '22 17:11

cobarzan