2

I am looking to do this for a community sublet advertising website, but theoretically the algorithm would be similar for any local search.

The less populated the area searched, the higher should be the default radius to search. On the other hand areas with high population density should have low default radius to keep things locally relevant.

This might be more of a mathematical question than a programming one but code is very welcome. So far I have calculated amount of sublets within 15 miles of each town or village and saved this in the database as an approximation for density. I intended to use this number to figure out how far to go with the search when someone searches for the town or village.

To test any proposed solution, I pulled out some approximate numbers I would want the algorithm to come up with. If there is a lot of sublets within 15 miles of a point, say 30k, I would want the default radius to search to be around 3 miles. If there is very little say 1 or 2, the default radius should go high up to 25 miles, even more miles if there are no places around. A mid range area with say ~1k sublets would have a default radius of 15 miles. These are just examples, the density will off course grow or shrink a with number of things in the database.

Population -> Default search radius
0          -> very high (~60 miles or more)
1          -> 25 miles
1k         -> 15 miles
30k        -> 3 miles

Am I going in the right direction? Python or PHP would be preferred for code centric answers.

Thank you

fmalina
  • 6,120
  • 4
  • 37
  • 47

1 Answers1

1

A reasonable approach would be to define regions so that they contain the same number of people, and then there will be approximately the same number of available apartments in each region.

To write this mathematically:

N = total number of people within a region
d = population density of the region (taken to be what you list as population)
A = Area of region
R = Radius of the region A

So, N = d*A = d*pi*R*R, and we want N to be constant, so R = K*sqrt(1/D), where K is a constant chosen to match your numbers, or approximately 500 miles. Then,

30K    ->   2.9  miles
 1K    ->   16   miles
  1    ->   500  miles

So it works for the first two, though not the extreme case of a population of 1 (but it's not clear that 1 is truly an important case to consider, rather than a special case all of it's own). Anyway, I think this approach makes some sense and at least gives something to consider.

tom10
  • 67,082
  • 10
  • 127
  • 137
  • 1
    Just a side note if anyone else wants to go for this in python 2.x: don't forget `1/float(D)` as the division won't work as expected – fmalina Apr 10 '12 at 02:49