You are implementing the software for an IP router. When a packet comes in, the router has to
decide which link to route the packet out on, based on its destination IP address. How would
you implement this? How would you optimize for speed? How would you optimize for space?
Assuming that destination IP addresses are stored in a hash table, you can to do a lookup based
on the destination IP address of the incoming packet. However, the question is what kind of
algorithm to use to generate the hash?
The two ends of the spectrum are using a hash table of size 2^32 (32 for the 32-bit IP address) in
which case the time complexity would be O (1) but the space complexity will be very high (not
practical). On the other end you can use a binary search lookup, with time complexity of O (log n).
The right fit will depend on the time/space trade-off the particular application in question can make.
See a fantastic paper on this topic at http://citeseer.nj.nec.com/crescenzi99ip.html.
decide which link to route the packet out on, based on its destination IP address. How would
you implement this? How would you optimize for speed? How would you optimize for space?
Assuming that destination IP addresses are stored in a hash table, you can to do a lookup based
on the destination IP address of the incoming packet. However, the question is what kind of
algorithm to use to generate the hash?
The two ends of the spectrum are using a hash table of size 2^32 (32 for the 32-bit IP address) in
which case the time complexity would be O (1) but the space complexity will be very high (not
practical). On the other end you can use a binary search lookup, with time complexity of O (log n).
The right fit will depend on the time/space trade-off the particular application in question can make.
See a fantastic paper on this topic at http://citeseer.nj.nec.com/crescenzi99ip.html.
Comments
Post a Comment
https://gengwg.blogspot.com/