r/AskProgramming • • Apr 11 '19

How does Tinder and Bumble implement nearest neighbors?

I'm looking for a data structure that would allow for efficient look ups of points within a certain radius from another point. That is, given a certain point and a radius, return all points within that radius to the given point. Basically, how does tinder and bumble accomplish this when you find other people X miles from me? I've heard of the K nearest neighbors algorithm but, this is hard because how do I appropriately select K? For example, suppose I select K = 1000 but in reality, k = 5000 would've return results all still within X miles from me. Anyone know of any data structures that can accomplish this efficient searching? It's okay if insertion of the data is not as efficient since searching will be used much more.

68 Upvotes

14 comments sorted by

16

u/looselytethered Apr 11 '19

Upvoting for discussion, this is a super interesting question

11

u/[deleted] Apr 11 '19 edited Apr 11 '19

There are many indexing structures that could be used to make k-Nearest Neighbors faster. You could use KD-trees if the features are indeed geometric coordinates, or you could use something like Orchad Algorithm to quickly eliminate false neighbors. Another way to dismiss false neighbors is to employ lower bounds, which is especially useful if you are using an elastic measure, like DTW.

But notice that the k in k-NN does not relate to miles. It is the number of neighbors you want to retrieve. So if you want the closest match, then it's k=1. Of course, "match" in Tinder is not done geographically, but according to features.

Expanding on Tinder, it doesn't really give you the exact distance. Here's an interesting read (which I regret not to remember enough to summarize here) https://robertheaton.com/2018/07/09/how-tinder-keeps-your-location-a-bit-private/

2

u/pigglybiggly Apr 12 '19

Great link. i've been on that site for an hour now

2

u/amolbh Apr 13 '19

Thanks for the article!

3

u/TheDudeFromCI Apr 12 '19

The first algorithms that come to mind are oct trees. Basically all points are stored within an oct tee data structure, and when you need to get all points within a circle, just recursively transverse down the tree.

For all cubes completely within the circle, all child points must be within range. For all cubes completely outside of the circle, all children are outside and can be ignored. For cubes that partially intersect, check subdivided cubes.

Edit: And when you get to the most subdivided state, if it's a partial intersect, the points within may be checked individually usually distance squared.

This data structure also allows to move around within the tree at anytime without effecting preformance.

3

u/munificent Apr 12 '19

What you want is called a spatial database, and there are a number of algorithms for implementing one efficiently.

2

u/thatprofessoryouhate Apr 12 '19

I actually figured they using some sort of estimation since accuracy can probably be fudged without concern.

Such as, define the world as a grid of 1 mile by 1 mile spaces. Assign each person to a grid. If I want to see everyone 1 mile from me, grab the grids next to me and get the people assigned to those grids.

1

u/bigsmily Apr 12 '19

I'm using similar concept building an app, where accuracy isn't a big deal. And, you can play with the distance:

https://gps-coordinates.org/distance-between-coordinates.php

Long is from -180 to 180, which is 360 and Lat is 180 in total. Keep in mind, and for each 0.01 is coordinates, you will get 0.69 miles (1.1 km) which was okay from my side. That would mean 36000 x 18000 = 648M cell (not the smallest).

However, tons of this is water, so I ended up with two layer schema: 360 x 180 grid, and each cell is empty at the start. If you wanna insert something, you can generate a 100 x 100 grid inside of the cell was empty ....

Hope this helps!!

2

u/BackpackerSimon Apr 12 '19

I’ve used this when working with C# to allow me to show the closest first using Entity Framework and Linq.

It has a distance method and then just take the smallest. As it scales I guess you would filter on town or spatial data first then sort

1

u/gas_them Apr 12 '19

You can overlay the "world" with a 1-mile square grid and make it possible to lookup the people in any grid and vice versa. This will allow you to quickly find people within a certain range, which you could refine further if you want an exact distance.

I think thats the most obvious, simplest way. You can overlay grids of different sizes (maybe factors of two is most optimal?) which would help even more.

1

u/eFrazes Apr 12 '19

I did this along time ago to map zip codes within range of a given lat/lon. My brother the math whiz wrote a formula with trig functions.

Now days you can use a google api to do the same thing without all the math.

1

u/nice1work1 Apr 12 '19

But then you pay Google.

1

u/bigsmily Apr 12 '19

To answer your question literally: Each company uses its variation of known algorithms, and there are many specialized for coordinates.

But, why did you compare your app to Tinder and Google maps? On tinder, you have to check if the person already got a match with that person ... Which may throw off certain options.

The expected scale matters too. You don't need to spend days on an algorithm that will be used by 100s of people only. Start with a simple SQL query: You get a point, you add the distance you want, and it will be like: WHERE DBx > (point.x+range.x) and DBx < (point.x-range.x) ....

Remember, in these apps, it's never a simple query; Google look up details, including your history related to the place, and your way to it probably. Tinder tries to arrange the results in specific order to include those who likes you, for instance ... KIS for now, but plan what you may need in your app.

1

u/[deleted] Apr 12 '19

OMG I did not understand a single thing u said in this post......makes me wonder there are soo many things to learn and implement.....