Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve algorithm to find the N closest cities to current location #28

Open
amaury1093 opened this issue Oct 25, 2020 · 1 comment
Open
Labels
good first issue Good for newcomers

Comments

@amaury1093
Copy link
Member

Summary

The current algorithm for find the N closest cities to the current location is very naive. We could optimize it.

Problem Definition

If you go to the page of a city (example here), there's a ranking section, which shows the 6 closest cities to the current location, and their cigarettes information.

Screenshot 2020-10-25 at 19 27 48

The current algorithm to calculate these closest cities is the following:

This algorithm is not optimal (O(n^2)). It's okay for 1000 cities, but there are surely better way to find the closest N cities around the current location.

Potential Solutions

  • data structures such as r-tree, quad-tree or k-d tree
@amaury1093 amaury1093 added the good first issue Good for newcomers label Oct 25, 2020
@marvinody
Copy link
Contributor

This may not solve it for all cases, but if you want a quick solution, you could potentially add a filter right after calculating size to only include cities within X units ( 50 miles, 100 km, whatever you want). It doesn't "in theory" reduce it to a small amount, but in practice we know a set of coordinates is only going to be close to some small set of cities.
You may end up with less than 6 so I'm not sure if that's suitable for you but just another potential solution that goes a different way entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants