You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
The text was updated successfully, but these errors were encountered:
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.
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.
The current algorithm to calculate these closest cities is the following:
webapp/gatsby-node.js
Lines 9 to 12 in dee5ac1
RankingSection
loads all cities usinguseStaticQuery
:webapp/src/components/RankingSection/RankingSection.tsx
Line 54 in dee5ac1
webapp/src/components/RankingSection/RankingSection.tsx
Lines 86 to 93 in dee5ac1
webapp/src/components/RankingSection/RankingSection.tsx
Lines 98 to 104 in dee5ac1
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
The text was updated successfully, but these errors were encountered: